From e18c4468e432f0b4af05c11ca125f60cd488327a Mon Sep 17 00:00:00 2001 From: Dafydd Harries Date: Wed, 18 Apr 2007 19:59:55 +0000 Subject: Merge branch 'master' of git://dev.laptop.org/chat-activity Conflicts: chat.py --- diff --git a/chat.py b/chat.py index 1398f55..f103036 100644 --- a/chat.py +++ b/chat.py @@ -28,19 +28,22 @@ from sugar.graphics.roundbox import RoundBox from sugar.graphics.button import Button from sugar.graphics.xocolor import XoColor from sugar.graphics.units import points_to_pixels as px +from sugar.presence import presenceservice -tp_name = 'org.freedesktop.Telepathy' -tp_path = '/org/freedesktop/Telepathy' -cm_name = tp_name + '.ConnectionManager' +from telepathy.client import Connection, Channel -tp_name = 'org.freedesktop.Telepathy' -tp_path = '/org/freedesktop/Telepathy' -tp_cm_iface = tp_name + '.ConnectionManager' -tp_cm_path = tp_path + '/ConnectionManager' -tp_conn_iface = tp_name + '.Connection' -tp_chan_iface = tp_name + '.Channel' -tp_chan_type_text = tp_chan_iface + '.Type.Text' -tp_conn_aliasing = tp_conn_iface + '.Interface.Aliasing' +from telepathy.interfaces import ( + CONN_INTERFACE, PROPERTIES_INTERFACE, + CHANNEL_INTERFACE_GROUP, CONN_INTERFACE_ALIASING, + CHANNEL_TYPE_TEXT) + +CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo' + +from telepathy.constants import ( + CONNECTION_HANDLE_TYPE_NONE, CONNECTION_HANDLE_TYPE_CONTACT, + CONNECTION_HANDLE_TYPE_ROOM, CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, + CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED, + CONNECTION_STATUS_CONNECTING) room = 'chat@conference.olpc.collabora.co.uk' @@ -58,66 +61,72 @@ class Chat(Activity): self.owner_color = profile.get_color() self.owner_nickname = profile.get_nick_name() - self.add_text(self.owner_nickname, self.make_owner_icon(), 'Hello') + #self.add_text(self.owner_nickname, self.make_owner_icon(), 'Hello') # test long line - self.add_text(self.owner_nickname, self.make_owner_icon(), - 'one two three four five six seven eight nine ten ' + - 'one two three four five six seven eight nine ten ' + - 'one two three four five six seven eight nine ten ' + - 'one two three four five six seven eight nine ten') + #self.add_text(self.owner_nickname, self.make_owner_icon(), + # 'one two three four five six seven eight nine ten ' + + # 'one two three four five six seven eight nine ten ' + + # 'one two three four five six seven eight nine ten ' + + # 'one two three four five six seven eight nine ten') + + pservice = presenceservice.get_instance() bus = dbus.Bus() - cm = bus.get_object(tp_cm_iface + '.gabble', tp_cm_path + '/gabble') - cm_iface = dbus.Interface(cm, tp_cm_iface) - name, path = cm_iface.RequestConnection('jabber', { - 'account': 'test@olpc.collabora.co.uk', - 'password': 'test' - }) - conn = bus.get_object(name, path) - conn_iface = dbus.Interface(conn, tp_conn_iface) - conn_iface.connect_to_signal('StatusChanged', self.status_changed_cb) - conn_iface.Connect() + name, path = pservice.get_preferred_connection() + conn = Connection(name, path) + conn[CONN_INTERFACE].connect_to_signal('StatusChanged', self.status_changed_cb) self.conn = conn - self.conn_iface = conn_iface self.conn_name = name - self.text_iface = None + self.chan = None - self.connect('destroy', self.destroy_cb) - - def destroy_cb(self, _): - print 'destroy' - self.conn_iface.Disconnect() + status = conn[CONN_INTERFACE].GetStatus() + if status == CONNECTION_STATUS_CONNECTED: + print "connected" + self.join_room () def received_cb(self, id, timestamp, sender, type, flags, text): try: - aliasing_iface = dbus.Interface(self.conn, tp_conn_aliasing) - # XXX: cache this - alias = aliasing_iface.RequestAliases([sender])[0] + # XXX: Shoule use PS instead of directly use TP + alias = self.conn[CONN_INTERFACE_ALIASING].RequestAliases([sender])[0] print '%s: %s' % (alias, text) + try: + handle = self.chan[CHANNEL_INTERFACE_GROUP].GetHandleOwners([sender])[0] + infos = self.conn[CONN_INTERFACE_BUDDY_INFO].GetProperties(handle) + color = infos['color'] + except dbus.DBusException, e: + print "failed to query buddy infos:", e + color = "#000000,#ffffff" icon = CanvasIcon( icon_name='theme:stock-buddy', - xo_color=XoColor('#000000,#ffffff')) + xo_color=XoColor(color)) self.add_text(alias, icon, text) except Exception, e: print e + def join_room(self): + try: + bus = dbus.Bus() + chan_handle = self.conn[CONN_INTERFACE].RequestHandles(2, [room])[0] + chan_path = self.conn[CONN_INTERFACE].RequestChannel(CHANNEL_TYPE_TEXT, + 2, chan_handle, True) + self.chan = Channel(self.conn_name, chan_path) + self.chan[CHANNEL_TYPE_TEXT].connect_to_signal('Received', self.received_cb) + + # XXX Muc shouldn't be semianonymous by default + self.chan[PROPERTIES_INTERFACE].SetProperties([(0, False)]) + + except Exception, e: + print e + + def status_changed_cb(self, status, reason): - if status == 0: - try: - print 'connected' - bus = dbus.Bus() - chan_handle = self.conn_iface.RequestHandles(2, [room])[0] - chan_path = self.conn_iface.RequestChannel(tp_chan_type_text, - 2, chan_handle, True) - chan = bus.get_object(self.conn_name, chan_path) - text_iface = dbus.Interface(chan, tp_chan_type_text) - text_iface.connect_to_signal('Received', self.received_cb) - self.text_iface = text_iface - except Exception, e: - print e - elif status == 2: + if status == CONNECTION_STATUS_CONNECTED and not self.chan: + self.join_room() + elif status == CONNECTION_STATUS_DISCONNECTED: print 'disconnected' + self.chan.Close() + self.chan = None def make_root(self): text = hippo.CanvasText( @@ -129,6 +138,8 @@ class Chat(Activity): self.conversation = conversation entry = Entry(padding=5) + # XXX make this entry unsensitive while we're not + # connected. entry.connect('activated', self.entry_activated_cb) hbox = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL) @@ -181,14 +192,14 @@ class Chat(Activity): self.conversation.append(box) aw, ah = self.conversation.get_allocation() - print 'allocation = %r' % ((aw, ah),) + #print 'allocation = %r' % ((aw, ah),) rw, rh = self.conversation.get_height_request(aw) - print 'request = %r' % ((rw, rh),) + #print 'request = %r' % ((rw, rh),) adj = self.scrolled_window.get_vadjustment() - print 'value = %r' % adj.value - print 'upper = %r' % adj.upper - print 'page_size = %r' % adj.page_size + #print 'value = %r' % adj.value + #print 'upper = %r' % adj.upper + #print 'page_size = %r' % adj.page_size #adj.set_value(adj.upper - adj.page_size) #adj.set_value(rh) adj.set_value(adj.upper - adj.page_size - 804) @@ -201,24 +212,5 @@ class Chat(Activity): self.add_text(self.owner_nickname, self.make_owner_icon(), text) entry.props.text = '' - if self.text_iface: - self.text_iface.Send(0, text) - -if __name__ == '__main__': - # hack for running outside sugar - - class _Handle: - activity_id = 'chat' - - @staticmethod - def get_presence_service(): - return None - - activity = Chat(_Handle) - activity.show_all() - - try: - gtk.main() - except KeyboardInterrupt: - pass - + if self.chan: + self.chan[CHANNEL_TYPE_TEXT].Send(CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, text) -- cgit v0.9.1