Need to check how proxies interact with pooling
[zeroinstall.git] / zeroinstall / 0launch-gui / mainwindow.py
blobc0943566c5885e4991adb877208438184d44ddb6
1 # Copyright (C) 2009, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import gtk
5 import sys
6 from logging import info
8 from zeroinstall import _, translation
9 from zeroinstall import SafeException
10 from zeroinstall.support import tasks, pretty_size
11 from zeroinstall.injector import download, iface_cache
12 from iface_browser import InterfaceBrowser
13 import dialog
14 from zeroinstall.gtkui import gtkutils
15 from zeroinstall.gtkui import help_box
17 ngettext = translation.ngettext
19 SHOW_PREFERENCES = 0
21 class MainWindow:
22 progress = None
23 progress_area = None
24 browser = None
25 window = None
26 cancel_download_and_run = None
27 policy = None
28 comment = None
29 systray_icon = None
30 systray_icon_blocker = None
32 def __init__(self, policy, widgets, download_only, select_only = False):
33 self.policy = policy
34 self.select_only = select_only
36 def update_ok_state():
37 self.window.set_response_sensitive(gtk.RESPONSE_OK, policy.solver.ready)
38 if policy.solver.ready and self.window.get_focus() is None:
39 run_button.grab_focus()
40 policy.watchers.append(update_ok_state)
42 self.window = widgets.get_widget('main')
43 self.window.set_default_size(gtk.gdk.screen_width() * 2 / 5, 300)
45 self.progress = widgets.get_widget('progress')
46 self.progress_area = widgets.get_widget('progress_area')
47 self.comment = widgets.get_widget('comment')
49 widgets.get_widget('stop').connect('clicked', lambda b: policy.handler.abort_all_downloads())
51 self.refresh_button = widgets.get_widget('refresh')
53 # Tree view
54 self.browser = InterfaceBrowser(policy, widgets)
56 prefs = widgets.get_widget('preferences')
57 self.window.action_area.set_child_secondary(prefs, True)
59 # Glade won't let me add this to the template!
60 if select_only:
61 run_button = dialog.MixedButton(_("_Select"), gtk.STOCK_EXECUTE, button = gtk.ToggleButton())
62 elif download_only:
63 run_button = dialog.MixedButton(_("_Download"), gtk.STOCK_EXECUTE, button = gtk.ToggleButton())
64 else:
65 run_button = dialog.MixedButton(_("_Run"), gtk.STOCK_EXECUTE, button = gtk.ToggleButton())
66 self.window.add_action_widget(run_button, gtk.RESPONSE_OK)
67 run_button.show_all()
68 run_button.set_flags(gtk.CAN_DEFAULT)
69 self.run_button = run_button
71 run_button.grab_focus()
73 def response(dialog, resp):
74 if resp in (gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT):
75 self.window.destroy()
76 sys.exit(1)
77 elif resp == gtk.RESPONSE_OK:
78 if self.cancel_download_and_run:
79 self.cancel_download_and_run.trigger()
80 if run_button.get_active():
81 self.cancel_download_and_run = tasks.Blocker("cancel downloads")
82 self.download_and_run(run_button, self.cancel_download_and_run)
83 elif resp == gtk.RESPONSE_HELP:
84 gui_help.display()
85 elif resp == SHOW_PREFERENCES:
86 import preferences
87 preferences.show_preferences(policy.config, notify_cb = lambda: policy.solve_with_downloads())
88 self.window.connect('response', response)
89 self.window.realize() # Make busy pointer work, even with --systray
91 def destroy(self):
92 self.window.destroy()
94 def show(self):
95 self.window.show()
97 def set_response_sensitive(self, response, sensitive):
98 self.window.set_response_sensitive(response, sensitive)
100 @tasks.async
101 def download_and_run(self, run_button, cancelled):
102 try:
103 if not self.select_only:
104 downloaded = self.policy.download_uncached_implementations()
106 if downloaded:
107 # We need to wait until everything is downloaded...
108 blockers = [downloaded, cancelled]
109 yield blockers
110 tasks.check(blockers)
112 if cancelled.happened:
113 return
115 uncached = self.policy.get_uncached_implementations()
116 else:
117 uncached = None # (we don't care)
119 if uncached:
120 missing = '\n- '.join([_('%(iface_name)s %(impl_version)s') % {'iface_name': iface.get_name(), 'impl_version': impl.get_version()} for iface, impl in uncached])
121 dialog.alert(self.window, _('Not all downloads succeeded; cannot run program.\n\nFailed to get:') + '\n- ' + missing)
122 else:
123 sels = self.policy.solver.selections
124 doc = sels.toDOM()
125 reply = doc.toxml('utf-8')
126 sys.stdout.write(('Length:%8x\n' % len(reply)) + reply)
127 self.window.destroy()
128 sys.exit(0) # Success
129 except SystemExit:
130 raise
131 except download.DownloadAborted as ex:
132 run_button.set_active(False)
133 # Don't bother reporting this to the user
134 except Exception as ex:
135 run_button.set_active(False)
136 self.report_exception(ex)
138 def update_download_status(self, only_update_visible = False):
139 """Called at regular intervals while there are downloads in progress,
140 and once at the end. Update the display."""
141 monitored_downloads = self.policy.handler.monitored_downloads
143 self.browser.update_download_status(only_update_visible)
145 if not monitored_downloads:
146 self.progress_area.hide()
147 self.window.window.set_cursor(None)
148 return
150 if not self.progress_area.get_property('visible'):
151 self.progress_area.show()
152 self.window.window.set_cursor(gtkutils.get_busy_pointer())
154 any_known = False
155 done = total = self.policy.handler.total_bytes_downloaded # Completed downloads
156 n_downloads = self.policy.handler.n_completed_downloads
157 # Now add downloads in progress...
158 for x in monitored_downloads:
159 if x.status != download.download_fetching: continue
160 n_downloads += 1
161 if x.expected_size:
162 any_known = True
163 so_far = x.get_bytes_downloaded_so_far()
164 total += x.expected_size or max(4096, so_far) # Guess about 4K for feeds/icons
165 done += so_far
167 progress_text = '%s / %s' % (pretty_size(done), pretty_size(total))
168 self.progress.set_text(
169 ngettext('Downloading one file (%(progress)s)',
170 'Downloading %(number)d files (%(progress)s)', n_downloads)
171 % {'progress': progress_text, 'number': n_downloads})
173 if total == 0 or (n_downloads < 2 and not any_known):
174 self.progress.pulse()
175 else:
176 self.progress.set_fraction(float(done) / total)
178 def set_message(self, message):
179 import pango
180 self.comment.set_text(message)
181 attrs = pango.AttrList()
182 attrs.insert(pango.AttrWeight(pango.WEIGHT_BOLD, end_index = len(message)))
183 self.comment.set_attributes(attrs)
184 self.comment.show()
186 def use_systray_icon(self):
187 try:
188 self.systray_icon = gtk.status_icon_new_from_icon_name("zeroinstall")
189 except Exception as ex:
190 info(_("No system tray support: %s"), ex)
191 else:
192 root_iface = iface_cache.iface_cache.get_interface(self.policy.root)
193 self.systray_icon.set_tooltip(_('Checking for updates for %s') % root_iface.get_name())
194 self.systray_icon.connect('activate', self.remove_systray_icon)
195 self.systray_icon_blocker = tasks.Blocker('Tray icon clicked')
197 def remove_systray_icon(self, i = None):
198 assert self.systray_icon, i
199 self.show()
200 self.systray_icon.set_visible(False)
201 self.systray_icon = None
202 self.systray_icon_blocker.trigger()
203 self.systray_icon_blocker = None
205 def report_exception(self, ex, tb = None):
206 if not isinstance(ex, SafeException):
207 import traceback
208 traceback.print_exception(ex, None, tb)
209 if self.systray_icon:
210 self.systray_icon.set_blinking(True)
211 self.systray_icon.set_tooltip(str(ex) + '\n' + _('(click for details)'))
212 else:
213 dialog.alert(self.window, str(ex) or repr(ex))
215 gui_help = help_box.HelpBox(_("Injector Help"),
216 (_('Overview'), '\n' +
217 _("""A program is made up of many different components, typically written by different \
218 groups of people. Each component is available in multiple versions. Zero Install is \
219 used when starting a program. Its job is to decide which implementation of each required \
220 component to use.
222 Zero Install starts with the program you want to run (like 'The Gimp') and chooses an \
223 implementation (like 'The Gimp 2.2.0'). However, this implementation \
224 will in turn depend on other components, such as 'GTK' (which draws the menus \
225 and buttons). Thus, it must choose implementations of \
226 each dependency (each of which may require further components, and so on).""")),
228 (_('List of components'), '\n' +
229 _("""The main window displays all these components, and the version of each chosen \
230 implementation. The top-most one represents the program you tried to run, and each direct \
231 child is a dependency. The 'Fetch' column shows the amount of data that needs to be \
232 downloaded, or '(cached)' if it is already on this computer.
234 If you are happy with the choices shown, click on the Download (or Run) button to \
235 download (and run) the program.""")),
237 (_('Choosing different versions'), '\n' +
238 _("""To control which implementations (versions) are chosen you can click on Preferences \
239 and adjust the network policy and the overall stability policy. These settings affect \
240 all programs run using Zero Install.
242 Alternatively, you can edit the policy of an individual component by clicking on the \
243 button at the end of its line in the table and choosing "Show Versions" from the menu. \
244 See that dialog's help text for more information.""") + '\n'),
246 (_('Reporting bugs'), '\n' +
247 _("""To report a bug, right-click over the component which you think contains the problem \
248 and choose 'Report a Bug...' from the menu. If you don't know which one is the cause, \
249 choose the top one (i.e. the program itself). The program's author can reassign the \
250 bug if necessary, or switch to using a different version of the library.""") + '\n'),
252 (_('The cache'), '\n' +
253 _("""Each version of a program that is downloaded is stored in the Zero Install cache. This \
254 means that it won't need to be downloaded again each time you run the program. The \
255 "0store manage" command can be used to view the cache.""") + '\n'),