Use new monitor API.
[zeroinstall.git] / gui.py
blobec916367e653df9c2451b7434f6b4c770dd73b83
1 import gtk, os, gobject
3 from policy import Policy
4 import download
5 import dialog
6 from reader import InvalidInterface
7 from model import SafeException
9 # Singleton Policy
10 policy = None
12 class GUIPolicy(Policy):
13 window = None
14 n_downloads = 0
15 pulse = None
17 def __init__(self, interface, prog_args):
18 Policy.__init__(self, interface)
19 global policy
20 assert policy is None
21 policy = self
23 import mainwindow
24 self.window = mainwindow.MainWindow(prog_args)
25 self.window.browser.set_root(policy.get_interface(policy.root))
27 def monitor_download(self, dl):
28 error_stream = dl.start()
29 def error_ready(src, cond):
30 got = os.read(src.fileno(), 100)
31 if not got:
32 error_stream.close()
33 self.n_downloads -= 1
34 if self.n_downloads == 0:
35 self.window.progress.hide()
36 gobject.source_remove(self.pulse)
37 self.pulse = None
38 try:
39 data = dl.error_stream_closed()
40 self.check_signed_data(dl, data)
41 except download.DownloadError, ex:
42 dialog.alert(self.window,
43 "Error downloading interface '%s':\n\n%s" %
44 (dl.interface.uri, ex))
45 except InvalidInterface, ex:
46 dialog.alert(self.window,
47 "Syntax error in downloaded interface '%s':\n\n%s" %
48 (dl.interface.uri, ex))
49 except SafeException, ex:
50 dialog.alert(self.window,
51 "Error updating interface '%s':\n\n%s" %
52 (dl.interface.uri, ex))
53 return False
54 dl.error_stream_data(got)
55 return True
57 gobject.io_add_watch(error_stream,
58 gobject.IO_IN | gobject.IO_HUP,
59 error_ready)
61 self.n_downloads += 1
62 if self.pulse is None:
63 progress = self.window.progress
64 self.pulse = gobject.timeout_add(50, lambda: progress.pulse() or True)
65 progress.show()
67 def confirm_trust_keys(self, interface, sigs, iface_xml):
68 import trust_box
69 trust_box.trust_box.confirm_trust(interface, sigs, iface_xml)
71 def main(self):
72 self.window.show()
73 gtk.main()
75 def pretty_size(size):
76 if size is None:
77 return '?'
78 if size < 2048:
79 return '%d bytes' % size
80 size = float(size)
81 for unit in ('Kb', 'Mb', 'Gb', 'Tb'):
82 size /= 1024
83 if size < 2048:
84 break
85 return '%.1f %s' % (size, unit)