Added --refresh option.
[zeroinstall.git] / iface_browser.py
blobf4f48ee832c09563231208bc7daf72760fec1cf1
1 import gtk
3 from model import Interface
4 import properties
5 from gui import policy, pretty_size
7 class InterfaceBrowser(gtk.ScrolledWindow):
8 model = None
9 root = None
10 edit_properties = None
12 INTERFACE = 0
13 INTERFACE_NAME = 1
14 VERSION = 2
15 SUMMARY = 3
16 DOWNLOAD_SIZE = 4
18 def __init__(self):
19 self.edit_properties = gtk.Action('edit_properties',
20 'Interface Properties...',
21 'Set which implementation of this interface to use.',
22 gtk.STOCK_PROPERTIES)
23 self.edit_properties.set_property('sensitive', False)
25 gtk.ScrolledWindow.__init__(self)
26 self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
27 self.set_shadow_type(gtk.SHADOW_IN)
29 self.model = gtk.TreeStore(object, str, str, str, str)
30 self.tree_view = tree_view = gtk.TreeView(self.model)
32 text = gtk.CellRendererText()
34 column = gtk.TreeViewColumn(_('Interface'), text,
35 text = InterfaceBrowser.INTERFACE_NAME)
36 tree_view.append_column(column)
38 column = gtk.TreeViewColumn(_('Version'), text,
39 text = InterfaceBrowser.VERSION)
40 tree_view.append_column(column)
42 column = gtk.TreeViewColumn(_('Fetch'), text,
43 text = InterfaceBrowser.DOWNLOAD_SIZE)
44 tree_view.append_column(column)
46 column = gtk.TreeViewColumn(_('Description'), text,
47 text = InterfaceBrowser.SUMMARY)
48 tree_view.append_column(column)
50 self.add(tree_view)
51 tree_view.show()
53 tree_view.set_enable_search(True)
55 selection = tree_view.get_selection()
57 def sel_changed(sel):
58 store, iter = sel.get_selected()
59 self.edit_properties.set_property('sensitive', iter != None)
60 selection.connect('changed', sel_changed)
62 def button_press(tree_view, bev):
63 if bev.button != 1 or bev.type != gtk.gdk._2BUTTON_PRESS:
64 return False
65 pos = tree_view.get_path_at_pos(int(bev.x), int(bev.y))
66 if not pos:
67 return False
68 path, col, x, y = pos
69 properties.edit(self.model[path][InterfaceBrowser.INTERFACE])
70 tree_view.connect('button-press-event', button_press)
72 def edit_selected(action):
73 store, iter = selection.get_selected()
74 assert iter
75 properties.edit(self.model[iter][InterfaceBrowser.INTERFACE])
76 self.edit_properties.connect('activate', edit_selected)
78 self.connect('destroy', lambda s: policy.watchers.remove(self.build_tree))
79 policy.watchers.append(self.build_tree)
81 def set_root(self, root):
82 assert isinstance(root, Interface)
83 self.root = root
84 policy.recalculate() # Calls build_tree
86 def build_tree(self):
87 self.model.clear()
88 parent = None
89 def add_node(parent, iface):
90 iter = self.model.append(parent)
91 self.model[iter][InterfaceBrowser.INTERFACE] = iface
92 self.model[iter][InterfaceBrowser.INTERFACE_NAME] = iface.get_name()
93 self.model[iter][InterfaceBrowser.SUMMARY] = iface.summary
95 impl = policy.implementation.get(iface, None)
96 if impl:
97 self.model[iter][InterfaceBrowser.VERSION] = impl.get_version()
98 if policy.get_cached(impl):
99 fetch = '(cached)'
100 else:
101 src = policy.get_best_source(impl)
102 fetch = pretty_size(src.size)
103 self.model[iter][InterfaceBrowser.DOWNLOAD_SIZE] = fetch
104 for child in impl.dependencies.values():
105 add_node(iter, policy.get_interface(child.interface))
106 else:
107 self.model[iter][InterfaceBrowser.VERSION] = '(choose)'
108 add_node(None, self.root)
109 self.tree_view.expand_all()