2007-02-22 20:11:31 +01:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
|
|
|
# Copyright (C) 2007, Collabora Ltd.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2007-03-08 18:51:10 +01:00
|
|
|
import gobject
|
2007-05-11 23:27:36 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
from dbus.gobject_service import ExportedGObject
|
2007-03-08 18:51:10 +01:00
|
|
|
from sugar import util
|
2007-04-13 21:59:36 +02:00
|
|
|
import logging
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
from telepathy.constants import CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES
|
|
|
|
from telepathy.interfaces import (CHANNEL_INTERFACE, CHANNEL_INTERFACE_GROUP)
|
2007-03-06 18:22:43 +01:00
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
_ACTIVITY_PATH = "/org/laptop/Sugar/Presence/Activities/"
|
|
|
|
_ACTIVITY_INTERFACE = "org.laptop.Sugar.Presence.Activity"
|
|
|
|
|
2007-04-30 03:44:57 +02:00
|
|
|
_PROP_ID = "id"
|
|
|
|
_PROP_NAME = "name"
|
|
|
|
_PROP_COLOR = "color"
|
|
|
|
_PROP_TYPE = "type"
|
|
|
|
_PROP_VALID = "valid"
|
|
|
|
_PROP_LOCAL = "local"
|
|
|
|
_PROP_JOINED = "joined"
|
|
|
|
_PROP_CUSTOM_PROPS = "custom-props"
|
|
|
|
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger = logging.getLogger('s-p-s.activity')
|
|
|
|
|
2007-05-11 23:27:36 +02:00
|
|
|
class Activity(ExportedGObject):
|
2007-05-25 17:21:02 +02:00
|
|
|
"""Represents a shared activity seen on the network, or a local activity
|
|
|
|
that has been, or will be, shared onto the network.
|
|
|
|
|
|
|
|
The activity might be public, restricted to a group, or invite-only.
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-03-08 18:51:10 +01:00
|
|
|
__gtype_name__ = "Activity"
|
|
|
|
|
|
|
|
__gsignals__ = {
|
2007-05-29 17:12:05 +02:00
|
|
|
'validity-changed':
|
|
|
|
# The activity's validity has changed.
|
|
|
|
# An activity is valid if its name, color, type and ID have been
|
|
|
|
# set.
|
|
|
|
# Arguments:
|
|
|
|
# validity: bool
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [bool]),
|
|
|
|
'disappeared':
|
|
|
|
# Nobody is in this activity any more.
|
|
|
|
# No arguments.
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, []),
|
2007-03-08 18:51:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-04-30 03:44:57 +02:00
|
|
|
_PROP_ID : (str, None, None, None,
|
2007-05-25 11:56:14 +02:00
|
|
|
gobject.PARAM_READWRITE |
|
|
|
|
gobject.PARAM_CONSTRUCT_ONLY),
|
2007-04-30 03:44:57 +02:00
|
|
|
_PROP_NAME : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_COLOR : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_TYPE : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_VALID : (bool, None, None, False, gobject.PARAM_READABLE),
|
|
|
|
_PROP_LOCAL : (bool, None, None, False,
|
2007-05-25 11:56:14 +02:00
|
|
|
gobject.PARAM_READWRITE |
|
|
|
|
gobject.PARAM_CONSTRUCT_ONLY),
|
2007-04-30 03:44:57 +02:00
|
|
|
_PROP_JOINED : (bool, None, None, False, gobject.PARAM_READABLE),
|
|
|
|
_PROP_CUSTOM_PROPS : (object, None, None,
|
2007-05-25 11:56:14 +02:00
|
|
|
gobject.PARAM_READWRITE |
|
|
|
|
gobject.PARAM_CONSTRUCT_ONLY)
|
2007-03-08 18:51:10 +01:00
|
|
|
}
|
|
|
|
|
2007-04-30 15:51:35 +02:00
|
|
|
_RESERVED_PROPNAMES = __gproperties__.keys()
|
2007-04-30 03:44:57 +02:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
def __init__(self, bus, object_id, ps, tp, **kwargs):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Initializes the activity and sets its properties to default values.
|
2007-05-24 15:26:30 +02:00
|
|
|
|
|
|
|
:Parameters:
|
2007-05-25 17:21:02 +02:00
|
|
|
`bus` : dbus.bus.BusConnection
|
|
|
|
A connection to the D-Bus session bus
|
2007-05-24 15:26:30 +02:00
|
|
|
`object_id` : int
|
|
|
|
PS ID for this activity, used to construct the object-path
|
2007-05-30 18:36:42 +02:00
|
|
|
`ps` : presenceservice.PresenceService
|
|
|
|
The presence service
|
2007-05-24 15:26:30 +02:00
|
|
|
`tp` : server plugin
|
|
|
|
The server plugin object (stands for "telepathy plugin")
|
|
|
|
:Keywords:
|
|
|
|
`id` : str
|
|
|
|
The globally unique activity ID (required)
|
|
|
|
`name` : str
|
|
|
|
Human-readable title for the activity
|
|
|
|
`color` : str
|
|
|
|
Activity color in #RRGGBB,#RRGGBB (stroke,fill) format
|
|
|
|
`type` : str
|
|
|
|
D-Bus service name representing the activity type
|
|
|
|
`local : bool
|
|
|
|
If True, this activity was initiated locally and is not
|
|
|
|
(yet) advertised on the network
|
|
|
|
(FIXME: is this description right?)
|
|
|
|
`custom-props` : dict
|
|
|
|
Activity-specific properties
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-03-08 18:51:10 +01:00
|
|
|
if not object_id or not isinstance(object_id, int):
|
|
|
|
raise ValueError("object id must be a valid number")
|
|
|
|
if not tp:
|
|
|
|
raise ValueError("telepathy CM must be valid")
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
self._ps = ps
|
2007-02-22 20:11:31 +01:00
|
|
|
self._object_id = object_id
|
2007-05-25 17:30:36 +02:00
|
|
|
self._object_path = dbus.ObjectPath(_ACTIVITY_PATH +
|
|
|
|
str(self._object_id))
|
2007-03-08 18:51:10 +01:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
self._buddies = set()
|
|
|
|
self._member_handles = set()
|
2007-03-08 18:51:10 +01:00
|
|
|
self._joined = False
|
|
|
|
|
2007-03-06 17:15:55 +01:00
|
|
|
# the telepathy client
|
|
|
|
self._tp = tp
|
2007-05-30 18:36:42 +02:00
|
|
|
self._self_handle = None
|
2007-04-13 19:11:59 +02:00
|
|
|
self._text_channel = None
|
2007-05-30 18:36:42 +02:00
|
|
|
self._text_channel_group_flags = 0
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-03-08 18:51:10 +01:00
|
|
|
self._valid = False
|
|
|
|
self._id = None
|
2007-05-14 03:40:24 +02:00
|
|
|
self._actname = None
|
2007-03-14 15:59:30 +01:00
|
|
|
self._color = None
|
2007-03-08 18:51:10 +01:00
|
|
|
self._local = False
|
|
|
|
self._type = None
|
2007-04-30 03:44:57 +02:00
|
|
|
self._custom_props = {}
|
2007-03-08 18:51:10 +01:00
|
|
|
|
2007-04-30 03:44:57 +02:00
|
|
|
# ensure no reserved property names are in custom properties
|
2007-05-25 11:56:14 +02:00
|
|
|
cprops = kwargs.get(_PROP_CUSTOM_PROPS)
|
|
|
|
if cprops is not None:
|
|
|
|
(rprops, cprops) = self._split_properties(cprops)
|
2007-04-30 03:44:57 +02:00
|
|
|
if len(rprops.keys()) > 0:
|
2007-05-25 11:56:14 +02:00
|
|
|
raise ValueError("Cannot use reserved property names '%s'"
|
|
|
|
% ", ".join(rprops.keys()))
|
2007-04-30 03:44:57 +02:00
|
|
|
|
|
|
|
if not kwargs.get(_PROP_ID):
|
2007-03-08 18:51:10 +01:00
|
|
|
raise ValueError("activity id is required")
|
2007-04-30 03:44:57 +02:00
|
|
|
if not util.validate_activity_id(kwargs[_PROP_ID]):
|
|
|
|
raise ValueError("Invalid activity id '%s'" % kwargs[_PROP_ID])
|
2007-03-08 18:51:10 +01:00
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
ExportedGObject.__init__(self, bus, self._object_path,
|
2007-05-11 23:27:36 +02:00
|
|
|
gobject_properties=kwargs)
|
2007-03-08 18:51:10 +01:00
|
|
|
if self.props.local and not self.props.valid:
|
2007-05-25 11:56:14 +02:00
|
|
|
raise RuntimeError("local activities require color, type, and "
|
|
|
|
"name")
|
2007-03-08 18:51:10 +01:00
|
|
|
|
2007-04-13 21:42:54 +02:00
|
|
|
# If not yet valid, query activity properties
|
|
|
|
if not self.props.valid:
|
|
|
|
tp.update_activity_properties(self._id)
|
|
|
|
|
2007-03-08 18:51:10 +01:00
|
|
|
def do_get_property(self, pspec):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Gets the value of a property associated with this activity.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
pspec -- Property specifier
|
|
|
|
|
|
|
|
returns The value of the given property.
|
|
|
|
"""
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-30 03:44:57 +02:00
|
|
|
if pspec.name == _PROP_ID:
|
2007-03-08 18:51:10 +01:00
|
|
|
return self._id
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_NAME:
|
2007-05-14 03:11:19 +02:00
|
|
|
return self._actname
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_COLOR:
|
2007-03-08 18:51:10 +01:00
|
|
|
return self._color
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_TYPE:
|
2007-03-08 18:51:10 +01:00
|
|
|
return self._type
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_VALID:
|
2007-03-08 18:51:10 +01:00
|
|
|
return self._valid
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_JOINED:
|
2007-03-08 18:51:10 +01:00
|
|
|
return self._joined
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_LOCAL:
|
2007-03-08 18:51:10 +01:00
|
|
|
return self._local
|
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Sets the value of a property associated with this activity.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
pspec -- Property specifier
|
|
|
|
value -- Desired value
|
|
|
|
|
2007-05-25 11:56:14 +02:00
|
|
|
Note that the "type" property can be set only once; attempting to set
|
|
|
|
it to something different later will raise a RuntimeError.
|
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-04-30 03:44:57 +02:00
|
|
|
if pspec.name == _PROP_ID:
|
|
|
|
if self._id:
|
|
|
|
raise RuntimeError("activity ID is already set")
|
2007-03-08 18:51:10 +01:00
|
|
|
self._id = value
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_NAME:
|
2007-05-14 03:11:19 +02:00
|
|
|
self._actname = value
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_COLOR:
|
2007-03-08 18:51:10 +01:00
|
|
|
self._color = value
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_TYPE:
|
2007-03-08 18:51:10 +01:00
|
|
|
if self._type:
|
|
|
|
raise RuntimeError("activity type is already set")
|
|
|
|
self._type = value
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_JOINED:
|
2007-03-08 18:51:10 +01:00
|
|
|
self._joined = value
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_LOCAL:
|
2007-03-08 18:51:10 +01:00
|
|
|
self._local = value
|
2007-04-30 03:44:57 +02:00
|
|
|
elif pspec.name == _PROP_CUSTOM_PROPS:
|
2007-04-30 15:51:35 +02:00
|
|
|
if not value:
|
|
|
|
value = {}
|
2007-04-30 03:44:57 +02:00
|
|
|
(rprops, cprops) = self._split_properties(value)
|
|
|
|
self._custom_props = {}
|
2007-04-30 15:51:35 +02:00
|
|
|
for (key, dvalue) in cprops.items():
|
2007-04-30 03:44:57 +02:00
|
|
|
self._custom_props[str(key)] = str(dvalue)
|
2007-03-08 18:51:10 +01:00
|
|
|
|
|
|
|
self._update_validity()
|
|
|
|
|
|
|
|
def _update_validity(self):
|
2007-05-25 11:56:14 +02:00
|
|
|
"""Sends a "validity-changed" signal if this activity's validity has
|
|
|
|
changed.
|
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
Determines whether this activity's status has changed from valid to
|
|
|
|
invalid, or invalid to valid, and emits a "validity-changed" signal
|
|
|
|
if either is true. "Valid" means that the object's type, ID, name,
|
|
|
|
colour and type properties have all been set to something valid
|
|
|
|
(i.e., not "None").
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
try:
|
|
|
|
old_valid = self._valid
|
2007-05-14 03:11:19 +02:00
|
|
|
if self._color and self._actname and self._id and self._type:
|
2007-03-08 18:51:10 +01:00
|
|
|
self._valid = True
|
|
|
|
else:
|
|
|
|
self._valid = False
|
|
|
|
|
|
|
|
if old_valid != self._valid:
|
|
|
|
self.emit("validity-changed", self._valid)
|
|
|
|
except AttributeError:
|
|
|
|
self._valid = False
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
# dbus signals
|
|
|
|
@dbus.service.signal(_ACTIVITY_INTERFACE,
|
|
|
|
signature="o")
|
|
|
|
def BuddyJoined(self, buddy_path):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Generates DBUS signal when a buddy joins this activity.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
buddy_path -- DBUS path to buddy object
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_ACTIVITY_INTERFACE,
|
|
|
|
signature="o")
|
|
|
|
def BuddyLeft(self, buddy_path):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Generates DBUS signal when a buddy leaves this activity.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
buddy_path -- DBUS path to buddy object
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_ACTIVITY_INTERFACE,
|
|
|
|
signature="o")
|
|
|
|
def NewChannel(self, channel_path):
|
2007-05-25 11:56:14 +02:00
|
|
|
"""Generates DBUS signal when a new channel is created for this
|
|
|
|
activity.
|
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
channel_path -- DBUS path to new channel
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
XXX - what is this supposed to do? Who is supposed to call it?
|
|
|
|
What is the channel path? Right now this is never called.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
# dbus methods
|
|
|
|
@dbus.service.method(_ACTIVITY_INTERFACE,
|
|
|
|
in_signature="", out_signature="s")
|
|
|
|
def GetId(self):
|
2007-05-25 17:21:02 +02:00
|
|
|
"""DBUS method to get this activity's (randomly generated) unique ID
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
:Returns: Activity ID as a string
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
return self.props.id
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_ACTIVITY_INTERFACE,
|
|
|
|
in_signature="", out_signature="s")
|
|
|
|
def GetColor(self):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""DBUS method to get this activity's colour
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
:Returns: Activity colour as a string in the format #RRGGBB,#RRGGBB
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
return self.props.color
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-08 16:16:06 +01:00
|
|
|
@dbus.service.method(_ACTIVITY_INTERFACE,
|
|
|
|
in_signature="", out_signature="s")
|
|
|
|
def GetType(self):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""DBUS method to get this activity's type
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
:Returns: Activity type as a string, in the same form as a D-Bus
|
|
|
|
well-known name
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
return self.props.type
|
2007-03-08 16:16:06 +01:00
|
|
|
|
2007-05-25 11:56:14 +02:00
|
|
|
@dbus.service.method(_ACTIVITY_INTERFACE,
|
|
|
|
in_signature="", out_signature="",
|
|
|
|
async_callbacks=('async_cb', 'async_err_cb'))
|
2007-04-13 19:11:59 +02:00
|
|
|
def Join(self, async_cb, async_err_cb):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""DBUS method to for the local user to attempt to join the activity
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
async_cb -- Callback method to be called if join attempt is successful
|
2007-05-25 11:56:14 +02:00
|
|
|
async_err_cb -- Callback method to be called if join attempt is
|
|
|
|
unsuccessful
|
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-04-13 19:11:59 +02:00
|
|
|
self.join(async_cb, async_err_cb)
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_ACTIVITY_INTERFACE,
|
|
|
|
in_signature="", out_signature="ao")
|
|
|
|
def GetJoinedBuddies(self):
|
2007-05-25 11:56:14 +02:00
|
|
|
"""DBUS method to return a list of valid buddies who are joined in
|
|
|
|
this activity
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
:Returns:
|
|
|
|
A list of buddy object paths corresponding to those buddies
|
|
|
|
in this activity who are 'valid' (i.e. for whom we have complete
|
|
|
|
information)
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
ret = []
|
2007-02-22 20:11:31 +01:00
|
|
|
for buddy in self._buddies:
|
2007-03-08 18:51:10 +01:00
|
|
|
if buddy.props.valid:
|
|
|
|
ret.append(buddy.object_path())
|
2007-02-22 20:11:31 +01:00
|
|
|
return ret
|
|
|
|
|
|
|
|
@dbus.service.method(_ACTIVITY_INTERFACE,
|
|
|
|
in_signature="", out_signature="soao")
|
|
|
|
def GetChannels(self):
|
2007-05-25 11:56:14 +02:00
|
|
|
"""DBUS method to get the list of channels associated with this
|
|
|
|
activity
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
:Returns:
|
|
|
|
a tuple containing:
|
|
|
|
- the D-Bus well-known service name of the connection
|
|
|
|
(FIXME: this is redundant; in Telepathy it can be derived
|
|
|
|
from that of the connection)
|
|
|
|
- the D-Bus object path of the connection
|
|
|
|
- a list of D-Bus object paths representing the channels
|
|
|
|
associated with this activity
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-06 17:50:49 +01:00
|
|
|
return self.get_channels()
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-07 20:09:40 +01:00
|
|
|
@dbus.service.method(_ACTIVITY_INTERFACE,
|
|
|
|
in_signature="", out_signature="s")
|
|
|
|
def GetName(self):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""DBUS method to get this activity's name
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
returns Activity name
|
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
return self.props.name
|
2007-03-07 20:09:40 +01:00
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
# methods
|
|
|
|
def object_path(self):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Retrieves our dbus.ObjectPath object
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
returns DBUS ObjectPath object
|
|
|
|
"""
|
2007-05-25 17:30:36 +02:00
|
|
|
return self._object_path
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-05 18:56:17 +01:00
|
|
|
def get_joined_buddies(self):
|
2007-05-25 11:56:14 +02:00
|
|
|
"""Local method to return a list of valid buddies who are joined in
|
|
|
|
this activity
|
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
This method is called by the PresenceService on the local machine.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
returns A list of buddy objects
|
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
ret = []
|
|
|
|
for buddy in self._buddies:
|
|
|
|
if buddy.props.valid:
|
|
|
|
ret.append(buddy)
|
|
|
|
return ret
|
2007-03-08 16:16:06 +01:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
def buddy_apparently_joined(self, buddy):
|
|
|
|
"""Adds a buddy to this activity and sends a BuddyJoined signal,
|
|
|
|
unless we can already see who's in the activity by being in it
|
|
|
|
ourselves.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
buddy -- Buddy object representing the buddy being added
|
2007-05-25 11:56:14 +02:00
|
|
|
|
|
|
|
Adds a buddy to this activity if the buddy is not already in the
|
|
|
|
buddy list.
|
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
If this activity is "valid", a BuddyJoined signal is also sent.
|
|
|
|
This method is called by the PresenceService on the local machine.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-05-30 18:36:42 +02:00
|
|
|
if not self._joined:
|
|
|
|
self._add_buddies((buddy,))
|
|
|
|
|
|
|
|
def _add_buddies(self, buddies):
|
|
|
|
buddies = set(buddies)
|
|
|
|
|
|
|
|
# disregard any who are already there
|
|
|
|
buddies -= self._buddies
|
|
|
|
|
|
|
|
self._buddies |= buddies
|
|
|
|
|
|
|
|
for buddy in buddies:
|
2007-05-29 17:12:05 +02:00
|
|
|
buddy.add_activity(self)
|
2007-03-08 18:51:10 +01:00
|
|
|
if self.props.valid:
|
|
|
|
self.BuddyJoined(buddy.object_path())
|
2007-03-05 18:56:17 +01:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
def _remove_buddies(self, buddies):
|
|
|
|
buddies = set(buddies)
|
|
|
|
|
|
|
|
# disregard any who are not already there
|
|
|
|
buddies &= self._buddies
|
|
|
|
|
|
|
|
self._buddies -= buddies
|
|
|
|
|
|
|
|
for buddy in buddies:
|
|
|
|
buddy.remove_activity(self)
|
|
|
|
if self.props.valid:
|
|
|
|
self.BuddyJoined(buddy.object_path())
|
|
|
|
|
|
|
|
if not self._buddies:
|
|
|
|
self.emit('disappeared')
|
|
|
|
|
|
|
|
def buddy_apparently_left(self, buddy):
|
|
|
|
"""Removes a buddy from this activity and sends a BuddyLeft signal,
|
|
|
|
unless we can already see who's in the activity by being in it
|
|
|
|
ourselves.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
buddy -- Buddy object representing the buddy being removed
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
Removes a buddy from this activity if the buddy is in the buddy list.
|
|
|
|
If this activity is "valid", a BuddyLeft signal is also sent.
|
|
|
|
This method is called by the PresenceService on the local machine.
|
|
|
|
"""
|
2007-05-30 18:36:42 +02:00
|
|
|
if not self._joined:
|
|
|
|
self._remove_buddies((buddy,))
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
def _text_channel_group_flags_changed_cb(self, flags):
|
|
|
|
self._text_channel_group_flags = flags
|
2007-05-29 17:12:05 +02:00
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
def _handle_share_join(self, tp, text_channel):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Called when a join to a network activity was successful.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
Called by the _shared_cb and _joined_cb methods.
|
|
|
|
"""
|
2007-04-13 19:11:59 +02:00
|
|
|
if not text_channel:
|
2007-05-25 11:56:14 +02:00
|
|
|
_logger.debug("Error sharing: text channel was None, shouldn't "
|
|
|
|
"happen")
|
2007-04-13 19:11:59 +02:00
|
|
|
raise RuntimeError("Plugin returned invalid text channel")
|
|
|
|
|
|
|
|
self._text_channel = text_channel
|
|
|
|
self._text_channel[CHANNEL_INTERFACE].connect_to_signal('Closed',
|
|
|
|
self._text_channel_closed_cb)
|
2007-05-30 18:36:42 +02:00
|
|
|
if CHANNEL_INTERFACE_GROUP in self._text_channel:
|
|
|
|
group = self._text_channel[CHANNEL_INTERFACE_GROUP]
|
|
|
|
|
|
|
|
# FIXME: make these method calls async?
|
|
|
|
|
|
|
|
group.connect_to_signal('GroupFlagsChanged',
|
|
|
|
self._text_channel_group_flags_changed_cb)
|
|
|
|
self._text_channel_group_flags = group.GetGroupFlags()
|
|
|
|
|
|
|
|
self._self_handle = group.GetSelfHandle()
|
|
|
|
|
|
|
|
# by the time we hook this, we need to know the group flags
|
|
|
|
group.connect_to_signal('MembersChanged',
|
|
|
|
self._text_channel_members_changed_cb)
|
|
|
|
# bootstrap by getting the current state. This is where we find
|
|
|
|
# out whether anyone was lying to us in their PEP info
|
|
|
|
members = set(group.GetMembers())
|
|
|
|
added = members - self._member_handles
|
|
|
|
removed = self._member_handles - members
|
|
|
|
if added or removed:
|
|
|
|
self._text_channel_members_changed_cb('', added, removed,
|
|
|
|
(), (), 0, 0)
|
|
|
|
|
|
|
|
# if we can see any member handles, we're probably able to see
|
|
|
|
# all members, so can stop caring about PEP announcements for this
|
|
|
|
# activity
|
|
|
|
self._joined = (self._self_handle in self._member_handles)
|
|
|
|
else:
|
|
|
|
self._joined = True
|
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def _shared_cb(self, tp, activity_id, text_channel, exc, userdata):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""XXX - not documented yet
|
|
|
|
"""
|
2007-04-13 19:11:59 +02:00
|
|
|
if activity_id != self.props.id:
|
|
|
|
# Not for us
|
|
|
|
return
|
|
|
|
|
2007-04-13 22:59:16 +02:00
|
|
|
(sigid, owner, async_cb, async_err_cb) = userdata
|
2007-04-13 19:11:59 +02:00
|
|
|
self._tp.disconnect(sigid)
|
|
|
|
|
|
|
|
if exc:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Share of activity %s failed: %s" % (self._id, exc))
|
2007-04-13 19:11:59 +02:00
|
|
|
async_err_cb(exc)
|
|
|
|
else:
|
|
|
|
self._handle_share_join(tp, text_channel)
|
|
|
|
self.send_properties()
|
2007-04-13 22:59:16 +02:00
|
|
|
owner.add_activity(self)
|
2007-04-13 19:11:59 +02:00
|
|
|
async_cb(dbus.ObjectPath(self._object_path))
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Share of activity %s succeeded." % self._id)
|
2007-04-13 19:11:59 +02:00
|
|
|
|
2007-04-13 22:59:16 +02:00
|
|
|
def _share(self, (async_cb, async_err_cb), owner):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""XXX - not documented yet
|
2007-05-25 11:56:14 +02:00
|
|
|
|
|
|
|
XXX - This method is called externally by the PresenceService
|
|
|
|
despite the fact that this is supposed to be an internal method!
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Starting share of activity %s" % self._id)
|
2007-04-13 19:11:59 +02:00
|
|
|
if self._joined:
|
2007-05-25 11:56:14 +02:00
|
|
|
async_err_cb(RuntimeError("Already shared activity %s"
|
|
|
|
% self.props.id))
|
2007-04-13 19:11:59 +02:00
|
|
|
return
|
|
|
|
sigid = self._tp.connect('activity-shared', self._shared_cb)
|
2007-05-25 11:56:14 +02:00
|
|
|
self._tp.share_activity(self.props.id, (sigid, owner, async_cb,
|
|
|
|
async_err_cb))
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("done with share attempt %s" % self._id)
|
2007-04-13 19:11:59 +02:00
|
|
|
|
|
|
|
def _joined_cb(self, tp, activity_id, text_channel, exc, userdata):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""XXX - not documented yet
|
|
|
|
"""
|
2007-04-13 19:11:59 +02:00
|
|
|
if activity_id != self.props.id:
|
|
|
|
# Not for us
|
|
|
|
return
|
|
|
|
|
|
|
|
(sigid, async_cb, async_err_cb) = userdata
|
|
|
|
self._tp.disconnect(sigid)
|
|
|
|
|
|
|
|
if exc:
|
|
|
|
async_err_cb(exc)
|
|
|
|
else:
|
|
|
|
self._handle_share_join(tp, text_channel)
|
|
|
|
async_cb()
|
|
|
|
|
|
|
|
def join(self, async_cb, async_err_cb):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Local method for the local user to attempt to join the activity.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
async_cb -- Callback method to be called if join attempt is successful
|
2007-05-25 11:56:14 +02:00
|
|
|
async_err_cb -- Callback method to be called if join attempt is
|
|
|
|
unsuccessful
|
|
|
|
|
|
|
|
The two callbacks are passed to the server_plugin ("tp") object,
|
|
|
|
which in turn passes them back as parameters in a callback to the
|
|
|
|
_joined_cb method; this callback is set up within this method.
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-04-13 19:11:59 +02:00
|
|
|
if self._joined:
|
2007-05-25 11:56:14 +02:00
|
|
|
async_err_cb(RuntimeError("Already joined activity %s"
|
|
|
|
% self.props.id))
|
2007-04-13 19:11:59 +02:00
|
|
|
return
|
|
|
|
sigid = self._tp.connect('activity-joined', self._joined_cb)
|
|
|
|
self._tp.join_activity(self.props.id, (sigid, async_cb, async_err_cb))
|
2007-03-06 17:50:49 +01:00
|
|
|
|
|
|
|
def get_channels(self):
|
2007-05-25 11:56:14 +02:00
|
|
|
"""Local method to get the list of channels associated with this
|
|
|
|
activity
|
|
|
|
|
|
|
|
returns XXX - expected a list of channels, instead returning a tuple?
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-06 17:50:49 +01:00
|
|
|
conn = self._tp.get_connection()
|
|
|
|
# FIXME add tubes and others channels
|
2007-05-25 11:56:14 +02:00
|
|
|
return (str(conn.service_name), conn.object_path,
|
|
|
|
[self._text_channel.object_path])
|
2007-03-06 18:22:43 +01:00
|
|
|
|
|
|
|
def leave(self):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Local method called when the user wants to leave the activity.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
(XXX - doesn't appear to be called anywhere!)
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-06 18:22:43 +01:00
|
|
|
if self._joined:
|
2007-04-13 19:11:59 +02:00
|
|
|
self._text_channel[CHANNEL_INTERFACE].Close()
|
2007-03-07 13:15:38 +01:00
|
|
|
|
2007-05-30 18:36:42 +02:00
|
|
|
def _text_channel_members_changed_cb(self, message, added, removed,
|
|
|
|
local_pending, remote_pending,
|
|
|
|
actor, reason):
|
|
|
|
# Note: D-Bus calls this with list arguments, but after GetMembers()
|
|
|
|
# we call it with set and tuple arguments; we cope with any iterable.
|
|
|
|
|
|
|
|
if (self._text_channel_group_flags &
|
|
|
|
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES):
|
|
|
|
map_chan = self._text_channel
|
|
|
|
else:
|
|
|
|
# we have global handles here
|
|
|
|
map_chan = None
|
|
|
|
|
|
|
|
# disregard any who are already there
|
|
|
|
added = set(added)
|
|
|
|
added -= self._member_handles
|
|
|
|
self._member_handles |= added
|
|
|
|
|
|
|
|
# for added people, we need a Buddy object
|
|
|
|
added_buddies = self._ps.map_handles_to_buddies(self._tp,
|
|
|
|
map_chan,
|
|
|
|
added)
|
|
|
|
self._add_buddies(added_buddies.itervalues())
|
|
|
|
|
|
|
|
# we treat all pending members as if they weren't there
|
|
|
|
removed = set(removed)
|
|
|
|
removed |= set(local_pending)
|
|
|
|
removed |= set(remote_pending)
|
|
|
|
# disregard any who aren't already there
|
|
|
|
removed &= self._member_handles
|
|
|
|
self._member_handles -= removed
|
|
|
|
|
|
|
|
# for removed people, don't bother creating a Buddy just so we can
|
|
|
|
# say it left. If we don't already have a Buddy object for someone,
|
|
|
|
# then obviously they're not in self._buddies!
|
|
|
|
removed_buddies = self._ps.map_handles_to_buddies(self._tp,
|
|
|
|
map_chan,
|
|
|
|
removed,
|
|
|
|
create=False)
|
|
|
|
self._remove_buddies(removed_buddies.itervalues())
|
|
|
|
|
|
|
|
# if we were among those removed, we'll have to start believing
|
|
|
|
# the spoofable PEP-based activity tracking again.
|
|
|
|
if self._self_handle not in self._member_handles:
|
|
|
|
self._joined = False
|
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
def _text_channel_closed_cb(self):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Callback method called when the text channel is closed.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
This callback is set up in the _handle_share_join method.
|
|
|
|
"""
|
2007-03-07 13:15:38 +01:00
|
|
|
self._joined = False
|
2007-05-30 18:36:42 +02:00
|
|
|
self._self_handle = None
|
2007-04-13 19:11:59 +02:00
|
|
|
self._text_channel = None
|
2007-03-14 15:59:30 +01:00
|
|
|
|
|
|
|
def send_properties(self):
|
2007-04-21 01:07:41 +02:00
|
|
|
"""Tells the Telepathy server what the properties of this activity are.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-14 15:59:30 +01:00
|
|
|
props = {}
|
2007-05-14 03:11:19 +02:00
|
|
|
props['name'] = self._actname
|
2007-03-14 15:59:30 +01:00
|
|
|
props['color'] = self._color
|
|
|
|
props['type'] = self._type
|
2007-04-30 03:44:57 +02:00
|
|
|
|
|
|
|
# Add custom properties
|
|
|
|
for (key, value) in self._custom_props.items():
|
|
|
|
props[key] = value
|
|
|
|
|
2007-03-14 15:59:30 +01:00
|
|
|
self._tp.set_activity_properties(self.props.id, props)
|
|
|
|
|
|
|
|
def set_properties(self, properties):
|
2007-05-25 11:56:14 +02:00
|
|
|
"""Sets name, colour and/or type properties for this activity all
|
|
|
|
at once.
|
|
|
|
|
|
|
|
properties - Dictionary object containing properties keyed by
|
|
|
|
property names
|
|
|
|
|
|
|
|
Note that if any of the name, colour and/or type property values is
|
|
|
|
changed from what it originally was, the update_validity method will
|
|
|
|
be called, resulting in a "validity-changed" signal being generated.
|
|
|
|
Called by the PresenceService on the local machine.
|
2007-04-21 01:07:41 +02:00
|
|
|
"""
|
2007-03-14 15:59:30 +01:00
|
|
|
changed = False
|
2007-04-30 03:44:57 +02:00
|
|
|
# split reserved properties from activity-custom properties
|
2007-04-30 15:51:35 +02:00
|
|
|
(rprops, cprops) = self._split_properties(properties)
|
2007-04-30 03:44:57 +02:00
|
|
|
if _PROP_NAME in rprops.keys():
|
|
|
|
name = rprops[_PROP_NAME]
|
2007-05-14 03:11:19 +02:00
|
|
|
if name != self._actname:
|
|
|
|
self._actname = name
|
2007-03-14 15:59:30 +01:00
|
|
|
changed = True
|
|
|
|
|
2007-04-30 03:44:57 +02:00
|
|
|
if _PROP_COLOR in rprops.keys():
|
|
|
|
color = rprops[_PROP_COLOR]
|
2007-03-14 15:59:30 +01:00
|
|
|
if color != self._color:
|
|
|
|
self._color = color
|
|
|
|
changed = True
|
|
|
|
|
2007-04-30 03:44:57 +02:00
|
|
|
if _PROP_TYPE in rprops.keys():
|
|
|
|
type = rprops[_PROP_TYPE]
|
2007-05-14 03:31:16 +02:00
|
|
|
if type != self._type:
|
|
|
|
# Type can never be changed after first set
|
|
|
|
if self._type:
|
2007-05-25 11:56:14 +02:00
|
|
|
_logger.debug("Activity type changed by network; this "
|
|
|
|
"is illegal")
|
2007-05-14 03:31:16 +02:00
|
|
|
else:
|
2007-04-30 03:44:57 +02:00
|
|
|
self._type = type
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# Set custom properties
|
|
|
|
if len(cprops.keys()) > 0:
|
|
|
|
self.props.custom_props = cprops
|
2007-03-14 15:59:30 +01:00
|
|
|
|
|
|
|
if changed:
|
|
|
|
self._update_validity()
|
2007-04-30 03:44:57 +02:00
|
|
|
|
|
|
|
def _split_properties(self, properties):
|
|
|
|
"""Extracts reserved properties.
|
2007-05-25 11:56:14 +02:00
|
|
|
|
|
|
|
properties - Dictionary object containing properties keyed by
|
|
|
|
property names
|
|
|
|
|
|
|
|
returns a tuple of 2 dictionaries, reserved properties and custom
|
|
|
|
properties
|
2007-04-30 03:44:57 +02:00
|
|
|
"""
|
|
|
|
rprops = {}
|
|
|
|
cprops = {}
|
2007-04-30 15:51:35 +02:00
|
|
|
for (key, value) in properties.items():
|
2007-04-30 03:44:57 +02:00
|
|
|
if key in self._RESERVED_PROPNAMES:
|
|
|
|
rprops[key] = value
|
|
|
|
else:
|
|
|
|
cprops[key] = value
|
|
|
|
return (rprops, cprops)
|