sugar-toolkit-gtk3/shell/view/frame/FriendsBox.py

101 lines
3.0 KiB
Python
Raw Normal View History

2006-10-15 01:24:45 +02:00
# Copyright (C) 2006, 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
2006-10-05 17:09:38 +02:00
from sugar.graphics.canvasicon import CanvasIcon
from sugar.graphics.iconcolor import IconColor
2006-10-05 17:09:38 +02:00
from sugar.graphics import style
from sugar.presence import PresenceService
from view.BuddyIcon import BuddyIcon
2006-09-20 12:27:38 +02:00
from model.BuddyModel import BuddyModel
2006-09-17 01:05:59 +02:00
from view.frame.MenuStrategy import MenuStrategy
2006-10-05 17:09:38 +02:00
class FriendsBox(hippo.CanvasBox):
def __init__(self, shell, menu_shell):
2006-10-05 17:09:38 +02:00
hippo.CanvasBox.__init__(self)
self._shell = shell
self._menu_shell = menu_shell
self._activity_ps = None
self._joined_hid = -1
self._left_hid = -1
2006-09-08 12:23:33 +02:00
self._buddies = {}
self._pservice = PresenceService.get_instance()
self._pservice.connect('activity-appeared',
self.__activity_appeared_cb)
2006-09-18 11:48:33 +02:00
shell.connect('activity-changed', self.__activity_changed_cb)
2006-08-28 21:38:36 +02:00
def add(self, buddy):
2006-09-25 19:15:23 +02:00
model = BuddyModel(buddy=buddy)
icon = BuddyIcon(self._shell, self._menu_shell, model)
2006-10-05 17:09:38 +02:00
style.apply_stylesheet(icon, 'frame.BuddyIcon')
2006-09-17 01:05:59 +02:00
icon.set_menu_strategy(MenuStrategy())
2006-10-05 17:09:38 +02:00
self.append(icon)
2006-08-28 21:38:36 +02:00
2006-09-08 12:23:33 +02:00
self._buddies[buddy.get_name()] = icon
2006-09-08 01:13:42 +02:00
def remove(self, buddy):
2006-10-05 17:09:38 +02:00
self.remove(self._buddies[buddy.get_name()])
2006-08-28 21:38:36 +02:00
2006-09-08 01:13:42 +02:00
def clear(self):
2006-10-05 17:09:38 +02:00
for item in self.get_children():
self.remove(item)
2006-09-08 12:23:33 +02:00
self._buddies = {}
2006-08-28 21:38:36 +02:00
def __activity_appeared_cb(self, pservice, activity_ps):
2006-09-18 11:48:33 +02:00
activity = self._shell.get_current_activity()
if activity and activity_ps.get_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:
2006-08-28 21:38:36 +02:00
for buddy in activity_ps.get_joined_buddies():
self.add(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 __activity_changed_cb(self, group, activity):
if activity:
ps = self._pservice.get_activity(activity.get_id())
self._set_activity_ps(ps)
else:
self._set_activity_ps(None)
2006-08-28 21:38:36 +02:00
def __buddy_joined_cb(self, activity, buddy):
self.add(buddy)
def __buddy_left_cb(self, activity, buddy):
self.remove(buddy)