1 # Copyright (C) 2008, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 from zeroinstall
.support
import tasks
6 from zeroinstall
.injector
.model
import Interface
, Feed
, stable
, testing
, developer
, stability_levels
7 from zeroinstall
.injector
.iface_cache
import iface_cache
8 from zeroinstall
.injector
import writer
, namespaces
, gpg
9 from zeroinstall
.gtkui
import help_box
12 from logging
import warn
14 from dialog
import DialogResponse
, Template
15 from impl_list
import ImplementationList
20 _dialogs
= {} # Interface -> Properties
33 def format_para(para
):
34 lines
= [l
.strip() for l
in para
.split('\n')]
35 return ' '.join(lines
)
37 def have_source_for(policy
, interface
):
38 # Note: we don't want to actually fetch the source interfaces at
39 # this point, so we check whether:
40 # - We have a feed of type 'src' (not fetched), or
41 # - We have a source implementation in a regular feed
43 for f
in interface
.feeds
:
44 if f
.machine
== 'src':
46 # Don't have any src feeds. Do we have a source implementation
47 # as part of a regular feed?
48 impls
= interface
.implementations
.values()
49 for f
in policy
.usable_feeds(interface
):
51 feed_iface
= iface_cache
.get_interface(f
.uri
)
52 if feed_iface
.implementations
:
53 impls
.extend(feed_iface
.implementations
.values())
54 except zeroinstall
.NeedDownload
:
55 pass # OK, will get called again later
57 warn("Failed to load feed '%s': %s", f
.uri
, str(ex
))
59 if x
.machine
== 'src':
64 def __init__(self
, widgets
):
65 description
= widgets
.get_widget('description')
66 description
.connect('button-press-event', self
.button_press
)
68 self
.buffer = description
.get_buffer()
69 self
.heading_style
= self
.buffer.create_tag(underline
= True, scale
= 1.2)
70 self
.link_style
= self
.buffer.create_tag(underline
= True, foreground
= 'blue')
71 description
.set_size_request(-1, 100)
73 def button_press(self
, tv
, bev
):
74 if bev
.type == gtk
.gdk
.BUTTON_PRESS
and bev
.button
== 1:
75 x
, y
= tv
.window_to_buffer_coords(tv
.get_window_type(bev
.window
),
76 int(bev
.x
), int(bev
.y
))
77 itr
= tv
.get_iter_at_location(x
, y
)
78 if itr
and self
.link_style
in itr
.get_tags():
79 if not itr
.begins_tag(self
.link_style
):
80 itr
.backward_to_tag_toggle(self
.link_style
)
82 end
.forward_to_tag_toggle(self
.link_style
)
83 target
= itr
.get_text(end
).strip()
85 browser
.open_in_browser(target
)
87 def set_details(self
, interface
):
89 heading_style
= self
.heading_style
91 buffer.delete(buffer.get_start_iter(), buffer.get_end_iter())
93 iter = buffer.get_start_iter()
95 buffer.insert_with_tags(iter,
96 '%s ' % interface
.get_name(), heading_style
)
97 buffer.insert(iter, '(%s)' % interface
.summary
)
99 buffer.insert(iter, '\n%s\n' % interface
.uri
)
101 # (converts to local time)
102 if interface
.last_modified
:
103 buffer.insert(iter, '\nLast upstream change: %s' % time
.ctime(interface
.last_modified
))
105 if interface
.last_checked
:
106 buffer.insert(iter, '\nLast checked: %s' % time
.ctime(interface
.last_checked
))
108 last_check_attempt
= iface_cache
.get_last_check_attempt(interface
.uri
)
109 if last_check_attempt
:
110 if interface
.last_checked
and interface
.last_checked
>= last_check_attempt
:
111 pass # Don't bother reporting successful attempts
113 buffer.insert(iter, '\nLast check attempt: %s (failed or in progress)' %
114 time
.ctime(last_check_attempt
))
116 buffer.insert_with_tags(iter, '\n\nDescription\n', heading_style
)
118 paragraphs
= [format_para(p
) for p
in (interface
.description
or "-").split('\n\n')]
120 buffer.insert(iter, '\n\n'.join(paragraphs
))
121 buffer.insert(iter, '\n')
124 for x
in interface
.get_metadata(namespaces
.XMLNS_IFACE
, 'homepage'):
126 buffer.insert(iter, '\n')
128 buffer.insert(iter, 'Homepage: ')
129 buffer.insert_with_tags(iter, '%s\n' % x
.content
, self
.link_style
)
131 buffer.insert_with_tags(iter, '\nSignatures\n', heading_style
)
132 sigs
= iface_cache
.get_cached_signatures(interface
.uri
)
135 if isinstance(sig
, gpg
.ValidSig
):
137 details
= sig
.get_details()
139 if item
[0] in ('pub', 'uid') and len(item
) > 9:
142 buffer.insert_with_tags(iter, 'Valid signature by "%s"\n- Dated: %s\n- Fingerprint: %s\n' %
143 (name
, time
.ctime(sig
.get_timestamp()), sig
.fingerprint
))
144 if not sig
.is_trusted():
145 if interface
.uri
.startswith('/'):
146 buffer.insert_with_tags(iter, 'WARNING: This key is not in the trusted list\n')
148 buffer.insert_with_tags(iter, 'WARNING: This key is not in the trusted list (either you removed it, or '
149 'you trust one of the other signatures)\n')
151 buffer.insert_with_tags(iter, '%s\n' % sig
)
153 buffer.insert_with_tags(iter, 'No signature information (old style interface or out-of-date cache)\n')
160 def __init__(self
, policy
, interface
, widgets
):
162 self
.interface
= interface
164 self
.model
= gtk
.ListStore(str, str, bool)
166 self
.description
= Description(widgets
)
168 self
.lines
= self
.build_model()
169 for line
in self
.lines
:
170 self
.model
.append(line
)
172 add_remote_feed_button
= widgets
.get_widget('add_remote_feed')
173 add_remote_feed_button
.connect('clicked', lambda b
: add_remote_feed(policy
, widgets
.get_widget(), interface
))
175 add_local_feed_button
= widgets
.get_widget('add_local_feed')
176 add_local_feed_button
.connect('clicked', lambda b
: add_local_feed(policy
, interface
))
178 self
.remove_feed_button
= widgets
.get_widget('remove_feed')
179 def remove_feed(button
):
180 model
, iter = self
.tv
.get_selection().get_selected()
181 feed_uri
= model
[iter][Feeds
.URI
]
182 for x
in interface
.feeds
:
183 if x
.uri
== feed_uri
:
185 interface
.extra_feeds
.remove(x
)
186 writer
.save_interface(interface
)
190 dialog
.alert(self
.get_toplevel(),
191 _("Can't remove '%s' as you didn't add it.") % feed_uri
)
193 raise Exception("Missing feed '%s'!" % feed_uri
)
194 self
.remove_feed_button
.connect('clicked', remove_feed
)
196 self
.tv
= widgets
.get_widget('feeds_list')
197 self
.tv
.set_model(self
.model
)
198 text
= gtk
.CellRendererText()
199 self
.tv
.append_column(gtk
.TreeViewColumn('Source', text
, text
= Feeds
.URI
, sensitive
= Feeds
.USED
))
200 self
.tv
.append_column(gtk
.TreeViewColumn('Arch', text
, text
= Feeds
.ARCH
, sensitive
= Feeds
.USED
))
202 sel
= self
.tv
.get_selection()
203 sel
.set_mode(gtk
.SELECTION_BROWSE
)
204 sel
.connect('changed', self
.sel_changed
)
205 sel
.select_path((0,))
207 def build_model(self
):
208 usable_feeds
= frozenset(self
.policy
.usable_feeds(self
.interface
))
209 unusable_feeds
= frozenset(self
.interface
.feeds
) - usable_feeds
211 out
= [[self
.interface
.uri
, None, True]]
213 if self
.interface
.feeds
:
214 for feed
in usable_feeds
:
215 out
.append([feed
.uri
, feed
.arch
, True])
216 for feed
in unusable_feeds
:
217 out
.append([feed
.uri
, feed
.arch
, False])
220 def sel_changed(self
, sel
):
221 model
, miter
= sel
.get_selected()
222 if not miter
: return # build in progress
223 iface
= model
[miter
][Feeds
.URI
]
224 # Only enable removing user_override feeds
225 enable_remove
= False
226 for x
in self
.interface
.feeds
:
230 self
.remove_feed_button
.set_sensitive( enable_remove
)
231 self
.description
.set_details(iface_cache
.get_interface(iface
))
234 new_lines
= self
.build_model()
235 if new_lines
!= self
.lines
:
236 self
.lines
= new_lines
238 for line
in self
.lines
:
239 self
.model
.append(line
)
240 self
.tv
.get_selection().select_path((0,))
242 self
.sel_changed(self
.tv
.get_selection())
250 def __init__(self
, policy
, interface
, show_versions
= False):
253 widgets
= Template('interface_properties')
255 self
.interface
= interface
257 window
= widgets
.get_widget('interface_properties')
259 window
.set_title('Properties for ' + interface
.get_name())
260 window
.set_default_size(-1, gtk
.gdk
.screen_height() / 3)
262 self
.compile_button
= widgets
.get_widget('compile')
263 self
.compile_button
.connect('clicked', lambda b
: compile.compile(policy
, interface
))
264 window
.set_default_response(gtk
.RESPONSE_CANCEL
)
266 def response(dialog
, resp
):
267 if resp
== gtk
.RESPONSE_CANCEL
:
269 elif resp
== gtk
.RESPONSE_HELP
:
270 properties_help
.display()
271 window
.connect('response', response
)
273 notebook
= widgets
.get_widget('interface_notebook')
276 feeds
= Feeds(policy
, interface
, widgets
)
278 stability
= widgets
.get_widget('preferred_stability')
279 stability
.set_active(0)
280 if interface
.stability_policy
:
281 i
= [stable
, testing
, developer
].index(interface
.stability_policy
)
283 warn("Unknown stability policy %s", interface
.stability_policy
)
287 stability
.set_active(i
)
289 def set_stability_policy(combo
):
290 i
= stability
.get_active()
294 name
= stability
.get_model()[i
][0].lower()
295 new_stability
= stability_levels
[name
]
296 interface
.set_stability_policy(new_stability
)
297 writer
.save_interface(interface
)
299 stability
.connect('changed', set_stability_policy
)
301 self
.use_list
= ImplementationList(policy
, interface
, widgets
)
305 feeds
.tv
.grab_focus()
311 window
.connect('destroy', lambda s
: policy
.watchers
.remove(updated
))
312 policy
.watchers
.append(updated
)
319 self
.window
.destroy()
321 def shade_compile(self
):
322 self
.compile_button
.set_sensitive(have_source_for(self
.policy
, self
.interface
))
324 def update_list(self
):
325 ranked_items
= self
.policy
.solver
.details
.get(self
.interface
, None)
326 if ranked_items
is None:
327 # The Solver didn't get this far, but we should still display them!
328 ranked_items
= [(impl
, "(solve aborted before here)")
329 for impl
in self
.interface
.implementations
.values()]
331 self
.use_list
.set_items(ranked_items
)
334 def add_remote_feed(policy
, parent
, interface
):
336 d
= gtk
.MessageDialog(parent
, 0, gtk
.MESSAGE_QUESTION
, gtk
.BUTTONS_CANCEL
,
337 _('Enter the URL of the new source of implementations of this interface:'))
338 d
.add_button(gtk
.STOCK_ADD
, gtk
.RESPONSE_OK
)
339 d
.set_default_response(gtk
.RESPONSE_OK
)
342 align
= gtk
.VBox(False, 0)
343 align
.set_border_width(4)
345 d
.vbox
.pack_start(align
)
346 entry
.set_activates_default(True)
352 error_label
= gtk
.Label('')
353 error_label
.set_padding(4, 4)
354 align
.pack_start(error_label
)
360 error_label
.set_text(message
)
366 got_response
= DialogResponse(d
)
368 tasks
.check(got_response
)
369 resp
= got_response
.response
372 if resp
== gtk
.RESPONSE_OK
:
374 url
= entry
.get_text()
376 raise zeroinstall
.SafeException(_('Enter a URL'))
377 fetch
= policy
.fetcher
.download_and_import_feed(url
, iface_cache
)
379 d
.set_sensitive(False)
381 d
.set_sensitive(True)
384 iface
= iface_cache
.get_interface(url
)
386 d
.set_sensitive(True)
388 error('Failed to read interface')
390 if not iface
.feed_for
:
391 error("Feed '%s' is not a feed for '%s'." % (iface
.get_name(), interface
.get_name()))
392 elif interface
.uri
not in iface
.feed_for
:
393 error("This is not a feed for '%s'.\nOnly for:\n%s" %
394 (interface
.uri
, '\n'.join(iface
.feed_for
)))
395 elif iface
.uri
in [f
.uri
for f
in interface
.feeds
]:
396 error("Feed from '%s' has already been added!" % iface
.uri
)
398 interface
.extra_feeds
.append(Feed(iface
.uri
, arch
= None, user_override
= True))
399 writer
.save_interface(interface
)
402 except zeroinstall
.SafeException
, ex
:
407 except Exception, ex
:
409 traceback
.print_exc()
410 policy
.handler
.report_error(ex
)
412 def add_local_feed(policy
, interface
):
413 sel
= gtk
.FileSelection(_('Select XML feed file'))
414 sel
.set_has_separator(False)
416 from zeroinstall
.injector
import reader
417 feed
= sel
.get_filename()
419 feed_targets
= policy
.get_feed_targets(feed
)
420 if interface
not in feed_targets
:
421 raise Exception("Not a valid feed for '%s'; this is a feed for:\n%s" %
423 '\n'.join([f
.uri
for f
in feed_targets
])))
424 if interface
.get_feed(feed
):
425 dialog
.alert(None, 'This feed is already registered.')
427 interface
.extra_feeds
.append(Feed(feed
, user_override
= True, arch
= None))
429 writer
.save_interface(interface
)
431 reader
.update_from_cache(interface
)
433 except Exception, ex
:
434 dialog
.alert(None, "Error in feed file '%s':\n\n%s" % (feed
, str(ex
)))
436 sel
.ok_button
.connect('clicked', ok
)
437 sel
.cancel_button
.connect('clicked', lambda b
: sel
.destroy())
440 def edit(policy
, interface
, show_versions
= False):
441 assert isinstance(interface
, Interface
)
442 if interface
in _dialogs
:
443 _dialogs
[interface
].destroy()
444 _dialogs
[interface
] = Properties(policy
, interface
, show_versions
)
446 properties_help
= help_box
.HelpBox("Injector Properties Help",
447 ('Interface properties', """
448 This window displays information about an interface. There are two tabs at the top: \
449 Feeds shows the places where the injector looks for implementations of the interface, while \
450 Versions shows the list of implementations found (from all feeds) in order of preference."""),
452 ('The Feeds tab', """
453 At the top is a list of feeds. By default, the injector uses the full name of the interface \
454 as the default feed location (so if you ask it to run the program "http://foo/bar.xml" then it will \
455 by default get the list of versions by downloading "http://foo/bar.xml".
457 You can add and remove feeds using the buttons on the right. The main feed may also add \
458 some extra feeds itself. If you've checked out a developer version of a program, you can use \
459 the 'Add Local Feed...' button to let the injector know about it, for example.
461 Below the list of feeds is a box describing the selected one:
463 - At the top is its short name.
464 - Below that is the address (a URL or filename).
465 - 'Last upstream change' shows the version of the cached copy of the interface file.
466 - 'Last checked' is the last time a fresh copy of the upstream interface file was \
468 - Then there is a longer description of the interface."""),
470 ('The Versions tab', """
471 This tab shows a list of all known implementations of the interface, from all the feeds. \
472 The columns have the following meanings:
474 Version gives the version number. High-numbered versions are considered to be \
475 better than low-numbered ones.
477 Released gives the date this entry was added to the feed.
479 Stability is 'stable' if the implementation is believed to be stable, 'buggy' if \
480 it is known to contain serious bugs, and 'testing' if its stability is not yet \
481 known. This information is normally supplied and updated by the author of the \
482 software, but you can override their rating by right-clicking here (overridden \
483 values are shown in upper-case). You can also use the special level 'preferred'.
485 Fetch indicates how much data needs to be downloaded to get this version if you don't \
486 have it. If the implementation has already been downloaded to your computer, \
487 it will say (cached). (local) means that you installed this version manually and \
488 told Zero Install about it by adding a feed. (package) means that this version \
489 is provided by your distribution's package manager, not by Zero Install. \
490 In off-line mode, only cached implementations are considered for use.
492 Arch indicates what kind of computer system the implementation is for, or 'any' \
493 if it works with all types of system.
496 The implementations are listed in the injector's currently preferred order (the one \
497 at the top will actually be used). Usable implementations all come before unusable \
500 Unusable ones are those for incompatible \
501 architectures, those marked as 'buggy', versions explicitly marked as incompatible with \
502 another interface you are using and, in off-line mode, uncached implementations. Unusable \
503 implementations are shown crossed out.
505 For the usable implementations, the order is as follows:
507 - Preferred implementations come first.
509 - Then, if network use is set to 'Minimal', cached implementations come before \
512 - Then, implementations at or above the selected stability level come before all others.
514 - Then, higher-numbered versions come before low-numbered ones.
516 - Then cached come before non-cached (for 'Full' network use mode).
520 If there is no binary available for your system then you may be able to compile one from \
521 source by clicking on the Compile button. If no source is available, the Compile button will \