2007-04-15 05:38:21 +02:00
|
|
|
"""UI class to access system-level presence object"""
|
2007-04-09 20:40:56 +02:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
2006-10-15 01:08:44 +02:00
|
|
|
#
|
|
|
|
# This library 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 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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 library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2007-04-13 22:27:58 +02:00
|
|
|
import logging
|
2006-06-13 21:25:54 +02:00
|
|
|
|
2007-05-18 16:31:32 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.exceptions
|
|
|
|
import dbus.glib
|
|
|
|
import gobject
|
|
|
|
|
2007-05-18 16:33:21 +02:00
|
|
|
from sugar.presence.buddy import Buddy
|
|
|
|
from sugar.presence.activity import Activity
|
2007-03-28 12:17:34 +02:00
|
|
|
|
2006-07-24 18:27:21 +02:00
|
|
|
|
2007-04-09 20:40:50 +02:00
|
|
|
DBUS_SERVICE = "org.laptop.Sugar.Presence"
|
|
|
|
DBUS_INTERFACE = "org.laptop.Sugar.Presence"
|
|
|
|
DBUS_PATH = "/org/laptop/Sugar/Presence"
|
2006-07-24 18:27:21 +02:00
|
|
|
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger = logging.getLogger('sugar.presence.presenceservice')
|
|
|
|
|
2006-07-24 18:27:21 +02:00
|
|
|
|
2006-06-13 00:31:26 +02:00
|
|
|
class PresenceService(gobject.GObject):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""UI-side interface to the dbus presence service
|
|
|
|
|
|
|
|
This class provides UI programmers with simplified access
|
|
|
|
to the dbus service of the same name. It allows for observing
|
|
|
|
various events from the presence service as GObject events,
|
|
|
|
as well as some basic introspection queries.
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'buddy-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'buddy-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2007-08-30 16:00:12 +02:00
|
|
|
# parameters: (activity: Activity, inviter: Buddy, message: unicode)
|
|
|
|
'activity-invitation': (gobject.SIGNAL_RUN_FIRST, None, ([object]*3)),
|
2007-04-09 20:40:50 +02:00
|
|
|
'private-invitation': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT,
|
|
|
|
gobject.TYPE_PYOBJECT])),
|
2006-12-04 20:12:24 +01:00
|
|
|
'activity-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-04-13 22:27:58 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-shared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT,
|
|
|
|
gobject.TYPE_PYOBJECT]))
|
2006-12-04 20:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_PS_BUDDY_OP = DBUS_PATH + "/Buddies/"
|
|
|
|
_PS_ACTIVITY_OP = DBUS_PATH + "/Activities/"
|
|
|
|
|
|
|
|
|
2007-04-23 18:06:41 +02:00
|
|
|
def __init__(self, allow_offline_iface=True):
|
2007-04-22 06:23:19 +02:00
|
|
|
"""Initialise the service and attempt to connect to events
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.GObject.__init__(self)
|
2007-04-23 16:08:55 +02:00
|
|
|
self._objcache = {}
|
2007-04-23 19:11:21 +02:00
|
|
|
|
|
|
|
# Get a connection to the session bus
|
|
|
|
self._bus = dbus.SessionBus()
|
|
|
|
self._bus.add_signal_receiver(self._name_owner_changed_cb,
|
|
|
|
signal_name="NameOwnerChanged",
|
|
|
|
dbus_interface="org.freedesktop.DBus")
|
|
|
|
|
2007-04-22 06:23:19 +02:00
|
|
|
# attempt to load the interface to the service...
|
2007-04-23 18:06:41 +02:00
|
|
|
self._allow_offline_iface = allow_offline_iface
|
2007-04-22 06:23:19 +02:00
|
|
|
self._get_ps()
|
2007-04-23 19:11:21 +02:00
|
|
|
|
|
|
|
def _name_owner_changed_cb(self, name, old, new):
|
|
|
|
if name != DBUS_SERVICE:
|
|
|
|
return
|
|
|
|
if (old and len(old)) and (not new and not len(new)):
|
|
|
|
# PS went away, clear out PS dbus service wrapper
|
|
|
|
self._ps_ = None
|
|
|
|
elif (not old and not len(old)) and (new and len(new)):
|
|
|
|
# PS started up
|
|
|
|
self._get_ps()
|
|
|
|
|
2007-04-22 06:23:19 +02:00
|
|
|
_ps_ = None
|
2007-04-23 18:06:41 +02:00
|
|
|
def _get_ps(self):
|
2007-04-22 06:23:19 +02:00
|
|
|
"""Retrieve dbus interface to PresenceService
|
|
|
|
|
|
|
|
Also registers for updates from various dbus events on the
|
|
|
|
interface.
|
|
|
|
|
|
|
|
If unable to retrieve the interface, we will temporarily
|
|
|
|
return an _OfflineInterface object to allow the calling
|
|
|
|
code to continue functioning as though it had accessed a
|
|
|
|
real presence service.
|
|
|
|
|
|
|
|
If successful, caches the presence service interface
|
|
|
|
for use by other methods and returns that interface
|
|
|
|
"""
|
|
|
|
if not self._ps_:
|
|
|
|
try:
|
2007-08-25 00:25:56 +02:00
|
|
|
# NOTE: We need to follow_name_owner_changes here
|
|
|
|
# because we can not connect to a signal unless
|
|
|
|
# we follow the changes or we start the service
|
|
|
|
# before we connect. Starting the service here
|
|
|
|
# causes a major bottleneck during startup
|
2007-04-22 06:23:19 +02:00
|
|
|
ps = dbus.Interface(
|
2007-08-25 00:25:56 +02:00
|
|
|
self._bus.get_object(DBUS_SERVICE,
|
|
|
|
DBUS_PATH,
|
|
|
|
follow_name_owner_changes=True),
|
2007-04-22 06:23:19 +02:00
|
|
|
DBUS_INTERFACE
|
|
|
|
)
|
|
|
|
except dbus.exceptions.DBusException, err:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.error(
|
2007-04-22 06:23:19 +02:00
|
|
|
"""Failure retrieving %r interface from the D-BUS service %r %r: %s""",
|
|
|
|
DBUS_INTERFACE, DBUS_SERVICE, DBUS_PATH, err
|
|
|
|
)
|
2007-04-23 18:06:41 +02:00
|
|
|
if self._allow_offline_iface:
|
|
|
|
return _OfflineInterface()
|
|
|
|
raise RuntimeError("Failed to connect to the presence service.")
|
2007-04-22 06:23:19 +02:00
|
|
|
else:
|
|
|
|
self._ps_ = ps
|
|
|
|
ps.connect_to_signal('BuddyAppeared', self._buddy_appeared_cb)
|
|
|
|
ps.connect_to_signal('BuddyDisappeared', self._buddy_disappeared_cb)
|
|
|
|
ps.connect_to_signal('ActivityAppeared', self._activity_appeared_cb)
|
|
|
|
ps.connect_to_signal('ActivityDisappeared', self._activity_disappeared_cb)
|
|
|
|
ps.connect_to_signal('ActivityInvitation', self._activity_invitation_cb)
|
|
|
|
ps.connect_to_signal('PrivateInvitation', self._private_invitation_cb)
|
|
|
|
return self._ps_
|
|
|
|
|
|
|
|
_ps = property(
|
|
|
|
_get_ps, None, None,
|
|
|
|
"""DBUS interface to the PresenceService (services/presence/presenceservice)"""
|
|
|
|
)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _new_object(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Turn new object path into (cached) Buddy/Activity instance
|
|
|
|
|
|
|
|
object_path -- full dbus path of the new object, must be
|
|
|
|
prefixed with either of _PS_BUDDY_OP or _PS_ACTIVITY_OP
|
|
|
|
|
|
|
|
Note that this method is called throughout the class whenever
|
|
|
|
the representation of the object is required, it is not only
|
2007-04-23 16:08:55 +02:00
|
|
|
called when the object is first discovered. The point is to only have
|
|
|
|
_one_ Python object for any D-Bus object represented by an object path,
|
|
|
|
effectively wrapping the D-Bus object in a single Python GObject.
|
2007-04-15 05:38:21 +02:00
|
|
|
|
|
|
|
returns presence Buddy or Activity representation
|
|
|
|
"""
|
2007-10-16 18:51:36 +02:00
|
|
|
_logger.debug('Creating proxy for %s', object_path)
|
2007-04-23 16:08:55 +02:00
|
|
|
obj = None
|
|
|
|
try:
|
|
|
|
obj = self._objcache[object_path]
|
|
|
|
except KeyError:
|
2007-04-09 20:40:56 +02:00
|
|
|
if object_path.startswith(self._PS_BUDDY_OP):
|
2007-05-18 16:33:21 +02:00
|
|
|
obj = Buddy(self._bus, self._new_object,
|
2006-12-04 20:12:24 +01:00
|
|
|
self._del_object, object_path)
|
|
|
|
elif object_path.startswith(self._PS_ACTIVITY_OP):
|
2007-05-08 17:23:07 +02:00
|
|
|
obj = Activity(self._bus, self._new_object,
|
2006-12-04 20:12:24 +01:00
|
|
|
self._del_object, object_path)
|
2007-05-03 05:25:15 +02:00
|
|
|
try:
|
|
|
|
# Pre-fill the activity's ID
|
|
|
|
foo = obj.props.id
|
|
|
|
except dbus.exceptions.DBusException, err:
|
|
|
|
pass
|
2006-12-04 20:12:24 +01:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown object type")
|
2007-04-23 16:08:55 +02:00
|
|
|
self._objcache[object_path] = obj
|
2007-10-16 18:51:36 +02:00
|
|
|
_logger.debug('Proxy is %r', obj)
|
2006-12-04 20:12:24 +01:00
|
|
|
return obj
|
|
|
|
|
2007-07-16 20:00:32 +02:00
|
|
|
def _have_object(self, object_path):
|
|
|
|
return object_path in self._objcache.keys()
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _del_object(self, object_path):
|
2007-04-23 16:08:55 +02:00
|
|
|
"""Fully remove an object from the object cache when it's no longer needed.
|
|
|
|
"""
|
|
|
|
del self._objcache[object_path]
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _emit_buddy_appeared_signal(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Emit GObject event with presence.buddy.Buddy object"""
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('buddy-appeared', self._new_object(object_path))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _buddy_appeared_cb(self, op):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Callback for dbus event (forwards to method to emit GObject event)"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.idle_add(self._emit_buddy_appeared_signal, op)
|
|
|
|
|
|
|
|
def _emit_buddy_disappeared_signal(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Emit GObject event with presence.buddy.Buddy object"""
|
2007-07-16 20:00:32 +02:00
|
|
|
# Don't try to create a new object here if needed; it will probably
|
|
|
|
# fail anyway because the object has already been destroyed in the PS
|
|
|
|
if self._have_object(object_path):
|
|
|
|
self.emit('buddy-disappeared', self._new_object(object_path))
|
2006-12-04 20:12:24 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _buddy_disappeared_cb(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Callback for dbus event (forwards to method to emit GObject event)"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.idle_add(self._emit_buddy_disappeared_signal, object_path)
|
|
|
|
|
2007-08-30 16:00:12 +02:00
|
|
|
def _emit_activity_invitation_signal(self, activity_path, buddy_path,
|
|
|
|
message):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Emit GObject event with presence.activity.Activity object"""
|
2007-08-30 16:00:12 +02:00
|
|
|
self.emit('activity-invitation', self._new_object(activity_path),
|
|
|
|
self._new_object(buddy_path), unicode(message))
|
2006-12-04 20:12:24 +01:00
|
|
|
return False
|
|
|
|
|
2007-08-30 16:00:12 +02:00
|
|
|
def _activity_invitation_cb(self, activity_path, buddy_path, message):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Callback for dbus event (forwards to method to emit GObject event)"""
|
2007-08-30 16:00:12 +02:00
|
|
|
gobject.idle_add(self._emit_activity_invitation_signal, activity_path,
|
|
|
|
buddy_path, message)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
def _emit_private_invitation_signal(self, bus_name, connection, channel):
|
2007-04-23 16:08:55 +02:00
|
|
|
"""Emit GObject event with bus_name, connection and channel"""
|
|
|
|
self.emit('private-invitation', bus_name, connection, channel)
|
2006-12-04 20:12:24 +01:00
|
|
|
return False
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
def _private_invitation_cb(self, bus_name, connection, channel):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Callback for dbus event (forwards to method to emit GObject event)"""
|
2007-04-09 20:40:56 +02:00
|
|
|
gobject.idle_add(self._emit_service_disappeared_signal, bus_name,
|
|
|
|
connection, channel)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _emit_activity_appeared_signal(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Emit GObject event with presence.activity.Activity object"""
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('activity-appeared', self._new_object(object_path))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _activity_appeared_cb(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Callback for dbus event (forwards to method to emit GObject event)"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.idle_add(self._emit_activity_appeared_signal, object_path)
|
|
|
|
|
|
|
|
def _emit_activity_disappeared_signal(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Emit GObject event with presence.activity.Activity object"""
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('activity-disappeared', self._new_object(object_path))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _activity_disappeared_cb(self, object_path):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Callback for dbus event (forwards to method to emit GObject event)"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.idle_add(self._emit_activity_disappeared_signal, object_path)
|
|
|
|
|
|
|
|
def get(self, object_path):
|
2007-04-23 16:08:55 +02:00
|
|
|
"""Return the Buddy or Activity object corresponding to the given
|
|
|
|
D-Bus object path.
|
2007-04-15 05:38:21 +02:00
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
return self._new_object(object_path)
|
|
|
|
|
|
|
|
def get_activities(self):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Retrieve set of all activities from service
|
|
|
|
|
|
|
|
returns list of Activity objects for all object paths
|
|
|
|
the service reports exist (using GetActivities)
|
|
|
|
"""
|
2007-04-22 06:31:32 +02:00
|
|
|
try:
|
|
|
|
resp = self._ps.GetActivities()
|
|
|
|
except dbus.exceptions.DBusException, err:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.warn(
|
2007-04-25 12:47:29 +02:00
|
|
|
"""Unable to retrieve activity list from presence service: %s"""
|
|
|
|
% err
|
2007-04-22 06:31:32 +02:00
|
|
|
)
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
acts = []
|
|
|
|
for item in resp:
|
|
|
|
acts.append(self._new_object(item))
|
|
|
|
return acts
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-27 21:47:58 +02:00
|
|
|
def _get_activities_cb(self, reply_handler, resp):
|
|
|
|
acts = []
|
|
|
|
for item in resp:
|
|
|
|
acts.append(self._new_object(item))
|
|
|
|
|
|
|
|
reply_handler(acts)
|
|
|
|
|
|
|
|
def _get_activities_error_cb(self, error_handler, e):
|
|
|
|
if error_handler:
|
|
|
|
error_handler(e)
|
|
|
|
else:
|
|
|
|
_logger.warn(
|
|
|
|
"""Unable to retrieve activity-list from presence service: %s"""
|
|
|
|
% e
|
|
|
|
)
|
|
|
|
|
2007-08-27 22:46:38 +02:00
|
|
|
def get_activities_async(self, reply_handler=None, error_handler=None):
|
2007-08-27 21:47:58 +02:00
|
|
|
"""Retrieve set of all activities from service asyncronously
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not reply_handler:
|
|
|
|
logging.error('Function get_activities_async called without a reply handler. Can not run.')
|
|
|
|
return
|
|
|
|
|
|
|
|
self._ps.GetActivities(
|
|
|
|
reply_handler=lambda resp:self._get_activities_cb(reply_handler, resp),
|
|
|
|
error_handler=lambda e:self._get_activities_error_cb(error_handler, e))
|
|
|
|
|
|
|
|
|
2007-10-17 13:53:24 +02:00
|
|
|
def get_activity(self, activity_id, warn_if_none=True):
|
|
|
|
"""Retrieve single Activity object for the given unique id
|
|
|
|
|
|
|
|
activity_id -- unique ID for the activity
|
|
|
|
|
|
|
|
returns single Activity object or None if the activity
|
2007-04-15 05:38:21 +02:00
|
|
|
is not found using GetActivityById on the service
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
try:
|
2007-04-09 20:40:56 +02:00
|
|
|
act_op = self._ps.GetActivityById(activity_id)
|
2007-04-22 06:31:32 +02:00
|
|
|
except dbus.exceptions.DBusException, err:
|
2007-10-17 13:53:24 +02:00
|
|
|
if warn_if_none:
|
|
|
|
_logger.warn("Unable to retrieve activity handle for %r from "
|
|
|
|
"presence service: %s", activity_id, err)
|
2006-12-04 20:12:24 +01:00
|
|
|
return None
|
|
|
|
return self._new_object(act_op)
|
|
|
|
|
|
|
|
def get_buddies(self):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Retrieve set of all buddies from service
|
|
|
|
|
|
|
|
returns list of Buddy objects for all object paths
|
|
|
|
the service reports exist (using GetBuddies)
|
|
|
|
"""
|
2007-04-22 06:31:32 +02:00
|
|
|
try:
|
|
|
|
resp = self._ps.GetBuddies()
|
|
|
|
except dbus.exceptions.DBusException, err:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.warn(
|
2007-04-25 12:47:29 +02:00
|
|
|
"""Unable to retrieve buddy-list from presence service: %s"""
|
|
|
|
% err
|
2007-04-22 06:31:32 +02:00
|
|
|
)
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
buddies = []
|
|
|
|
for item in resp:
|
|
|
|
buddies.append(self._new_object(item))
|
|
|
|
return buddies
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-27 21:47:58 +02:00
|
|
|
def _get_buddies_cb(self, reply_handler, resp):
|
|
|
|
buddies = []
|
|
|
|
for item in resp:
|
|
|
|
buddies.append(self._new_object(item))
|
|
|
|
|
|
|
|
reply_handler(buddies)
|
|
|
|
|
|
|
|
def _get_buddies_error_cb(self, error_handler, e):
|
|
|
|
if error_handler:
|
|
|
|
error_handler(e)
|
|
|
|
else:
|
|
|
|
_logger.warn(
|
|
|
|
"""Unable to retrieve buddy-list from presence service: %s"""
|
|
|
|
% e
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_buddies_async(self, reply_handler=None, error_handler=None):
|
|
|
|
"""Retrieve set of all buddies from service asyncronously
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not reply_handler:
|
|
|
|
logging.error('Function get_buddies_async called without a reply handler. Can not run.')
|
|
|
|
return
|
|
|
|
|
|
|
|
self._ps.GetBuddies(
|
|
|
|
reply_handler=lambda resp:self._get_buddies_cb(reply_handler, resp),
|
|
|
|
error_handler=lambda e:self._get_buddies_error_cb(error_handler, e))
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
def get_buddy(self, key):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Retrieve single Buddy object for the given public key
|
|
|
|
|
|
|
|
key -- buddy's public encryption key
|
|
|
|
|
|
|
|
returns single Buddy object or None if the activity
|
|
|
|
is not found using GetBuddyByPublicKey on the
|
|
|
|
service
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
try:
|
2007-04-09 20:40:56 +02:00
|
|
|
buddy_op = self._ps.GetBuddyByPublicKey(dbus.ByteArray(key))
|
2007-04-22 06:31:32 +02:00
|
|
|
except dbus.exceptions.DBusException, err:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.warn(
|
2007-04-25 12:47:29 +02:00
|
|
|
"""Unable to retrieve buddy handle for %r from presence service: %s"""
|
|
|
|
% key, err
|
2007-04-22 06:31:32 +02:00
|
|
|
)
|
2006-12-04 20:12:24 +01:00
|
|
|
return None
|
|
|
|
return self._new_object(buddy_op)
|
|
|
|
|
2007-05-18 16:24:58 +02:00
|
|
|
def get_buddy_by_telepathy_handle(self, tp_conn_name, tp_conn_path,
|
|
|
|
handle):
|
|
|
|
"""Retrieve single Buddy object for the given public key
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`tp_conn_name` : str
|
|
|
|
The well-known bus name of a Telepathy connection
|
|
|
|
`tp_conn_path` : dbus.ObjectPath
|
|
|
|
The object path of the Telepathy connection
|
|
|
|
`handle` : int or long
|
|
|
|
The handle of a Telepathy contact on that connection,
|
|
|
|
of type HANDLE_TYPE_CONTACT. This may not be a
|
|
|
|
channel-specific handle.
|
|
|
|
:Returns: the Buddy object, or None if the buddy is not found
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
buddy_op = self._ps.GetBuddyByTelepathyHandle(tp_conn_name,
|
|
|
|
tp_conn_path,
|
|
|
|
handle)
|
|
|
|
except dbus.exceptions.DBusException, err:
|
|
|
|
_logger.warn('Unable to retrieve buddy handle for handle %u at '
|
|
|
|
'conn %s:%s from presence service: %s',
|
|
|
|
handle, tp_conn_name, tp_conn_path, err)
|
|
|
|
return None
|
|
|
|
return self._new_object(buddy_op)
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def get_owner(self):
|
2007-04-23 16:08:55 +02:00
|
|
|
"""Retrieves the laptop "owner" Buddy object."""
|
2006-12-04 20:12:24 +01:00
|
|
|
try:
|
2007-04-09 20:40:56 +02:00
|
|
|
owner_op = self._ps.GetOwner()
|
2007-04-22 06:31:32 +02:00
|
|
|
except dbus.exceptions.DBusException, err:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.warn(
|
2007-04-25 12:47:29 +02:00
|
|
|
"""Unable to retrieve local user/owner from presence service: %s"""
|
|
|
|
% err
|
2007-04-22 06:31:32 +02:00
|
|
|
)
|
2007-04-23 16:08:55 +02:00
|
|
|
raise RuntimeError("Could not get owner object from presence service.")
|
2006-12-04 20:12:24 +01:00
|
|
|
return self._new_object(owner_op)
|
|
|
|
|
2007-11-20 23:22:49 +01:00
|
|
|
def _share_activity_cb(self, activity, op):
|
2007-10-29 18:21:33 +01:00
|
|
|
"""Finish sharing the activity
|
2007-08-21 13:08:33 +02:00
|
|
|
"""
|
2007-11-20 23:22:49 +01:00
|
|
|
psact = self._new_object(op)
|
2007-05-03 05:25:15 +02:00
|
|
|
psact._joined = True
|
2007-10-29 18:21:33 +01:00
|
|
|
_logger.debug('%r: Just shared, setting up tubes', activity)
|
|
|
|
psact.set_up_tubes(reply_handler=lambda:
|
|
|
|
self.emit("activity-shared", True, psact, None),
|
|
|
|
error_handler=lambda e:
|
|
|
|
self._share_activity_error_cb(activity, e))
|
2007-04-13 22:27:58 +02:00
|
|
|
|
|
|
|
def _share_activity_error_cb(self, activity, err):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Notify with GObject event of unsuccessful sharing of activity"""
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Error sharing activity %s: %s" % (activity.get_id(), err))
|
2007-04-13 22:27:58 +02:00
|
|
|
self.emit("activity-shared", False, None, err)
|
|
|
|
|
2007-08-23 14:48:16 +02:00
|
|
|
def share_activity(self, activity, properties={}, private=True):
|
2007-08-22 16:54:12 +02:00
|
|
|
"""Ask presence service to ask the activity to share itself publicly.
|
2007-04-15 05:38:21 +02:00
|
|
|
|
2007-08-22 16:54:12 +02:00
|
|
|
Uses the AdvertiseActivity method on the service to ask for the
|
2007-04-15 05:38:21 +02:00
|
|
|
sharing of the given activity. Arranges to emit activity-shared
|
|
|
|
event with:
|
|
|
|
|
|
|
|
(success, Activity, err)
|
|
|
|
|
|
|
|
on success/failure.
|
|
|
|
|
|
|
|
returns None
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
actid = activity.get_id()
|
2007-05-03 05:25:15 +02:00
|
|
|
|
|
|
|
# Ensure the activity is not already shared/joined
|
|
|
|
for obj in self._objcache.values():
|
2007-05-08 17:23:07 +02:00
|
|
|
if not isinstance(object, Activity):
|
2007-05-03 05:25:15 +02:00
|
|
|
continue
|
|
|
|
if obj.props.id == actid or obj.props.joined:
|
2007-08-23 14:48:16 +02:00
|
|
|
raise RuntimeError("Activity %s is already shared." %
|
|
|
|
actid)
|
2007-05-03 05:25:15 +02:00
|
|
|
|
2007-10-09 13:15:06 +02:00
|
|
|
atype = activity.get_bundle_id()
|
2007-04-09 20:40:56 +02:00
|
|
|
name = activity.props.title
|
2007-11-20 23:22:49 +01:00
|
|
|
properties['private'] = bool(private)
|
2007-08-30 13:17:33 +02:00
|
|
|
self._ps.ShareActivity(actid, atype, name, properties,
|
|
|
|
reply_handler=lambda op: \
|
2007-11-20 23:22:49 +01:00
|
|
|
self._share_activity_cb(activity, op),
|
2007-08-30 13:17:33 +02:00
|
|
|
error_handler=lambda e: \
|
|
|
|
self._share_activity_error_cb(activity, e))
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-16 16:41:00 +02:00
|
|
|
def get_preferred_connection(self):
|
|
|
|
"""Gets the preferred telepathy connection object that an activity
|
|
|
|
should use when talking directly to telepathy
|
|
|
|
|
|
|
|
returns the bus name and the object path of the Telepathy connection"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
bus_name, object_path = self._ps.GetPreferredConnection()
|
|
|
|
except dbus.exceptions.DBusException:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return bus_name, object_path
|
|
|
|
|
2007-04-22 06:23:19 +02:00
|
|
|
class _OfflineInterface( object ):
|
|
|
|
"""Offline-presence-service interface
|
|
|
|
|
|
|
|
Used to mimic the behaviour of a real PresenceService sufficiently
|
|
|
|
to avoid crashing client code that expects the given interface.
|
|
|
|
|
|
|
|
XXX we could likely return a "MockOwner" object reasonably
|
|
|
|
easily, but would it be worth it?
|
|
|
|
"""
|
|
|
|
def raiseException( self, *args, **named ):
|
|
|
|
"""Raise dbus.exceptions.DBusException"""
|
|
|
|
raise dbus.exceptions.DBusException(
|
|
|
|
"""PresenceService Interface not available"""
|
|
|
|
)
|
|
|
|
GetActivities = raiseException
|
|
|
|
GetActivityById = raiseException
|
|
|
|
GetBuddies = raiseException
|
|
|
|
GetBuddyByPublicKey = raiseException
|
|
|
|
GetOwner = raiseException
|
|
|
|
GetPreferredConnection = raiseException
|
|
|
|
def ShareActivity(
|
|
|
|
self, actid, atype, name, properties,
|
|
|
|
reply_handler, error_handler,
|
|
|
|
):
|
|
|
|
"""Pretend to share and fail..."""
|
|
|
|
exc = IOError(
|
|
|
|
"""Unable to share activity as PresenceService is not currenly available"""
|
|
|
|
)
|
|
|
|
return error_handler( exc )
|
2007-04-10 14:39:02 +02:00
|
|
|
|
|
|
|
class _MockPresenceService(gobject.GObject):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Test fixture allowing testing of items that use PresenceService
|
|
|
|
|
|
|
|
See PresenceService for usage and purpose
|
|
|
|
"""
|
2007-04-10 14:39:02 +02:00
|
|
|
__gsignals__ = {
|
|
|
|
'buddy-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'buddy-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-invitation': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'private-invitation': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT,
|
|
|
|
gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
|
|
|
def get_activities(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_activity(self, activity_id):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_buddies(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_buddy(self, key):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_owner(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def share_activity(self, activity, properties={}):
|
|
|
|
return None
|
|
|
|
|
2006-08-23 17:38:56 +02:00
|
|
|
_ps = None
|
2007-04-23 18:47:50 +02:00
|
|
|
def get_instance(allow_offline_iface=False):
|
2007-04-15 05:38:21 +02:00
|
|
|
"""Retrieve this process' view of the PresenceService"""
|
2006-12-04 20:12:24 +01:00
|
|
|
global _ps
|
|
|
|
if not _ps:
|
2007-04-23 18:47:50 +02:00
|
|
|
_ps = PresenceService(allow_offline_iface)
|
2006-12-04 20:12:24 +01:00
|
|
|
return _ps
|
2007-04-10 14:39:02 +02:00
|
|
|
|