Better formatting of error messages.
[zeroinstall.git] / impl_list.py
blob7fa5e22e5c0a4a1588ec9ebfe8c072e9958cb480
1 import gtk
2 import model
3 from gui import policy, pretty_size
4 import writer
6 def popup_menu(bev, values, fn):
7 menu = gtk.Menu()
8 for value in values:
9 if value is None:
10 item = gtk.SeparatorMenuItem()
11 else:
12 item = gtk.MenuItem(str(value).capitalize())
13 item.connect('activate', lambda item, v=value: fn(v))
14 item.show()
15 menu.append(item)
16 menu.popup(None, None, None, bev.button, bev.time)
18 # Columns
19 ITEM = 0
20 ARCH = 1
21 STABILITY = 2
22 VERSION = 3
23 CACHED = 4
24 ID = 5
25 SIZE = 6
26 UNUSABLE = 7
28 class ImplementationList(gtk.ScrolledWindow):
29 tree_view = None
30 model = None
32 def __init__(self, interface):
33 gtk.ScrolledWindow.__init__(self, None, None)
34 self.set_shadow_type(gtk.SHADOW_IN)
36 self.model = gtk.ListStore(object, str, str, str, bool, str, str, bool)
37 self.tree_view = gtk.TreeView(self.model)
39 text = gtk.CellRendererText()
40 text_strike = gtk.CellRendererText()
41 toggle = gtk.CellRendererToggle()
43 stability = gtk.TreeViewColumn('Stability', text, text = STABILITY)
45 for column in (gtk.TreeViewColumn('Version', text, text = VERSION),
46 stability,
47 gtk.TreeViewColumn('C', toggle, active = CACHED),
48 gtk.TreeViewColumn('Arch', text, text = ARCH),
49 gtk.TreeViewColumn('Size', text, text = SIZE),
50 gtk.TreeViewColumn('ID', text_strike, text = ID,
51 strikethrough = UNUSABLE)):
52 self.tree_view.append_column(column)
54 self.add(self.tree_view)
56 def button_press(tree_view, bev):
57 if bev.button not in (1, 3):
58 return False
59 pos = tree_view.get_path_at_pos(int(bev.x), int(bev.y))
60 if not pos:
61 return False
62 path, col, x, y = pos
63 if col == stability:
64 impl = self.model[path][ITEM]
65 upstream = impl.upstream_stability or model.testing
66 choices = model.stability_levels.values()
67 choices.sort()
68 choices.reverse()
69 def set(new):
70 if isinstance(new, model.Stability):
71 impl.user_stability = new
72 else:
73 impl.user_stability = None
74 writer.save_interface(interface)
75 policy.recalculate()
76 popup_menu(bev, ['Unset (%s)' % upstream, None] + choices,
77 set)
78 self.tree_view.connect('button-press-event', button_press)
80 def get_selection(self):
81 return self.tree_view.get_selection()
83 def set_items(self, items):
84 self.model.clear()
85 for item in items:
86 new = self.model.append()
87 self.model[new][ITEM] = item
88 self.model[new][VERSION] = item.get_version()
89 self.model[new][CACHED] = policy.get_cached(item)
90 if item.user_stability:
91 self.model[new][STABILITY] = str(item.user_stability).upper()
92 else:
93 self.model[new][STABILITY] = item.upstream_stability or \
94 model.testing
95 self.model[new][ARCH] = item.arch or 'any'
96 self.model[new][ID] = item.id
97 self.model[new][SIZE] = pretty_size(item.size)
98 self.model[new][UNUSABLE] = policy.is_unusable(item)
100 def clear(self):
101 self.model.clear()