1 import gtk
, os
, gobject
3 from zeroinstall
.injector
.policy
import Policy
4 from zeroinstall
.injector
import download
5 from zeroinstall
.injector
.model
import SafeException
6 from zeroinstall
.injector
.reader
import InvalidInterface
12 class GUIPolicy(Policy
):
17 def __init__(self
, interface
, prog_args
, download_only
):
18 Policy
.__init
__(self
, interface
)
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)
34 if self
.n_downloads
== 0:
35 self
.window
.progress
.hide()
36 gobject
.source_remove(self
.pulse
)
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
))
54 dl
.error_stream_data(got
)
57 gobject
.io_add_watch(error_stream
,
58 gobject
.IO_IN | gobject
.IO_HUP
,
62 if self
.pulse
is None:
63 progress
= self
.window
.progress
64 self
.pulse
= gobject
.timeout_add(50, lambda: progress
.pulse() or True)
67 def recalculate(self
):
68 Policy
.recalculate(self
)
69 self
.window
.set_response_sensitive(gtk
.RESPONSE_OK
, self
.ready
)
71 def confirm_trust_keys(self
, interface
, sigs
, iface_xml
):
73 trust_box
.trust_box
.confirm_trust(interface
, sigs
, iface_xml
)
79 def get_best_source(self
, impl
):
80 """Return the best download source for this implementation."""
81 return impl
.download_sources
[0]
83 # XXX: Remove this. Moved to Policy.
84 def refresh_all(self
, force
= True):
85 for x
in self
.walk_interfaces():
86 self
.begin_iface_download(x
, force
)
88 def pretty_size(size
):
92 return '%d bytes' % size
94 for unit
in ('Kb', 'Mb', 'Gb', 'Tb'):
98 return '%.1f %s' % (size
, unit
)