Implement friends removal, lots of cleanups

This commit is contained in:
Marco Pesenti Gritti 2006-09-15 15:28:18 +02:00
parent 16574cbfcc
commit f2f25f874d
9 changed files with 80 additions and 69 deletions

View File

@ -3,67 +3,54 @@ from ConfigParser import ConfigParser
import gobject import gobject
from sugar.canvas.IconColor import IconColor from model.BuddyInfo import BuddyInfo
from sugar.presence import PresenceService
from sugar import env from sugar import env
class Friend:
def __init__(self, name, color):
self._name = name
self._color = color
def get_name(self):
return self._name
def get_color(self):
return IconColor(self._color)
def get_buddy(self):
pservice = PresenceService.get_instance()
return pservice.get_buddy_by_name(self._name)
class Friends(gobject.GObject): class Friends(gobject.GObject):
__gsignals__ = { __gsignals__ = {
'friend-added': (gobject.SIGNAL_RUN_FIRST, 'friend-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([object])), gobject.TYPE_NONE, ([object])),
'friend-removed': (gobject.SIGNAL_RUN_FIRST, 'friend-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([object])), gobject.TYPE_NONE, ([str])),
} }
def __init__(self): def __init__(self):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
self._list = [] self._friends = {}
self._path = os.path.join(env.get_profile_path(), 'friends') self._path = os.path.join(env.get_profile_path(), 'friends')
self.load() self.load()
def has_buddy(self, buddy): def has_buddy(self, buddy):
for friend in self: return self._friends.has_key(buddy.get_name())
if friend.get_name() == buddy.get_name():
return True
return False
def add_friend(self, name, color): def add_friend(self, buddy_info):
friend = Friend(name, color) self._friends[buddy_info.get_name()] = buddy_info
self._list.append(friend) self.emit('friend-added', buddy_info)
self.emit('friend-added', friend) def make_friend(self, buddy):
def add_buddy(self, buddy):
if not self.has_buddy(buddy): if not self.has_buddy(buddy):
self.add_friend(buddy.get_name(), buddy.get_color()) self.add_friend(BuddyInfo(buddy))
self.save() self.save()
def remove(self, buddy_info):
del self._friends[buddy_info.get_name()]
self.save()
self.emit('friend-removed', buddy_info.get_name())
def __iter__(self): def __iter__(self):
return self._list.__iter__() return self._friends.values().__iter__()
def load(self): def load(self):
cp = ConfigParser() cp = ConfigParser()
if cp.read([self._path]): if cp.read([self._path]):
for name in cp.sections(): for name in cp.sections():
self.add_friend(name, cp.get(name, 'color')) buddy = BuddyInfo()
buddy.set_name(name)
buddy.set_color(cp.get(name, 'color'))
self.add_friend(buddy)
def save(self): def save(self):
cp = ConfigParser() cp = ConfigParser()

View File

@ -1,8 +1,6 @@
import gobject import gobject
from sugar.presence import PresenceService from sugar.presence import PresenceService
from sugar.activity import ActivityFactory
from sugar.activity import Activity
from model.Friends import Friends from model.Friends import Friends
from model.Invites import Invites from model.Invites import Invites
from model.Owner import ShellOwner from model.Owner import ShellOwner
@ -66,21 +64,3 @@ class ShellModel(gobject.GObject):
def get_current_activity(self): def get_current_activity(self):
return self._current_activity return self._current_activity
def join_activity(self, bundle_id, activity_id):
activity = self.get_activity(activity_id)
if activity:
activity.present()
else:
activity_ps = self._pservice.get_activity(activity_id)
if activity_ps:
activity = ActivityFactory.create(bundle_id)
activity.join(activity_ps.object_path())
else:
logging.error('Cannot start activity.')
def start_activity(self, activity_type):
activity = ActivityFactory.create(activity_type)
activity.execute('test', [])
return activity

View File

@ -88,7 +88,10 @@ class BuddyIcon(IconItem):
activity.invite(buddy) activity.invite(buddy)
elif action == BuddyPopup.ACTION_MAKE_FRIEND: elif action == BuddyPopup.ACTION_MAKE_FRIEND:
friends = model.get_friends() friends = model.get_friends()
friends.add_buddy(buddy) friends.make_friend(buddy)
elif action == BuddyPopup.ACTION_REMOVE_FRIEND:
friends = model.get_friends()
friends.remove(buddy)
def _popdown_cb(self, friend): def _popdown_cb(self, friend):
if not self._hover_popup: if not self._hover_popup:

View File

@ -9,16 +9,17 @@ from sugar.canvas.IconItem import IconItem
class BuddyPopup(gtk.Window): class BuddyPopup(gtk.Window):
ACTION_MAKE_FRIEND = 0 ACTION_MAKE_FRIEND = 0
ACTION_INVITE = 1 ACTION_INVITE = 1
ACTION_REMOVE_FRIEND = 2
__gsignals__ = { __gsignals__ = {
'action': (gobject.SIGNAL_RUN_FIRST, 'action': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([int])), gobject.TYPE_NONE, ([int])),
} }
def __init__(self, shell, friend): def __init__(self, shell, buddy):
gtk.Window.__init__(self, gtk.WINDOW_POPUP) gtk.Window.__init__(self, gtk.WINDOW_POPUP)
self._friend = friend self._buddy = buddy
self._hover = False self._hover = False
self._popdown_on_leave = False self._popdown_on_leave = False
self._width = 13 self._width = 13
@ -35,14 +36,14 @@ class BuddyPopup(gtk.Window):
model = goocanvas.CanvasModelSimple() model = goocanvas.CanvasModelSimple()
root = model.get_root_item() root = model.get_root_item()
color = friend.get_color() color = buddy.get_color()
rect = goocanvas.Rect(fill_color=color.get_fill_color(), rect = goocanvas.Rect(fill_color=color.get_fill_color(),
stroke_color=color.get_stroke_color(), stroke_color=color.get_stroke_color(),
line_width=3) line_width=3)
grid.set_constraints(rect, 0, 0, self._width, self._height) grid.set_constraints(rect, 0, 0, self._width, self._height)
root.add_child(rect) root.add_child(rect)
text = goocanvas.Text(text=friend.get_name(), font="Sans bold 18", text = goocanvas.Text(text=buddy.get_name(), font="Sans bold 18",
fill_color='black', anchor=gtk.ANCHOR_SW) fill_color='black', anchor=gtk.ANCHOR_SW)
grid.set_constraints(text, 1, 3, self._width, self._height) grid.set_constraints(text, 1, 3, self._width, self._height)
root.add_child(text) root.add_child(text)
@ -55,9 +56,16 @@ class BuddyPopup(gtk.Window):
box = CanvasBox(grid, CanvasBox.HORIZONTAL, 1) box = CanvasBox(grid, CanvasBox.HORIZONTAL, 1)
grid.set_constraints(box, 0, 5) grid.set_constraints(box, 0, 5)
friends = shell.get_model().get_friends()
if friends.has_buddy(buddy):
icon = IconItem(icon_name='stock-remove-friend')
icon.connect('clicked', self._action_clicked_cb,
BuddyPopup.ACTION_REMOVE_FRIEND)
else:
icon = IconItem(icon_name='stock-make-friend') icon = IconItem(icon_name='stock-make-friend')
icon.connect('clicked', self._action_clicked_cb, icon.connect('clicked', self._action_clicked_cb,
BuddyPopup.ACTION_MAKE_FRIEND) BuddyPopup.ACTION_MAKE_FRIEND)
box.set_constraints(icon, 3, 3) box.set_constraints(icon, 3, 3)
box.add_child(icon) box.add_child(icon)

View File

@ -4,7 +4,10 @@ import wnck
from sugar.canvas.Grid import Grid from sugar.canvas.Grid import Grid
from view.home.HomeWindow import HomeWindow from view.home.HomeWindow import HomeWindow
from sugar.presence import PresenceService
from view.ActivityHost import ActivityHost from view.ActivityHost import ActivityHost
from sugar.activity import ActivityFactory
from sugar.activity import Activity
from view.frame.Frame import Frame from view.frame.Frame import Frame
from globalkeys import KeyGrabber from globalkeys import KeyGrabber
import sugar import sugar
@ -71,6 +74,26 @@ class Shell(gobject.GObject):
def get_grid(self): def get_grid(self):
return self._grid return self._grid
def join_activity(self, bundle_id, activity_id):
pservice = PresenceService.get_instance()
activity = self._model.get_activity(activity_id)
if activity:
activity.present()
else:
activity_ps = pservice.get_activity(activity_id)
if activity_ps:
activity = ActivityFactory.create(bundle_id)
activity.join(activity_ps.object_path())
else:
logging.error('Cannot start activity.')
def start_activity(self, activity_type):
activity = ActivityFactory.create(activity_type)
activity.execute('test', [])
return activity
def set_zoom_level(self, level): def set_zoom_level(self, level):
if level == sugar.ZOOM_ACTIVITY: if level == sugar.ZOOM_ACTIVITY:
self._screen.toggle_showing_desktop(False) self._screen.toggle_showing_desktop(False)

View File

@ -5,7 +5,7 @@ from sugar.canvas.IconColor import IconColor
from sugar.canvas.CanvasBox import CanvasBox from sugar.canvas.CanvasBox import CanvasBox
from sugar.presence import PresenceService from sugar.presence import PresenceService
from view.BuddyIcon import BuddyIcon from view.BuddyIcon import BuddyIcon
from model.Friends import Friend from model.BuddyInfo import BuddyInfo
class RightPanel(CanvasBox): class RightPanel(CanvasBox):
def __init__(self, shell): def __init__(self, shell):
@ -24,8 +24,7 @@ class RightPanel(CanvasBox):
self.__activity_changed_cb) self.__activity_changed_cb)
def add(self, buddy): def add(self, buddy):
friend = Friend(buddy.get_name(), buddy.get_color()) icon = BuddyIcon(self._shell, BuddyInfo(buddy))
icon = BuddyIcon(self._shell, friend)
icon.set_popup_distance(1) icon.set_popup_distance(1)
self.set_constraints(icon, 3, 3) self.set_constraints(icon, 3, 3)
self.add_child(icon) self.add_child(icon)

