Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/model/owner.py
blob: 2075f08c0fd53e7b73cdb324efff727b2b2652b2 (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
# Copyright (C) 2006-2007 Red Hat, Inc.
# Copyright (C) 2008 One Laptop Per Child
#
# 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 gobject
import os
import cjson
import gconf

from telepathy.interfaces import CHANNEL_TYPE_TEXT

from sugar import env
from sugar.presence import presenceservice
from sugar import util
from jarabe.model.invites import Invites

class Owner(gobject.GObject):
    """Class representing the owner of this machine/instance. This class
    runs in the shell and serves up the buddy icon and other stuff. It's the
    server portion of the Owner, paired with the client portion in Buddy.py.
    """
    __gtype_name__ = "ShellOwner"

    __gsignals__ = {
        'nick-changed'  : (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                           ([gobject.TYPE_STRING])),
        'color-changed' : (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                           ([gobject.TYPE_PYOBJECT])),
        'icon-changed'  : (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                           ([gobject.TYPE_PYOBJECT]))
    }

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

        client = gconf.client_get_default()
        self._nick = client.get_string("/desktop/sugar/user/nick")

        self._icon = None
        self._icon_hash = ""
        icon = os.path.join(env.get_profile_path(), "buddy-icon.jpg")
        if not os.path.exists(icon):
            raise RuntimeError("missing buddy icon")

        fd = open(icon, "r")
        self._icon = fd.read()
        fd.close()
        if not self._icon:
            raise RuntimeError("invalid buddy icon")

        # Get the icon's hash
        import hashlib
        digest = hashlib.md5(self._icon).digest()
        self._icon_hash = util.printable_hash(digest)

        self._pservice = presenceservice.get_instance()
        self._pservice.connect('activity-invitation',
                               self._activity_invitation_cb)
        self._pservice.connect('private-invitation',
                               self._private_invitation_cb)
        self._pservice.connect('activity-disappeared',
                              self._activity_disappeared_cb)

        self._invites = Invites()

    def get_invites(self):
        return self._invites

    def get_nick(self):
        return self._nick

    def _activity_invitation_cb(self, pservice, activity, buddy, message):
        self._invites.add_invite(activity.props.type,
                                 activity.props.id)

    def _private_invitation_cb(self, pservice, bus_name, connection,
                               channel, channel_type):
        """Handle a private-invitation from Presence Service.

        This is a connection by a non-Sugar XMPP client, so
        launch Chat or VideoChat with the Telepathy connection and
        channel.
        """
        if channel_type == CHANNEL_TYPE_TEXT:
            bundle_id = 'org.laptop.Chat'
        else:
            bundle_id = 'org.laptop.VideoChat'
        tp_channel = cjson.encode([bus_name, connection, channel])
        self._invites.add_private_invite(tp_channel, bundle_id)

    def _activity_disappeared_cb(self, pservice, activity):
        self._invites.remove_activity(activity.props.id)

_model = None

def get_model():
    global _model
    if _model is None:
        _model = Owner()
    return _model