From 6c1675cfda11e479165265cdac52624285850492 Mon Sep 17 00:00:00 2001 From: Morgan Collett Date: Thu, 11 Oct 2007 20:54:47 +0000 Subject: Implement BuddyHandleJoined/Left which signals the handles as well as buddy object paths In the process, add a buddy to handle mapping to get at the channel-specific handles --- diff --git a/src/activity.py b/src/activity.py index a51ab33..ce34105 100644 --- a/src/activity.py +++ b/src/activity.py @@ -145,6 +145,7 @@ class Activity(ExportedGObject): # we've joined. If _joined is False, this will be incomplete. # { member handle, possibly channel-specific => Buddy } self._handle_to_buddy = {} + self._buddy_to_handle = {} # The buddies the PS thinks are in the channel. If _joined is True # this is kept in sync with reality. If _joined is False this is # based on buddies' claimed activities. @@ -317,15 +318,12 @@ class Activity(ExportedGObject): self.emit("validity-changed", self._valid) if self._valid: # Pretend everyone joined - for buddy in self._buddies: - self.BuddyJoined(buddy.object_path()) - handle = buddy.get_identifier_by_plugin(self._tp) - assert handle is not None - self.BuddyHandleJoined(buddy.object_path(), handle[0]) + for (handle, buddy) in self._handle_to_buddy.iteritems(): + self.BuddyHandleJoined(buddy.object_path(), handle) else: # Pretend everyone left - for buddy in self._buddies: - self.BuddyLeft(buddy.object_path()) + for (handle, buddy) in self._handle_to_buddy.iteritems(): + self.BuddyHandleLeft(buddy.object_path(), handle) except AttributeError: self._valid = False @@ -337,6 +335,8 @@ class Activity(ExportedGObject): """Generates DBUS signal when a buddy joins this activity. buddy_path -- DBUS path to buddy object + + XXX Deprecated - use BuddyHandleJoined """ _logger.debug('BuddyJoined: %s', buddy_path) @@ -350,6 +350,7 @@ class Activity(ExportedGObject): """ _logger.debug('BuddyHandleJoined: %s (handle %u)' % (buddy_path, handle)) + self.BuddyJoined(buddy_path) @dbus.service.signal(_ACTIVITY_INTERFACE, signature="o") @@ -357,6 +358,8 @@ class Activity(ExportedGObject): """Generates DBUS signal when a buddy leaves this activity. buddy_path -- DBUS path to buddy object + + XXX Deprecated - use BuddyHandleLeft """ _logger.debug('BuddyLeft: %s', buddy_path) @@ -370,6 +373,7 @@ class Activity(ExportedGObject): """ _logger.debug('BuddyHandleLeft: %s (handle %u)' % (buddy_path, handle)) + self.BuddyLeft(buddy_path) @dbus.service.signal(_ACTIVITY_INTERFACE, signature="a{sv}") @@ -670,16 +674,16 @@ class Activity(ExportedGObject): return ret def buddy_apparently_joined(self, buddy): - """Adds a buddy to this activity and sends a BuddyJoined signal, - unless we can already see who's in the activity by being in it - ourselves. + """Adds a buddy to this activity and sends a BuddyHandleJoined + signal, unless we can already see who's in the activity by being + in it ourselves. buddy -- Buddy object representing the buddy being added Adds a buddy to this activity if the buddy is not already in the buddy list. - If this activity is "valid", a BuddyJoined signal is also sent. + If this activity is "valid", a BuddyHandleJoined signal is also sent. This method is called by the PresenceService on the local machine. """ @@ -704,12 +708,11 @@ class Activity(ExportedGObject): for buddy in buddies: buddy.add_activity(self) if self._valid: - self.BuddyJoined(buddy.object_path()) - handle = buddy.get_identifier_by_plugin(self._tp) - assert handle is not None - self.BuddyHandleJoined(buddy.object_path(), handle[0]) + handle = self._buddy_to_handle.get(buddy) + self.BuddyHandleJoined(buddy.object_path(), handle) else: - _logger.debug('Suppressing BuddyJoined: activity not "valid"') + _logger.debug( + 'Suppressing BuddyHandleJoined: activity not "valid"') def _remove_buddies(self, buddies): buddies = set(buddies) @@ -723,22 +726,24 @@ class Activity(ExportedGObject): for buddy in buddies: buddy.remove_activity(self) if self._valid: - self.BuddyLeft(buddy.object_path()) + handle = self._buddy_to_handle.get(buddy) + self.BuddyHandleLeft(buddy.object_path(), handle) else: - _logger.debug('Suppressing BuddyLeft: activity not "valid"') + _logger.debug( + 'Suppressing BuddyHandleLeft: activity not "valid"') if not self._buddies: self.emit('disappeared') def buddy_apparently_left(self, buddy): - """Removes a buddy from this activity and sends a BuddyLeft signal, - unless we can already see who's in the activity by being in it - ourselves. + """Removes a buddy from this activity and sends a BuddyHandleLeft + signal, unless we can already see who's in the activity by being + in it ourselves. buddy -- Buddy object representing the buddy being removed Removes a buddy from this activity if the buddy is in the buddy list. - If this activity is "valid", a BuddyLeft signal is also sent. + If this activity is "valid", a BuddyHandleLeft signal is also sent. This method is called by the PresenceService on the local machine. """ self._claimed_buddies.discard(buddy) @@ -821,6 +826,7 @@ class Activity(ExportedGObject): self._text_channel = text_channel self._handle_to_buddy = {} + self._buddy_to_handle = {} self.NewChannel(text_channel.object_path) self._clean_up_matches() tubes_channel = Channel(self._tp.get_connection().service_name, @@ -1033,6 +1039,7 @@ class Activity(ExportedGObject): added) for handle, buddy in added_buddies.iteritems(): self._handle_to_buddy[handle] = buddy + self._buddy_to_handle[buddy] = handle self._add_buddies(added_buddies.itervalues()) # we treat all pending members as if they weren't there @@ -1046,7 +1053,7 @@ class Activity(ExportedGObject): removed) removed_buddies = set() for handle in removed: - buddy = self._handle_to_buddy.pop(handle, None) + buddy = self._handle_to_buddy.get(handle, None) removed_buddies.add(buddy) # If we're not in the room yet, the "removal" may be spurious - # Gabble removes the inviter from members at the same time it adds @@ -1056,6 +1063,12 @@ class Activity(ExportedGObject): if self._joined: self._remove_buddies(removed_buddies) + # Remove buddies from self._handle_to_buddy now that we have emitted + # BuddyHandleLeft + for handle in removed: + buddy = self._handle_to_buddy.pop(handle, None) + self._buddy_to_handle.pop(buddy) + # if we were among those removed, we'll have to start believing # the spoofable PEP-based activity tracking again. if self._self_handle not in self._handle_to_buddy and self._joined: @@ -1103,6 +1116,7 @@ class Activity(ExportedGObject): self._add_buddies(new_buddies) self._handle_to_buddy = {} + self._buddy_to_handle = {} self._self_handle = None self._text_channel = None _logger.debug('Text channel closed') -- cgit v0.9.1