View File

@ -12,6 +12,7 @@ class FriendsGroup(goocanvas.Group):
self._shell = shell self._shell = shell
self._icon_layout = IconLayout(1200, 900) self._icon_layout = IconLayout(1200, 900)
self._friends = {}
me = MyIcon(100) me = MyIcon(100)
me.translate(600 - (me.get_property('size') / 2), me.translate(600 - (me.get_property('size') / 2),
@ -24,11 +25,18 @@ class FriendsGroup(goocanvas.Group):
self.add_friend(friend) self.add_friend(friend)
friends.connect('friend-added', self._friend_added_cb) friends.connect('friend-added', self._friend_added_cb)
friends.connect('friend-removed', self._friend_removed_cb)
def add_friend(self, friend): def add_friend(self, buddy_info):
icon = BuddyIcon(self._shell, friend) icon = BuddyIcon(self._shell, buddy_info)
self.add_child(icon) self.add_child(icon)
self._icon_layout.add_icon(icon) self._icon_layout.add_icon(icon)
def _friend_added_cb(self, data_model, friend): self._friends[buddy_info.get_name()] = icon
self.add_friend(friend)
def _friend_added_cb(self, data_model, buddy_info):
self.add_friend(buddy_info)
def _friend_removed_cb(self, data_model, name):
self.remove_child(self._friends[name])
del self._friends[name]

View File

@ -25,7 +25,7 @@ class HomeWindow(gtk.Window):
self._add_page(HomeGroup(shell)) self._add_page(HomeGroup(shell))
self._add_page(FriendsGroup(shell)) self._add_page(FriendsGroup(shell))
self._add_page(MeshGroup()) self._add_page(MeshGroup(shell))
def _add_page(self, group): def _add_page(self, group):
view = CanvasView() view = CanvasView()

View File

@ -32,8 +32,11 @@ class ActivityItem(IconItem):
return self._service return self._service
class MeshGroup(goocanvas.Group): class MeshGroup(goocanvas.Group):
def __init__(self): def __init__(self, shell):
goocanvas.Group.__init__(self) goocanvas.Group.__init__(self)
self._shell = shell
self._icon_layout = IconLayout(1200, 900) self._icon_layout = IconLayout(1200, 900)
self._activities = {} self._activities = {}