From e7d16f4c5cee3459dd7f30f5f64bf13ec435be11 Mon Sep 17 00:00:00 2001 From: josch Date: Sat, 3 Mar 2012 20:39:47 +0100 Subject: [PATCH] preliminary download code - no GUI yet --- README.md | 3 ++- pyferea.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 84799f2..37c8875 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ Possible future work - build debian package - add hooks to load custom javascript/css for feeds - i18n - - downloading + - downloading (only gui code missing) - list of unread items - make re-sorting fast + - save database in xdg compliant location diff --git a/pyferea.py b/pyferea.py index 5b9959e..b4c57b3 100644 --- a/pyferea.py +++ b/pyferea.py @@ -34,6 +34,7 @@ from cStringIO import StringIO import shelve import time from datetime import datetime +import os, re def get_time_pretty(time): """ @@ -277,6 +278,53 @@ class ContentPane (Gtk.Notebook): self.emit("progress-changed", view.get_progress()) web_view.connect("notify::progress", _progress_changed_cb) + def _mime_type_policy_decision_requested_cb(view, frame, request, mime, policy): + # to make downloads possible, handle policy decisions and download + # everything that the webview cannot show + if view.can_show_mime_type(mime): + policy.use() + else: + policy.download() + return True + web_view.connect("mime-type-policy-decision-requested", _mime_type_policy_decision_requested_cb) + + def _download_requested_cb(view, download): + download_dir = os.path.expanduser('~/Downloads') + user_dirs = os.path.expanduser('~/.config/user-dirs.dirs') + if os.path.exists(user_dirs): + match = re.search('XDG_DOWNLOAD_DIR="(.*?)"', open(user_dirs).read()) + if match: + # TODO: what about $HOME_FOO or ${HOME}? how to parse that correctly? + download_dir = os.path.expanduser(match.group(1).replace('$HOME', '~')) + if not os.path.exists(download_dir): + os.makedirs(download_dir) + # TODO: check if destination file exists + download.set_destination_uri("file://"+download_dir+"/"+download.get_suggested_filename()) + + def _status_changed_cb(download, status): + if download.get_status().value_name == 'WEBKIT_DOWNLOAD_STATUS_CANCELLED': + print "download cancelled" + elif download.get_status().value_name == 'WEBKIT_DOWNLOAD_STATUS_CREATED': + print "download created" + elif download.get_status().value_name == 'WEBKIT_DOWNLOAD_STATUS_ERROR': + print "download error" + elif download.get_status().value_name == 'WEBKIT_DOWNLOAD_STATUS_FINISHED': + print "download finished" + elif download.get_status().value_name == 'WEBKIT_DOWNLOAD_STATUS_STARTED': + print "download started" + download.connect('notify::status', _status_changed_cb) + + def _progress_changed_cb(download, progress): + print "download", download.get_progress()*100, "%", download.get_current_size(), "bytes", download.get_elapsed_time(), "seconds" + download.connect('notify::progress', _progress_changed_cb) + + print "download total size:", download.get_total_size() + print "download uri:", download.get_uri() + print "download destination:", download_dir+"/"+download.get_suggested_filename() + + return True + web_view.connect("download-requested", _download_requested_cb) + scrolled_window = Gtk.ScrolledWindow() scrolled_window.props.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC scrolled_window.props.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC