Renamed.
[zeroinstall.git] / trust_box.py
blobed04fee3e73e6231aaa4df12e1a162744b9429ec
1 import gtk
2 from model import SafeException
4 import gui
5 import dialog
6 import gpg
7 import trust
9 def pretty_fp(fp):
10 s = fp[0:4]
11 for x in range(4, len(fp), 4):
12 s += ' ' + fp[x:x + 4]
13 return s
15 class TrustBox(dialog.Dialog):
16 model = None
17 tree_view = None
18 interfaces = None # Interface -> (xml, [Keys])
20 def __init__(self):
21 dialog.Dialog.__init__(self)
22 self.set_title('Confirm trust')
23 self.interfaces = {}
25 label = gtk.Label('Please confirm that you trust '
26 'these keys to sign software updates:')
27 label.set_padding(8, 8)
28 self.vbox.pack_start(label, False, True, 0)
30 swin = gtk.ScrolledWindow()
31 self.vbox.pack_start(swin, True, True, 0)
32 swin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
33 swin.set_shadow_type(gtk.SHADOW_IN)
34 swin.set_border_width(8)
36 self.model = gtk.ListStore(str, object)
37 self.tree_view = gtk.TreeView(self.model)
38 self.tree_view.get_selection().set_mode(gtk.SELECTION_NONE)
39 swin.add(self.tree_view)
41 self.tree_view.set_size_request(-1, 100)
43 text = gtk.CellRendererText()
45 for column in [gtk.TreeViewColumn('Key fingerprint', text, text = 0)]:
46 self.tree_view.append_column(column)
48 self.vbox.show_all()
50 self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
51 self.add_button(gtk.STOCK_ADD, gtk.RESPONSE_OK)
52 self.set_default_response(gtk.RESPONSE_OK)
54 def response(box, resp):
55 if resp == gtk.RESPONSE_OK:
56 self.trust_keys()
57 self.hide()
58 self.connect('response', response)
60 self.connect('delete-event', lambda box, ev: True)
62 def confirm_trust(self, interface, sigs, iface_xml):
63 valid_sigs = [s for s in sigs if isinstance(s, gpg.ValidSig)]
64 if not valid_sigs:
65 raise SafeException('No valid signatures found')
67 self.interfaces[interface] = (iface_xml, valid_sigs)
69 self.rebuild_model()
70 self.tree_view.expand_all()
71 trust_box.present()
73 def rebuild_model(self):
74 ifaces = self.interfaces.keys()
75 ifaces.sort() # Keep the order stable
76 self.model.clear()
77 for i in ifaces:
78 for sig in self.interfaces[i][1]:
79 titer = self.model.append()
80 self.model[titer][0] = pretty_fp(sig.fingerprint)
81 self.model[titer][1] = sig
83 def trust_keys(self):
84 for row in self.model:
85 sig = row[1]
86 print "Trusing", sig.fingerprint
87 trust.trust_db.trust_key(sig.fingerprint)
89 ifaces = self.interfaces
90 self.interfaces = {}
91 self.rebuild_model()
92 for i in ifaces:
93 iface_xml, sigs = ifaces[i]
94 if not gui.policy.update_interface_if_trusted(i, sigs, iface_xml):
95 raise Exception('Bug: still not trusted!!')
97 # Singleton, to avoid opening too many windows at once
98 trust_box = TrustBox()