Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/model/invites.py
blob: 68b8d057e0cfce0205cac359b7c8e43a793506e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Copyright (C) 2006-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 logging
from functools import partial

import gobject
import dbus
from telepathy.interfaces import CHANNEL, \
                                 CHANNEL_DISPATCHER, \
                                 CHANNEL_DISPATCH_OPERATION, \
                                 CHANNEL_TYPE_CONTACT_LIST, \
                                 CHANNEL_TYPE_DBUS_TUBE, \
                                 CHANNEL_TYPE_STREAMED_MEDIA, \
                                 CHANNEL_TYPE_STREAM_TUBE, \
                                 CHANNEL_TYPE_TEXT, \
                                 CLIENT
from telepathy.constants import HANDLE_TYPE_ROOM

from sugar.graphics.xocolor import XoColor

from jarabe.model import telepathyclient
from jarabe.model import bundleregistry
from jarabe.journal import misc

CONNECTION_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties'


class ActivityInvite(object):
    """Invitation to a shared activity."""
    def __init__(self, dispatch_operation_path, channel, handler,
                 activity_properties):
        self._dispatch_operation_path = dispatch_operation_path
        self._channel = channel
        self._handler = handler

        if activity_properties is not None:
            self._activity_properties = activity_properties
        else:
            self._activity_properties = {}

    def get_bundle_id(self):
        if CLIENT in self._handler:
            return self._handler[len(CLIENT + '.'):]
        else:
            return None

    def get_color(self):
        color = self._activity_properties.get('color', None)
        return XoColor(color)

    def join(self):
        logging.error('ActivityInvite.join handler %r', self._handler)

        registry = bundleregistry.get_registry()
        bundle_id = self.get_bundle_id()
        bundle = registry.get_bundle(bundle_id)
        if bundle is None:
            self._call_handle_with()
        else:
            bus = dbus.SessionBus()
            bus.add_signal_receiver(self.__name_owner_changed_cb,
	                                'NameOwnerChanged',
	                                'org.freedesktop.DBus',
	                                arg0=self._handler)
            misc.launch(bundle, color=self.get_color(), handle_invite=True)

    def __name_owner_changed_cb(self, name, old_owner, new_owner):
        logging.debug('ActivityInvite.__name_owner_changed_cb %r %r %r', name, new_owner, old_owner)
        if name == self._handler and new_owner and not old_owner:
            self._call_handle_with()

    def _call_handle_with(self):
        bus = dbus.Bus()
        obj = bus.get_object(CHANNEL_DISPATCHER, self._dispatch_operation_path)
        dispatch_operation = dbus.Interface(obj, CHANNEL_DISPATCH_OPERATION)
        dispatch_operation.HandleWith(self._handler,
                                      reply_handler=self.__handle_with_reply_cb,
                                      error_handler=self.__handle_with_reply_cb)

    def __handle_with_reply_cb(self, error=None):
        if error is not None:
            raise error
        else:
            logging.debug('__handle_with_reply_cb')

