Handle invitations using Mission Control 5.
src/sugar/activity/activity.py: If the activity is being invoked to handle an invite, create a Client.Handler instance and share the activity when HandleChannels is invoked. src/sugar/activity/activityfactory.py, src/sugar/activity/activityhandle.py, src/sugar/activity/main.py: Add a -i switch that indicates to the activity that it should handle the channel from an invitation. src/sugar/presence/activity.py: Expose Activity.room_handle. src/sugar/presence/presenceservice.py: Add get_activity_by_handle(). src/sugar/presence/util.py: Add get_account_for_connection().
This commit is contained in:
+112
-26
@@ -52,15 +52,25 @@ import logging
|
||||
import os
|
||||
import time
|
||||
from hashlib import sha1
|
||||
import gconf
|
||||
from functools import partial
|
||||
|
||||
import gconf
|
||||
import gtk
|
||||
import gobject
|
||||
import dbus
|
||||
import dbus.service
|
||||
from dbus import PROPERTIES_IFACE
|
||||
import cjson
|
||||
from telepathy.server import DBusProperties
|
||||
from telepathy.interfaces import CONNECTION, \
|
||||
CHANNEL, \
|
||||
CHANNEL_TYPE_TEXT, \
|
||||
CLIENT, \
|
||||
CLIENT_HANDLER
|
||||
from telepathy.constants import CONNECTION_HANDLE_TYPE_CONTACT
|
||||
|
||||
from sugar import util
|
||||
from sugar import dispatch
|
||||
from sugar.presence import presenceservice
|
||||
from sugar.activity.activityservice import ActivityService
|
||||
from sugar.activity.namingalert import NamingAlert
|
||||
@@ -88,6 +98,7 @@ J_DBUS_SERVICE = 'org.laptop.Journal'
|
||||
J_DBUS_PATH = '/org/laptop/Journal'
|
||||
J_DBUS_INTERFACE = 'org.laptop.Journal'
|
||||
|
||||
CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties'
|
||||
|
||||
class _ActivitySession(gobject.GObject):
|
||||
|
||||
@@ -300,10 +311,50 @@ class Activity(Window, gtk.Container):
|
||||
if self._jobject.metadata.has_key('share-scope'):
|
||||
share_scope = self._jobject.metadata['share-scope']
|
||||
|
||||
self.shared_activity = None
|
||||
self._join_id = None
|
||||
|
||||
if handle.handle_invite:
|
||||
wait_loop = gobject.MainLoop()
|
||||
self._client_handler = _ClientHandler(
|
||||
self.get_bundle_id(),
|
||||
partial(self.__got_channel_cb, wait_loop))
|
||||
# The current API requires that self.shared_activity is set before
|
||||
# exiting from __init__, so we wait until we have got the shared
|
||||
# activity.
|
||||
wait_loop.run()
|
||||
else:
|
||||
pservice = presenceservice.get_instance()
|
||||
mesh_instance = pservice.get_activity(self._activity_id,
|
||||
warn_if_none=False)
|
||||
self._set_up_sharing(mesh_instance, share_scope)
|
||||
|
||||
if handle.object_id is None and create_jobject:
|
||||
logging.debug('Creating a jobject.')
|
||||
self._jobject = datastore.create()
|
||||
title = _('%s Activity') % get_bundle_name()
|
||||
self._jobject.metadata['title'] = title
|
||||
self.set_title(self._jobject.metadata['title'])
|
||||
self._jobject.metadata['title_set_by_user'] = '0'
|
||||
self._jobject.metadata['activity'] = self.get_bundle_id()
|
||||
self._jobject.metadata['activity_id'] = self.get_id()
|
||||
self._jobject.metadata['keep'] = '0'
|
||||
self._jobject.metadata['preview'] = ''
|
||||
self._jobject.metadata['share-scope'] = SCOPE_PRIVATE
|
||||
if self.shared_activity is not None:
|
||||
icon_color = self.shared_activity.props.color
|
||||
else:
|
||||
client = gconf.client_get_default()
|
||||
icon_color = client.get_string('/desktop/sugar/user/color')
|
||||
self._jobject.metadata['icon-color'] = icon_color
|
||||
|
||||
self._jobject.file_path = ''
|
||||
# Cannot call datastore.write async for creates:
|
||||
# https://dev.laptop.org/ticket/3071
|
||||
datastore.write(self._jobject)
|
||||
|
||||
def _set_up_sharing(self, mesh_instance, share_scope):
|
||||
# handle activity share/join
|
||||
pservice = presenceservice.get_instance()
|
||||
mesh_instance = pservice.get_activity(self._activity_id,
|
||||
warn_if_none=False)
|
||||
logging.debug("*** Act %s, mesh instance %r, scope %s",
|
||||
self._activity_id, mesh_instance, share_scope)
|
||||
if mesh_instance is not None:
|
||||
@@ -331,29 +382,19 @@ class Activity(Window, gtk.Container):
|
||||
else:
|
||||
logging.debug('Unknown share scope %r', share_scope)
|
||||
|
||||
if handle.object_id is None and create_jobject:
|
||||
logging.debug('Creating a jobject.')
|
||||
self._jobject = datastore.create()
|
||||
title = _('%s Activity') % get_bundle_name()
|
||||
self._jobject.metadata['title'] = title
|
||||
self.set_title(self._jobject.metadata['title'])
|
||||
self._jobject.metadata['title_set_by_user'] = '0'
|
||||
self._jobject.metadata['activity'] = self.get_bundle_id()
|
||||
self._jobject.metadata['activity_id'] = self.get_id()
|
||||
self._jobject.metadata['keep'] = '0'
|
||||
self._jobject.metadata['preview'] = ''
|
||||
self._jobject.metadata['share-scope'] = SCOPE_PRIVATE
|
||||
if self.shared_activity is not None:
|
||||
icon_color = self.shared_activity.props.color
|
||||
else:
|
||||
client = gconf.client_get_default()
|
||||
icon_color = client.get_string('/desktop/sugar/user/color')
|
||||
self._jobject.metadata['icon-color'] = icon_color
|
||||
def __got_channel_cb(self, wait_loop, connection_path, channel_path):
|
||||
logging.debug('Activity.__got_channel_cb')
|
||||
connection_name = connection_path.replace('/', '.')[1:]
|
||||
|
||||
self._jobject.file_path = ''
|
||||
# Cannot call datastore.write async for creates:
|
||||
# https://dev.laptop.org/ticket/3071
|
||||
datastore.write(self._jobject)
|
||||
bus = dbus.SessionBus()
|
||||
channel = bus.get_object(connection_name, channel_path)
|
||||
room_handle = channel.Get(CHANNEL, 'TargetHandle')
|
||||
|
||||
pservice = presenceservice.get_instance()
|
||||
mesh_instance = pservice.get_activity_by_handle(connection_path,
|
||||
room_handle)
|
||||
self._set_up_sharing(mesh_instance, SCOPE_PRIVATE)
|
||||
wait_loop.quit()
|
||||
|
||||
def get_active(self):
|
||||
return self._active
|
||||
@@ -646,6 +687,7 @@ class Activity(Window, gtk.Container):
|
||||
|
||||
def __joined_cb(self, activity, success, err):
|
||||
"""Callback when join has finished"""
|
||||
logging.debug('Activity.__joined_cb %r', success)
|
||||
self.shared_activity.disconnect(self._join_id)
|
||||
self._join_id = None
|
||||
if not success:
|
||||
@@ -854,6 +896,50 @@ class Activity(Window, gtk.Container):
|
||||
# DEPRECATED
|
||||
_shared_activity = property(lambda self: self.shared_activity, None)
|
||||
|
||||
SUGAR_CLIENT_PATH = '/org/freedesktop/Telepathy/Client/Sugar'
|
||||
|
||||
class _ClientHandler(dbus.service.Object, DBusProperties):
|
||||
def __init__(self, bundle_id, got_channel_cb):
|
||||
self._interfaces = set([CLIENT, CLIENT_HANDLER, PROPERTIES_IFACE])
|
||||
self._got_channel_cb = got_channel_cb
|
||||
|
||||
bus = dbus.Bus()
|
||||
name = CLIENT + '.' + bundle_id
|
||||
bus_name = dbus.service.BusName(name, bus=bus)
|
||||
|
||||
path = '/' + name.replace('.', '/')
|
||||
dbus.service.Object.__init__(self, bus_name, path)
|
||||
DBusProperties.__init__(self)
|
||||
|
||||
self._implement_property_get(CLIENT, {
|
||||
'Interfaces': lambda: list(self._interfaces),
|
||||
})
|
||||
self._implement_property_get(CLIENT_HANDLER, {
|
||||
'HandlerChannelFilter': self.__get_filters_cb,
|
||||
})
|
||||
|
||||
def __get_filters_cb(self):
|
||||
logging.debug('__get_filters_cb')
|
||||
filters = {
|
||||
CHANNEL + '.ChannelType' : CHANNEL_TYPE_TEXT,
|
||||
CHANNEL + '.TargetHandleType': CONNECTION_HANDLE_TYPE_CONTACT,
|
||||
}
|
||||
filter_dict = dbus.Dictionary(filters, signature='sv')
|
||||
logging.debug('__get_filters_cb %r', dbus.Array([filter_dict], signature='a{sv}'))
|
||||
return dbus.Array([filter_dict], signature='a{sv}')
|
||||
|
||||
@dbus.service.method(dbus_interface=CLIENT_HANDLER,
|
||||
in_signature='ooa(oa{sv})aota{sv}', out_signature='')
|
||||
def HandleChannels(self, account, connection, channels, requests_satisfied,
|
||||
user_action_time, handler_info):
|
||||
logging.debug('HandleChannels\n\t%r\n\t%r\n\t%r\n\t%r\n\t%r\n\t%r',
|
||||
account, connection, channels, requests_satisfied,
|
||||
user_action_time, handler_info)
|
||||
try:
|
||||
for channel in channels:
|
||||
self._got_channel_cb(connection, channel[0])
|
||||
except Exception, e:
|
||||
logging.exception(e)
|
||||
|
||||
_session = None
|
||||
|
||||
|
||||
@@ -121,7 +121,8 @@ def get_environment(activity):
|
||||
return environ
|
||||
|
||||
|
||||
def get_command(activity, activity_id=None, object_id=None, uri=None):
|
||||
def get_command(activity, activity_id=None, object_id=None, uri=None,
|
||||
activity_invite=False):
|
||||
if not activity_id:
|
||||
activity_id = create_activity_id()
|
||||
|
||||
@@ -133,6 +134,8 @@ def get_command(activity, activity_id=None, object_id=None, uri=None):
|
||||
command.extend(['-o', object_id])
|
||||
if uri is not None:
|
||||
command.extend(['-u', uri])
|
||||
if activity_invite:
|
||||
command.append('-i')
|
||||
|
||||
# if the command is in $BUNDLE_ROOT/bin, execute the absolute path so there
|
||||
# is no need to mangle with the shell's PATH
|
||||
@@ -236,8 +239,8 @@ class ActivityCreationHandler(gobject.GObject):
|
||||
environ = get_environment(self._bundle)
|
||||
(log_path, log_file) = open_log_file(self._bundle)
|
||||
command = get_command(self._bundle, self._handle.activity_id,
|
||||
self._handle.object_id,
|
||||
self._handle.uri)
|
||||
self._handle.object_id, self._handle.uri,
|
||||
self._handle.handle_invite)
|
||||
|
||||
dev_null = file('/dev/null', 'w')
|
||||
environment_dir = None
|
||||
|
||||
@@ -23,7 +23,8 @@ STABLE.
|
||||
class ActivityHandle(object):
|
||||
"""Data structure storing simple activity metadata"""
|
||||
|
||||
def __init__(self, activity_id=None, object_id=None, uri=None):
|
||||
def __init__(self, activity_id=None, object_id=None, uri=None,
|
||||
handle_invite=False):
|
||||
"""Initialise the handle from activity_id
|
||||
|
||||
activity_id -- unique id for the activity to be
|
||||
@@ -45,14 +46,18 @@ class ActivityHandle(object):
|
||||
activity, rather than a journal object
|
||||
(downloads stored on the file system for
|
||||
example or web pages)
|
||||
handle_invite -- the activity is being launched for handling an invite
|
||||
from the network
|
||||
"""
|
||||
self.activity_id = activity_id
|
||||
self.object_id = object_id
|
||||
self.uri = uri
|
||||
self.handle_invite = handle_invite
|
||||
|
||||
def get_dict(self):
|
||||
"""Retrieve our settings as a dictionary"""
|
||||
result = {'activity_id': self.activity_id}
|
||||
result = {'activity_id': self.activity_id,
|
||||
'handle_invite': self.handle_invite}
|
||||
if self.object_id:
|
||||
result['object_id'] = self.object_id
|
||||
if self.uri:
|
||||
@@ -65,5 +70,6 @@ def create_from_dict(handle_dict):
|
||||
"""Create a handle from a dictionary of parameters"""
|
||||
result = ActivityHandle(handle_dict['activity_id'],
|
||||
object_id = handle_dict.get('object_id'),
|
||||
uri = handle_dict.get('uri'))
|
||||
uri = handle_dict.get('uri'),
|
||||
handle_invite = handle_dict.get('handle_invite'))
|
||||
return result
|
||||
|
||||
@@ -75,6 +75,10 @@ def main():
|
||||
parser.add_option('-s', '--single-process', dest='single_process',
|
||||
action='store_true',
|
||||
help='start all the instances in the same process')
|
||||
parser.add_option('-i', '--handle-invite', dest='handle_invite',
|
||||
action='store_true',
|
||||
help='the activity is being launched for handling an '
|
||||
'invite from the network')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
logger.start()
|
||||
@@ -121,7 +125,8 @@ def main():
|
||||
activity_constructor = getattr(module, class_name)
|
||||
activity_handle = activityhandle.ActivityHandle(
|
||||
activity_id=options.activity_id,
|
||||
object_id=options.object_id, uri=options.uri)
|
||||
object_id=options.object_id, uri=options.uri,
|
||||
handle_invite=options.handle_invite)
|
||||
|
||||
if options.single_process is True:
|
||||
sessionbus = dbus.SessionBus()
|
||||
|
||||
Reference in New Issue
Block a user