aa60ea9c2de8c6b89cff183db246a3dade1e723c
[hermes] / package / src / org / maemo / hermes / gui / mapcontact.py
1 import gtk
2 import hildon
3 from ctypes import *
4 from pygobject import *
5
6 class MapContact(hildon.PannableArea):
7     """Widget which shows a list of friends from various feeds and allows
8        the mapping to a particular contact.
9        
10        Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
11        Released under the Artistic Licence."""
12
13
14     # -----------------------------------------------------------------------
15     def __init__(self, friends, contact):
16         """Constructor. Passed a list of `friends' and the contact we're mapping."""
17         
18         hildon.PannableArea.__init__(self)
19         self.friends = friends
20         self.contact = contact
21         self.treestore = gtk.ListStore(gtk.gdk.Pixbuf, str, gtk.gdk.Pixbuf, gobject.TYPE_PYOBJECT)
22         
23         accounts = {}
24         _tick    = gtk.icon_theme_get_default().load_icon('widgets_tickmark_list', 48, 0)
25         
26         self.contact_mapped = False
27         mapped_iter = None
28         best_iter   = None
29         last_name   = '' # Keep track for when we switch name positions and focus there
30         for key in sorted(self.friends.keys(), cmp = lambda a, b: cmp(a.lower(), b.lower())):
31             friend = self.friends[key]
32             if friend.get_source() not in accounts:
33                 try:
34                     accounts[friend.get_source()] = gtk.gdk.pixbuf_new_from_file("/opt/hermes/share/account-%s.png" % (friend.get_source()))
35                 except glib.GError, e:
36                     accounts[friend.get_source()] = None
37                     print "Couldn't find icon:", e.message
38               
39             pixbuf = None
40             if friend.get_contact() is not None:
41                 if friend.get_contact() == contact:
42                     pixbuf = _tick
43                     self.contact_mapped = True
44                     mapped_iter = self.treestore.append([accounts[friend.get_source()], friend.get_name(), pixbuf, friend])
45                 else:
46                     continue
47             else:
48                 position = self.treestore.append([accounts[friend.get_source()], friend.get_name(), pixbuf, friend])
49                 if cmp(last_name, contact.get_name()) != cmp(friend.get_name(), contact.get_name()):
50                     best_iter = position
51
52                 last_name = friend.get_name()
53         
54         self.treeview = gtk.TreeView(self.treestore)
55         hildon.hildon_gtk_tree_view_set_ui_mode(self.treeview, gtk.HILDON_UI_MODE_EDIT)
56         
57         self.treeview.append_column(gtk.TreeViewColumn('Account', gtk.CellRendererPixbuf(), pixbuf = 0))
58         self.treeview.append_column(gtk.TreeViewColumn('Name', gtk.CellRendererText(), text = 1))
59         
60         cell = gtk.CellRendererPixbuf()
61         cell.set_property('xalign', 1.0)
62         self.treeview.append_column(gtk.TreeViewColumn('Picture', cell, pixbuf = 2))
63         
64         if mapped_iter:
65             path = self.treestore.get_path(mapped_iter)
66             self.treeview.get_selection().select_path(path)
67             self.treeview.scroll_to_cell(path)
68         elif best_iter:
69             path = self.treestore.get_path(best_iter)
70             self.treeview.get_selection().unselect_all()
71             self.treeview.scroll_to_cell(path)
72         else:
73             self.treeview.get_selection().unselect_all()
74           
75         self.add(self.treeview)
76         self.set_size_request_policy(hildon.SIZE_REQUEST_CHILDREN)
77
78       
79     # -----------------------------------------------------------------------
80     def get_selected_friend(self):
81         """Return the selected friend, or `None' if none."""
82     
83         (model, iter) = self.treeview.get_selection().get_selected()
84         if not iter:
85             return None
86              
87         return model.get_value(iter, 3)
88
89
90 _account_selected = gobject.signal_new('account-selected', MapContact, gobject.SIGNAL_ACTION, gobject.TYPE_NONE, [gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])
91