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
|
2007-02-21 17:55:44 +01:00
|
|
|
from gettext import gettext as _
|
2007-07-01 11:25:35 +02:00
|
|
|
import logging
|
2006-10-15 01:24:45 +02:00
|
|
|
|
2006-10-03 18:52:11 +02:00
|
|
|
import gobject
|
2006-10-05 18:53:34 +02:00
|
|
|
import hippo
|
2006-09-25 21:20:28 +02:00
|
|
|
|
2007-07-01 11:25:35 +02:00
|
|
|
from sugar.graphics.palette import Palette
|
2007-07-05 16:56:17 +02:00
|
|
|
from sugar.graphics.menuitem import MenuItem
|
2007-11-09 16:51:43 +01:00
|
|
|
from sugar.graphics.icon import Icon
|
2007-04-09 20:40:56 +02:00
|
|
|
from sugar.presence import presenceservice
|
2006-10-03 18:52:11 +02:00
|
|
|
|
2007-07-01 11:25:35 +02:00
|
|
|
class BuddyMenu(Palette):
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, shell, buddy):
|
|
|
|
self._buddy = buddy
|
|
|
|
self._shell = shell
|
|
|
|
|
2007-07-01 11:25:35 +02:00
|
|
|
Palette.__init__(self, buddy.get_nick())
|
2008-01-09 21:21:06 +01:00
|
|
|
self._active_activity_changed_hid = None
|
|
|
|
self.connect('destroy', self.__destroy_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-05-14 07:21:22 +02:00
|
|
|
self._buddy.connect('icon-changed', self._buddy_icon_changed_cb)
|
|
|
|
self._buddy.connect('nick-changed', self._buddy_nick_changed_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2008-01-09 21:21:06 +01:00
|
|
|
owner = self._get_shell_model().get_owner()
|
2007-04-10 21:55:55 +02:00
|
|
|
if buddy.get_nick() != owner.get_nick():
|
2007-02-21 17:55:44 +01:00
|
|
|
self._add_items()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2008-01-09 21:21:06 +01:00
|
|
|
def _get_shell_model(self):
|
|
|
|
return self._shell.get_model()
|
|
|
|
|
|
|
|
def _get_home_model(self):
|
|
|
|
return self._get_shell_model().get_home()
|
|
|
|
|
|
|
|
def __destroy_cb(self, menu):
|
|
|
|
if self._active_activity_changed_hid is not None:
|
|
|
|
home_model = self._get_home_model()
|
|
|
|
home_model.disconnect(self._active_activity_changed_hid)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-02-21 17:55:44 +01:00
|
|
|
def _add_items(self):
|
2007-04-09 20:40:56 +02:00
|
|
|
pservice = presenceservice.get_instance()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2008-01-09 21:21:06 +01:00
|
|
|
friends = self._get_shell_model().get_friends()
|
2006-12-04 20:12:24 +01:00
|
|
|
if friends.has_buddy(self._buddy):
|
2007-08-16 12:51:33 +02:00
|
|
|
menu_item = MenuItem(_('Remove friend'), 'list-remove')
|
2007-07-01 11:25:35 +02:00
|
|
|
menu_item.connect('activate', self._remove_friend_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
else:
|
2007-08-16 12:51:33 +02:00
|
|
|
menu_item = MenuItem(_('Make friend'), 'list-add')
|
2007-07-01 11:25:35 +02:00
|
|
|
menu_item.connect('activate', self._make_friend_cb)
|
2007-08-08 12:56:19 +02:00
|
|
|
|
|
|
|
self.menu.append(menu_item)
|
2007-07-01 11:25:35 +02:00
|
|
|
menu_item.show()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-11-09 16:51:43 +01:00
|
|
|
self._invite_menu = MenuItem('')
|
2007-08-31 13:56:58 +02:00
|
|
|
self._invite_menu.connect('activate', self._invite_friend_cb)
|
|
|
|
self.menu.append(self._invite_menu)
|
2007-11-09 16:51:43 +01:00
|
|
|
|
2008-01-09 21:21:06 +01:00
|
|
|
home_model = self._get_home_model()
|
|
|
|
self._active_activity_changed_hid = home_model.connect(
|
|
|
|
'active-activity-changed', self._cur_activity_changed_cb)
|
2007-11-09 16:51:43 +01:00
|
|
|
activity = home_model.get_active_activity()
|
|
|
|
self._update_invite_menu(activity)
|
|
|
|
|
|
|
|
def _update_invite_menu(self, activity):
|
|
|
|
if activity is None:
|
|
|
|
self._invite_menu.hide()
|
|
|
|
else:
|
|
|
|
title = activity.get_title()
|
|
|
|
label = self._invite_menu.get_children()[0]
|
|
|
|
label.set_text(_('Invite to %s') % title)
|
|
|
|
|
|
|
|
icon = Icon(file=activity.get_icon_path())
|
|
|
|
icon.props.xo_color = activity.get_icon_color()
|
|
|
|
self._invite_menu.set_image(icon)
|
|
|
|
icon.show()
|
2007-08-31 13:56:58 +02:00
|
|
|
|
|
|
|
self._invite_menu.show()
|
2007-11-09 16:51:43 +01:00
|
|
|
|
|
|
|
def _cur_activity_changed_cb(self, home_model, activity_model):
|
|
|
|
self._update_invite_menu(activity_model)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-05-14 07:21:22 +02:00
|
|
|
def _buddy_icon_changed_cb(self, buddy):
|
2006-12-04 20:12:24 +01:00
|
|
|
pass
|
2007-05-14 07:21:22 +02:00
|
|
|
|
|
|
|
def _buddy_nick_changed_cb(self, buddy, nick):
|
2007-07-01 11:25:35 +02:00
|
|
|
self.set_primary_text(nick)
|
|
|
|
|
|
|
|
def _make_friend_cb(self, menuitem):
|
2008-01-09 21:21:06 +01:00
|
|
|
friends = self._get_shell_model().get_friends()
|
2007-07-01 11:25:35 +02:00
|
|
|
friends.make_friend(self._buddy)
|
|
|
|
|
|
|
|
def _remove_friend_cb(self, menuitem):
|
2008-01-09 21:21:06 +01:00
|
|
|
friends = self._get_shell_model().get_friends()
|
2007-07-01 11:25:35 +02:00
|
|
|
friends.remove(self._buddy)
|
|
|
|
|
|
|
|
def _invite_friend_cb(self, menuitem):
|
|
|
|
activity = self._shell.get_current_activity()
|
|
|
|
activity.invite(self._buddy)
|
|
|
|
|