This subclasses TubeConnection to make a version (SugarTubeConnection) that can resolve a handle to a Buddy.
Patches are for sugar(.presence) and Connect. Morgan
From fe68fb70cff7753b4f158b7127a0bc5712235fc3 Mon Sep 17 00:00:00 2001 From: Morgan Collett <[EMAIL PROTECTED]> Date: Tue, 13 May 2008 16:59:34 +0200 Subject: [PATCH] #6473: Better method for resolving handles to buddies --- src/sugar/presence/Makefile.am | 1 + src/sugar/presence/sugartubeconn.py | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 0 deletions(-) create mode 100644 src/sugar/presence/sugartubeconn.py diff --git a/src/sugar/presence/Makefile.am b/src/sugar/presence/Makefile.am index cb52a41..0c4368b 100644 --- a/src/sugar/presence/Makefile.am +++ b/src/sugar/presence/Makefile.am @@ -3,6 +3,7 @@ sugar_PYTHON = \ __init__.py \ activity.py \ buddy.py \ + sugartubeconn.py \ tubeconn.py \ presenceservice.py diff --git a/src/sugar/presence/sugartubeconn.py b/src/sugar/presence/sugartubeconn.py new file mode 100644 index 0000000..ce4d559 --- /dev/null +++ b/src/sugar/presence/sugartubeconn.py @@ -0,0 +1,60 @@ +"""Subclass of TubeConnection that converts handles to Sugar Buddies""" +# Copyright (C) 2008 One Laptop Per Child +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +from telepathy.constants import ( + CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES) + +from tubeconn import TubeConnection +import presenceservice + + +class SugarTubeConnection(TubeConnection): + """Subclass of TubeConnection that converts handles to Sugar Buddies""" + + def __new__(cls, conn, tubes_iface, tube_id, address=None, + group_iface=None, mainloop=None): + self = super(SugarTubeConnection, cls).__new__( + cls, conn, tubes_iface, tube_id, address=address, + group_iface=group_iface, mainloop=mainloop) + self._conn = conn + self._group_iface = group_iface + return self + + def get_buddy(self, cs_handle): + """Retrieve a Buddy object given a telepathy handle. + + cs_handle: A channel-specific CONTACT type handle. + returns: sugar.presence Buddy object or None + """ + pservice = presenceservice.get_instance() + if self.self_handle == cs_handle: + # It's me, just get my global handle + handle = self._conn.GetSelfHandle() + elif self._group_iface.GetGroupFlags() & \ + CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES: + # The group (channel) has channel specific handles + handle = self._group_iface.GetHandleOwners([cs_handle])[0] + else: + # The group does not have channel specific handles + handle = cs_handle + + # deal with failure to get the handle owner + if handle == 0: + return None + return pservice.get_buddy_by_telepathy_handle( + self._conn.service_name, self._conn.object_path, handle) -- 1.5.4.3
From abeae363528ce25c946a747e9c6b79ce1a108382 Mon Sep 17 00:00:00 2001 From: Morgan Collett <[EMAIL PROTECTED]> Date: Tue, 13 May 2008 17:00:35 +0200 Subject: [PATCH] #6473: Better method for resolving handles to buddies --- activity.py | 43 +++++++++++-------------------------------- game.py | 18 ++++++++++-------- 2 files changed, 21 insertions(+), 40 deletions(-) diff --git a/activity.py b/activity.py index f4b7054..7663d68 100644 --- a/activity.py +++ b/activity.py @@ -25,7 +25,7 @@ import telepathy.client from sugar.activity.activity import Activity, ActivityToolbox from sugar.presence import presenceservice -from sugar.presence.tubeconn import TubeConnection +from sugar.presence.sugartubeconn import SugarTubeConnection import sugar.logger import gridwidget @@ -88,8 +88,10 @@ class ConnectActivity(Activity): # we are joining the activity self.buddies_panel.add_watcher(owner) self.connect('joined', self._joined_cb) - self._shared_activity.connect('buddy-joined', self._buddy_joined_cb) - self._shared_activity.connect('buddy-left', self._buddy_left_cb) + self._shared_activity.connect('buddy-joined', + self._buddy_joined_cb) + self._shared_activity.connect('buddy-left', + self._buddy_left_cb) if self.get_shared(): # oh, OK, we've already joined self._joined_cb() @@ -102,30 +104,6 @@ class ConnectActivity(Activity): self.connect('key-press-event', self.key_press_cb) - def _get_buddy(self, cs_handle): - """Get a Buddy from a channel specific handle.""" - # FIXME: After Update.1, design a better API for sugar.presence - # to track handles of buddies, and use that instead - logger.debug('Trying to find owner of handle %u...', cs_handle) - group = self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP] - my_csh = group.GetSelfHandle() - logger.debug('My handle in that group is %u', my_csh) - if my_csh == cs_handle: - handle = self.conn.GetSelfHandle() - logger.debug('CS handle %u belongs to me, %u', cs_handle, handle) - elif group.GetGroupFlags() & telepathy.CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES: - handle = group.GetHandleOwners([cs_handle])[0] - logger.debug('CS handle %u belongs to %u', cs_handle, handle) - else: - handle = cs_handle - logger.debug('non-CS handle %u belongs to itself', handle) - - # XXX: deal with failure to get the handle owner - assert handle != 0 - - return self.pservice.get_buddy_by_telepathy_handle( - self.conn.service_name, self.conn.object_path, handle) - def key_press_cb(self, widget, event): logger.debug('Keypress: %r, %r', widget, event) @@ -195,14 +173,15 @@ class ConnectActivity(Activity): if (self.game is None and type == telepathy.TUBE_TYPE_DBUS and service == SERVICE): if state == telepathy.TUBE_STATE_LOCAL_PENDING: - self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].AcceptDBusTube(id) + self.tubes_chan[ + telepathy.CHANNEL_TYPE_TUBES].AcceptDBusTube(id) - tube_conn = TubeConnection(self.conn, + tube_conn = SugarTubeConnection(self.conn, self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES], - id, group_iface=self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP]) + id, group_iface=self.text_chan[ + telepathy.CHANNEL_INTERFACE_GROUP]) self.game = ConnectGame(tube_conn, self.grid, self.initiating, - self.buddies_panel, self.info_panel, self.owner, - self._get_buddy, self) + self.buddies_panel, self.info_panel, self.owner, self) def _buddy_joined_cb (self, activity, buddy): logger.debug("buddy joined") diff --git a/game.py b/game.py index 15f9258..4adc146 100644 --- a/game.py +++ b/game.py @@ -53,7 +53,7 @@ def dump_grid(seq): class ConnectGame(ExportedGObject): def __init__(self, tube, grid, is_initiator, buddies_panel, info_panel, - owner, get_buddy, activity): + owner, activity): super(ConnectGame, self).__init__(tube, PATH) self.tube = tube self.grid = grid @@ -63,7 +63,6 @@ class ConnectGame(ExportedGObject): self.buddies_panel = buddies_panel self.info_panel = info_panel self.owner = owner - self._get_buddy = get_buddy self.activity = activity # list indexed by player ID @@ -81,13 +80,13 @@ class ConnectGame(ExportedGObject): _logger.debug('removing participants: %r', removed) for handle, bus_name in added: - buddy = self._get_buddy(handle) + buddy = self.tube.get_buddy(handle) _logger.debug('Buddy %r was added', buddy) if buddy is not None: self.buddies_panel.add_watcher(buddy) for handle in removed: - buddy = self._get_buddy(handle) + buddy = self.tube.get_buddy(handle) _logger.debug('Buddy %r was removed', buddy) if buddy is not None: self.buddies_panel.remove_watcher(buddy) @@ -137,9 +136,11 @@ class ConnectGame(ExportedGObject): # OK, now I'm synced with the game, I can welcome others self.add_hello_handler() - buddy = self._get_buddy(self.tube.bus_name_to_handle[bus_names[0]]) + buddy = self.tube.get_buddy( + self.tube.bus_name_to_handle[bus_names[0]]) self.buddies_panel.add_player(buddy) - buddy = self._get_buddy(self.tube.bus_name_to_handle[bus_names[1]]) + buddy = self.tube.get_buddy( + self.tube.bus_name_to_handle[bus_names[1]]) self.buddies_panel.add_player(buddy) if self.get_active_player() == self.player_id: @@ -165,7 +166,7 @@ class ConnectGame(ExportedGObject): _logger.debug('Newcomer %s has joined', sender) self.ordered_bus_names.append(sender) if len(self.ordered_bus_names) == 2: - buddy = self._get_buddy(self.tube.bus_name_to_handle[sender]) + buddy = self.tube.get_buddy(self.tube.bus_name_to_handle[sender]) self.buddies_panel.add_player(buddy) _logger.debug('Bus names are now: %r', self.ordered_bus_names) _logger.debug('Welcoming newcomer and sending them the game state') @@ -210,7 +211,8 @@ class ConnectGame(ExportedGObject): def change_turn(self): try: bus_name = self.ordered_bus_names[self.get_active_player()] - buddy = self._get_buddy(self.tube.bus_name_to_handle[bus_name]) + buddy = self.tube.get_buddy( + self.tube.bus_name_to_handle[bus_name]) self.buddies_panel.set_is_playing(buddy) except: _logger.error('argh!', exc_info=1) -- 1.5.4.3
_______________________________________________ Sugar mailing list Sugar@lists.laptop.org http://lists.laptop.org/listinfo/sugar