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