sugar-toolkit-gtk3/shell/view/BuddyMenu.py

82 lines
2.0 KiB
Python
Raw Normal View History

2006-10-03 18:52:11 +02:00
import gtk
import gobject
2006-10-05 18:53:34 +02:00
import hippo
2006-09-25 21:20:28 +02:00
2006-10-03 18:52:11 +02:00
from sugar.graphics.menu import Menu
from sugar.graphics.canvasicon import CanvasIcon
from sugar.presence import PresenceService
2006-09-25 21:35:25 +02:00
_ICON_SIZE = 75
2006-09-13 16:26:54 +02:00
2006-09-16 15:43:07 +02:00
class BuddyMenu(Menu):
2006-09-15 02:54:25 +02:00
ACTION_MAKE_FRIEND = 0
2006-09-15 03:33:09 +02:00
ACTION_INVITE = 1
ACTION_REMOVE_FRIEND = 2
2006-09-15 02:54:25 +02:00
def __init__(self, shell, buddy):
self._buddy = buddy
2006-09-16 10:46:10 +02:00
self._shell = shell
2006-10-03 18:52:11 +02:00
icon_item = None
pixbuf = self._get_buddy_icon_pixbuf()
if pixbuf:
scaled_pixbuf = pixbuf.scale_simple(_ICON_SIZE, _ICON_SIZE,
gtk.gdk.INTERP_BILINEAR)
del pixbuf
2006-10-05 18:53:34 +02:00
icon_item = hippo.CanvasImage(pixbuf=scaled_pixbuf)
2006-10-03 18:52:11 +02:00
Menu.__init__(self, buddy.get_name(), icon_item)
self._buddy.connect('icon-changed', self.__buddy_icon_changed_cb)
2006-09-16 10:46:10 +02:00
owner = shell.get_model().get_owner()
if buddy.get_name() != owner.get_name():
2006-09-16 15:36:39 +02:00
self._add_actions()
2006-09-16 10:46:10 +02:00
2006-09-25 21:20:28 +02:00
def _get_buddy_icon_pixbuf(self):
buddy_object = self._buddy.get_buddy()
if not buddy_object:
return None
pixbuf = None
2006-09-25 21:20:28 +02:00
icon_data = buddy_object.get_icon()
icon_data_string = ""
for item in icon_data:
if item < 0:
item = item + 128
icon_data_string += chr(item)
2006-09-25 21:20:28 +02:00
pbl = gtk.gdk.PixbufLoader()
pbl.write(icon_data_string)
try:
pbl.close()
pixbuf = pbl.get_pixbuf()
except gobject.GError:
pass
2006-09-25 21:20:28 +02:00
del pbl
return pixbuf
2006-09-16 15:36:39 +02:00
def _add_actions(self):
shell_model = self._shell.get_model()
pservice = PresenceService.get_instance()
friends = shell_model.get_friends()
2006-09-16 10:46:10 +02:00
if friends.has_buddy(self._buddy):
2006-10-03 18:52:11 +02:00
icon = CanvasIcon(icon_name='stock-remove')
2006-09-16 15:43:07 +02:00
self.add_action(icon, BuddyMenu.ACTION_REMOVE_FRIEND)
else:
2006-10-03 18:52:11 +02:00
icon = CanvasIcon(icon_name='stock-add')
2006-09-16 15:43:07 +02:00
self.add_action(icon, BuddyMenu.ACTION_MAKE_FRIEND)
2006-09-14 13:03:11 +02:00
2006-09-21 15:02:46 +02:00
activity_id = shell_model.get_current_activity()
if activity_id != None:
activity_ps = pservice.get_activity(activity_id)
# FIXME check that the buddy is not in the activity already
2006-10-04 15:26:41 +02:00
icon = CanvasIcon(icon_name='stock-invite')
2006-09-16 15:43:07 +02:00
self.add_action(icon, BuddyMenu.ACTION_INVITE)
2006-09-25 21:20:28 +02:00
def __buddy_icon_changed_cb(self, buddy):
pass