Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/presence/buddy.py
blob: a5c18ed56c95d457a0cbaf33181fe92ed176e0cf (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# 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

import os
import gobject
import dbus, dbus.service

from sugar import profile
from sugar import env

_BUDDY_PATH = "/org/laptop/Sugar/Presence/Buddies/"
_BUDDY_INTERFACE = "org.laptop.Sugar.Presence.Buddy"
_OWNER_INTERFACE = "org.laptop.Sugar.Presence.Buddy.Owner"

class NotFoundError(dbus.DBusException):
    def __init__(self):
        dbus.DBusException.__init__(self)
        self._dbus_error_name = _PRESENCE_INTERFACE + '.NotFound'

class DBusGObjectMetaclass(gobject.GObjectMeta, dbus.service.InterfaceType): pass
class DBusGObject(dbus.service.Object, gobject.GObject): __metaclass__ = DBusGObjectMetaclass


class Buddy(DBusGObject):
    """Represents another person on the network and keeps track of the
    activities and resources they make available for sharing."""

    __gtype_name__ = "Buddy"

    __gsignals__ = {
        'validity-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                            ([gobject.TYPE_BOOLEAN])),
        'property-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                            ([gobject.TYPE_PYOBJECT])),
        'icon-changed':     (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                            ([gobject.TYPE_PYOBJECT]))
    }

    __gproperties__ = {
        'key'              : (str, None, None, None,
                              gobject.PARAM_READWRITE | gobject.PARAM_CONSTRUCT_ONLY),
        'icon'             : (object, None, None, gobject.PARAM_READWRITE),
        'nick'             : (str, None, None, None, gobject.PARAM_READWRITE),
        'color'            : (str, None, None, None, gobject.PARAM_READWRITE),
        'current-activity' : (str, None, None, None, gobject.PARAM_READWRITE),
        'valid'            : (bool, None, None, False, gobject.PARAM_READABLE),
        'owner'            : (bool, None, None, False, gobject.PARAM_READABLE)
    }

    def __init__(self, bus_name, object_id, **kwargs):
        if not bus_name:
            raise ValueError("DBus bus name must be valid")
        if not object_id or not isinstance(object_id, int):
            raise ValueError("object id must be a valid number")

        self._bus_name = bus_name
        self._object_id = object_id
        self._object_path = _BUDDY_PATH + str(self._object_id)
        dbus.service.Object.__init__(self, self._bus_name, self._object_path)

        self._activities = {}   # Activity ID -> Activity
        self.handles = {} # tp client -> handle

        self._valid = False
        self._owner = False
        self._key = None
        self._icon = ''
        self._current_activity = None
        self._nick = None
        self._color = None

        if not kwargs.get("key"):
            raise ValueError("key required")

        gobject.GObject.__init__(self, **kwargs)

    def do_get_property(self, pspec):
        if pspec.name == "key":
            return self._key
        elif pspec.name == "icon":
            return self._icon
        elif pspec.name == "nick":
            return self._nick
        elif pspec.name == "color":
            return self._color
        elif pspec.name == "current-activity":
            if not self._current_activity:
                return None
            if not self._activities.has_key(self._current_activity):
                return None
            return self._current_activity
        elif pspec.name == "valid":
            return self._valid
        elif pspec.name == "owner":
            return self._owner

    def do_set_property(self, pspec, value):
        if pspec.name == "icon":
            if str(value) != self._icon:
                self._icon = str(value)
                self.IconChanged(self._icon)
                self.emit('icon-changed', self._icon)
        elif pspec.name == "nick":
            self._nick = value
        elif pspec.name == "color":
            self._color = value
        elif pspec.name == "current-activity":
            self._current_activity = value
        elif pspec.name == "key":
            self._key = value

        self._update_validity()

    # dbus signals
    @dbus.service.signal(_BUDDY_INTERFACE,
                        signature="ay")
    def IconChanged(self, icon_data):
        pass

    @dbus.service.signal(_BUDDY_INTERFACE,
                        signature="o")
    def JoinedActivity(self, activity_path):
        pass

    @dbus.service.signal(_BUDDY_INTERFACE,
                        signature="o")
    def LeftActivity(self, activity_path):
        pass

    @dbus.service.signal(_BUDDY_INTERFACE,
                        signature="a{sv}")
    def PropertyChanged(self, updated):
        pass

    # dbus methods
    @dbus.service.method(_BUDDY_INTERFACE,
                        in_signature="", out_signature="ay")
    def GetIcon(self):
        if not self.props.icon:
            return ""
        return dbus.ByteArray(self.props.icon)

    @dbus.service.method(_BUDDY_INTERFACE,
                        in_signature="", out_signature="ao")
    def GetJoinedActivities(self):
        acts = []
        for act in self.get_joined_activities():
            acts.append(act.object_path())
        return acts

    @dbus.service.method(_BUDDY_INTERFACE,
                        in_signature="", out_signature="a{sv}")
    def GetProperties(self):
        props = {}
        props['nick'] = self.props.nick
        props['owner'] = self.props.owner
        props['key'] = self.props.key
        props['color'] = self.props.color
        props['current-activity'] = self.props.current_activity
        return props

    # methods
    def object_path(self):
        return dbus.ObjectPath(self._object_path)

    def add_activity(self, activity):
        actid = activity.props.id
        if self._activities.has_key(actid):
            return
        self._activities[actid] = activity
        if activity.props.valid:
            self.JoinedActivity(activity.object_path())

    def remove_activity(self, activity):
        actid = activity.props.id
        if not self._activities.has_key(actid):
            return
        del self._activities[actid]
        if activity.props.valid:
            self.LeftActivity(activity.object_path())

    def get_joined_activities(self):
        acts = []
        for act in self._activities.values():
            if act.props.valid:
                acts.append(act)
        return acts

    def set_properties(self, properties):
        changed = False
        if "nick" in properties.keys():
            nick = properties["nick"]
            if nick != self._nick:
                self._nick = nick
                changed = True
        if "color" in properties.keys():
            color = properties["color"]
            if color != self._color:
                self._color = color
                changed = True
        if "current-activity" in properties.keys():
            curact = properties["current-activity"]
            if curact != self._current_activity:
                self._current_activity = curact
                changed = True

        if not changed:
            return

        # Try emitting PropertyChanged before updating validity
        # to avoid leaking a PropertyChanged signal before the buddy is
        # actually valid the first time after creation
        if self._valid:
            self.PropertyChanged(properties)
            self.emit('property-changed', properties)

        self._update_validity()

    def _update_validity(self):
        try:
            old_valid = self._valid
            if self._color and self._nick and self._key:
                self._valid = True
            else:
                self._valid = False

            if old_valid != self._valid:
                self.emit("validity-changed", self._valid)
        except AttributeError:
            self._valid = False


class Owner(Buddy):
    """Class representing the owner of the machine.  This is the client
    portion of the Owner, paired with the server portion in Owner.py."""

    _SHELL_SERVICE = "org.laptop.Shell"
    _SHELL_OWNER_INTERFACE = "org.laptop.Shell.Owner"
    _SHELL_PATH = "/org/laptop/Shell"

    def __init__(self, bus_name, object_id):
        key = profile.get_pubkey()
        nick = profile.get_nick_name()
        color = profile.get_color().to_string()

        icon_file = os.path.join(env.get_profile_path(), "buddy-icon.jpg")
        f = open(icon_file, "r")
        icon = f.read()
        f.close()

        self._bus = dbus.SessionBus()
        self._bus.add_signal_receiver(self._name_owner_changed_handler,
                                      signal_name="NameOwnerChanged",
                                      dbus_interface="org.freedesktop.DBus")

        # Connect to the shell to get notifications on Owner object
        # property changes
        try:
            self._connect_to_shell()
        except dbus.DBusException:
            pass

        Buddy.__init__(self, bus_name, object_id, key=key, nick=nick, color=color,
                icon=icon)
        self._owner = True

        # enable this to change random buddy properties
        if False:
            gobject.timeout_add(5000, self._update_something)

    def _update_something(self):
        import random
        it = random.randint(0, 3)
        if it == 0:
            data = get_random_image()
            self._icon_changed_cb(data)
        elif it == 1:
            from sugar.graphics import xocolor
            xo = xocolor.XoColor()
            self._color_changed_cb(xo.to_string())
        elif it == 2:
            names = ["Liam", "Noel", "Guigsy", "Whitey", "Bonehead"]
            foo = random.randint(0, len(names) - 1)
            self._nick_changed_cb(names[foo])
        elif it == 3:
            bork = random.randint(25, 65)
            it = ""
            for i in range(0, bork):
                it += chr(random.randint(40, 127))
            from sugar import util
            self._cur_activity_changed_cb(util.unique_id(it))
        return True

    def _name_owner_changed_handler(self, name, old, new):
        if name != self._SHELL_SERVICE:
            return
        if (old and len(old)) and (not new and not len(new)):
            # shell went away
            self._shell_owner = None
        elif (not old and not len(old)) and (new and len(new)):
            # shell started
            self._connect_to_shell()

    def _connect_to_shell(self):
        obj = self._bus.get_object(self._SHELL_SERVICE, self._SHELL_PATH)
        self._shell_owner = dbus.Interface(obj, self._SHELL_OWNER_INTERFACE)
        self._shell_owner.connect_to_signal('IconChanged', self._icon_changed_cb)
        self._shell_owner.connect_to_signal('ColorChanged', self._color_changed_cb)
        self._shell_owner.connect_to_signal('NickChanged', self._nick_changed_cb)
        self._shell_owner.connect_to_signal('CurrentActivityChanged',
                self._cur_activity_changed_cb)

    def _icon_changed_cb(self, icon):
        self.props.icon = icon

    def _color_changed_cb(self, color):
        props = {'color': color}
        self.set_properties(props)

    def _nick_changed_cb(self, nick):
        props = {'nick': nick}
        self.set_properties(props)

    def _cur_activity_changed_cb(self, activity_id):
        if not self._activities.has_key(activity_id):
            # This activity is local-only
            activity_id = None
        props = {'current-activity': activity_id}
        self.set_properties(props)



def get_random_image():
    import cairo, math, random, gtk

    def rand():
        return random.random()

    SIZE = 200

    s = cairo.ImageSurface(cairo.FORMAT_ARGB32, SIZE, SIZE)
    cr = cairo.Context(s)

    # background gradient
    cr.save()
    g = cairo.LinearGradient(0, 0, 1, 1)
    g.add_color_stop_rgba(1, rand(), rand(), rand(), rand())
    g.add_color_stop_rgba(0, rand(), rand(), rand(), rand())
    cr.set_source(g)
    cr.rectangle(0, 0, SIZE, SIZE);
    cr.fill()
    cr.restore()

    # random path
    cr.set_line_width(10 * rand() + 5)
    cr.move_to(SIZE * rand(), SIZE * rand())
    cr.line_to(SIZE * rand(), SIZE * rand())
    cr.rel_line_to(SIZE * rand() * -1, 0)
    cr.close_path()
    cr.stroke()

    # a circle
    cr.set_source_rgba(rand(), rand(), rand(), rand())
    cr.arc(SIZE * rand(), SIZE * rand(), 100 * rand() + 30, 0, 2 * math.pi)
    cr.fill()

    # another circle
    cr.set_source_rgba(rand(), rand(), rand(), rand())
    cr.arc(SIZE * rand(), SIZE * rand(), 100 * rand() + 30, 0, 2 * math.pi)
    cr.fill()

    def img_convert_func(buf, data):
        data[0] += buf
        return True

    data = [""]
    pixbuf = gtk.gdk.pixbuf_new_from_data(s.get_data(), gtk.gdk.COLORSPACE_RGB,
            True, 8, s.get_width(), s.get_height(), s.get_stride())
    pixbuf.save_to_callback(img_convert_func, "jpeg", {"quality": "90"}, data)
    del pixbuf

    return str(data[0])