From f1df5361e18d4b54a18ec3f4f23d53be6b93242d Mon Sep 17 00:00:00 2001 From: Morgan Collett Date: Wed, 3 Oct 2007 20:39:07 +0100 Subject: [PATCH] Cache handle-buddies so we can get them when the buddy has already left --- sugar/presence/activity.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/sugar/presence/activity.py b/sugar/presence/activity.py index 0042493f..de52ea73 100644 --- a/sugar/presence/activity.py +++ b/sugar/presence/activity.py @@ -90,6 +90,8 @@ class Activity(gobject.GObject): self._tags = None self._private = True self._joined = False + # Cache for get_buddy_by_handle + self._handle_to_buddy = {} def _get_properties_reply_cb(self, new_props): self._properties_changed_cb(new_props) @@ -215,12 +217,24 @@ class Activity(gobject.GObject): return buddies def get_buddy_by_handle(self, handle): - """Retrieve the Buddy object given a telepathy handle.""" - buddyhandle = self._activity.GetBuddyByHandle(handle) - if buddyhandle: - buddy = self._ps_new_object(buddyhandle) - else: - buddy = None + """Retrieve the Buddy object given a telepathy handle. + + buddies are cached in self._handle_to_buddy, so we can + still get the buddy after they have left the activity. + """ + buddy = self._handle_to_buddy.get(handle, None) + if not buddy: + try: + buddyhandle = self._activity.GetBuddyByHandle(handle) + except: + # FIXME: Need to catch NotFoundError but that's defined + # in presence-service psutils + buddyhandle = None + if buddyhandle: + buddy = self._ps_new_object(buddyhandle) + self._handle_to_buddy[handle] = buddy + else: + buddy = None return buddy def invite(self, buddy, message, response_cb):