Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/model/BuddyModel.py
blob: 11c6567608441cc308502c3591bf49635c36610c (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
# 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 sugar.presence import presenceservice
from sugar.graphics.xocolor import XoColor
import gobject

_NOT_PRESENT_COLOR = "#888888,#BBBBBB"

class BuddyModel(gobject.GObject):
    __gsignals__ = {
        'appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
        'disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
        'nick-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                         ([gobject.TYPE_PYOBJECT])),
        'color-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                         ([gobject.TYPE_PYOBJECT])),
        'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                         ([])),
        'current-activity-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                                    ([gobject.TYPE_PYOBJECT]))
    }

    def __init__(self, key=None, buddy=None, nick=None):
        if (key and buddy) or (not key and not buddy):
            raise RuntimeError("Must specify only _one_ of key or buddy.")

        gobject.GObject.__init__(self)

        self._ba_handler = None
        self._pc_handler = None
        self._dis_handler = None
        self._bic_handler = None
        self._cac_handler = None

        self._pservice = presenceservice.get_instance()

        self._buddy = None

        if not buddy:
            self._key = key        
            # connect to the PS's buddy-appeared signal and
            # wait for the buddy to appear
            self._ba_handler = self._pservice.connect('buddy-appeared',
                    self._buddy_appeared_cb)
            # Set color to 'inactive'/'disconnected'
            self._set_color_from_string(_NOT_PRESENT_COLOR)
            self._nick = nick

            self._pservice.get_buddies_async(reply_handler=self._get_buddies_cb)
        else:
            self._update_buddy(buddy)

    def _get_buddies_cb(self, list):
        buddy = None
        for iter_buddy in list:
            if iter_buddy.props.key == self._key:
                buddy = iter_buddy
                break

        if buddy:
            if self._ba_handler:
                # Once we have the buddy, we no longer need to
                # monitor buddy-appeared events
                self._pservice.disconnect(self._ba_handler)
                self._ba_handler = None

            self._update_buddy(buddy)

    def _set_color_from_string(self, color_string):
        self._color = XoColor(color_string)

    def get_key(self):
        return self._key

    def get_nick(self):
        return self._nick

    def get_color(self):
        return self._color

    def get_buddy(self):
        return self._buddy

    def is_owner(self):
        if not self._buddy:
            return False
        return self._buddy.props.owner

    def is_present(self):
        if self._buddy:
            return True
        return False

    def get_current_activity(self):
        if self._buddy:
            return self._buddy.props.current_activity
        return None

    def _update_buddy(self, buddy):
        if not buddy:
            raise ValueError("Buddy cannot be None.")

        self._buddy = buddy
        self._key = self._buddy.props.key
        self._nick = self._buddy.props.nick
        self._set_color_from_string(self._buddy.props.color)

        self._pc_handler = self._buddy.connect('property-changed', self._buddy_property_changed_cb)
        self._bic_handler = self._buddy.connect('icon-changed', self._buddy_icon_changed_cb)

    def _buddy_appeared_cb(self, pservice, buddy):
        if self._buddy or buddy.props.key != self._key:
            return

        if self._ba_handler:
            # Once we have the buddy, we no longer need to
            # monitor buddy-appeared events
            self._pservice.disconnect(self._ba_handler)
            self._ba_handler = None

        self._update_buddy(buddy)
        self.emit('appeared')

    def _buddy_property_changed_cb(self, buddy, keys):
        if not self._buddy:
            return
        if 'color' in keys:
            self._set_color_from_string(self._buddy.props.color)
            self.emit('color-changed', self.get_color())
        if 'current-activity' in keys:
            self.emit('current-activity-changed', buddy.props.current_activity)
        if 'nick' in keys:
            self._nick = self._buddy.props.nick
            self.emit('nick-changed', self.get_nick())            

    def _buddy_disappeared_cb(self, buddy):
        if buddy != self._buddy:
            return
        self._buddy.disconnect(self._pc_handler)
        self._buddy.disconnect(self._dis_handler)
        self._buddy.disconnect(self._bic_handler)
        self._buddy.disconnect(self._cac_handler)
        self._set_color_from_string(_NOT_PRESENT_COLOR)
        self.emit('disappeared')
        self._buddy = None

    def _buddy_icon_changed_cb(self, buddy):
        self.emit('icon-changed')