2007-06-24 14:45:05 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2006-10-15 01:24:45 +02:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2006-10-02 01:50:43 +02:00
|
|
|
import hippo
|
2006-08-28 14:58:21 +02:00
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
from sugar.presence import presenceservice
|
2007-09-03 01:48:03 +02:00
|
|
|
from sugar.graphics.tray import VTray, TrayIcon
|
2007-08-20 14:49:17 +02:00
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
from view.BuddyMenu import BuddyMenu
|
|
|
|
from view.frame.frameinvoker import FrameWidgetInvoker
|
2006-09-20 12:27:38 +02:00
|
|
|
from model.BuddyModel import BuddyModel
|
2006-08-28 18:40:41 +02:00
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
class FriendIcon(TrayIcon):
|
2007-07-01 12:55:10 +02:00
|
|
|
def __init__(self, shell, buddy):
|
2007-09-03 01:48:03 +02:00
|
|
|
TrayIcon.__init__(self, icon_name='computer-xo',
|
|
|
|
xo_color=buddy.get_color())
|
2007-08-20 14:49:17 +02:00
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
palette = BuddyMenu(shell, buddy)
|
|
|
|
self.set_palette(palette)
|
2007-08-20 14:49:17 +02:00
|
|
|
palette.set_group_id('frame')
|
2007-09-03 01:48:03 +02:00
|
|
|
palette.props.invoker = FrameWidgetInvoker(self)
|
2007-02-24 14:58:38 +01:00
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
class FriendsTray(VTray):
|
2007-07-01 12:55:10 +02:00
|
|
|
def __init__(self, shell):
|
2007-09-03 01:48:03 +02:00
|
|
|
VTray.__init__(self)
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._shell = shell
|
|
|
|
self._activity_ps = None
|
|
|
|
self._joined_hid = -1
|
|
|
|
self._left_hid = -1
|
|
|
|
self._buddies = {}
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
self._pservice = presenceservice.get_instance()
|
2006-12-04 20:12:24 +01:00
|
|
|
self._pservice.connect('activity-appeared',
|
|
|
|
self.__activity_appeared_cb)
|
|
|
|
|
2007-09-29 22:02:49 +02:00
|
|
|
self._owner = self._pservice.get_owner()
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
# Add initial activities the PS knows about
|
2007-08-27 21:47:58 +02:00
|
|
|
self._pservice.get_activities_async(reply_handler=self._get_activities_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2006-12-24 15:39:00 +01:00
|
|
|
home_model = shell.get_model().get_home()
|
2007-09-29 22:02:49 +02:00
|
|
|
home_model.connect('pending-activity-changed',
|
|
|
|
self._pending_activity_changed_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-27 21:47:58 +02:00
|
|
|
def _get_activities_cb(self, list):
|
|
|
|
for activity in list:
|
|
|
|
self.__activity_appeared_cb(self._pservice, activity)
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def add_buddy(self, buddy):
|
2007-05-03 04:30:59 +02:00
|
|
|
if self._buddies.has_key(buddy.props.key):
|
2006-12-04 20:12:24 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
model = BuddyModel(buddy=buddy)
|
2007-09-03 01:48:03 +02:00
|
|
|
|
2007-07-01 12:55:10 +02:00
|
|
|
icon = FriendIcon(self._shell, model)
|
2007-09-03 01:48:03 +02:00
|
|
|
self.add_item(icon)
|
|
|
|
icon.show()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-05-03 04:30:59 +02:00
|
|
|
self._buddies[buddy.props.key] = icon
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def remove_buddy(self, buddy):
|
2007-05-14 19:04:59 +02:00
|
|
|
if not self._buddies.has_key(buddy.props.key):
|
2006-12-04 20:12:24 +01:00
|
|
|
return
|
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
self.remove_item(self._buddies[buddy.props.key])
|
2007-09-17 15:15:36 +02:00
|
|
|
del self._buddies[buddy.props.key]
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
for item in self.get_children():
|
2007-09-03 01:48:03 +02:00
|
|
|
self.remove_item(item)
|
2006-12-04 20:12:24 +01:00
|
|
|
self._buddies = {}
|
|
|
|
|
|
|
|
def __activity_appeared_cb(self, pservice, activity_ps):
|
|
|
|
activity = self._shell.get_current_activity()
|
2007-04-25 19:35:08 +02:00
|
|
|
if activity and activity_ps.props.id == activity.get_id():
|
2007-09-29 22:02:49 +02:00
|
|
|
self._set_activity_ps(activity_ps, True)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-09-29 22:02:49 +02:00
|
|
|
def _set_activity_ps(self, activity_ps, shared_activity):
|
2006-12-04 20:12:24 +01:00
|
|
|
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()
|
|
|
|
|
2007-09-29 22:02:49 +02:00
|
|
|
if shared_activity is True:
|
2006-12-04 20:12:24 +01:00
|
|
|
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)
|
2007-09-29 22:02:49 +02:00
|
|
|
else:
|
|
|
|
# only display myself if not shared
|
|
|
|
self.add_buddy(self._owner)
|
|
|
|
|
|
|
|
def _pending_activity_changed_cb(self, home_model, home_activity):
|
|
|
|
if home_activity is None:
|
2007-07-18 08:04:01 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
activity_id = home_activity.get_activity_id()
|
2007-09-29 22:02:49 +02:00
|
|
|
if activity_id is None:
|
2007-07-18 08:04:01 +02:00
|
|
|
return
|
|
|
|
|
2007-09-29 22:02:49 +02:00
|
|
|
# check if activity is shared
|
2007-07-18 08:04:01 +02:00
|
|
|
activity = None
|
|
|
|
for act in self._pservice.get_activities():
|
|
|
|
if activity_id == act.props.id:
|
|
|
|
activity = act
|
|
|
|
break
|
|
|
|
if activity:
|
2007-09-29 22:02:49 +02:00
|
|
|
self._set_activity_ps(activity, True)
|
2006-12-04 20:12:24 +01:00
|
|
|
else:
|
2007-09-29 22:02:49 +02:00
|
|
|
self._set_activity_ps(home_activity, False)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def __buddy_joined_cb(self, activity, buddy):
|
|
|
|
self.add_buddy(buddy)
|
|
|
|
|
|
|
|
def __buddy_left_cb(self, activity, buddy):
|
|
|
|
self.remove_buddy(buddy)
|