2007-02-21 12:41:26 +01:00
|
|
|
# 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
|
|
|
|
|
|
|
|
import gobject
|
2007-05-11 23:27:36 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
from dbus.gobject_service import ExportedGObject
|
2007-05-24 16:38:54 +02:00
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
2007-04-12 20:44:23 +02:00
|
|
|
import logging
|
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
from telepathy.client import ManagerRegistry, Connection
|
2007-02-23 18:04:16 +01:00
|
|
|
from telepathy.interfaces import (CONN_MGR_INTERFACE, CONN_INTERFACE)
|
2007-05-24 12:34:48 +02:00
|
|
|
from telepathy.constants import (CONNECTION_STATUS_CONNECTING,
|
|
|
|
CONNECTION_STATUS_CONNECTED,
|
|
|
|
CONNECTION_STATUS_DISCONNECTED)
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
from server_plugin import ServerPlugin
|
|
|
|
from linklocal_plugin import LinkLocalPlugin
|
2007-03-14 15:59:30 +01:00
|
|
|
from sugar import util
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-24 15:27:36 +02:00
|
|
|
from buddy import Buddy, ShellOwner
|
2007-02-23 17:30:25 +01:00
|
|
|
from activity import Activity
|
2007-02-21 13:06:45 +01:00
|
|
|
|
|
|
|
_PRESENCE_SERVICE = "org.laptop.Sugar.Presence"
|
|
|
|
_PRESENCE_INTERFACE = "org.laptop.Sugar.Presence"
|
|
|
|
_PRESENCE_PATH = "/org/laptop/Sugar/Presence"
|
|
|
|
|
|
|
|
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger = logging.getLogger('s-p-s.presenceservice')
|
|
|
|
|
|
|
|
|
2007-02-21 13:06:45 +01:00
|
|
|
class NotFoundError(dbus.DBusException):
|
2007-04-27 13:01:41 +02:00
|
|
|
def __init__(self, msg):
|
|
|
|
dbus.DBusException.__init__(self, msg)
|
2007-02-21 13:06:45 +01:00
|
|
|
self._dbus_error_name = _PRESENCE_INTERFACE + '.NotFound'
|
2007-02-21 12:41:26 +01:00
|
|
|
|
2007-05-11 23:27:36 +02:00
|
|
|
class PresenceService(ExportedGObject):
|
2007-04-17 04:44:54 +02:00
|
|
|
__gtype_name__ = "PresenceService"
|
|
|
|
|
|
|
|
__gsignals__ = {
|
|
|
|
'connection-status': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_BOOLEAN]))
|
|
|
|
}
|
2007-02-21 12:41:26 +01:00
|
|
|
|
2007-05-24 15:27:36 +02:00
|
|
|
def _create_owner(self):
|
|
|
|
# Overridden by TestPresenceService
|
2007-05-25 17:21:02 +02:00
|
|
|
return ShellOwner(self, self._session_bus, self._get_next_object_id())
|
2007-05-24 15:27:36 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
self._next_object_id = 0
|
2007-04-17 04:44:54 +02:00
|
|
|
self._connected = False
|
2007-02-21 13:58:16 +01:00
|
|
|
|
2007-02-21 13:20:59 +01:00
|
|
|
self._buddies = {} # key -> Buddy
|
2007-03-05 18:56:17 +01:00
|
|
|
self._handles_buddies = {} # tp client -> (handle -> Buddy)
|
2007-02-21 13:20:59 +01:00
|
|
|
self._activities = {} # activity id -> Activity
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
self._session_bus = dbus.SessionBus()
|
|
|
|
self._session_bus.add_signal_receiver(self._connection_disconnected_cb,
|
|
|
|
signal_name="Disconnected",
|
|
|
|
dbus_interface="org.freedesktop.DBus")
|
2007-02-21 13:58:16 +01:00
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
# Create the Owner object
|
2007-05-24 15:27:36 +02:00
|
|
|
self._owner = self._create_owner()
|
2007-03-02 17:10:44 +01:00
|
|
|
self._buddies[self._owner.props.key] = self._owner
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
self._registry = ManagerRegistry()
|
|
|
|
self._registry.LoadManagers()
|
2007-02-21 13:58:16 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
# Set up the server connection
|
2007-03-09 22:29:49 +01:00
|
|
|
self._server_plugin = ServerPlugin(self._registry, self._owner)
|
2007-03-05 18:56:17 +01:00
|
|
|
self._handles_buddies[self._server_plugin] = {}
|
2007-02-23 17:30:25 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
self._server_plugin.connect('status', self._server_status_cb)
|
|
|
|
self._server_plugin.connect('contact-online', self._contact_online)
|
|
|
|
self._server_plugin.connect('contact-offline', self._contact_offline)
|
2007-02-26 17:18:52 +01:00
|
|
|
self._server_plugin.connect('avatar-updated', self._avatar_updated)
|
2007-05-24 12:34:48 +02:00
|
|
|
self._server_plugin.connect('buddy-properties-changed',
|
|
|
|
self._buddy_properties_changed)
|
|
|
|
self._server_plugin.connect('buddy-activities-changed',
|
|
|
|
self._buddy_activities_changed)
|
|
|
|
self._server_plugin.connect('activity-invitation',
|
|
|
|
self._activity_invitation)
|
|
|
|
self._server_plugin.connect('private-invitation',
|
|
|
|
self._private_invitation)
|
|
|
|
self._server_plugin.connect('activity-properties-changed',
|
|
|
|
self._activity_properties_changed)
|
2007-02-26 01:24:48 +01:00
|
|
|
self._server_plugin.start()
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
# Set up the link local connection
|
2007-03-09 22:29:49 +01:00
|
|
|
self._ll_plugin = LinkLocalPlugin(self._registry, self._owner)
|
2007-03-05 18:56:17 +01:00
|
|
|
self._handles_buddies[self._ll_plugin] = {}
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
ExportedGObject.__init__(self, self._session_bus, _PRESENCE_PATH)
|
|
|
|
|
|
|
|
# for activation to work in a race-free way, we should really
|
|
|
|
# export the bus name only after we export our initial object;
|
|
|
|
# so this comes after the parent __init__
|
|
|
|
self._bus_name = dbus.service.BusName(_PRESENCE_SERVICE,
|
|
|
|
bus=self._session_bus)
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-05-14 03:33:14 +02:00
|
|
|
def _connection_disconnected_cb(self, foo=None):
|
|
|
|
"""Log event when D-Bus kicks us off the bus for some reason"""
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Disconnected from session bus!!!")
|
2007-04-13 19:11:59 +02:00
|
|
|
|
2007-02-26 04:45:16 +01:00
|
|
|
def _server_status_cb(self, plugin, status, reason):
|
2007-05-22 17:43:45 +02:00
|
|
|
|
2007-04-17 04:44:54 +02:00
|
|
|
# FIXME: figure out connection status when we have a salut plugin too
|
|
|
|
old_status = self._connected
|
2007-03-06 17:15:55 +01:00
|
|
|
if status == CONNECTION_STATUS_CONNECTED:
|
2007-04-17 04:44:54 +02:00
|
|
|
self._connected = True
|
2007-05-22 17:43:45 +02:00
|
|
|
self._handles_buddies[plugin][plugin.self_handle] = self._owner
|
|
|
|
self._owner.add_telepathy_handle(plugin, plugin.self_handle)
|
2007-04-17 04:44:54 +02:00
|
|
|
else:
|
|
|
|
self._connected = False
|
2007-05-22 17:43:45 +02:00
|
|
|
if plugin.self_handle is not None:
|
|
|
|
self._handles_buddies.setdefault(plugin, {}).pop(
|
|
|
|
plugin.self_handle, None)
|
|
|
|
self._owner.remove_telepathy_handle(plugin, plugin.self_handle)
|
2007-04-17 04:44:54 +02:00
|
|
|
|
|
|
|
if self._connected != old_status:
|
|
|
|
self.emit('connection-status', self._connected)
|
2007-02-23 18:04:16 +01:00
|
|
|
|
2007-02-28 17:02:43 +01:00
|
|
|
def _contact_online(self, tp, handle, props):
|
2007-02-26 12:28:50 +01:00
|
|
|
new_buddy = False
|
2007-04-10 20:12:12 +02:00
|
|
|
key = props["key"]
|
|
|
|
buddy = self._buddies.get(key)
|
2007-02-23 17:30:25 +01:00
|
|
|
if not buddy:
|
|
|
|
# we don't know yet this buddy
|
|
|
|
objid = self._get_next_object_id()
|
2007-05-25 17:21:02 +02:00
|
|
|
buddy = Buddy(self._session_bus, objid, key=key)
|
2007-03-02 17:10:44 +01:00
|
|
|
buddy.connect("validity-changed", self._buddy_validity_changed_cb)
|
2007-05-18 16:22:32 +02:00
|
|
|
buddy.connect("disappeared", self._buddy_disappeared_cb)
|
2007-02-23 17:30:25 +01:00
|
|
|
self._buddies[key] = buddy
|
|
|
|
|
2007-05-18 16:22:32 +02:00
|
|
|
self._handles_buddies[tp][handle] = buddy
|
2007-02-26 12:28:50 +01:00
|
|
|
# store the handle of the buddy for this CM
|
2007-05-18 16:22:32 +02:00
|
|
|
buddy.add_telepathy_handle(tp, handle)
|
2007-02-26 12:28:50 +01:00
|
|
|
|
2007-02-28 17:02:43 +01:00
|
|
|
buddy.set_properties(props)
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
def _buddy_validity_changed_cb(self, buddy, valid):
|
|
|
|
if valid:
|
|
|
|
self.BuddyAppeared(buddy.object_path())
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("New Buddy: %s (%s)", buddy.props.nick,
|
|
|
|
buddy.props.color)
|
2007-03-02 17:10:44 +01:00
|
|
|
else:
|
|
|
|
self.BuddyDisappeared(buddy.object_path())
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("Buddy left: %s (%s)", buddy.props.nick,
|
|
|
|
buddy.props.color)
|
2007-04-17 04:43:26 +02:00
|
|
|
|
2007-05-18 16:22:32 +02:00
|
|
|
def _buddy_disappeared_cb(self, buddy):
|
|
|
|
if buddy.props.valid:
|
|
|
|
self.BuddyDisappeared(buddy.object_path())
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug('Buddy left: %s (%s)', buddy.props.nick,
|
|
|
|
buddy.props.color)
|
2007-05-18 16:22:32 +02:00
|
|
|
self._buddies.pop(buddy.props.key)
|
|
|
|
|
2007-02-23 17:30:25 +01:00
|
|
|
def _contact_offline(self, tp, handle):
|
2007-04-17 04:43:26 +02:00
|
|
|
if not self._handles_buddies[tp].has_key(handle):
|
|
|
|
return
|
|
|
|
|
2007-03-05 18:56:17 +01:00
|
|
|
buddy = self._handles_buddies[tp].pop(handle)
|
2007-03-02 17:10:44 +01:00
|
|
|
key = buddy.props.key
|
2007-02-23 17:30:25 +01:00
|
|
|
|
2007-02-26 12:28:50 +01:00
|
|
|
# the handle of the buddy for this CM is not valid anymore
|
2007-05-18 16:22:32 +02:00
|
|
|
buddy.remove_telepathy_handle(tp, handle)
|
2007-02-23 14:15:51 +01:00
|
|
|
|
|
|
|
def _get_next_object_id(self):
|
|
|
|
"""Increment and return the object ID counter."""
|
|
|
|
self._next_object_id = self._next_object_id + 1
|
|
|
|
return self._next_object_id
|
|
|
|
|
2007-02-26 17:18:52 +01:00
|
|
|
def _avatar_updated(self, tp, handle, avatar):
|
2007-03-05 18:56:17 +01:00
|
|
|
buddy = self._handles_buddies[tp].get(handle)
|
2007-03-02 17:10:44 +01:00
|
|
|
if buddy and not buddy.props.owner:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Buddy %s icon updated" % buddy.props.nick)
|
2007-03-02 17:10:44 +01:00
|
|
|
buddy.props.icon = avatar
|
2007-02-26 17:18:52 +01:00
|
|
|
|
2007-04-17 04:42:35 +02:00
|
|
|
def _buddy_properties_changed(self, tp, handle, properties):
|
2007-03-05 18:56:17 +01:00
|
|
|
buddy = self._handles_buddies[tp].get(handle)
|
2007-02-27 23:51:53 +01:00
|
|
|
if buddy:
|
2007-04-17 04:42:35 +02:00
|
|
|
buddy.set_properties(properties)
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("Buddy %s properties updated: %s", buddy.props.nick,
|
2007-05-24 15:25:40 +02:00
|
|
|
properties.keys())
|
2007-02-27 23:51:53 +01:00
|
|
|
|
2007-03-06 17:15:55 +01:00
|
|
|
def _new_activity(self, activity_id, tp):
|
2007-03-08 18:51:10 +01:00
|
|
|
try:
|
|
|
|
objid = self._get_next_object_id()
|
2007-05-25 17:21:02 +02:00
|
|
|
activity = Activity(self._session_bus, objid, tp, id=activity_id)
|
2007-05-24 12:34:48 +02:00
|
|
|
except Exception:
|
|
|
|
# FIXME: catching bare Exception considered harmful
|
|
|
|
_logger.debug("Invalid activity:", exc_info=1)
|
2007-03-08 18:51:10 +01:00
|
|
|
return None
|
|
|
|
|
2007-05-24 12:34:48 +02:00
|
|
|
activity.connect("validity-changed",
|
|
|
|
self._activity_validity_changed_cb)
|
2007-03-05 18:56:17 +01:00
|
|
|
self._activities[activity_id] = activity
|
|
|
|
return activity
|
|
|
|
|
|
|
|
def _remove_activity(self, activity):
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("remove activity %s" % activity.props.id)
|
2007-03-05 18:56:17 +01:00
|
|
|
|
|
|
|
self.ActivityDisappeared(activity.object_path())
|
2007-03-08 18:51:10 +01:00
|
|
|
del self._activities[activity.props.id]
|
2007-05-24 12:34:48 +02:00
|
|
|
|
2007-03-14 15:59:30 +01:00
|
|
|
def _buddy_activities_changed(self, tp, contact_handle, activities):
|
2007-04-13 22:59:16 +02:00
|
|
|
acts = []
|
|
|
|
for act in activities:
|
|
|
|
acts.append(str(act))
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("Handle %s activities changed: %s", contact_handle, acts)
|
2007-03-05 18:56:17 +01:00
|
|
|
buddies = self._handles_buddies[tp]
|
|
|
|
buddy = buddies.get(contact_handle)
|
|
|
|
|
|
|
|
if not buddy:
|
|
|
|
# We don't know this buddy
|
2007-05-24 12:34:48 +02:00
|
|
|
# FIXME: What should we do here?
|
2007-03-05 18:56:17 +01:00
|
|
|
# FIXME: Do we need to check if the buddy is valid or something?
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("contact_activities_changed: buddy unknown")
|
2007-03-05 18:56:17 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
old_activities = set()
|
|
|
|
for activity in buddy.get_joined_activities():
|
2007-03-08 18:51:10 +01:00
|
|
|
old_activities.add(activity.props.id)
|
2007-03-05 18:56:17 +01:00
|
|
|
|
|
|
|
new_activities = set(activities)
|
|
|
|
|
|
|
|
activities_joined = new_activities - old_activities
|
|
|
|
for act in activities_joined:
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("Handle %s joined activity %s", contact_handle, act)
|
2007-03-05 18:56:17 +01:00
|
|
|
activity = self._activities.get(act)
|
2007-05-24 19:11:43 +02:00
|
|
|
if activity is None:
|
2007-03-08 18:51:10 +01:00
|
|
|
# new activity, can fail
|
2007-03-06 17:15:55 +01:00
|
|
|
activity = self._new_activity(act, tp)
|
2007-03-05 18:56:17 +01:00
|
|
|
|
2007-05-24 19:11:43 +02:00
|
|
|
if activity is not None:
|
2007-03-08 18:51:10 +01:00
|
|
|
activity.buddy_joined(buddy)
|
|
|
|
buddy.add_activity(activity)
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-03-05 18:56:17 +01:00
|
|
|
activities_left = old_activities - new_activities
|
|
|
|
for act in activities_left:
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("Handle %s left activity %s", contact_handle, act)
|
2007-03-05 18:56:17 +01:00
|
|
|
activity = self._activities.get(act)
|
|
|
|
if not activity:
|
|
|
|
continue
|
2007-05-24 12:34:48 +02:00
|
|
|
|
2007-03-05 18:56:17 +01:00
|
|
|
activity.buddy_left(buddy)
|
|
|
|
buddy.remove_activity(activity)
|
|
|
|
|
|
|
|
if not activity.get_joined_buddies():
|
|
|
|
self._remove_activity(activity)
|
|
|
|
|
2007-03-07 19:46:48 +01:00
|
|
|
def _activity_invitation(self, tp, act_id):
|
2007-03-07 15:27:47 +01:00
|
|
|
activity = self._activities.get(act_id)
|
|
|
|
if activity:
|
2007-03-07 19:46:48 +01:00
|
|
|
self.ActivityInvitation(activity.object_path())
|
|
|
|
|
|
|
|
def _private_invitation(self, tp, chan_path):
|
|
|
|
conn = tp.get_connection()
|
2007-05-24 12:34:48 +02:00
|
|
|
self.PrivateInvitation(str(conn.service_name), conn.object_path,
|
|
|
|
chan_path)
|
2007-03-07 15:27:47 +01:00
|
|
|
|
2007-02-21 13:06:45 +01:00
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def ActivityAppeared(self, activity):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def ActivityDisappeared(self, activity):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def BuddyAppeared(self, buddy):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def BuddyDisappeared(self, buddy):
|
|
|
|
pass
|
|
|
|
|
2007-03-07 19:46:48 +01:00
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def ActivityInvitation(self, activity):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="soo")
|
|
|
|
def PrivateInvitation(self, bus_name, connection, channel):
|
|
|
|
pass
|
|
|
|
|
2007-05-24 16:39:31 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature='',
|
|
|
|
out_signature="ao")
|
2007-02-21 13:06:45 +01:00
|
|
|
def GetActivities(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
ret = []
|
|
|
|
for act in self._activities.values():
|
2007-04-10 16:59:35 +02:00
|
|
|
if act.props.valid:
|
|
|
|
ret.append(act.object_path())
|
2007-02-21 13:58:16 +01:00
|
|
|
return ret
|
2007-02-21 13:06:45 +01:00
|
|
|
|
2007-05-24 12:34:48 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature="s",
|
|
|
|
out_signature="o")
|
2007-02-21 13:06:45 +01:00
|
|
|
def GetActivityById(self, actid):
|
2007-05-24 15:52:20 +02:00
|
|
|
act = self._activities.get(actid, None)
|
2007-04-17 04:44:54 +02:00
|
|
|
if not act or not act.props.valid:
|
|
|
|
raise NotFoundError("The activity was not found.")
|
|
|
|
return act.object_path()
|
2007-02-21 13:06:45 +01:00
|
|
|
|
2007-05-24 16:39:31 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature='',
|
|
|
|
out_signature="ao")
|
2007-02-21 13:06:45 +01:00
|
|
|
def GetBuddies(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
ret = []
|
|
|
|
for buddy in self._buddies.values():
|
2007-04-10 16:59:35 +02:00
|
|
|
if buddy.props.valid:
|
|
|
|
ret.append(buddy.object_path())
|
2007-02-21 13:58:16 +01:00
|
|
|
return ret
|
2007-02-21 13:06:45 +01:00
|
|
|
|
2007-05-24 12:21:21 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE,
|
|
|
|
in_signature="ay", out_signature="o",
|
|
|
|
byte_arrays=True)
|
2007-02-21 13:06:45 +01:00
|
|
|
def GetBuddyByPublicKey(self, key):
|
2007-02-21 13:58:16 +01:00
|
|
|
if self._buddies.has_key(key):
|
2007-04-10 16:59:35 +02:00
|
|
|
buddy = self._buddies[key]
|
|
|
|
if buddy.props.valid:
|
|
|
|
return buddy.object_path()
|
2007-02-21 13:08:40 +01:00
|
|
|
raise NotFoundError("The buddy was not found.")
|
2007-02-21 13:06:45 +01:00
|
|
|
|
2007-05-18 16:22:32 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature='sou',
|
|
|
|
out_signature='o')
|
|
|
|
def GetBuddyByTelepathyHandle(self, tp_conn_name, tp_conn_path, handle):
|
|
|
|
"""Get the buddy corresponding to a Telepathy handle.
|
|
|
|
|
|
|
|
: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 object path of a Buddy
|
|
|
|
:Raises NotFoundError: if the buddy is not found.
|
|
|
|
"""
|
|
|
|
for tp, handles in self._handles_buddies.iteritems():
|
|
|
|
conn = tp.get_connection()
|
|
|
|
if conn is None:
|
|
|
|
continue
|
|
|
|
if (conn.service_name == tp_conn_name
|
|
|
|
and conn.object_path == tp_conn_path):
|
|
|
|
buddy = handles.get(handle)
|
|
|
|
if buddy is not None and buddy.props.valid:
|
|
|
|
return buddy.object_path()
|
|
|
|
# either the handle is invalid, or we don't have a Buddy
|
|
|
|
# object for that buddy because we don't have all their
|
|
|
|
# details yet
|
|
|
|
raise NotFoundError("The buddy %u was not found on the "
|
|
|
|
"connection to %s:%s"
|
|
|
|
% (handle, tp_conn_name, tp_conn_path))
|
|
|
|
raise NotFoundError("The buddy %u was not found: we have no "
|
|
|
|
"connection to %s:%s" % (handle, tp_conn_name,
|
|
|
|
tp_conn_path))
|
|
|
|
|
2007-05-24 16:39:31 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE,
|
|
|
|
in_signature='', out_signature="o")
|
2007-02-21 13:06:45 +01:00
|
|
|
def GetOwner(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
if not self._owner:
|
|
|
|
raise NotFoundError("The owner was not found.")
|
|
|
|
else:
|
2007-05-07 20:18:20 +02:00
|
|
|
return self._owner.object_path()
|
2007-02-21 12:41:26 +01:00
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature="sssa{sv}",
|
|
|
|
out_signature="o", async_callbacks=('async_cb', 'async_err_cb'))
|
2007-05-24 12:34:48 +02:00
|
|
|
def ShareActivity(self, actid, atype, name, properties, async_cb,
|
|
|
|
async_err_cb):
|
|
|
|
self._share_activity(actid, atype, name, properties,
|
|
|
|
(async_cb, async_err_cb))
|
2007-02-21 12:41:26 +01:00
|
|
|
|
2007-05-24 16:39:31 +02:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE,
|
|
|
|
in_signature='', out_signature="so")
|
2007-04-13 18:29:50 +02:00
|
|
|
def GetPreferredConnection(self):
|
|
|
|
conn = self._server_plugin.get_connection()
|
|
|
|
return str(conn.service_name), conn.object_path
|
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
def cleanup(self):
|
2007-03-05 18:56:17 +01:00
|
|
|
for tp in self._handles_buddies:
|
2007-03-02 17:10:44 +01:00
|
|
|
tp.cleanup()
|
2007-02-21 12:41:26 +01:00
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
def _share_activity(self, actid, atype, name, properties, callbacks):
|
2007-03-06 17:15:55 +01:00
|
|
|
objid = self._get_next_object_id()
|
|
|
|
# FIXME check which tp client we should use to share the activity
|
2007-03-08 18:51:10 +01:00
|
|
|
color = self._owner.props.color
|
2007-05-25 17:21:02 +02:00
|
|
|
activity = Activity(self._session_bus, objid, self._server_plugin,
|
2007-05-24 12:34:48 +02:00
|
|
|
id=actid, type=atype, name=name, color=color,
|
|
|
|
local=True)
|
|
|
|
activity.connect("validity-changed",
|
|
|
|
self._activity_validity_changed_cb)
|
2007-03-06 17:15:55 +01:00
|
|
|
self._activities[actid] = activity
|
2007-04-13 22:59:16 +02:00
|
|
|
activity._share(callbacks, self._owner)
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-04-13 21:15:09 +02:00
|
|
|
# local activities are valid at creation by definition, but we can't
|
|
|
|
# connect to the activity's validity-changed signal until its already
|
|
|
|
# issued the signal, which happens in the activity's constructor
|
|
|
|
# for local activities.
|
|
|
|
self._activity_validity_changed_cb(activity, activity.props.valid)
|
|
|
|
|
2007-03-08 18:51:10 +01:00
|
|
|
def _activity_validity_changed_cb(self, activity, valid):
|
|
|
|
if valid:
|
|
|
|
self.ActivityAppeared(activity.object_path())
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("New Activity: %s (%s)", activity.props.name,
|
2007-05-24 15:25:40 +02:00
|
|
|
activity.props.id)
|
2007-03-08 18:51:10 +01:00
|
|
|
else:
|
|
|
|
self.ActivityDisappeared(activity.object_path())
|
2007-05-24 12:34:48 +02:00
|
|
|
_logger.debug("Activity disappeared: %s (%s)", activity.props.name,
|
2007-05-24 15:25:40 +02:00
|
|
|
activity.props.id)
|
2007-03-08 18:51:10 +01:00
|
|
|
|
2007-03-14 15:59:30 +01:00
|
|
|
def _activity_properties_changed(self, tp, act_id, props):
|
|
|
|
activity = self._activities.get(act_id)
|
|
|
|
if activity:
|
|
|
|
activity.set_properties(props)
|
|
|
|
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-04-30 00:01:23 +02:00
|
|
|
def main(test_num=0, randomize=False):
|
2007-02-21 12:41:26 +01:00
|
|
|
loop = gobject.MainLoop()
|
2007-05-24 16:38:54 +02:00
|
|
|
dbus_mainloop_wrapper = DBusGMainLoop(set_as_default=True)
|
2007-05-24 15:27:36 +02:00
|
|
|
|
|
|
|
if test_num > 0:
|
|
|
|
from pstest import TestPresenceService
|
|
|
|
ps = TestPresenceService(test_num, randomize)
|
|
|
|
else:
|
|
|
|
ps = PresenceService()
|
|
|
|
|
2007-02-21 12:41:26 +01:00
|
|
|
try:
|
|
|
|
loop.run()
|
|
|
|
except KeyboardInterrupt:
|
2007-03-02 17:10:44 +01:00
|
|
|
ps.cleanup()
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug('Ctrl+C pressed, exiting...')
|
2007-02-21 12:41:26 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|