From 6710618a43633d3671044336242538a96dde86d0 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 10 Apr 2007 15:55:55 -0400 Subject: [PATCH] Use public keys rather than nicknames to differentiate buddies --- shell/model/BuddyModel.py | 29 ++++++++++++++++------------- shell/model/Friends.py | 18 +++++++++++------- shell/model/MeshModel.py | 23 +++++++++++------------ shell/model/Owner.py | 2 +- shell/view/BuddyMenu.py | 4 ++-- shell/view/frame/FriendsBox.py | 8 ++++---- shell/view/home/FriendsBox.py | 8 ++++---- shell/view/home/MeshBox.py | 32 ++++++++++++++++---------------- 8 files changed, 65 insertions(+), 59 deletions(-) diff --git a/shell/model/BuddyModel.py b/shell/model/BuddyModel.py index fee1a174..a06a728a 100644 --- a/shell/model/BuddyModel.py +++ b/shell/model/BuddyModel.py @@ -32,9 +32,9 @@ class BuddyModel(gobject.GObject): ([gobject.TYPE_PYOBJECT])) } - def __init__(self, name=None, buddy=None): - if name and buddy: - raise RuntimeError("Must specify only _one_ of name or buddy.") + def __init__(self, key=None, buddy=None): + if (key and buddy) or (not key and not buddy): + raise RuntimeError("Must specify only _one_ of key or buddy.") gobject.GObject.__init__(self) @@ -48,12 +48,11 @@ class BuddyModel(gobject.GObject): self._buddy = None - # If given just a name, try to get the buddy from the PS first + # If given just a key, try to get the buddy from the PS first if not buddy: - self._name = name - # FIXME: use public key, not name + self._key = key for iter_buddy in self._pservice.get_buddies(): - if iter_buddy.props.nick == name: + if iter_buddy.props.key == key: buddy = iter_buddy break @@ -65,15 +64,19 @@ class BuddyModel(gobject.GObject): # wait for the buddy to appear self._ba_handler = self._pservice.connect('buddy-appeared', self._buddy_appeared_cb) - self._name = name + self._key = key # Set color to 'inactive'/'disconnected' self._set_color_from_string(_NOT_PRESENT_COLOR) + self._name = "Unknown buddy" def _set_color_from_string(self, color_string): self._color = XoColor(color_string) - def get_name(self): - return self._name + def get_key(self): + return self._key + + def get_nick(self): + return self._nick def get_color(self): return self._color @@ -96,15 +99,15 @@ class BuddyModel(gobject.GObject): raise ValueError("Buddy cannot be None.") self._buddy = buddy - self._name = self._buddy.props.nick + self._key = self._buddy.props.key + self._nick = self._buddy.props.nick self._set_color_from_string(self._buddy.props.color) self._pc_handler = self._buddy.connect('property-changed', self._buddy_property_changed_cb) self._bic_handler = self._buddy.connect('icon-changed', self._buddy_icon_changed_cb) def _buddy_appeared_cb(self, pservice, buddy): - # FIXME: use public key rather than buddy name - if self._buddy or buddy.get_name() != self._name: + if self._buddy or buddy.props.key != self._key: return if self._ba_handler: diff --git a/shell/model/Friends.py b/shell/model/Friends.py index 36c443f2..e3d7130e 100644 --- a/shell/model/Friends.py +++ b/shell/model/Friends.py @@ -40,10 +40,10 @@ class Friends(gobject.GObject): self.load() def has_buddy(self, buddy): - return self._friends.has_key(buddy.get_name()) + return self._friends.has_key(buddy.get_key()) def add_friend(self, buddy_info): - self._friends[buddy_info.get_name()] = buddy_info + self._friends[buddy_info.get_key()] = buddy_info self.emit('friend-added', buddy_info) def make_friend(self, buddy): @@ -52,9 +52,9 @@ class Friends(gobject.GObject): self.save() def remove(self, buddy_info): - del self._friends[buddy_info.get_name()] + del self._friends[buddy_info.get_key()] self.save() - self.emit('friend-removed', buddy_info.get_name()) + self.emit('friend-removed', buddy_info.get_key()) def __iter__(self): return self._friends.values().__iter__() @@ -65,8 +65,11 @@ class Friends(gobject.GObject): try: success = cp.read([self._path]) if success: - for name in cp.sections(): - buddy = BuddyModel(name) + for key in cp.sections(): + # HACK: don't screw up on old friends files + if len(key) < 20: + continue + buddy = BuddyModel(key=key) self.add_friend(buddy) except Exception, exc: logging.error("Error parsing friends file: %s" % exc) @@ -75,8 +78,9 @@ class Friends(gobject.GObject): cp = ConfigParser() for friend in self: - section = friend.get_name() + section = friend.get_key() cp.add_section(section) + cp.set(section, 'nick', friend.get_nick()) cp.set(section, 'color', friend.get_color().to_string()) fileobject = open(self._path, 'w') diff --git a/shell/model/MeshModel.py b/shell/model/MeshModel.py index d51dcbfe..b14d6f72 100644 --- a/shell/model/MeshModel.py +++ b/shell/model/MeshModel.py @@ -159,9 +159,9 @@ class MeshModel(gobject.GObject): return self._buddies.values() def _buddy_activity_changed_cb(self, buddy, cur_activity): - if not self._buddies.has_key(buddy.props.nick): + if not self._buddies.has_key(buddy.props.key): return - buddy_model = self._buddies[buddy.props.nick] + buddy_model = self._buddies[buddy.props.key] if cur_activity == None: self.emit('buddy-moved', buddy_model, None) else: @@ -173,14 +173,13 @@ class MeshModel(gobject.GObject): self.emit('buddy-moved', buddy_model, activity_model) def _buddy_appeared_cb(self, pservice, buddy): - model = BuddyModel(buddy=buddy) - if self._buddies.has_key(model.get_name()): - del model + if self._buddies.has_key(buddy.props.key): return + model = BuddyModel(buddy=buddy) model.connect('current-activity-changed', self._buddy_activity_changed_cb) - self._buddies[model.get_name()] = model + self._buddies[buddy.props.key] = model self.emit('buddy-added', model) cur_activity = buddy.props.current_activity @@ -188,10 +187,10 @@ class MeshModel(gobject.GObject): self._notify_buddy_change(model, cur_activity) def _buddy_disappeared_cb(self, pservice, buddy): - if not self._buddies.has_key(buddy.props.nick): + if not self._buddies.has_key(buddy.props.key): return - self.emit('buddy-removed', self._buddies[buddy.props.nick]) - del self._buddies[buddy.props.nick] + self.emit('buddy-removed', self._buddies[buddy.props.key]) + del self._buddies[buddy.props.key] def _activity_appeared_cb(self, pservice, activity): self._check_activity(activity) @@ -222,9 +221,9 @@ class MeshModel(gobject.GObject): for buddy in self._pservice.get_buddies(): cur_activity = buddy.props.current_activity - name = buddy.props.nick - if cur_activity == activity and self._buddies.has_key(name): - buddy_model = self._buddies[name] + key = buddy.props.key + if cur_activity == activity and self._buddies.has_key(key): + buddy_model = self._buddies[key] self.emit('buddy-moved', buddy_model, model) def _activity_disappeared_cb(self, pservice, activity): diff --git a/shell/model/Owner.py b/shell/model/Owner.py index ddee4a90..32879dbc 100644 --- a/shell/model/Owner.py +++ b/shell/model/Owner.py @@ -72,7 +72,7 @@ class ShellOwner(gobject.GObject): def get_invites(self): return self._invites - def get_name(self): + def get_nick(self): return self._nick def _handle_invite(self, issuer, bundle_id, activity_id): diff --git a/shell/view/BuddyMenu.py b/shell/view/BuddyMenu.py index 935bbde7..d4874228 100644 --- a/shell/view/BuddyMenu.py +++ b/shell/view/BuddyMenu.py @@ -33,7 +33,7 @@ class BuddyMenu(Menu): self._buddy = buddy self._shell = shell - Menu.__init__(self, buddy.get_name()) + Menu.__init__(self, buddy.get_nick()) self.props.border = 0 self.props.padding = units.points_to_pixels(5) pixbuf = self._get_buddy_icon_pixbuf() @@ -53,7 +53,7 @@ class BuddyMenu(Menu): self._buddy.connect('icon-changed', self.__buddy_icon_changed_cb) owner = shell.get_model().get_owner() - if buddy.get_name() != owner.get_name(): + if buddy.get_nick() != owner.get_nick(): self._add_items() def _get_buddy_icon_pixbuf(self): diff --git a/shell/view/frame/FriendsBox.py b/shell/view/frame/FriendsBox.py index 4f4394b0..e5b49d17 100644 --- a/shell/view/frame/FriendsBox.py +++ b/shell/view/frame/FriendsBox.py @@ -59,20 +59,20 @@ class FriendsBox(hippo.CanvasBox): self._active_activity_changed_cb) def add_buddy(self, buddy): - if self._buddies.has_key(buddy.get_name()): + if self._buddies.has_key(buddy.get_key()): return model = BuddyModel(buddy=buddy) icon = FriendIcon(self._shell, self._popup_context, model) self.append(icon) - self._buddies[buddy.get_name()] = icon + self._buddies[buddy.get_key()] = icon def remove_buddy(self, buddy): - if not self._buddies.has_key(buddy.get_name()): + if not self._buddies.has_key(buddy.get_key()): return - self.remove(self._buddies[buddy.get_name()]) + self.remove(self._buddies[buddy.get_key()]) def clear(self): for item in self.get_children(): diff --git a/shell/view/home/FriendsBox.py b/shell/view/home/FriendsBox.py index c0178dd1..6f1cdb7d 100644 --- a/shell/view/home/FriendsBox.py +++ b/shell/view/home/FriendsBox.py @@ -48,14 +48,14 @@ class FriendsBox(SpreadBox, hippo.CanvasItem): icon = FriendView(self._shell, self._menu_shell, buddy_info) self.add_item(icon) - self._friends[buddy_info.get_name()] = icon + self._friends[buddy_info.get_key()] = 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_item(self._friends[name]) - del self._friends[name] + def _friend_removed_cb(self, data_model, key): + self.remove_item(self._friends[key]) + del self._friends[key] def do_allocate(self, width, height, origin_changed): SpreadBox.do_allocate(self, width, height, origin_changed) diff --git a/shell/view/home/MeshBox.py b/shell/view/home/MeshBox.py index 20829a0e..40347f38 100644 --- a/shell/view/home/MeshBox.py +++ b/shell/view/home/MeshBox.py @@ -176,17 +176,17 @@ class ActivityView(SnowflakeBox): self.append(icon, hippo.PACK_FIXED) self.set_root(icon) - def has_buddy_icon(self, name): - return self._icons.has_key(name) + def has_buddy_icon(self, key): + return self._icons.has_key(key) - def add_buddy_icon(self, name, icon): - self._icons[name] = icon + def add_buddy_icon(self, key, icon): + self._icons[key] = icon self.append(icon, hippo.PACK_FIXED) - def remove_buddy_icon(self, name): - icon = self._icons[name] + def remove_buddy_icon(self, key): + icon = self._icons[key] self.remove(icon) - del self._icons[name] + del self._icons[key] def _clicked_cb(self, item): bundle_id = self._model.get_service().get_type() @@ -280,24 +280,24 @@ class MeshBox(SpreadBox): icon = BuddyIcon(self._shell, self._menu_shell, buddy_model) self.add_item(icon) - self._buddies[buddy_model.get_name()] = icon + self._buddies[buddy_model.get_key()] = icon def _remove_alone_buddy(self, buddy_model): - icon = self._buddies[buddy_model.get_name()] + icon = self._buddies[buddy_model.get_key()] self.remove_item(icon) - del self._buddies[buddy_model.get_name()] + del self._buddies[buddy_model.get_key()] def _remove_buddy(self, buddy_model): - name = buddy_model.get_name() - if self._buddies.has_key(name): + key = buddy_model.get_key() + if self._buddies.has_key(key): self._remove_alone_buddy(buddy_model) else: for activity in self._activities.values(): - if activity.has_buddy_icon(name): - activity.remove_buddy_icon(name) + if activity.has_buddy_icon(key): + activity.remove_buddy_icon(key) def _move_buddy(self, buddy_model, activity_model): - name = buddy_model.get_name() + key = buddy_model.get_key() self._remove_buddy(buddy_model) @@ -307,7 +307,7 @@ class MeshBox(SpreadBox): activity = self._activities[activity_model.get_id()] icon = BuddyIcon(self._shell, self._menu_shell, buddy_model) - activity.add_buddy_icon(buddy_model.get_name(), icon) + activity.add_buddy_icon(buddy_model.get_key(), icon) def _add_activity(self, activity_model): icon = ActivityView(self._shell, self._menu_shell, activity_model)