[Telepathy-commits] [telepathy-doc/master] Open a chat window when a text channel opens

Davyd Madeley davyd at madeley.id.au
Mon Mar 9 09:12:06 PDT 2009


---
 docs/examples/pygtk_chat_client/ChatWindow.py   |   28 ++++++++++++++++
 docs/examples/pygtk_chat_client/RosterWindow.py |    8 +++++
 docs/examples/pygtk_chat_client/example.py      |   39 +++++++++++++++++++++-
 3 files changed, 73 insertions(+), 2 deletions(-)
 create mode 100644 docs/examples/pygtk_chat_client/ChatWindow.py

diff --git a/docs/examples/pygtk_chat_client/ChatWindow.py b/docs/examples/pygtk_chat_client/ChatWindow.py
new file mode 100644
index 0000000..51c8dff
--- /dev/null
+++ b/docs/examples/pygtk_chat_client/ChatWindow.py
@@ -0,0 +1,28 @@
+import gtk
+import gobject
+
+class ChatWindow(gtk.Window):
+    def __init__(self, channel):
+        super(ChatWindow, self).__init__()
+
+        self.sm = channel.sm
+        self.channel = channel
+
+        self.set_default_size(300, 300)
+        self.set_title('Chat with %s' % channel.target_id)
+
+        vbox = gtk.VBox()
+        self.add(vbox)
+
+        sw = gtk.ScrolledWindow()
+        vbox.pack_start (sw)
+
+        self.buffer = gtk.TextBuffer()
+        tv = gtk.TextView(self.buffer)
+        sw.add(tv)
+
+        vbox.show_all()
+
+        self.connect('destroy', lambda s: s.channel.close())
+
+gobject.type_register(ChatWindow)
diff --git a/docs/examples/pygtk_chat_client/RosterWindow.py b/docs/examples/pygtk_chat_client/RosterWindow.py
index f5b46ac..55b1b31 100644
--- a/docs/examples/pygtk_chat_client/RosterWindow.py
+++ b/docs/examples/pygtk_chat_client/RosterWindow.py
@@ -1,6 +1,8 @@
 import gtk
 import gobject
 
+from ChatWindow import ChatWindow
+
 class RosterWindow(gtk.Window):
 
     def __init__(self, sm):
@@ -22,6 +24,7 @@ class RosterWindow(gtk.Window):
         vbox.show_all()
 
         self.sm.connect('contacts-updated', self._contacts_updated)
+        self.sm.connect('new-chat', self._new_chat)
 
         # set up the treeview
         renderer = gtk.CellRendererText()
@@ -64,4 +67,9 @@ class RosterWindow(gtk.Window):
         for contact in contacts:
             self.contacts.append ((contact,))
 
+    def _new_chat(self, sm, channel):
+        w = ChatWindow(channel)
+        w.set_transient_for(self)
+        w.show()
+
 gobject.type_register(RosterWindow)
diff --git a/docs/examples/pygtk_chat_client/example.py b/docs/examples/pygtk_chat_client/example.py
index be98ddb..8e5c264 100755
--- a/docs/examples/pygtk_chat_client/example.py
+++ b/docs/examples/pygtk_chat_client/example.py
@@ -86,17 +86,21 @@ class Connection(telepathy.client.Connection):
 
         if CONNECTION_INTERFACE_REQUESTS in self.interfaces:
             self[CONNECTION_INTERFACE_REQUESTS].EnsureChannel(d,
-                reply_handler = lambda a, b, c: channel_obj(self, b, a),
+                reply_handler = lambda a, b, c: channel_obj(self, b, c, a),
                 error_handler = self._ensure_channel_error)
         else:
             self.sm.error("Requests interface unavailable, get a better CM")
 
 class Channel(telepathy.client.Channel):
-    def __init__(self, conn, object_path, yours = False):
+    def __init__(self, conn, object_path, props, yours = False):
         self.conn = conn
         self.sm = conn.sm
         self.yours = yours
 
+        self.target_id = props[CHANNEL + '.TargetID']
+        self.handle = props[CHANNEL + '.TargetHandle']
+        self.handle_type = props[CHANNEL + '.TargetHandleType']
+
         print 'Channel came up... requesting interfaces'
         super(Channel, self).__init__(conn.service_name, object_path)
 
@@ -104,6 +108,13 @@ class Channel(telepathy.client.Channel):
                                    reply_handler = self._interfaces_cb,
                                    error_handler = self.sm.error)
     
+    def close(self):
+        self[CHANNEL].Close(reply_handler = generic_reply,
+                            error_handler = self.sm.error)
+
+    def get_target_id(self):
+        return self.target_id
+
     def _interfaces_cb(self, interfaces):
         self.interfaces = interfaces
 
@@ -149,6 +160,12 @@ class ContactList(Channel):
 
         self.sm.contacts_updated(map.keys())
 
+class TextChannel(Channel):
+    channel_type = CHANNEL_TYPE_TEXT
+
+    def ready(self):
+        self.sm.new_text_channel(self)
+
 class Contact(object):
     def __init__(self, sm, handle, attributes={}):
         self.sm = sm
@@ -194,6 +211,8 @@ class StateMachine(gobject.GObject):
     __gsignals__ = {
         'contacts-updated'  : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                                (gobject.TYPE_PYOBJECT,)),
+        'new-chat'          : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                               (gobject.TYPE_PYOBJECT,)),
     }
 
     def __init__(self):
@@ -239,11 +258,20 @@ class StateMachine(gobject.GObject):
             conn[CONNECTION_INTERFACE_SIMPLE_PRESENCE].connect_to_signal(
                 'PresencesChanged', self._presences_changed)
 
+        if CONNECTION_INTERFACE_REQUESTS in conn.interfaces:
+            conn[CONNECTION_INTERFACE_REQUESTS].connect_to_signal(
+                'NewChannels', self._new_channels)
+        else:
+            self.sm.error("Requests interface unavailable, get a better CM")
+
         # request the contact lists
         print 'Requesting roster...'
         self.conn.ensure_channel (ContactList, HANDLE_TYPE_LIST, 'subscribe')
         self.conn.ensure_channel (ContactList, HANDLE_TYPE_LIST, 'publish')
 
+    def new_text_channel(self, channel):
+        self.emit('new-chat', channel)
+
     def _aliases_changed(self, aliases):
         for handle, alias in aliases:
             if handle not in self.contacts: continue
@@ -258,6 +286,13 @@ class StateMachine(gobject.GObject):
 
         self.contacts_updated(presences.keys())
 
+    def _new_channels(self, channels):
+        for channel_path, props in channels:
+            channel_type = props[CHANNEL + '.ChannelType']
+
+            if channel_type == TextChannel.channel_type:
+                TextChannel(self.conn, channel_path, props)
+
     def contacts_updated(self, handles):
         contacts = [ self.contacts[h] for h in handles if h in self.contacts]
 
-- 
1.5.6.5



More information about the telepathy-commits mailing list