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