1 | from gi.repository import Gtk |
---|
2 | from gi.repository import WebKit |
---|
3 | |
---|
4 | |
---|
5 | def __destroy_cb(widget, data=None): |
---|
6 | global download |
---|
7 | if download is not None: |
---|
8 | download.cancel() |
---|
9 | Gtk.main_quit() |
---|
10 | |
---|
11 | |
---|
12 | def __progress_change_cb(download, something): |
---|
13 | progress = download.get_progress() |
---|
14 | print progress |
---|
15 | |
---|
16 | |
---|
17 | def __state_change_cb(download, gparamspec): |
---|
18 | state = download.get_status() |
---|
19 | if state == WebKit.DownloadStatus.STARTED: |
---|
20 | print 'STARTED' |
---|
21 | elif state == WebKit.DownloadStatus.FINISHED: |
---|
22 | print 'FINISHED' |
---|
23 | elif state == WebKit.DownloadStatus.CANCELLED: |
---|
24 | print 'CANCELLED' |
---|
25 | else: |
---|
26 | print 'Other state: %s' % state |
---|
27 | |
---|
28 | |
---|
29 | def __error_cb(download, err_code, err_detail, reason): |
---|
30 | print 'Error downloading URI code %s, detail %s: %s' % (err_code, err_detail, reason) |
---|
31 | |
---|
32 | |
---|
33 | # We need to keep the download in memory |
---|
34 | download = None |
---|
35 | def __clicked_start_button(widget): |
---|
36 | print 'Button clicked' |
---|
37 | |
---|
38 | url = 'http://download.laptop.org/xo-1.5/os/candidate/12.1.0-21/21021o1.zd' |
---|
39 | nr = WebKit.NetworkRequest() |
---|
40 | nr.set_uri(url) |
---|
41 | global download |
---|
42 | download = WebKit.Download(network_request=nr) |
---|
43 | |
---|
44 | download.connect('notify::progress', __progress_change_cb) |
---|
45 | download.connect('notify::status', __state_change_cb) |
---|
46 | download.connect('error', __error_cb) |
---|
47 | |
---|
48 | print download.get_uri() |
---|
49 | download.set_destination_uri('file:///tmp/21021o1.zd') |
---|
50 | download.start() |
---|
51 | |
---|
52 | |
---|
53 | def __clicked_cancel_button(widget): |
---|
54 | global download |
---|
55 | if download is not None: |
---|
56 | download.cancel() |
---|
57 | print 'Download CANCELLED!' |
---|
58 | |
---|
59 | window = Gtk.Window() |
---|
60 | window.set_title('Download a file with WebKit example') |
---|
61 | window.set_default_size(300, 60) |
---|
62 | window.connect('destroy', __destroy_cb) |
---|
63 | |
---|
64 | start_button = Gtk.Button('Download file!') |
---|
65 | start_button.connect('clicked', __clicked_start_button) |
---|
66 | |
---|
67 | cancel_button = Gtk.Button('Cancel download!') |
---|
68 | cancel_button.connect('clicked', __clicked_cancel_button) |
---|
69 | |
---|
70 | vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) |
---|
71 | vbox.add(start_button) |
---|
72 | vbox.add(cancel_button) |
---|
73 | |
---|
74 | window.add(vbox) |
---|
75 | window.show_all() |
---|
76 | Gtk.main() |
---|