[gnome-games/sudoku-tube] Minor refactor



commit b9b75e5caf26eb379005b4038c1d5906a8ec9335
Author: Zhang Sen <zh jesse gmail com>
Date:   Sat Nov 14 15:15:11 2009 +0800

    Minor refactor

 gnome-sudoku/src/lib/contact_selector.py |   43 ++++++++++++++----------------
 1 files changed, 20 insertions(+), 23 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/contact_selector.py b/gnome-sudoku/src/lib/contact_selector.py
index 8356784..88b027f 100644
--- a/gnome-sudoku/src/lib/contact_selector.py
+++ b/gnome-sudoku/src/lib/contact_selector.py
@@ -50,12 +50,12 @@ class _Contact:
 class ContactList:
     """the list of contacts from *one* connection"""
 
-    def __init__(self, list_store, conn):
-        self._list_store = list_store
+    def __init__(self, conn, add_contact_func, update_presence_func):
         self._conn = conn
+        self._add_contact_func = add_contact_func
+        self._update_presence_func = update_presence_func
 
-        # dict of {handle=>Contact}
-        # If ListStore supports searching, this _contact_list is not needed.
+        # contacts in this connection: dict of {handle=>Contact}
         self._contact_list = {}
 
         self._conn.call_when_ready(self._connection_ready_cb)
@@ -78,7 +78,7 @@ class ContactList:
         self._ensure_channel()
 
     def _ensure_channel(self):
-        """Request the channel we needed to fetch contact-list"""
+        """Request the channel in order to fetch contact-list"""
         # TODO is it ok to include only these two groups?
         groups = ["subscribe", "publish"]
         for group in groups:
@@ -122,10 +122,11 @@ class ContactList:
     def _get_contact_attributes_cb(self, attributes):
         logger.debug("Got contact information")
         for handle, member in attributes.iteritems():
-            contact_info = self._parse_member_attributes(member)
-            contact = _Contact(*contact_info)
+            cid, calias, cpres = self._parse_member_attributes(member)
+            contact = _Contact(cid, calias, cpres)
             if handle not in self._contact_list:
-                self._add_contact(handle, contact)
+                self._contact_list[handle] = contact
+                self._add_contact_func(self._conn, handle, contact)
 
     def _parse_member_attributes(self, member):
         """Parse contact information out from dbus.Struct member"""
@@ -145,10 +146,6 @@ class ContactList:
         assert alias is not None
         return (contact_id, alias, presence)
 
-    def _add_contact(self, handle, contact):
-        self._contact_list[handle] = contact
-        self._list_store.add_contact(self._conn, handle, contact)
-
     def _contact_presence_changed_cb(self, presences):
         for handle, presence in presences.iteritems():
             if handle in self._contact_list:
@@ -156,25 +153,23 @@ class ContactList:
                     self._contact_list[handle].alias,
                     self._contact_list[handle].presence[1],
                     presence[1]))
-                self._update_contact_presence(handle, presence)
+                self._contact_list[handle].presence = presence
+                self._update_presence_func(self._conn, handle, presence)
+
             else:
                 # request its info and consequently add it to the list
                 self._request_contact_info([handle])
 
-    def _update_contact_presence(self, handle, presence):
-        logger.debug("update presence of %s" % self._contact_list[handle].alias)
-        self._contact_list[handle].presence = presence
-        self._list_store.update_contact_presence(self._conn, handle, presence)
-
     def _error_cb(self, *args):
         logger.error("Error happends: %s" % args)
 
 
 class ContactListStore(gtk.ListStore):
-    """ListStore that contains contact list from (all connections of) telepathy
+    """A listStore that maintains all the connections from telepathy and also
+    the contact lists from these connections.
     """
 
-    # indices of columns that are of interest to display
+    # index of column that are of interest to display
     COLUMN_CONTACT = 2
 
     def __init__(self):
@@ -202,9 +197,11 @@ class ContactListStore(gtk.ListStore):
         logger.debug("Got %s telepathy connections" % len(connections))
 
         for conn in connections:
-            self._contact_lists[conn] = ContactList(self, conn)
+            self._contact_lists[conn] = ContactList(conn,
+                    self._add_contact,
+                    self._update_contact_presence)
 
-    def add_contact(self, conn, handle, contact):
+    def _add_contact(self, conn, handle, contact):
         """Add a row to ListStore.
 
         conn and handle are used to uniquely refer to a certain row/contact"""
@@ -212,7 +209,7 @@ class ContactListStore(gtk.ListStore):
                 (handle, contact.alias))
         self.append((conn, handle, contact))
 
-    def update_contact_presence(self, conn, handle, presence):
+    def _update_contact_presence(self, conn, handle, presence):
         # TODO: bruteforce
         for row in self:
             if (row[0], row[1]) == (conn, handle):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]