[Telepathy-commits] [telepathy-doc/master] Add new RosterWindow class

Davyd Madeley davyd at madeley.id.au
Mon Mar 9 05:35:55 PDT 2009


---
 docs/examples/pygtk_chat_client/RosterWindow.py |   27 +++++++++++++++
 docs/examples/pygtk_chat_client/example.py      |   41 ++++++++++++++++++-----
 2 files changed, 59 insertions(+), 9 deletions(-)
 create mode 100644 docs/examples/pygtk_chat_client/RosterWindow.py

diff --git a/docs/examples/pygtk_chat_client/RosterWindow.py b/docs/examples/pygtk_chat_client/RosterWindow.py
new file mode 100644
index 0000000..f7d0b22
--- /dev/null
+++ b/docs/examples/pygtk_chat_client/RosterWindow.py
@@ -0,0 +1,27 @@
+import gtk
+import gobject
+
+class RosterWindow(gtk.Window):
+
+    def __init__(self, sm):
+        self.sm = sm
+        super(RosterWindow, self).__init__()
+
+        vbox = gtk.VBox()
+        self.add(vbox)
+
+        sw = gtk.ScrolledWindow()
+        vbox.pack_start (sw)
+
+        tv = gtk.TreeView()
+        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'
+
+gobject.type_register(RosterWindow)
diff --git a/docs/examples/pygtk_chat_client/example.py b/docs/examples/pygtk_chat_client/example.py
index ab34b36..6279ba6 100755
--- a/docs/examples/pygtk_chat_client/example.py
+++ b/docs/examples/pygtk_chat_client/example.py
@@ -154,14 +154,22 @@ class Contact(object):
     def get_status(self):
         return self.presence[1]
 
-class StateMachine(object):
-    def __init__(self, account, password):
+class StateMachine(gobject.GObject):
+    __gsignals__ = {
+        'contacts-updated'  : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                               ()),
+    }
+
+    def __init__(self):
+        super(StateMachine, self).__init__()
+
+        self.contacts = {}
+
+    def tp_connect(self, account, password):
         """e.g. account  = 'bob at example.com/test'
                 password = 'bigbob'
         """
 
-        self.contacts = {}
-
         reg = telepathy.client.ManagerRegistry()
         reg.LoadManagers()
 
@@ -180,7 +188,7 @@ class StateMachine(object):
     def error(self, error):
         print "Telepathy Error: %s" % error
         print "Disconnecting..."
-        self.disconnect()
+        self.tp_disconnect()
 
     def connection_ready(self, conn):
         print "Connection Ready"
@@ -217,28 +225,43 @@ class StateMachine(object):
     def contacts_updated(self, handles):
         print ' -- Contacts updated --'
 
-        for contact in [ self.contacts[h] for h in handles if h in self.contacts]:
+        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())
 
-    def disconnect(self):
+        self.emit('contacts-updated')
+
+    def tp_disconnect(self):
+        print 'Disconnecting...'
         try:
             self.conn.disconnect()
         except:
             gtk.main_quit()
 
+gobject.type_register(StateMachine)
+
 if __name__ == '__main__':
     import getpass
+
+    from RosterWindow import RosterWindow
+
     account = sys.argv[1]
     password = getpass.getpass()
     
-    sm = StateMachine(account, password)
+    sm = StateMachine()
+    roster = RosterWindow(sm)
+    roster.show()
+
+    sm.tp_connect(account, password)
+    roster.connect ('destroy', lambda w: sm.tp_disconnect())
         
     try:
         print 'Running...'
         gtk.main()
     except KeyboardInterrupt:
         print "Terminating connection..."
-        sm.disconnect()
+        sm.tp_disconnect()
         # reengage the mainloop so that we can disconnect cleanly
         gtk.main()
-- 
1.5.6.5




More information about the telepathy-commits mailing list