diff options
author | Manuel Kaufmann <humitos@gmail.com> | 2012-11-06 12:23:31 (GMT) |
---|---|---|
committer | Manuel Kaufmann <humitos@gmail.com> | 2012-11-06 12:23:31 (GMT) |
commit | 4f70caa7599a0ceab8469c8abcea7e2a17592339 (patch) | |
tree | e3e1d24fc256c1c14781cd8bf0929d7a98827b47 | |
parent | 9cf83eb95943bcf49e6a3dd467e8e856d45dcbe2 (diff) |
Download a file using WebKit.WebView
This test case leaves a .goutputstream file when I close the window
-rw-r--r-- | tests/download_file_with_webkit_webview.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/download_file_with_webkit_webview.py b/tests/download_file_with_webkit_webview.py new file mode 100644 index 0000000..0f0745d --- /dev/null +++ b/tests/download_file_with_webkit_webview.py @@ -0,0 +1,75 @@ +from gi.repository import Gtk +from gi.repository import WebKit + + +# We need to keep the download in memory +gdownload = None + + +def __destroy_cb(widget, data=None): + global gdownload + gdownload.cancel() + Gtk.main_quit() + + +def __progress_change_cb(download, something): + progress = download.get_progress() + print progress + + +def __state_change_cb(download, gparamspec): + state = download.get_status() + if state == WebKit.DownloadStatus.STARTED: + print 'STARTED' + elif state == WebKit.DownloadStatus.FINISHED: + print 'FINISHED' + elif state == WebKit.DownloadStatus.CANCELLED: + print 'CANCELLED' + else: + print 'Other state: %s' % state + + +def __error_cb(download, err_code, err_detail, reason): + print 'Error downloading URI code %s, detail %s: %s' % (err_code, err_detail, reason) + + +def __download_requested_cb(webview, download): + print '__download_requested_cb' + + global gdownload + gdownload = download + + download.connect('notify::progress', __progress_change_cb) + download.connect('notify::status', __state_change_cb) + download.connect('error', __error_cb) + + print download.get_uri() + download.set_destination_uri('file:///tmp/21021o1.zd') + download.start() + + return True + + +def __policy_decision_requested(webview, frame, request, + mimetype, policy_decision): + if not webview.can_show_mime_type(mimetype): + policy_decision.download() + return True + + return False + + +window = Gtk.Window() +window.set_title('Download a file with WebKit example') +window.set_default_size(800, 500) +window.connect('destroy', __destroy_cb) + +url = 'http://download.laptop.org/xo-1.5/os/candidate/12.1.0-21' +webview = WebKit.WebView() +webview.connect('download-requested', __download_requested_cb) +webview.connect('mime-type-policy-decision-requested', __policy_decision_requested) +webview.load_uri(url) +window.add(webview) + +window.show_all() +Gtk.main() |