From f2f25f874d639c31d4c8182c18ee8f11acb0b579 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 15 Sep 2006 15:28:18 +0200 Subject: [PATCH] Implement friends removal, lots of cleanups --- shell/model/Friends.py | 51 ++++++++++++--------------------- shell/model/ShellModel.py | 20 ------------- shell/view/BuddyIcon.py | 5 +++- shell/view/BuddyPopup.py | 22 +++++++++----- shell/view/Shell.py | 23 +++++++++++++++ shell/view/frame/RightPanel.py | 5 ++-- shell/view/home/FriendsGroup.py | 16 ++++++++--- shell/view/home/HomeWindow.py | 2 +- shell/view/home/MeshGroup.py | 5 +++- 9 files changed, 80 insertions(+), 69 deletions(-) diff --git a/shell/model/Friends.py b/shell/model/Friends.py index 6beca663..a11acc06 100644 --- a/shell/model/Friends.py +++ b/shell/model/Friends.py @@ -3,67 +3,54 @@ from ConfigParser import ConfigParser import gobject -from sugar.canvas.IconColor import IconColor -from sugar.presence import PresenceService +from model.BuddyInfo import BuddyInfo 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): __gsignals__ = { 'friend-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([object])), 'friend-removed': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, ([object])), + gobject.TYPE_NONE, ([str])), } def __init__(self): gobject.GObject.__init__(self) - self._list = [] + self._friends = {} self._path = os.path.join(env.get_profile_path(), 'friends') self.load() def has_buddy(self, buddy): - for friend in self: - if friend.get_name() == buddy.get_name(): - return True - return False + return self._friends.has_key(buddy.get_name()) - def add_friend(self, name, color): - friend = Friend(name, color) - self._list.append(friend) + def add_friend(self, buddy_info): + self._friends[buddy_info.get_name()] = buddy_info + self.emit('friend-added', buddy_info) - self.emit('friend-added', friend) - - def add_buddy(self, buddy): + def make_friend(self, buddy): if not self.has_buddy(buddy): - self.add_friend(buddy.get_name(), buddy.get_color()) + self.add_friend(BuddyInfo(buddy)) 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): - return self._list.__iter__() + return self._friends.values().__iter__() def load(self): cp = ConfigParser() if cp.read([self._path]): 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): cp = ConfigParser() diff --git a/shell/model/ShellModel.py b/shell/model/ShellModel.py index 2f9dd255..4929fe79 100644 --- a/shell/model/ShellModel.py +++ b/shell/model/ShellModel.py @@ -1,8 +1,6 @@ import gobject from sugar.presence import PresenceService -from sugar.activity import ActivityFactory -from sugar.activity import Activity from model.Friends import Friends from model.Invites import Invites from model.Owner import ShellOwner @@ -66,21 +64,3 @@ class ShellModel(gobject.GObject): def get_current_activity(self): 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 diff --git a/shell/view/BuddyIcon.py b/shell/view/BuddyIcon.py index 1743b79b..3e26b084 100644 --- a/shell/view/BuddyIcon.py +++ b/shell/view/BuddyIcon.py @@ -88,7 +88,10 @@ class BuddyIcon(IconItem): activity.invite(buddy) elif action == BuddyPopup.ACTION_MAKE_FRIEND: 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): if not self._hover_popup: diff --git a/shell/view/BuddyPopup.py b/shell/view/BuddyPopup.py index a5a04fd6..4aab3f6b 100644 --- a/shell/view/BuddyPopup.py +++ b/shell/view/BuddyPopup.py @@ -9,16 +9,17 @@ from sugar.canvas.IconItem import IconItem class BuddyPopup(gtk.Window): ACTION_MAKE_FRIEND = 0 ACTION_INVITE = 1 + ACTION_REMOVE_FRIEND = 2 __gsignals__ = { 'action': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([int])), } - def __init__(self, shell, friend): + def __init__(self, shell, buddy): gtk.Window.__init__(self, gtk.WINDOW_POPUP) - self._friend = friend + self._buddy = buddy self._hover = False self._popdown_on_leave = False self._width = 13 @@ -35,14 +36,14 @@ class BuddyPopup(gtk.Window): model = goocanvas.CanvasModelSimple() root = model.get_root_item() - color = friend.get_color() + color = buddy.get_color() rect = goocanvas.Rect(fill_color=color.get_fill_color(), stroke_color=color.get_stroke_color(), line_width=3) grid.set_constraints(rect, 0, 0, self._width, self._height) 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) grid.set_constraints(text, 1, 3, self._width, self._height) root.add_child(text) @@ -55,9 +56,16 @@ class BuddyPopup(gtk.Window): box = CanvasBox(grid, CanvasBox.HORIZONTAL, 1) grid.set_constraints(box, 0, 5) - icon = IconItem(icon_name='stock-make-friend') - icon.connect('clicked', self._action_clicked_cb, - BuddyPopup.ACTION_MAKE_FRIEND) + 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.connect('clicked', self._action_clicked_cb, + BuddyPopup.ACTION_MAKE_FRIEND) + box.set_constraints(icon, 3, 3) box.add_child(icon) diff --git a/shell/view/Shell.py b/shell/view/Shell.py index 6236df99..76d3f263 100644 --- a/shell/view/Shell.py +++ b/shell/view/Shell.py @@ -4,7 +4,10 @@ import wnck from sugar.canvas.Grid import Grid from view.home.HomeWindow import HomeWindow +from sugar.presence import PresenceService from view.ActivityHost import ActivityHost +from sugar.activity import ActivityFactory +from sugar.activity import Activity from view.frame.Frame import Frame from globalkeys import KeyGrabber import sugar @@ -71,6 +74,26 @@ class Shell(gobject.GObject): def get_grid(self): 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): if level == sugar.ZOOM_ACTIVITY: self._screen.toggle_showing_desktop(False) diff --git a/shell/view/frame/RightPanel.py b/shell/view/frame/RightPanel.py index 20e96222..6ed4a0d6 100644 --- a/shell/view/frame/RightPanel.py +++ b/shell/view/frame/RightPanel.py @@ -5,7 +5,7 @@ from sugar.canvas.IconColor import IconColor from sugar.canvas.CanvasBox import CanvasBox from sugar.presence import PresenceService from view.BuddyIcon import BuddyIcon -from model.Friends import Friend +from model.BuddyInfo import BuddyInfo class RightPanel(CanvasBox): def __init__(self, shell): @@ -24,8 +24,7 @@ class RightPanel(CanvasBox): self.__activity_changed_cb) def add(self, buddy): - friend = Friend(buddy.get_name(), buddy.get_color()) - icon = BuddyIcon(self._shell, friend) + icon = BuddyIcon(self._shell, BuddyInfo(buddy)) icon.set_popup_distance(1) self.set_constraints(icon, 3, 3) self.add_child(icon) diff --git a/shell/view/home/FriendsGroup.py b/shell/view/home/FriendsGroup.py index 3904080b..17497388 100644 --- a/shell/view/home/FriendsGroup.py +++ b/shell/view/home/FriendsGroup.py @@ -12,6 +12,7 @@ class FriendsGroup(goocanvas.Group): self._shell = shell self._icon_layout = IconLayout(1200, 900) + self._friends = {} me = MyIcon(100) me.translate(600 - (me.get_property('size') / 2), @@ -24,11 +25,18 @@ class FriendsGroup(goocanvas.Group): self.add_friend(friend) friends.connect('friend-added', self._friend_added_cb) + friends.connect('friend-removed', self._friend_removed_cb) - def add_friend(self, friend): - icon = BuddyIcon(self._shell, friend) + def add_friend(self, buddy_info): + icon = BuddyIcon(self._shell, buddy_info) self.add_child(icon) self._icon_layout.add_icon(icon) - def _friend_added_cb(self, data_model, friend): - self.add_friend(friend) + self._friends[buddy_info.get_name()] = icon + + 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] diff --git a/shell/view/home/HomeWindow.py b/shell/view/home/HomeWindow.py index 773ee770..ba1129e4 100644 --- a/shell/view/home/HomeWindow.py +++ b/shell/view/home/HomeWindow.py @@ -25,7 +25,7 @@ class HomeWindow(gtk.Window): self._add_page(HomeGroup(shell)) self._add_page(FriendsGroup(shell)) - self._add_page(MeshGroup()) + self._add_page(MeshGroup(shell)) def _add_page(self, group): view = CanvasView() diff --git a/shell/view/home/MeshGroup.py b/shell/view/home/MeshGroup.py index 90dce0e0..61118238 100644 --- a/shell/view/home/MeshGroup.py +++ b/shell/view/home/MeshGroup.py @@ -32,8 +32,11 @@ class ActivityItem(IconItem): return self._service class MeshGroup(goocanvas.Group): - def __init__(self): + def __init__(self, shell): goocanvas.Group.__init__(self) + + self._shell = shell + self._icon_layout = IconLayout(1200, 900) self._activities = {}