Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/view/frame/FriendsBox.py
blob: 28069264b6d5f7fbbd4e655b1774a4e6ae5e9fff (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
# 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 hippo

from sugar.graphics.palette import Palette
from sugar.graphics.icon import CanvasIcon
from sugar.graphics import style
from sugar.presence import presenceservice

from view.BuddyIcon import BuddyIcon
from model.BuddyModel import BuddyModel
from view.frame.frameinvoker import FrameCanvasInvoker

class FriendIcon(BuddyIcon):
    def __init__(self, shell, buddy):
        BuddyIcon.__init__(self, shell, buddy)

        palette = self.get_palette()
        palette.set_group_id('frame')
        palette.props.invoker = FrameCanvasInvoker(self)

    def prelight(self, enter):
        if enter:
            self.props.background_color = style.COLOR_BLACK.get_int()
        else:
            self.props.background_color = style.COLOR_TOOLBAR_GREY.get_int()

class FriendsBox(hippo.CanvasBox):
    def __init__(self, shell):
        hippo.CanvasBox.__init__(self)
        self._shell = shell
        self._activity_ps = None
        self._joined_hid = -1
        self._left_hid = -1
        self._buddies = {}

        self._pservice = presenceservice.get_instance()
        self._pservice.connect('activity-appeared',
                               self.__activity_appeared_cb)

        # Add initial activities the PS knows about
        self._pservice.get_activities_async(reply_handler=self._get_activities_cb)

        home_model = shell.get_model().get_home()
        home_model.connect('active-activity-changed',
                           self._active_activity_changed_cb)

    def _get_activities_cb(self, list):
        for activity in list:
            self.__activity_appeared_cb(self._pservice, activity)

    def add_buddy(self, buddy):
        if self._buddies.has_key(buddy.props.key):
            return

        model = BuddyModel(buddy=buddy)
        icon = FriendIcon(self._shell, model)
        self.append(icon)

        self._buddies[buddy.props.key] = icon

    def remove_buddy(self, buddy):
        if not self._buddies.has_key(buddy.props.key):
            return

        self.remove(self._buddies[buddy.props.key])

    def clear(self):
        for item in self.get_children():
            self.remove(item)
        self._buddies = {}

    def __activity_appeared_cb(self, pservice, activity_ps):
        activity = self._shell.get_current_activity()
        if activity and activity_ps.props.id == activity.get_id():
            self._set_activity_ps(activity_ps)

    def _set_activity_ps(self, activity_ps):
        if self._activity_ps == activity_ps:
            return

        if self._joined_hid > 0:
            self._activity_ps.disconnect(self._joined_hid)
            self._joined_hid = -1
        if self._left_hid > 0:
            self._activity_ps.disconnect(self._left_hid)
            self._left_hid = -1

        self._activity_ps = activity_ps

        self.clear()

        if activity_ps != None:
            for buddy in activity_ps.get_joined_buddies():
                self.add_buddy(buddy)

            self._joined_hid = activity_ps.connect(
                            'buddy-joined', self.__buddy_joined_cb)
            self._left_hid = activity_ps.connect(
                            'buddy-left', self.__buddy_left_cb)

    def _active_activity_changed_cb(self, home_model, home_activity):
        if not home_activity:
            self._set_activity_ps(None)
            return

        activity_id = home_activity.get_activity_id()
        if not activity_id:
            self._set_activity_ps(None)
            return
        
        # HACK to suppress warning in logs when activity isn't found
        # (if it's locally launched and not shared yet)
        activity = None
        for act in self._pservice.get_activities():
            if activity_id == act.props.id:
                activity = act
                break
        if activity:
            self._set_activity_ps(activity)
        else:
            self._set_activity_ps(None)

    def __buddy_joined_cb(self, activity, buddy):
        self.add_buddy(buddy)

    def __buddy_left_cb(self, activity, buddy):
        self.remove_buddy(buddy)