Added --refresh option.
[zeroinstall.git] / gui.py
blob4e396aee57fbad870877e33a6a209f45932cada9
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, download_only):
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, download_only)
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 get_best_source(self, impl):
76 """Return the best download source for this implementation."""
77 return impl.download_sources[0]
79 def refresh_all(self):
80 for x in policy.walk_interfaces():
81 policy.begin_iface_download(x, True)
83 def pretty_size(size):
84 if size is None:
85 return '?'
86 if size < 2048:
87 return '%d bytes' % size
88 size = float(size)
89 for unit in ('Kb', 'Mb', 'Gb', 'Tb'):
90 size /= 1024
91 if size < 2048:
92 break
93 return '%.1f %s' % (size, unit)