class Invites(gobject.GObject):
    __gsignals__ = {
        'invite-added':   (gobject.SIGNAL_RUN_FIRST,
                           gobject.TYPE_NONE, ([object])),
        'invite-removed': (gobject.SIGNAL_RUN_FIRST,
                           gobject.TYPE_NONE, ([object]))
    }

    def __init__(self):
        gobject.GObject.__init__(self)

        self._dispatch_operations = {}

        logging.info('KILL_PS listen for when the owner joins an activity')
        #ps = presenceservice.get_instance()
        #owner = ps.get_owner()
        #owner.connect('joined-activity', self._owner_joined_cb)

        client_handler = telepathyclient.get_instance()
        client_handler.got_dispatch_operation.connect(
                self.__got_dispatch_operation_cb)

    def __got_dispatch_operation_cb(self, **kwargs):
        logging.debug('__got_dispatch_operation_cb')
        dispatch_operation_path = kwargs['dispatch_operation_path']
        channel_path, channel_properties = kwargs['channels'][0]
        properties = kwargs['properties']
        channel_type = channel_properties[CHANNEL + '.ChannelType']
        handle_type = channel_properties[CHANNEL + '.TargetHandleType']

        if handle_type == HANDLE_TYPE_ROOM and \
           channel_type == CHANNEL_TYPE_TEXT:
            logging.debug('May be an activity, checking its properties')
            connection_path = properties[CHANNEL_DISPATCH_OPERATION + '.Connection']
            connection_name = connection_path.replace('/', '.')[1:]

            bus = dbus.Bus()
            connection = bus.get_object(connection_name, connection_path)
            connection.GetProperties(
                    channel_properties[CHANNEL + '.TargetHandle'],
                    dbus_interface=CONNECTION_INTERFACE_ACTIVITY_PROPERTIES,
                    reply_handler=partial(self.__get_properties_cb,
                                          channel_path,
                                          channel_properties,
                                          dispatch_operation_path),
                    error_handler=partial(self.__error_handler_cb,
                                          channel_path,
                                          channel_properties,
                                          dispatch_operation_path))
        else:
            self._dispatch_non_sugar_invitation(channel_path,
                                                channel_properties,
                                                dispatch_operation_path)

    def __get_properties_cb(self, channel_path, channel_properties,
                            dispatch_operation_path, properties):
        logging.debug('__get_properties_cb %r', properties)
        handler = '%s.%s' % (CLIENT, properties['type'])
        self._add_invite(dispatch_operation_path, channel_path, handler,
                         properties)

    def __error_handler_cb(self, channel_path, channel_properties,
                           dispatch_operation_path, error):
        logging.debug('__error_handler_cb %r', error)
        if error.get_dbus_name() == 'org.freedesktop.Telepathy.Error.NotAvailable':
            self._dispatch_non_sugar_invitation(channel_path,
                                                channel_properties,
                                                dispatch_operation_path)
        else:
            raise error

    def _dispatch_non_sugar_invitation(self, channel_path, channel_properties,
                                       dispatch_operation_path):
        handler = None
        channel_type = channel_properties[CHANNEL + '.ChannelType']
        if channel_type == CHANNEL_TYPE_CONTACT_LIST:
            self._handle_with(dispatch_operation_path, CLIENT + '.Sugar')
        elif channel_type == CHANNEL_TYPE_TEXT:
            handler = CLIENT + '.org.laptop.Chat'
        elif channel_type == CHANNEL_TYPE_STREAMED_MEDIA:
            handler = CLIENT + '.org.laptop.VideoChat'
        elif channel_type == CHANNEL_TYPE_DBUS_TUBE:
            handler = channel_properties[CHANNEL_TYPE_DBUS_TUBE + '.ServiceName']
        elif channel_type == CHANNEL_TYPE_STREAM_TUBE:
            handler = channel_properties[CHANNEL_TYPE_STREAM_TUBE + '.Service']
        else:
            self._handle_with(dispatch_operation_path, '')

        if handler is not None:
            logging.debug('Adding an invite from a non-Sugar client')
            self._add_invite(dispatch_operation_path, channel_path, handler)

    def _handle_with(self, dispatch_operation_path, handler):
        logging.debug('_handle_with %r %r', dispatch_operation_path, handler)
        bus = dbus.Bus()
        obj = bus.get_object(CHANNEL_DISPATCHER, dispatch_operation_path)
        dispatch_operation = dbus.Interface(obj, CHANNEL_DISPATCH_OPERATION)
        dispatch_operation.HandleWith(handler,
                                      reply_handler=self.__handle_with_reply_cb,
                                      error_handler=self.__handle_with_reply_cb)

    def __handle_with_reply_cb(self, error=None):
        if error is not None:
            logging.error('__handle_with_reply_cb %r', error)
        else:
            logging.debug('__handle_with_reply_cb')

    def _add_invite(self, dispatch_operation_path, channel, handler,
                    activity_properties=None):
        logging.debug('_add_invite %r %r %r', dispatch_operation_path, channel, handler)
        if dispatch_operation_path in self._dispatch_operations:
            # there is no point to have more than one invite for the same
            # dispatch operation
            return

        invite = ActivityInvite(dispatch_operation_path, channel, handler,
                                activity_properties)
        self._dispatch_operations[dispatch_operation_path] = invite
        self.emit('invite-added', invite)

    def _remove_invite(self, invite):
        del self._dispatch_operations[invite.get_activity_id()]
        self.emit('invite-removed', invite)

    def remove_activity(self, activity_id):
        invite = self._dispatch_operations.get(activity_id)
        if invite is not None:
            self.remove_invite(invite)

    def remove_private_channel(self, private_channel):
        invite = self._dispatch_operations.get(private_channel)
        if invite is not None:
            self.remove_private_invite(invite)

    def _owner_joined_cb(self, owner, activity):
        self.remove_activity(activity.props.id)

    def __iter__(self):
        return self._dispatch_operations.values().__iter__()


_instance = None

def get_instance():
    global _instance
    if not _instance:
        _instance = Invites()
    return _instance