[Telepathy-commits] [telepathy-doc/master] Receive messages to a dialog box
Davyd Madeley
davyd at madeley.id.au
Mon Mar 9 19:02:03 PDT 2009
---
docs/examples/pygtk_chat_client/ChatWindow.py | 63 +++++++++++++++++++++++++
docs/examples/pygtk_chat_client/example.py | 53 +++++++++++---------
2 files changed, 92 insertions(+), 24 deletions(-)
diff --git a/docs/examples/pygtk_chat_client/ChatWindow.py b/docs/examples/pygtk_chat_client/ChatWindow.py
index 51c8dff..95eca39 100644
--- a/docs/examples/pygtk_chat_client/ChatWindow.py
+++ b/docs/examples/pygtk_chat_client/ChatWindow.py
@@ -1,6 +1,14 @@
import gtk
import gobject
+from telepathy.interfaces import CHANNEL, \
+ CHANNEL_TYPE_TEXT, \
+ CHANNEL_INTERFACE_MESSAGES
+
+from example import generic_reply, Contact
+
+DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
+
class ChatWindow(gtk.Window):
def __init__(self, channel):
super(ChatWindow, self).__init__()
@@ -25,4 +33,59 @@ class ChatWindow(gtk.Window):
self.connect('destroy', lambda s: s.channel.close())
+ if CHANNEL_INTERFACE_MESSAGES in channel.interfaces:
+ channel[CHANNEL_INTERFACE_MESSAGES].connect_to_signal(
+ 'MessageReceived', self._message_received)
+ channel[DBUS_PROPERTIES].Get(CHANNEL_INTERFACE_MESSAGES,
+ 'PendingMessages',
+ reply_handler = self._get_pending_messages,
+ error_handler = self.sm.error)
+ else:
+ print 'Iface.Messages not supported, falling back'
+ channel[CHANNEL_TYPE_TEXT].connect_to_signal(
+ 'Received', self._simple_message_received)
+ channel[CHANNEL_TYPE_TEXT].ListPendingMessages(False,
+ reply_handler = self._simple_get_pending_messages,
+ error_handler = self.sm.error)
+
+ def _get_pending_messages(self, messages):
+ for message in messages:
+ self._message_received(message)
+
+ def _message_received(self, message):
+ print 'Received message'
+
+ # we need to acknowledge the message
+ msg_id = message[0]['pending-message-id']
+ self.channel[CHANNEL_TYPE_TEXT].AcknowledgePendingMessages([msg_id],
+ reply_handler = generic_reply,
+ error_handler = self.sm.error)
+
+ def _simple_get_pending_messages(self, messages):
+ for message in messages:
+ self._simple_message_received(*message)
+
+ def _simple_message_received(self, msg_id, timestamp, sender,
+ msg_type, flags, content):
+
+ def _simple_sender_known():
+ contact = self.sm.contacts[sender]
+ self._update_buffer(contact.alias, content)
+
+ # we need to acknowledge the message
+ self.channel[CHANNEL_TYPE_TEXT].AcknowledgePendingMessages([msg_id],
+ reply_handler = generic_reply,
+ error_handler = self.sm.error)
+
+ if sender not in self.sm.contacts:
+ # we need to look the sender up
+ Contact.lookup_from_handles(self.sm, [sender],
+ callback = _simple_sender_known)
+ else:
+ _simple_sender_known()
+
+ def _update_buffer (self, sender, msg):
+ iter = self.buffer.get_end_iter()
+ self.buffer.insert(iter, '%s: %s\n' % (sender, msg))
+
gobject.type_register(ChatWindow)
diff --git a/docs/examples/pygtk_chat_client/example.py b/docs/examples/pygtk_chat_client/example.py
index 8e5c264..c8b0f31 100755
--- a/docs/examples/pygtk_chat_client/example.py
+++ b/docs/examples/pygtk_chat_client/example.py
@@ -18,7 +18,6 @@ from telepathy.interfaces import CONNECTION_MANAGER, \
CHANNEL, \
CHANNEL_TYPE_CONTACT_LIST, \
CHANNEL_TYPE_TEXT, \
- CHANNEL_INTERFACE_MESSAGES, \
CHANNEL_INTERFACE_GROUP
from telepathy.constants import CONNECTION_STATUS_CONNECTED, \
CONNECTION_STATUS_DISCONNECTED, \
@@ -136,29 +135,7 @@ class ContactList(Channel):
print 'Channel does not implement Group... strange'
def _members_cb(self, handles):
- # these are the interfaces we're seeking
- interfaces = [
- CONNECTION_INTERFACE_ALIASING,
- CONNECTION_INTERFACE_SIMPLE_PRESENCE,
- ]
-
- # work out what interfaces are available
- interfaces = list (set(interfaces) & set(self.conn.interfaces))
- interfaces += [ CONNECTION ]
-
- # look them up via the contacts interface
- if CONNECTION_INTERFACE_CONTACTS in self.conn.interfaces:
- self.conn[CONNECTION_INTERFACE_CONTACTS].GetContactAttributes(
- handles, interfaces, False,
- reply_handler = self._attributes_cb,
- error_handler = self.sm.error)
-
- def _attributes_cb(self, map):
- for handle, attributes in map.iteritems():
- contact = Contact (self.sm, handle, attributes)
- self.sm.contacts[handle] = contact
-
- self.sm.contacts_updated(map.keys())
+ Contact.lookup_from_handles(self.sm, handles)
class TextChannel(Channel):
channel_type = CHANNEL_TYPE_TEXT
@@ -179,6 +156,34 @@ class Contact(object):
elif key == CONNECTION_INTERFACE_SIMPLE_PRESENCE + '/presence':
self.presence = value
+ @classmethod
+ def lookup_from_handles(self, sm, handles, callback = None):
+ # these are the interfaces we're seeking
+ interfaces = [
+ CONNECTION_INTERFACE_ALIASING,
+ CONNECTION_INTERFACE_SIMPLE_PRESENCE,
+ ]
+
+ # work out what interfaces are available
+ interfaces = list (set(interfaces) & set(sm.conn.interfaces))
+ interfaces += [ CONNECTION ]
+
+ def _new_handle_attributes_cb(map):
+ for handle, attributes in map.iteritems():
+ contact = Contact (sm, handle, attributes)
+ sm.contacts[handle] = contact
+
+ sm.contacts_updated(map.keys())
+
+ if callback is not None: callback()
+
+ # look them up via the contacts interface
+ if CONNECTION_INTERFACE_CONTACTS in sm.conn.interfaces:
+ sm.conn[CONNECTION_INTERFACE_CONTACTS].GetContactAttributes(
+ handles, interfaces, False,
+ reply_handler = _new_handle_attributes_cb,
+ error_handler = sm.error)
+
def get_state(self):
return self.presence[0]
--
1.5.6.5
More information about the telepathy-commits
mailing list