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