X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fchannel%2Fcontact_list.py;h=e184d2d6ddb97092530d8697205138a86ae9c8dc;hp=111ae2a77af7e5cf2202d170fcba8e1f4d0ae03c;hb=1c892d1b9bf14b28eb54ce3590ed2ee29d5e3d25;hpb=2d6e3e256844637d566644e7b481dfdaad62c26d diff --git a/src/channel/contact_list.py b/src/channel/contact_list.py index 111ae2a..e184d2d 100644 --- a/src/channel/contact_list.py +++ b/src/channel/contact_list.py @@ -2,92 +2,127 @@ import logging import telepathy -import util.go_utils as gobject_utils +import tp import util.coroutines as coroutines -import gtk_toolbox +import util.misc as misc_utils import handle _moduleLogger = logging.getLogger("channel.contact_list") -class AbstractListChannel( - telepathy.server.ChannelTypeContactList, - telepathy.server.ChannelInterfaceGroup, +class AllContactsListChannel( + tp.ChannelTypeContactList, + tp.ChannelInterfaceGroup, ): - "Abstract Contact List channels" - - def __init__(self, connection, manager, props, h): - self._manager = manager - self._props = props - - try: - # Older python-telepathy way - telepathy.server.ChannelTypeContactList.__init__(self, connection, h) - except TypeError: - # Newer python-telepathy way - telepathy.server.ChannelTypeContactList.__init__(self, connection, manager, props) - telepathy.server.ChannelInterfaceGroup.__init__(self) - - self._session = connection.session - - -class AllContactsListChannel(AbstractListChannel): """ The group of contacts for whom you receive presence """ - def __init__(self, connection, manager, props, h): - AbstractListChannel.__init__(self, connection, manager, props, h) - - self._callback = coroutines.func_sink( - coroutines.expand_positional( - self._on_contacts_refreshed + def __init__(self, connection, manager, props, listHandle): + tp.ChannelTypeContactList.__init__(self, connection, manager, props) + tp.ChannelInterfaceGroup.__init__(self) + + self.__manager = manager + self.__props = props + self.__session = connection.session + self.__listHandle = listHandle + self.__members = set() + + if self._conn.options.useGVContacts: + self._callback = coroutines.func_sink( + coroutines.expand_positional( + self._on_contacts_refreshed + ) ) - ) - self._session.addressbook.updateSignalHandler.register_sink( - self._callback - ) + self.__session.addressbook.updateSignalHandler.register_sink( + self._callback + ) + + addressbook = connection.session.addressbook + contacts = addressbook.get_numbers() + self._process_refresh(addressbook, set(contacts), set(), set()) + else: + self._callback = None self.GroupFlagsChanged(0, 0) - addressbook = connection.session.addressbook - contacts = addressbook.get_contacts() - self._process_refresh(addressbook, contacts, []) - @gtk_toolbox.log_exception(_moduleLogger) + @misc_utils.log_exception(_moduleLogger) def Close(self): self.close() def close(self): - self._session.addressbook.updateSignalHandler.unregister_sink( - self._callback - ) - self._callback = None + _moduleLogger.debug("Closing contact list") + if self._callback is not None: + self.__session.addressbook.updateSignalHandler.unregister_sink( + self._callback + ) + self._callback = None - telepathy.server.ChannelTypeContactList.Close(self) - if self._manager.channel_exists(self._props): - # Older python-telepathy requires doing this manually - self._manager.remove_channel(self) + tp.ChannelTypeContactList.Close(self) self.remove_from_connection() - @gobject_utils.async - @gtk_toolbox.log_exception(_moduleLogger) + @misc_utils.log_exception(_moduleLogger) + def GetLocalPendingMembersWithInfo(self): + return [] + + @misc_utils.log_exception(_moduleLogger) def _on_contacts_refreshed(self, addressbook, added, removed, changed): - self._process_refresh(addressbook, added, removed) + self._process_refresh(addressbook, added, removed, changed) + + def _is_on_list(self, number): + return True + + def _is_on_list_changed(self, number): + return (number in self.__members) ^ (self._is_on_list(number)) + + def _is_now_on_list(self, number): + return number not in self.__members and self._is_on_list(number) - def _process_refresh(self, addressbook, added, removed): + def _was_on_list(self, number): + return number in self.__members and not self._is_on_list(number) + + def _process_refresh(self, addressbook, added, removed, changed): + _moduleLogger.info( + "%s Added: %r, Removed: %r" % (self.__listHandle.get_name(), len(added), len(removed)) + ) connection = self._conn + + # convert changed into added/removed + alsoAdded = set( + number + for number in changed + if self._is_now_on_list(number) + ) + alsoRemoved = set( + number + for number in changed + if self._was_on_list(number) + ) + + # Merge the added/removed with changed + added = [ + contactNumber + for contactNumber in added + if self._is_on_list(contactNumber) + ] + added.extend(alsoAdded) + added.sort() handlesAdded = [ - handle.create_handle(connection, "contact", contactId, phoneNumber) - for contactId in added - for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId) + handle.create_handle(connection, "contact", contactNumber) + for contactNumber in added ] + self.__members.union(added) + + removed = list(removed) + removed.extend(alsoRemoved) handlesRemoved = [ - handle.create_handle(connection, "contact", contactId, phoneNumber) - for contactId in removed - for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId) + handle.create_handle(connection, "contact", contactNumber) + for contactNumber in removed ] + self.__members.difference(removed) + message = "" actor = 0 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE @@ -100,32 +135,45 @@ class AllContactsListChannel(AbstractListChannel): ) -def create_contact_list_channel(connection, h): - if h.get_name() == 'subscribe': - # The group of contacts for whom you receive presence - ChannelClass = AllContactsListChannel - elif h.get_name() == 'publish': - # The group of contacts who may receive your presence - ChannelClass = AllContactsListChannel - elif h.get_name() == 'hide': - # A group of contacts who are on the publish list but are temporarily - # disallowed from receiving your presence - # This doesn't make sense to support - _moduleLogger.warn("Unsuported type %s" % h.get_name()) - elif h.get_name() == 'allow': - # A group of contacts who may send you messages - # @todo Allow-List would be cool to support - _moduleLogger.warn("Unsuported type %s" % h.get_name()) - elif h.get_name() == 'deny': - # A group of contacts who may not send you messages - # @todo Deny-List would be cool to support - _moduleLogger.warn("Unsuported type %s" % h.get_name()) - elif h.get_name() == 'stored': - # On protocols where the user's contacts are stored, this contact list - # contains all stored contacts regardless of subscription status. - ChannelClass = AllContactsListChannel - else: - raise TypeError("Unknown list type : " + h.get_name()) - return ChannelClass(connection, h) +class DenyContactsListChannel(AllContactsListChannel): + + def _is_on_list(self, number): + return self._conn.session.addressbook.is_blocked(number) + + +_LIST_TO_FACTORY = { + # The group of contacts for whom you receive presence + 'subscribe': AllContactsListChannel, + # The group of contacts who may receive your presence + 'publish': AllContactsListChannel, + # A group of contacts who are on the publish list but are temporarily + # disallowed from receiving your presence + # This doesn't make sense to support + 'hide': None, + # A group of contacts who may send you messages + # Is this meant to serve as a definitive white list for all contact? + 'allow': None, + # A group of contacts who may not send you messages + 'deny': DenyContactsListChannel, + # On protocols where the user's contacts are stored, this contact list + # contains all stored contacts regardless of subscription status. + 'stored': AllContactsListChannel, +} + + +_SUPPORTED_LISTS = frozenset( + name + for name in _LIST_TO_FACTORY.iterkeys() + if name is not None +) + + +def create_contact_list_channel(connection, manager, props, h): + factory = _LIST_TO_FACTORY.get(h.get_name(), None) + if factory is None: + raise telepathy.errors.NotCapable("Unsuported type %s" % h.get_name()) + return factory(connection, manager, props, h) +def get_spported_lists(): + return _SUPPORTED_LISTS