1 import gtk
, os
, gobject
, sys
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
8 from checking
import CheckingBox
15 class GUIPolicy(Policy
):
18 monitored_downloads
= None
19 checking
= None # GtkDialog ("Checking for updates...")
20 original_implementation
= None
22 def __init__(self
, interface
, prog_args
, download_only
, refresh
):
23 Policy
.__init
__(self
, interface
)
27 self
.monitored_downloads
= []
30 self
.window
= mainwindow
.MainWindow(prog_args
, download_only
)
31 root
= policy
.get_interface(policy
.root
)
32 self
.window
.browser
.set_root(root
)
35 if root
.name
is not None:
36 self
.checking
= CheckingBox(root
)
37 def checking_destroyed(c
):
39 if self
.ready
is False or self
.versions_changed():
43 download_box
.download_with_gui(self
.window
, prog_args
,
44 run_afterwards
= not download_only
)
45 self
.checking
.connect('destroy', checking_destroyed
)
47 self
.refresh_all(force
= False)
49 def monitor_download(self
, dl
):
50 self
.monitored_downloads
.append(dl
)
52 error_stream
= dl
.start()
53 def error_ready(src
, cond
):
54 got
= os
.read(src
.fileno(), 100)
57 self
.monitored_downloads
.remove(dl
)
58 if len(self
.monitored_downloads
) == 0:
59 gobject
.source_remove(self
.pulse
)
60 self
.window
.progress
.hide()
63 data
= dl
.error_stream_closed()
64 self
.check_signed_data(dl
, data
)
65 except download
.DownloadError
, ex
:
66 dialog
.alert(self
.window
,
67 "Error downloading interface '%s':\n\n%s" %
68 (dl
.interface
.uri
, ex
))
69 except InvalidInterface
, ex
:
70 dialog
.alert(self
.window
,
71 "Syntax error in downloaded interface '%s':\n\n%s" %
72 (dl
.interface
.uri
, ex
))
73 except SafeException
, ex
:
74 dialog
.alert(self
.window
,
75 "Error updating interface '%s':\n\n%s" %
76 (dl
.interface
.uri
, ex
))
77 if len(self
.monitored_downloads
) == 0 and self
.checking
:
78 self
.checking
.updates_done(self
.versions_changed())
80 dl
.error_stream_data(got
)
83 gobject
.io_add_watch(error_stream
,
84 gobject
.IO_IN | gobject
.IO_HUP
,
87 if self
.pulse
is None:
90 self
.checking
.progress
.pulse()
92 self
.window
.progress
.pulse()
94 self
.pulse
= gobject
.timeout_add(50, pulse
)
95 self
.window
.progress
.show()
97 def recalculate(self
):
98 Policy
.recalculate(self
)
103 print >>sys
.stderr
, "Your version of the injector is very old. " \
104 "Try upgrading (http://0install.net/injector.html)"
106 self
.window
.set_response_sensitive(gtk
.RESPONSE_OK
, self
.ready
)
108 def confirm_trust_keys(self
, interface
, sigs
, iface_xml
):
110 trust_box
.confirm_trust(interface
, sigs
, iface_xml
)
119 def get_best_source(self
, impl
):
120 """Return the best download source for this implementation."""
121 return impl
.download_sources
[0]
123 # XXX: Remove this. Moved to Policy.
124 def refresh_all(self
, force
= True):
125 for x
in self
.walk_interfaces():
126 self
.begin_iface_download(x
, force
)
128 def abort_all_downloads(self
):
129 for x
in self
.monitored_downloads
[:]:
132 def set_original_implementations(self
):
133 assert self
.original_implementation
is None
134 self
.original_implementation
= policy
.implementation
.copy()
136 def versions_changed(self
):
137 """Return whether we have now chosen any different implementations.
138 If so, we want to show the dialog to the user to confirm the new ones."""
140 if not self
.original_implementation
:
142 return True # Shouldn't happen?
143 if len(self
.original_implementation
) != len(self
.implementation
):
146 for iface
in self
.original_implementation
:
147 old
= self
.original_implementation
[iface
]
149 print "Old interface is None"
151 new
= self
.implementation
.get(iface
, None)
153 print "New interface is None"
160 def pretty_size(size
):
164 return '%d bytes' % size
166 for unit
in ('Kb', 'Mb', 'Gb', 'Tb'):
170 return '%.1f %s' % (size
, unit
)