[Telepathy-commits] [telepathy-doc/master] Build a roster window

Davyd Madeley davyd at madeley.id.au
Mon Mar 9 08:24:20 PDT 2009


---
 docs/examples/pygtk_chat_client/RosterWindow.py |   48 +++++++++++++++++++++--
 docs/examples/pygtk_chat_client/example.py      |   44 ++++++++++++++++----
 2 files changed, 79 insertions(+), 13 deletions(-)

diff --git a/docs/examples/pygtk_chat_client/RosterWindow.py b/docs/examples/pygtk_chat_client/RosterWindow.py
index f7d0b22..f5b46ac 100644
--- a/docs/examples/pygtk_chat_client/RosterWindow.py
+++ b/docs/examples/pygtk_chat_client/RosterWindow.py
@@ -7,21 +7,61 @@ class RosterWindow(gtk.Window):
         self.sm = sm
         super(RosterWindow, self).__init__()
 
+        self.set_default_size(200, 400)
+
         vbox = gtk.VBox()
         self.add(vbox)
 
         sw = gtk.ScrolledWindow()
         vbox.pack_start (sw)
 
-        tv = gtk.TreeView()
+        self.contacts = gtk.ListStore(gobject.TYPE_PYOBJECT)
+        tv = gtk.TreeView(self.contacts)
         sw.add(tv)
 
         vbox.show_all()
 
-        self.contacts = gtk.ListStore(gobject.TYPE_PYOBJECT)
         self.sm.connect('contacts-updated', self._contacts_updated)
 
-    def _contacts_updated(self, sm):
-        print '::contacts-updated'
+        # set up the treeview
+        renderer = gtk.CellRendererText()
+        column = gtk.TreeViewColumn('Contacts', renderer)
+        tv.append_column(column)
+
+        def text_func(column, cell, model, iter):
+            contact = model.get_value(iter, 0)
+            cell.set_property('markup', '%s\n<span size="small" style="italic">%s</span>' % (
+                contact.alias, contact.get_status()))
+
+        column.set_cell_data_func(renderer, text_func)
+
+        def sort_func(model, iter1, iter2):
+            a = model.get_value(iter1, 0)
+            b = model.get_value(iter2, 0)
+            return cmp(a,b)
+
+        self.contacts.set_default_sort_func(sort_func)
+        self.contacts.set_sort_column_id(-1, gtk.SORT_ASCENDING)
+
+    def _contacts_updated(self, sm, updated):
+        # build a set of contacts in the state machine
+        contacts = set(self.sm.contacts.values())
+        updated = set(updated)
+
+        # iterate through the liststore, updating the contacts
+        for row in self.contacts:
+            contact = row[0]
+
+            if contact in updated or \
+               contact not in contacts:
+                self.contacts.remove(row.iter)
+                continue
+
+            contacts.remove(contact)
+
+        # iterate through the remaining items in the set, adding them to the
+        # list store
+        for contact in contacts:
+            self.contacts.append ((contact,))
 
 gobject.type_register(RosterWindow)
diff --git a/docs/examples/pygtk_chat_client/example.py b/docs/examples/pygtk_chat_client/example.py
index 6279ba6..7ac6e6e 100755
--- a/docs/examples/pygtk_chat_client/example.py
+++ b/docs/examples/pygtk_chat_client/example.py
@@ -22,6 +22,13 @@ from telepathy.interfaces import CONNECTION_MANAGER, \
                                  CHANNEL_INTERFACE_GROUP
 from telepathy.constants import CONNECTION_STATUS_CONNECTED, \
                                 CONNECTION_STATUS_DISCONNECTED, \
+                                CONNECTION_PRESENCE_TYPE_AVAILABLE, \
+                                CONNECTION_PRESENCE_TYPE_BUSY, \
+                                CONNECTION_PRESENCE_TYPE_AWAY, \
+                                CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY, \
+                                CONNECTION_PRESENCE_TYPE_UNSET, \
+                                CONNECTION_PRESENCE_TYPE_UNKNOWN, \
+                                CONNECTION_PRESENCE_TYPE_OFFLINE, \
                                 HANDLE_TYPE_CONTACT, \
                                 HANDLE_TYPE_LIST, \
                                 HANDLE_TYPE_GROUP
@@ -151,13 +158,38 @@ class Contact(object):
             elif key == CONNECTION_INTERFACE_SIMPLE_PRESENCE + '/presence':
                 self.presence = value
 
+    def get_state(self):
+        return self.presence[0]
+
     def get_status(self):
-        return self.presence[1]
+        if self.presence[2] != '':
+            return self.presence[2]
+        else:
+            return self.presence[1]
+
+    def __repr__(self):
+        return 'Contact(%s)' % self.contact_id
+
+    def __cmp__(self, other):
+        ordering_map = {
+            CONNECTION_PRESENCE_TYPE_AVAILABLE      : 0,
+            CONNECTION_PRESENCE_TYPE_BUSY           : 1,
+            CONNECTION_PRESENCE_TYPE_AWAY           : 2,
+            CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY  : 3,
+            CONNECTION_PRESENCE_TYPE_UNSET          : 4,
+            CONNECTION_PRESENCE_TYPE_UNKNOWN        : 4,
+            CONNECTION_PRESENCE_TYPE_OFFLINE        : 5,
+        }
+
+        c = cmp (ordering_map[self.get_state()], ordering_map[other.get_state()])
+        if c != 0: return c
+        c = cmp (self.alias, other.alias)
+        return c
 
 class StateMachine(gobject.GObject):
     __gsignals__ = {
         'contacts-updated'  : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                               ()),
+                               (gobject.TYPE_PYOBJECT,)),
     }
 
     def __init__(self):
@@ -223,15 +255,9 @@ class StateMachine(gobject.GObject):
         self.contacts_updated(presences.keys())
 
     def contacts_updated(self, handles):
-        print ' -- Contacts updated --'
-
         contacts = [ self.contacts[h] for h in handles if h in self.contacts]
 
-        for contact in contacts:
-            print "%s: %s (%s)" % (
-                contact.contact_id, contact.alias, contact.get_status())
-
-        self.emit('contacts-updated')
+        self.emit('contacts-updated', contacts)
 
     def tp_disconnect(self):
         print 'Disconnecting...'
-- 
1.5.6.5




More information about the telepathy-commits mailing list