2007-04-09 20:40:56 +02:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
2010-08-12 16:20:14 +02:00
|
|
|
# Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
|
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.
|
|
|
|
|
2008-10-28 14:19:01 +01:00
|
|
|
"""UI interface to an activity in the presence service
|
|
|
|
|
|
|
|
STABLE.
|
|
|
|
"""
|
|
|
|
|
2007-08-30 13:14:35 +02:00
|
|
|
import logging
|
2010-06-28 16:42:23 +02:00
|
|
|
from functools import partial
|
2007-08-30 13:14:35 +02:00
|
|
|
|
2006-07-25 22:52:45 +02:00
|
|
|
import dbus
|
2010-08-10 14:21:42 +02:00
|
|
|
from dbus import PROPERTIES_IFACE
|
2007-10-29 18:21:33 +01:00
|
|
|
import gobject
|
2010-06-22 16:31:21 +02:00
|
|
|
from telepathy.client import Channel
|
|
|
|
from telepathy.interfaces import CHANNEL, \
|
2010-06-30 18:01:58 +02:00
|
|
|
CHANNEL_INTERFACE_GROUP, \
|
2010-06-22 16:31:21 +02:00
|
|
|
CHANNEL_TYPE_TUBES, \
|
|
|
|
CHANNEL_TYPE_TEXT, \
|
2010-06-30 18:01:58 +02:00
|
|
|
CONNECTION, \
|
|
|
|
PROPERTIES_INTERFACE
|
|
|
|
from telepathy.constants import CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES, \
|
|
|
|
HANDLE_TYPE_ROOM, \
|
2010-07-12 20:33:19 +02:00
|
|
|
HANDLE_TYPE_CONTACT, \
|
2010-06-30 18:01:58 +02:00
|
|
|
PROPERTY_FLAG_WRITE
|
2006-07-19 21:27:37 +02:00
|
|
|
|
2010-07-02 11:16:40 +02:00
|
|
|
from sugar.presence.buddy import Buddy
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties'
|
2010-06-28 16:42:23 +02:00
|
|
|
CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo'
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-08-30 13:14:35 +02:00
|
|
|
_logger = logging.getLogger('sugar.presence.activity')
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
class Activity(gobject.GObject):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""UI interface for an Activity in the presence service
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-10-15 11:18:34 +02:00
|
|
|
Activities in the presence service represent your and other user's
|
|
|
|
shared activities.
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
Properties:
|
2009-08-25 19:55:48 +02:00
|
|
|
id
|
|
|
|
color
|
|
|
|
name
|
|
|
|
type
|
|
|
|
joined
|
2007-04-15 06:27:48 +02:00
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'buddy-joined': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2009-08-25 21:12:40 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'buddy-left': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'new-channel': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'joined': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
|
2006-12-04 20:12:24 +01:00
|
|
|
}
|
|
|
|
|
2007-04-24 17:49:43 +02:00
|
|
|
__gproperties__ = {
|
2009-08-25 21:12:40 +02:00
|
|
|
'id': (str, None, None, None, gobject.PARAM_READABLE),
|
|
|
|
'name': (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
'tags': (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
'color': (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
'type': (str, None, None, None, gobject.PARAM_READABLE),
|
|
|
|
'private': (bool, None, None, True, gobject.PARAM_READWRITE),
|
|
|
|
'joined': (bool, None, None, False, gobject.PARAM_READABLE),
|
2007-04-24 17:49:43 +02:00
|
|
|
}
|
|
|
|
|
2010-08-12 16:03:40 +02:00
|
|
|
def __init__(self, account_path, connection, room_handle=None,
|
|
|
|
properties=None):
|
2010-06-28 16:42:23 +02:00
|
|
|
if room_handle is None and properties is None:
|
|
|
|
raise ValueError('Need to pass one of room_handle or properties')
|
|
|
|
|
|
|
|
if properties is None:
|
|
|
|
properties = {}
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.GObject.__init__(self)
|
2007-04-09 20:40:56 +02:00
|
|
|
|
2010-07-12 20:33:19 +02:00
|
|
|
self._account_path = account_path
|
2010-06-22 16:31:21 +02:00
|
|
|
self.telepathy_conn = connection
|
|
|
|
self.telepathy_text_chan = None
|
|
|
|
self.telepathy_tubes_chan = None
|
|
|
|
|
2010-07-15 10:50:05 +02:00
|
|
|
self.room_handle = room_handle
|
2010-06-30 18:01:58 +02:00
|
|
|
self._join_command = None
|
2010-08-12 16:20:14 +02:00
|
|
|
self._share_command = None
|
2010-06-28 16:42:23 +02:00
|
|
|
self._id = properties.get('id', None)
|
|
|
|
self._color = properties.get('color', None)
|
|
|
|
self._name = properties.get('name', None)
|
|
|
|
self._type = properties.get('type', None)
|
|
|
|
self._tags = properties.get('tags', None)
|
|
|
|
self._private = properties.get('private', True)
|
|
|
|
self._joined = properties.get('joined', False)
|
2010-08-10 14:21:42 +02:00
|
|
|
self._channel_self_handle = None
|
|
|
|
self._text_channel_group_flags = 0
|
2010-07-16 15:43:34 +02:00
|
|
|
self._buddies = {}
|
2007-10-16 18:51:36 +02:00
|
|
|
|
2010-06-28 16:42:23 +02:00
|
|
|
self._get_properties_call = None
|
2010-07-15 10:50:05 +02:00
|
|
|
if not self.room_handle is None:
|
2010-06-28 16:42:23 +02:00
|
|
|
self._start_tracking_properties()
|
|
|
|
|
|
|
|
def _start_tracking_properties(self):
|
2010-06-22 16:31:21 +02:00
|
|
|
bus = dbus.SessionBus()
|
|
|
|
self._get_properties_call = bus.call_async(
|
2010-06-28 16:42:23 +02:00
|
|
|
self.telepathy_conn.requested_bus_name,
|
|
|
|
self.telepathy_conn.object_path,
|
2010-06-22 16:31:21 +02:00
|
|
|
CONN_INTERFACE_ACTIVITY_PROPERTIES,
|
|
|
|
'GetProperties',
|
|
|
|
'u',
|
2010-07-15 10:50:05 +02:00
|
|
|
(self.room_handle,),
|
2010-07-12 20:33:19 +02:00
|
|
|
reply_handler=self.__got_properties_cb,
|
|
|
|
error_handler=self.__error_handler_cb,
|
2010-06-22 16:31:21 +02:00
|
|
|
utf8_strings=True)
|
|
|
|
|
2010-06-28 16:42:23 +02:00
|
|
|
# As only one Activity instance is needed per activity process,
|
|
|
|
# we can afford listening to ActivityPropertiesChanged like this.
|
|
|
|
self.telepathy_conn.connect_to_signal(
|
|
|
|
'ActivityPropertiesChanged',
|
|
|
|
self.__activity_properties_changed_cb,
|
|
|
|
dbus_interface=CONN_INTERFACE_ACTIVITY_PROPERTIES)
|
|
|
|
|
|
|
|
def __activity_properties_changed_cb(self, room_handle, properties):
|
|
|
|
_logger.debug('%r: Activity properties changed to %r', self, properties)
|
|
|
|
self._update_properties(properties)
|
|
|
|
|
2010-07-12 20:33:19 +02:00
|
|
|
def __got_properties_cb(self, properties):
|
|
|
|
_logger.debug('__got_properties_cb %r', properties)
|
2007-08-30 13:14:35 +02:00
|
|
|
self._get_properties_call = None
|
2010-06-22 16:31:21 +02:00
|
|
|
self._update_properties(properties)
|
2007-08-30 13:14:35 +02:00
|
|
|
|
2010-07-12 20:33:19 +02:00
|
|
|
def __error_handler_cb(self, error):
|
|
|
|
_logger.debug('__error_handler_cb %r', error)
|
2010-06-22 16:31:21 +02:00
|
|
|
|
|
|
|
def _update_properties(self, new_props):
|
2007-08-30 13:14:35 +02:00
|
|
|
val = new_props.get('name', self._name)
|
|
|
|
if isinstance(val, str) and val != self._name:
|
|
|
|
self._name = val
|
|
|
|
self.notify('name')
|
|
|
|
val = new_props.get('tags', self._tags)
|
|
|
|
if isinstance(val, str) and val != self._tags:
|
|
|
|
self._tags = val
|
|
|
|
self.notify('tags')
|
|
|
|
val = new_props.get('color', self._color)
|
|
|
|
if isinstance(val, str) and val != self._color:
|
|
|
|
self._color = val
|
|
|
|
self.notify('color')
|
|
|
|
val = bool(new_props.get('private', self._private))
|
|
|
|
if val != self._private:
|
|
|
|
self._private = val
|
|
|
|
self.notify('private')
|
|
|
|
val = new_props.get('id', self._id)
|
|
|
|
if isinstance(val, str) and self._id is None:
|
|
|
|
self._id = val
|
|
|
|
self.notify('id')
|
|
|
|
val = new_props.get('type', self._type)
|
|
|
|
if isinstance(val, str) and self._type is None:
|
|
|
|
self._type = val
|
|
|
|
self.notify('type')
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def object_path(self):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Get our dbus object path"""
|
2006-12-04 20:12:24 +01:00
|
|
|
return self._object_path
|
|
|
|
|
2007-04-24 17:49:43 +02:00
|
|
|
def do_get_property(self, pspec):
|
|
|
|
"""Retrieve a particular property from our property dictionary"""
|
2007-08-30 13:14:35 +02:00
|
|
|
|
|
|
|
if pspec.name == "joined":
|
|
|
|
return self._joined
|
|
|
|
|
|
|
|
if self._get_properties_call is not None:
|
2007-10-16 18:51:36 +02:00
|
|
|
_logger.debug('%r: Blocking on GetProperties() because someone '
|
|
|
|
'wants property %s', self, pspec.name)
|
2007-08-30 13:14:35 +02:00
|
|
|
self._get_properties_call.block()
|
|
|
|
|
2007-04-24 17:49:43 +02:00
|
|
|
if pspec.name == "id":
|
|
|
|
return self._id
|
|
|
|
elif pspec.name == "name":
|
|
|
|
return self._name
|
|
|
|
elif pspec.name == "color":
|
|
|
|
return self._color
|
|
|
|
elif pspec.name == "type":
|
|
|
|
return self._type
|
2007-08-30 13:14:35 +02:00
|
|
|
elif pspec.name == "tags":
|
|
|
|
return self._tags
|
|
|
|
elif pspec.name == "private":
|
|
|
|
return self._private
|
|
|
|
|
|
|
|
def do_set_property(self, pspec, val):
|
|
|
|
"""Set a particular property in our property dictionary"""
|
2009-08-25 21:12:40 +02:00
|
|
|
# FIXME: need an asynchronous API to set these properties,
|
|
|
|
# particularly 'private'
|
2010-06-28 16:42:23 +02:00
|
|
|
|
2007-08-30 13:14:35 +02:00
|
|
|
if pspec.name == "name":
|
|
|
|
self._name = val
|
|
|
|
elif pspec.name == "color":
|
|
|
|
self._color = val
|
|
|
|
elif pspec.name == "tags":
|
|
|
|
self._tags = val
|
|
|
|
elif pspec.name == "private":
|
|
|
|
self._private = val
|
2010-06-28 16:42:23 +02:00
|
|
|
else:
|
|
|
|
raise ValueError('Unknown property "%s"', pspec.name)
|
|
|
|
|
|
|
|
self._publish_properties()
|
2007-04-24 17:49:43 +02:00
|
|
|
|
2007-10-29 18:21:33 +01:00
|
|
|
def set_private(self, val, reply_handler, error_handler):
|
2010-06-28 16:42:23 +02:00
|
|
|
_logger.debug('set_private %r', val)
|
2007-10-29 18:21:33 +01:00
|
|
|
self._activity.SetProperties({'private': bool(val)},
|
|
|
|
reply_handler=reply_handler,
|
|
|
|
error_handler=error_handler)
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def get_joined_buddies(self):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Retrieve the set of Buddy objects attached to this activity
|
2007-10-16 18:51:36 +02:00
|
|
|
|
2008-07-10 15:20:50 +02:00
|
|
|
returns list of presence Buddy objects that we can successfully
|
|
|
|
create from the buddy object paths that PS has for this activity.
|
2007-04-15 06:27:48 +02:00
|
|
|
"""
|
2010-07-16 15:43:34 +02:00
|
|
|
return self._buddies.values()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-10-03 13:14:47 +02:00
|
|
|
def get_buddy_by_handle(self, handle):
|
2007-10-03 21:39:07 +02:00
|
|
|
"""Retrieve the Buddy object given a telepathy handle.
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-10-12 17:33:52 +02:00
|
|
|
buddy object paths are cached in self._handle_to_buddy_path,
|
|
|
|
so we can get the buddy without calling PS.
|
2007-10-03 21:39:07 +02:00
|
|
|
"""
|
2007-10-12 17:33:52 +02:00
|
|
|
object_path = self._handle_to_buddy_path.get(handle, None)
|
2007-10-12 17:43:11 +02:00
|
|
|
if object_path:
|
|
|
|
buddy = self._ps_new_object(object_path)
|
|
|
|
return buddy
|
|
|
|
return None
|
2007-10-03 13:14:47 +02:00
|
|
|
|
2007-08-30 13:14:35 +02:00
|
|
|
def invite(self, buddy, message, response_cb):
|
|
|
|
"""Invite the given buddy to join this activity.
|
|
|
|
|
|
|
|
The callback will be called with one parameter: None on success,
|
|
|
|
or an exception on failure.
|
|
|
|
"""
|
2010-07-08 17:20:51 +02:00
|
|
|
if not self._joined:
|
|
|
|
raise RuntimeError('Cannot invite a buddy to an activity that is'
|
|
|
|
'not shared.')
|
|
|
|
self.telepathy_text_chan.AddMembers([buddy.contact_handle], message,
|
|
|
|
dbus_interface=CHANNEL_INTERFACE_GROUP,
|
|
|
|
reply_handler=partial(self.__invite_cb, response_cb),
|
|
|
|
error_handler=partial(self.__invite_cb, response_cb))
|
|
|
|
|
|
|
|
def __invite_cb(self, response_cb, error=None):
|
|
|
|
response_cb(error)
|
2007-08-30 13:14:35 +02:00
|
|
|
|
2007-10-29 18:21:33 +01:00
|
|
|
def set_up_tubes(self, reply_handler, error_handler):
|
2010-07-08 17:20:51 +02:00
|
|
|
raise NotImplementedError()
|
2007-10-29 18:21:33 +01:00
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
def __joined_cb(self, join_command, error):
|
|
|
|
_logger.debug('%r: Join finished %r', self, error)
|
2010-07-16 15:43:34 +02:00
|
|
|
if error is not None:
|
|
|
|
self.emit('joined', error is None, str(error))
|
|
|
|
self.telepathy_text_chan = join_command.text_channel
|
|
|
|
self.telepathy_tubes_chan = join_command.tubes_channel
|
2010-08-10 14:21:42 +02:00
|
|
|
self._channel_self_handle = join_command.channel_self_handle
|
|
|
|
self._text_channel_group_flags = join_command.text_channel_group_flags
|
2010-07-16 15:43:34 +02:00
|
|
|
self._start_tracking_buddies()
|
|
|
|
self._start_tracking_channel()
|
2007-05-03 05:25:15 +02:00
|
|
|
|
2010-07-02 11:16:40 +02:00
|
|
|
def _start_tracking_buddies(self):
|
|
|
|
group = self.telepathy_text_chan[CHANNEL_INTERFACE_GROUP]
|
2010-07-16 15:43:34 +02:00
|
|
|
|
|
|
|
group.GetAllMembers(reply_handler=self.__get_all_members_cb,
|
|
|
|
error_handler=self.__error_handler_cb)
|
|
|
|
|
2010-07-02 11:16:40 +02:00
|
|
|
group.connect_to_signal('MembersChanged',
|
|
|
|
self.__text_channel_members_changed_cb)
|
|
|
|
|
2010-07-02 11:35:10 +02:00
|
|
|
def _start_tracking_channel(self):
|
|
|
|
channel = self.telepathy_text_chan[CHANNEL]
|
|
|
|
channel.connect_to_signal('Closed', self.__text_channel_closed_cb)
|
|
|
|
|
2010-07-16 15:43:34 +02:00
|
|
|
def __get_all_members_cb(self, members, local_pending, remote_pending):
|
2010-08-12 16:03:40 +02:00
|
|
|
_logger.debug('__get_all_members_cb %r %r', members,
|
|
|
|
self._text_channel_group_flags)
|
2010-08-10 14:21:42 +02:00
|
|
|
if self._channel_self_handle in members:
|
|
|
|
members.remove(self._channel_self_handle)
|
|
|
|
|
|
|
|
if not members:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._resolve_handles(members, reply_cb=self._add_initial_buddies)
|
|
|
|
|
|
|
|
def _resolve_handles(self, input_handles, reply_cb):
|
|
|
|
def get_handle_owners_cb(handles):
|
|
|
|
self.telepathy_conn.InspectHandles(HANDLE_TYPE_CONTACT, handles,
|
|
|
|
reply_handler=reply_cb,
|
2010-07-16 15:43:34 +02:00
|
|
|
error_handler=self.__error_handler_cb,
|
|
|
|
dbus_interface=CONNECTION)
|
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
if self._text_channel_group_flags & \
|
|
|
|
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES:
|
|
|
|
|
|
|
|
group = self.telepathy_text_chan[CHANNEL_INTERFACE_GROUP]
|
|
|
|
group.GetHandleOwners(input_handles,
|
|
|
|
reply_handler=get_handle_owners_cb,
|
|
|
|
error_handler=self.__error_handler_cb)
|
|
|
|
else:
|
|
|
|
get_handle_owners_cb(input_handles)
|
|
|
|
|
|
|
|
def _add_initial_buddies(self, contact_ids):
|
|
|
|
_logger.debug('__add_initial_buddies %r', contact_ids)
|
2010-07-16 15:43:34 +02:00
|
|
|
for contact_id in contact_ids:
|
|
|
|
self._buddies[contact_id] = self._get_buddy(contact_id)
|
|
|
|
# Once we have the initial members, we can finish the join process
|
|
|
|
self._joined = True
|
|
|
|
self.emit('joined', True, None)
|
|
|
|
|
2010-07-02 11:16:40 +02:00
|
|
|
def __text_channel_members_changed_cb(self, message, added, removed,
|
|
|
|
local_pending, remote_pending,
|
|
|
|
actor, reason):
|
2010-07-09 17:31:11 +02:00
|
|
|
_logger.debug('__text_channel_members_changed_cb %r',
|
2010-07-02 11:16:40 +02:00
|
|
|
[added, message, added, removed, local_pending,
|
|
|
|
remote_pending, actor, reason])
|
2010-08-10 14:21:42 +02:00
|
|
|
if self._channel_self_handle in added:
|
|
|
|
added.remove(self._channel_self_handle)
|
2010-07-12 20:33:19 +02:00
|
|
|
if added:
|
2010-08-10 14:21:42 +02:00
|
|
|
self._resolve_handles(added, reply_cb=self._add_buddies)
|
2010-07-12 20:33:19 +02:00
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
if self._channel_self_handle in removed:
|
|
|
|
removed.remove(self._channel_self_handle)
|
2010-07-12 20:33:19 +02:00
|
|
|
if removed:
|
2010-08-10 14:21:42 +02:00
|
|
|
self._resolve_handles(added, reply_cb=self._remove_buddies)
|
2010-07-12 20:33:19 +02:00
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
def _add_buddies(self, contact_ids):
|
2010-07-12 20:33:19 +02:00
|
|
|
for contact_id in contact_ids:
|
2010-07-16 15:43:34 +02:00
|
|
|
if contact_id not in self._buddies:
|
|
|
|
buddy = self._get_buddy(contact_id)
|
|
|
|
self.emit('buddy-joined', buddy)
|
|
|
|
self._buddies[contact_id] = buddy
|
2010-07-12 20:33:19 +02:00
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
def _remove_buddies(self, contact_ids):
|
2010-07-12 20:33:19 +02:00
|
|
|
for contact_id in contact_ids:
|
2010-07-16 15:43:34 +02:00
|
|
|
if contact_id in self._buddies:
|
|
|
|
buddy = self._get_buddy(contact_id)
|
|
|
|
self.emit('buddy-left', buddy)
|
|
|
|
del self._buddies[contact_id]
|
|
|
|
|
|
|
|
def _get_buddy(self, contact_id):
|
|
|
|
if contact_id in self._buddies:
|
|
|
|
return self._buddies[contact_id]
|
|
|
|
else:
|
|
|
|
return Buddy(self._account_path, contact_id)
|
2010-07-02 11:39:12 +02:00
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
def join(self):
|
2007-10-29 18:21:33 +01:00
|
|
|
"""Join this activity.
|
|
|
|
|
|
|
|
Emits 'joined' and otherwise does nothing if we're already joined.
|
2007-04-15 06:27:48 +02:00
|
|
|
"""
|
2010-06-30 18:01:58 +02:00
|
|
|
if self._join_command is not None:
|
|
|
|
return
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
if self._joined:
|
2010-06-30 18:01:58 +02:00
|
|
|
self.emit('joined', True, None)
|
2007-04-09 20:40:56 +02:00
|
|
|
return
|
2007-10-29 18:21:33 +01:00
|
|
|
|
2007-10-16 18:51:36 +02:00
|
|
|
_logger.debug('%r: joining', self)
|
2007-10-29 18:21:33 +01:00
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
self._join_command = _JoinCommand(self.telepathy_conn,
|
2010-07-15 10:50:05 +02:00
|
|
|
self.room_handle)
|
2010-06-30 18:01:58 +02:00
|
|
|
self._join_command.connect('finished', self.__joined_cb)
|
|
|
|
self._join_command.run()
|
2010-06-28 16:42:23 +02:00
|
|
|
|
|
|
|
def share(self, share_activity_cb, share_activity_error_cb):
|
2010-07-15 10:50:05 +02:00
|
|
|
if not self.room_handle is None:
|
2010-06-28 16:42:23 +02:00
|
|
|
raise ValueError('Already have a room handle')
|
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
self._share_command = _ShareCommand(self.telepathy_conn, self._id)
|
|
|
|
self._share_command.connect('finished',
|
|
|
|
partial(self.__shared_cb,
|
|
|
|
share_activity_cb,
|
|
|
|
share_activity_error_cb))
|
|
|
|
self._share_command.run()
|
|
|
|
|
|
|
|
def __shared_cb(self, share_activity_cb, share_activity_error_cb,
|
|
|
|
share_command, error):
|
|
|
|
_logger.debug('%r: Share finished %r', self, error)
|
|
|
|
if error is None:
|
|
|
|
self._joined = True
|
2010-07-15 10:50:05 +02:00
|
|
|
self.room_handle = share_command.room_handle
|
2010-06-30 18:01:58 +02:00
|
|
|
self.telepathy_text_chan = share_command.text_channel
|
|
|
|
self.telepathy_tubes_chan = share_command.tubes_channel
|
2010-08-10 14:21:42 +02:00
|
|
|
self._channel_self_handle = share_command.channel_self_handle
|
2010-08-12 16:03:40 +02:00
|
|
|
self._text_channel_group_flags = \
|
|
|
|
share_command.text_channel_group_flags
|
2010-06-30 18:01:58 +02:00
|
|
|
self._publish_properties()
|
|
|
|
self._start_tracking_properties()
|
2010-07-02 11:16:40 +02:00
|
|
|
self._start_tracking_buddies()
|
2010-07-02 11:35:10 +02:00
|
|
|
self._start_tracking_channel()
|
2010-06-30 18:01:58 +02:00
|
|
|
share_activity_cb(self)
|
|
|
|
else:
|
|
|
|
share_activity_error_cb(self, error)
|
2010-06-28 16:42:23 +02:00
|
|
|
|
|
|
|
def _publish_properties(self):
|
|
|
|
properties = {}
|
|
|
|
|
|
|
|
if self._color is not None:
|
2010-07-08 17:20:51 +02:00
|
|
|
properties['color'] = str(self._color)
|
2010-06-28 16:42:23 +02:00
|
|
|
if self._name is not None:
|
2010-07-08 17:20:51 +02:00
|
|
|
properties['name'] = str(self._name)
|
2010-06-28 16:42:23 +02:00
|
|
|
if self._type is not None:
|
|
|
|
properties['type'] = self._type
|
|
|
|
if self._tags is not None:
|
|
|
|
properties['tags'] = self._tags
|
|
|
|
properties['private'] = self._private
|
|
|
|
|
|
|
|
self.telepathy_conn.SetProperties(
|
2010-07-15 10:50:05 +02:00
|
|
|
self.room_handle,
|
2010-06-28 16:42:23 +02:00
|
|
|
properties,
|
|
|
|
dbus_interface=CONN_INTERFACE_ACTIVITY_PROPERTIES)
|
|
|
|
|
|
|
|
def __share_error_cb(self, share_activity_error_cb, error):
|
|
|
|
logging.debug('%r: Share failed because: %s', self, error)
|
|
|
|
share_activity_error_cb(self, error)
|
2007-10-29 18:21:33 +01:00
|
|
|
|
|
|
|
# GetChannels() wrapper
|
2007-04-09 20:40:56 +02:00
|
|
|
|
|
|
|
def get_channels(self):
|
2009-08-25 19:55:48 +02:00
|
|
|
"""Retrieve communications channel descriptions for the activity
|
|
|
|
|
2007-10-02 11:56:36 +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-15 06:27:48 +02:00
|
|
|
"""
|
2010-07-02 15:13:42 +02:00
|
|
|
bus_name = self.telepathy_conn.requested_bus_name
|
|
|
|
connection_path = self.telepathy_conn.object_path
|
|
|
|
channels = [self.telepathy_text_chan.object_path,
|
|
|
|
self.telepathy_tubes_chan.object_path]
|
|
|
|
|
2007-10-16 18:51:36 +02:00
|
|
|
_logger.debug('%r: bus name is %s, connection is %s, channels are %r',
|
2010-07-02 15:13:42 +02:00
|
|
|
self, bus_name, connection_path, channels)
|
|
|
|
return bus_name, connection_path, channels
|
2007-04-09 20:40:56 +02:00
|
|
|
|
2007-10-29 18:21:33 +01:00
|
|
|
# Leaving
|
2010-07-02 11:35:10 +02:00
|
|
|
def __text_channel_closed_cb(self):
|
|
|
|
self._joined = False
|
2007-07-03 14:22:30 +02:00
|
|
|
self.emit("joined", False, "left activity")
|
|
|
|
|
2007-05-03 05:25:15 +02:00
|
|
|
def leave(self):
|
2007-07-03 14:22:30 +02:00
|
|
|
"""Leave this shared activity"""
|
2007-10-16 18:51:36 +02:00
|
|
|
_logger.debug('%r: leaving', self)
|
2010-07-02 11:35:10 +02:00
|
|
|
self.telepathy_text_chan.Close()
|
2010-06-30 18:01:58 +02:00
|
|
|
|
2010-10-15 19:53:25 +02:00
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
class _BaseCommand(gobject.GObject):
|
|
|
|
__gsignals__ = {
|
|
|
|
'finished': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([object])),
|
|
|
|
}
|
2010-10-15 19:53:25 +02:00
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2010-07-16 15:43:34 +02:00
|
|
|
self.text_channel = None
|
2010-08-10 14:21:42 +02:00
|
|
|
self.text_channel_group_flags = None
|
2010-07-16 15:43:34 +02:00
|
|
|
self.tubes_channel = None
|
|
|
|
self.room_handle = None
|
2010-08-10 14:21:42 +02:00
|
|
|
self.channel_self_handle = None
|
2010-07-16 15:43:34 +02:00
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
def run(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class _ShareCommand(_BaseCommand):
|
|
|
|
def __init__(self, connection, activity_id):
|
|
|
|
_BaseCommand.__init__(self)
|
|
|
|
|
|
|
|
self._connection = connection
|
|
|
|
self._activity_id = activity_id
|
|
|
|
self._finished = False
|
|
|
|
self._join_command = None
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self._connection.RequestHandles(
|
|
|
|
HANDLE_TYPE_ROOM,
|
|
|
|
[self._activity_id],
|
|
|
|
reply_handler=self.__got_handles_cb,
|
|
|
|
error_handler=self.__error_handler_cb,
|
|
|
|
dbus_interface=CONNECTION)
|
|
|
|
|
|
|
|
def __got_handles_cb(self, handles):
|
|
|
|
logging.debug('__got_handles_cb %r', handles)
|
|
|
|
self.room_handle = handles[0]
|
|
|
|
|
|
|
|
self._join_command = _JoinCommand(self._connection, self.room_handle)
|
|
|
|
self._join_command.connect('finished', self.__joined_cb)
|
|
|
|
self._join_command.run()
|
|
|
|
|
|
|
|
def __joined_cb(self, join_command, error):
|
|
|
|
_logger.debug('%r: Join finished %r', self, error)
|
|
|
|
if error is not None:
|
|
|
|
self._finished = True
|
|
|
|
self.emit('finished', error)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.text_channel = join_command.text_channel
|
2010-08-10 14:21:42 +02:00
|
|
|
self.text_channel_group_flags = join_command.text_channel_group_flags
|
2010-06-30 18:01:58 +02:00
|
|
|
self.tubes_channel = join_command.tubes_channel
|
|
|
|
|
|
|
|
self._connection.AddActivity(
|
|
|
|
self._activity_id,
|
|
|
|
self.room_handle,
|
|
|
|
reply_handler=self.__added_activity_cb,
|
|
|
|
error_handler=self.__error_handler_cb,
|
|
|
|
dbus_interface=CONN_INTERFACE_BUDDY_INFO)
|
|
|
|
|
|
|
|
def __added_activity_cb(self):
|
|
|
|
self._finished = True
|
|
|
|
self.emit('finished', None)
|
|
|
|
|
|
|
|
def __error_handler_cb(self, error):
|
|
|
|
self._finished = True
|
|
|
|
self.emit('finished', error)
|
|
|
|
|
2010-10-15 19:53:25 +02:00
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
class _JoinCommand(_BaseCommand):
|
|
|
|
def __init__(self, connection, room_handle):
|
|
|
|
_BaseCommand.__init__(self)
|
|
|
|
|
|
|
|
self._connection = connection
|
|
|
|
self._finished = False
|
2010-07-16 15:43:34 +02:00
|
|
|
self.room_handle = room_handle
|
2010-08-10 14:21:42 +02:00
|
|
|
self._global_self_handle = None
|
2010-06-30 18:01:58 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self._finished:
|
|
|
|
raise RuntimeError('This command has already finished')
|
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
self._connection.Get(CONNECTION, 'SelfHandle',
|
|
|
|
reply_handler=self.__get_self_handle_cb,
|
|
|
|
error_handler=self.__error_handler_cb,
|
|
|
|
dbus_interface=PROPERTIES_IFACE)
|
|
|
|
|
|
|
|
def __get_self_handle_cb(self, handle):
|
|
|
|
self._global_self_handle = handle
|
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
self._connection.RequestChannel(CHANNEL_TYPE_TEXT,
|
2010-07-15 10:50:05 +02:00
|
|
|
HANDLE_TYPE_ROOM, self.room_handle, True,
|
2010-06-30 18:01:58 +02:00
|
|
|
reply_handler=self.__create_text_channel_cb,
|
|
|
|
error_handler=self.__error_handler_cb,
|
|
|
|
dbus_interface=CONNECTION)
|
|
|
|
|
|
|
|
self._connection.RequestChannel(CHANNEL_TYPE_TUBES,
|
2010-07-15 10:50:05 +02:00
|
|
|
HANDLE_TYPE_ROOM, self.room_handle, True,
|
2010-06-30 18:01:58 +02:00
|
|
|
reply_handler=self.__create_tubes_channel_cb,
|
|
|
|
error_handler=self.__error_handler_cb,
|
|
|
|
dbus_interface=CONNECTION)
|
|
|
|
|
|
|
|
def __create_text_channel_cb(self, channel_path):
|
|
|
|
Channel(self._connection.requested_bus_name, channel_path,
|
|
|
|
ready_handler=self.__text_channel_ready_cb)
|
|
|
|
|
|
|
|
def __create_tubes_channel_cb(self, channel_path):
|
|
|
|
Channel(self._connection.requested_bus_name, channel_path,
|
|
|
|
ready_handler=self.__tubes_channel_ready_cb)
|
|
|
|
|
|
|
|
def __error_handler_cb(self, error):
|
|
|
|
self._finished = True
|
|
|
|
self.emit('finished', error)
|
|
|
|
|
|
|
|
def __tubes_channel_ready_cb(self, channel):
|
|
|
|
_logger.debug('%r: Tubes channel %r is ready', self, channel)
|
|
|
|
self.tubes_channel = channel
|
|
|
|
self._tubes_ready()
|
|
|
|
|
|
|
|
def __text_channel_ready_cb(self, channel):
|
|
|
|
_logger.debug('%r: Text channel %r is ready', self, channel)
|
|
|
|
self.text_channel = channel
|
|
|
|
self._tubes_ready()
|
|
|
|
|
|
|
|
def _tubes_ready(self):
|
|
|
|
if self.text_channel is None or \
|
|
|
|
self.tubes_channel is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
_logger.debug('%r: finished setting up tubes', self)
|
|
|
|
|
|
|
|
self._add_self_to_channel()
|
|
|
|
|
|
|
|
def __text_channel_group_flags_changed_cb(self, added, removed):
|
2010-08-12 16:03:40 +02:00
|
|
|
_logger.debug('__text_channel_group_flags_changed_cb %r %r', added,
|
|
|
|
removed)
|
2010-08-10 14:21:42 +02:00
|
|
|
self.text_channel_group_flags |= added
|
|
|
|
self.text_channel_group_flags &= ~removed
|
2010-06-30 18:01:58 +02:00
|
|
|
|
|
|
|
def _add_self_to_channel(self):
|
|
|
|
# FIXME: cope with non-Group channels here if we want to support
|
|
|
|
# non-OLPC-compatible IMs
|
|
|
|
|
|
|
|
group = self.text_channel[CHANNEL_INTERFACE_GROUP]
|
|
|
|
|
|
|
|
def got_all_members(members, local_pending, remote_pending):
|
2010-08-12 16:03:40 +02:00
|
|
|
_logger.debug('got_all_members members %r local_pending %r '
|
|
|
|
'remote_pending %r', members, local_pending,
|
|
|
|
remote_pending)
|
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
if self.text_channel_group_flags & \
|
|
|
|
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES:
|
|
|
|
self_handle = self.channel_self_handle
|
|
|
|
else:
|
|
|
|
self_handle = self._global_self_handle
|
2010-06-30 18:01:58 +02:00
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
if self_handle in local_pending:
|
2010-07-09 17:31:11 +02:00
|
|
|
_logger.debug('%r: We are in local pending - entering', self)
|
2010-08-10 14:21:42 +02:00
|
|
|
group.AddMembers([self_handle], '',
|
2010-06-30 18:01:58 +02:00
|
|
|
reply_handler=lambda: None,
|
|
|
|
error_handler=lambda e: self._join_failed_cb(e,
|
|
|
|
'got_all_members AddMembers'))
|
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
if members:
|
|
|
|
self.__text_channel_members_changed_cb('', members, (),
|
|
|
|
(), (), 0, 0)
|
|
|
|
|
2010-06-30 18:01:58 +02:00
|
|
|
def got_group_flags(flags):
|
2010-08-10 14:21:42 +02:00
|
|
|
self.text_channel_group_flags = flags
|
2010-06-30 18:01:58 +02:00
|
|
|
# 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
|
|
|
|
group.GetAllMembers(reply_handler=got_all_members,
|
|
|
|
error_handler=self.__error_handler_cb)
|
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
def got_self_handle(channel_self_handle):
|
|
|
|
self.channel_self_handle = channel_self_handle
|
2010-06-30 18:01:58 +02:00
|
|
|
group.connect_to_signal('GroupFlagsChanged',
|
|
|
|
self.__text_channel_group_flags_changed_cb)
|
|
|
|
group.GetGroupFlags(reply_handler=got_group_flags,
|
|
|
|
error_handler=self.__error_handler_cb)
|
|
|
|
|
|
|
|
group.GetSelfHandle(reply_handler=got_self_handle,
|
|
|
|
error_handler=self.__error_handler_cb)
|
|
|
|
|
|
|
|
def __text_channel_members_changed_cb(self, message, added, removed,
|
|
|
|
local_pending, remote_pending,
|
|
|
|
actor, reason):
|
2010-08-12 16:03:40 +02:00
|
|
|
_logger.debug('__text_channel_members_changed_cb added %r removed %r '
|
|
|
|
'local_pending %r remote_pending %r channel_self_handle '
|
|
|
|
'%r', added, removed, local_pending, remote_pending,
|
|
|
|
self.channel_self_handle)
|
|
|
|
|
2010-08-10 14:21:42 +02:00
|
|
|
if self.text_channel_group_flags & \
|
|
|
|
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES:
|
|
|
|
self_handle = self.channel_self_handle
|
|
|
|
else:
|
|
|
|
self_handle = self._global_self_handle
|
|
|
|
|
|
|
|
if self_handle in added:
|
2010-06-30 18:01:58 +02:00
|
|
|
if PROPERTIES_INTERFACE not in self.text_channel:
|
2010-08-11 12:59:18 +02:00
|
|
|
self._finished = True
|
|
|
|
self.emit('finished', None)
|
2010-06-30 18:01:58 +02:00
|
|
|
else:
|
|
|
|
self.text_channel[PROPERTIES_INTERFACE].ListProperties(
|
2010-08-11 12:59:18 +02:00
|
|
|
reply_handler=self.__list_properties_cb,
|
|
|
|
error_handler=self.__error_handler_cb)
|
2010-06-30 18:01:58 +02:00
|
|
|
|
2010-08-11 12:59:18 +02:00
|
|
|
def __list_properties_cb(self, prop_specs):
|
2010-06-30 18:01:58 +02:00
|
|
|
# FIXME: invite-only ought to be set on private activities; but
|
|
|
|
# since only the owner can change invite-only, that would break
|
|
|
|
# activity scope changes.
|
|
|
|
props = {
|
|
|
|
'anonymous': False, # otherwise buddy resolution breaks
|
|
|
|
'invite-only': False, # anyone who knows about the channel can join
|
|
|
|
'invite-restricted': False, # so non-owners can invite others
|
|
|
|
'persistent': False, # vanish when there are no members
|
|
|
|
'private': True, # don't appear in server room lists
|
|
|
|
}
|
|
|
|
props_to_set = []
|
2010-08-12 16:20:14 +02:00
|
|
|
for ident, name, sig_, flags in prop_specs:
|
2010-06-30 18:01:58 +02:00
|
|
|
value = props.pop(name, None)
|
|
|
|
if value is not None:
|
|
|
|
if flags & PROPERTY_FLAG_WRITE:
|
|
|
|
props_to_set.append((ident, value))
|
|
|
|
# FIXME: else error, but only if we're creating the room?
|
|
|
|
# FIXME: if props is nonempty, then we want to set props that aren't
|
|
|
|
# supported here - raise an error?
|
|
|
|
|
|
|
|
if props_to_set:
|
|
|
|
self.text_channel[PROPERTIES_INTERFACE].SetProperties(
|
2010-08-11 12:59:18 +02:00
|
|
|
props_to_set, reply_handler=self.__set_properties_cb,
|
|
|
|
error_handler=self.__error_handler_cb)
|
2010-06-30 18:01:58 +02:00
|
|
|
else:
|
2010-08-11 12:59:18 +02:00
|
|
|
self._finished = True
|
|
|
|
self.emit('finished', None)
|
2010-06-30 18:01:58 +02:00
|
|
|
|
2010-08-11 12:59:18 +02:00
|
|
|
def __set_properties_cb(self):
|
|
|
|
self._finished = True
|
|
|
|
self.emit('finished', None)
|