1 | from gi.repository import Gtk |
---|
2 | from gi.repository import WebKit |
---|
3 | |
---|
4 | |
---|
5 | def __destroy_cb(widget, data=None): |
---|
6 | Gtk.main_quit() |
---|
7 | |
---|
8 | |
---|
9 | def __progress_change_cb(download, something): |
---|
10 | progress = download.get_progress() |
---|
11 | print progress |
---|
12 | |
---|
13 | |
---|
14 | def __state_change_cb(download, gparamspec): |
---|
15 | state = download.get_status() |
---|
16 | if state == WebKit.DownloadStatus.STARTED: |
---|
17 | print 'STARTED' |
---|
18 | elif state == WebKit.DownloadStatus.FINISHED: |
---|
19 | print 'FINISHED' |
---|
20 | elif state == WebKit.DownloadStatus.CANCELLED: |
---|
21 | print 'CANCELLED' |
---|
22 | else: |
---|
23 | print 'Other state: %s' % state |
---|
24 | |
---|
25 | |
---|
26 | def __error_cb(download, err_code, err_detail, reason): |
---|
27 | print 'Error downloading URI code %s, detail %s: %s' % (err_code, err_detail, reason) |
---|
28 | |
---|
29 | |
---|
30 | # We need to keep the download in memory |
---|
31 | download = None |
---|
32 | def __clicked_button(widget): |
---|
33 | print 'Button clicked' |
---|
34 | |
---|
35 | url = 'http://mkaufmann.com.ar/~humitos/olpc/audio/02%20-%20A%20veces%20no.mp3' |
---|
36 | # url = 'http://mkaufmann.com.ar/~humitos/olpc/audio/Example.ogg' |
---|
37 | nr = WebKit.NetworkRequest() |
---|
38 | nr.set_uri(url) |
---|
39 | global download |
---|
40 | download = WebKit.Download(network_request=nr) |
---|
41 | |
---|
42 | download.connect('notify::progress', __progress_change_cb) |
---|
43 | download.connect('notify::status', __state_change_cb) |
---|
44 | download.connect('error', __error_cb) |
---|
45 | |
---|
46 | print download.get_uri() |
---|
47 | download.set_destination_uri('file:///tmp/new_file.mp3') |
---|
48 | download.start() |
---|
49 | |
---|
50 | |
---|
51 | window = Gtk.Window() |
---|
52 | window.set_title('Download a file with WebKit example') |
---|
53 | window.set_default_size(300, 180) |
---|
54 | window.connect('destroy', __destroy_cb) |
---|
55 | |
---|
56 | button = Gtk.Button('Download file!') |
---|
57 | button.connect('clicked', __clicked_button) |
---|
58 | |
---|
59 | window.add(button) |
---|
60 | window.show_all() |
---|
61 | Gtk.main() |
---|
62 | |
---|