diff --git a/services/presence/Makefile.am b/services/presence/Makefile.am index 57de87d9..4e286240 100644 --- a/services/presence/Makefile.am +++ b/services/presence/Makefile.am @@ -13,6 +13,7 @@ sugar_PYTHON = \ buddyiconcache.py \ linklocal_plugin.py \ presenceservice.py \ + psutils.py \ server_plugin.py bin_SCRIPTS = sugar-presence-service diff --git a/services/presence/buddy.py b/services/presence/buddy.py index a5c18ed5..4906b9d2 100644 --- a/services/presence/buddy.py +++ b/services/presence/buddy.py @@ -170,7 +170,10 @@ class Buddy(DBusGObject): props['owner'] = self.props.owner props['key'] = self.props.key props['color'] = self.props.color - props['current-activity'] = self.props.current_activity + if self.props.current_activity: + props['current-activity'] = self.props.current_activity + else: + props['current-activity'] = "" return props # methods diff --git a/services/presence/presenceservice.py b/services/presence/presenceservice.py index d4ad38c3..bb000945 100644 --- a/services/presence/presenceservice.py +++ b/services/presence/presenceservice.py @@ -89,8 +89,7 @@ class PresenceService(dbus.service.Object): def _contact_online(self, tp, handle, props): new_buddy = False - key = props['key'] - buddy = self._buddies.get(key) + buddy = self._buddies.get(props["key"]) if not buddy: # we don't know yet this buddy objid = self._get_next_object_id() @@ -253,26 +252,33 @@ class PresenceService(dbus.service.Object): def GetActivities(self): ret = [] for act in self._activities.values(): - ret.append(act.object_path()) + if act.props.valid: + ret.append(act.object_path()) return ret @dbus.service.method(_PRESENCE_INTERFACE, in_signature="s", out_signature="o") def GetActivityById(self, actid): if self._activities.has_key(actid): - return self._activities[actid].object_path() + act = self._activities[actid] + if act.props.valid: + return act.object_path() raise NotFoundError("The activity was not found.") @dbus.service.method(_PRESENCE_INTERFACE, out_signature="ao") def GetBuddies(self): ret = [] for buddy in self._buddies.values(): - ret.append(buddy.object_path()) + if buddy.props.valid: + ret.append(buddy.object_path()) return ret @dbus.service.method(_PRESENCE_INTERFACE, in_signature="ay", out_signature="o") def GetBuddyByPublicKey(self, key): + key = psutils.bytes_to_string(key) if self._buddies.has_key(key): - return self._buddies[key].object_path() + buddy = self._buddies[key] + if buddy.props.valid: + return buddy.object_path() raise NotFoundError("The buddy was not found.") @dbus.service.method(_PRESENCE_INTERFACE, out_signature="o") diff --git a/services/presence/psutils.py b/services/presence/psutils.py new file mode 100644 index 00000000..995ea3ab --- /dev/null +++ b/services/presence/psutils.py @@ -0,0 +1,26 @@ +# Copyright (C) 2007, Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +def bytes_to_string(bytes): + # Handle both DBus byte arrays and strings + try: + # DBus Byte array + ret = ''.join([chr(item) for item in bytes]) + except TypeError: + # Python string + ret = ''.join([str(item) for item in bytes]) + return ret diff --git a/services/presence/server_plugin.py b/services/presence/server_plugin.py index ffa2ab21..c674343b 100644 --- a/services/presence/server_plugin.py +++ b/services/presence/server_plugin.py @@ -25,6 +25,7 @@ from buddyiconcache import BuddyIconCache import logging import os import hashlib +import psutils from telepathy.client import ConnectionManager, ManagerRegistry, Connection, Channel from telepathy.interfaces import ( @@ -304,7 +305,7 @@ class ServerPlugin(gobject.GObject): # Set our OLPC buddy properties props = {} props['color'] = self._owner.props.color - props['key'] = self._owner.props.key + props['key'] = dbus.ByteArray(self._owner.props.key) try: self._conn[CONN_INTERFACE_BUDDY_INFO].SetProperties(props) except dbus.DBusException, e: @@ -405,15 +406,8 @@ class ServerPlugin(gobject.GObject): if not props.has_key('key'): raise InvalidBuddyError("no key") - # Convert from D-Bus array types to a standard python byte array - key = "" - for item in props["key"]: - try: - # int type - key = key + "%s" % chr(item) - except TypeError: - # string type - key = key + str(item) + # Convert key from dbus byte array to python string + props["key"] = psutils.bytes_to_string(props["key"]) jid = self._conn[CONN_INTERFACE].InspectHandles(CONNECTION_HANDLE_TYPE_CONTACT, [handle])[0] nick = self._conn[CONN_INTERFACE_ALIASING].RequestAliases([handle])[0]