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
|
# 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
from gettext import gettext as _
import logging
import gtk
import gobject
import hippo
from sugar.graphics.palette import Palette
from sugar.graphics import units
from sugar.presence import presenceservice
class BuddyMenu(Palette):
def __init__(self, shell, buddy):
self._buddy = buddy
self._shell = shell
Palette.__init__(self, buddy.get_nick())
pixbuf = self._get_buddy_icon_pixbuf()
if pixbuf:
scaled_pixbuf = pixbuf.scale_simple(units.grid_to_pixels(1),
units.grid_to_pixels(1),
gtk.gdk.INTERP_BILINEAR)
del pixbuf
image = gtk.Image()
image.set_from_pixbuf(scaled_pixbuf)
self.set_content(image)
image.show()
self._buddy.connect('icon-changed', self._buddy_icon_changed_cb)
self._buddy.connect('nick-changed', self._buddy_nick_changed_cb)
owner = shell.get_model().get_owner()
if buddy.get_nick() != owner.get_nick():
self._add_items()
def _get_buddy_icon_pixbuf(self):
buddy_object = self._buddy.get_buddy()
if not buddy_object:
return None
icon_data = buddy_object.props.icon
if not icon_data:
return None
pbl = gtk.gdk.PixbufLoader()
pbl.write(icon_data)
pixbuf = None
try:
pbl.close()
pixbuf = pbl.get_pixbuf()
except gobject.GError:
pass
del pbl
return pixbuf
def _add_items(self):
shell_model = self._shell.get_model()
pservice = presenceservice.get_instance()
friends = shell_model.get_friends()
if friends.has_buddy(self._buddy):
menu_item = gtk.MenuItem(_('Remove friend')) #, 'theme:stock-remove')
menu_item.connect('activate', self._remove_friend_cb)
else:
menu_item = gtk.MenuItem(_('Make friend')) #, 'theme:stock-add')
menu_item.connect('activate', self._make_friend_cb)
self.append_menu_item(menu_item)
menu_item.show()
activity = shell_model.get_home().get_current_activity()
if activity != None:
activity_ps = pservice.get_activity(activity.get_activity_id())
# FIXME check that the buddy is not in the activity already
menu_item = gtk.MenuItem(_('Invite')) #, 'theme:stock-invite')
menu_item.connect('activate', self._invite_cb)
self.append_menu_item(menu_item)
menu_item.show()
def _buddy_icon_changed_cb(self, buddy):
pass
def _buddy_nick_changed_cb(self, buddy, nick):
self.set_primary_text(nick)
def _make_friend_cb(self, menuitem):
friends = self._shell.get_model().get_friends()
friends.make_friend(self._buddy)
def _remove_friend_cb(self, menuitem):
friends = self._shell.get_model().get_friends()
friends.remove(self._buddy)
def _invite_friend_cb(self, menuitem):
activity = self._shell.get_current_activity()
activity.invite(self._buddy)
|