6 import util.coroutines as coroutines
11 _moduleLogger = logging.getLogger("channel.contact_list")
14 class AllContactsListChannel(
15 tp.ChannelTypeContactList,
16 tp.ChannelInterfaceGroup,
19 The group of contacts for whom you receive presence
22 def __init__(self, connection, manager, props, listHandle):
23 tp.ChannelTypeContactList.__init__(self, connection, manager, props)
24 tp.ChannelInterfaceGroup.__init__(self)
26 self.__manager = manager
28 self.__session = connection.session
29 self.__listHandle = listHandle
30 self.__members = set()
32 self._callback = coroutines.func_sink(
33 coroutines.expand_positional(
34 self._on_contacts_refreshed
37 self.__session.addressbook.updateSignalHandler.register_sink(
41 self.GroupFlagsChanged(0, 0)
43 addressbook = connection.session.addressbook
44 contacts = addressbook.get_numbers()
45 self._process_refresh(addressbook, set(contacts), set(), set())
48 @gtk_toolbox.log_exception(_moduleLogger)
53 _moduleLogger.debug("Closing contact list")
54 self.__session.addressbook.updateSignalHandler.unregister_sink(
59 tp.ChannelTypeContactList.Close(self)
60 self.remove_from_connection()
62 @gtk_toolbox.log_exception(_moduleLogger)
63 def GetLocalPendingMembersWithInfo(self):
66 @gtk_toolbox.log_exception(_moduleLogger)
67 def _on_contacts_refreshed(self, addressbook, added, removed, changed):
68 self._process_refresh(addressbook, added, removed, changed)
70 def _is_on_list(self, number):
73 def _is_on_list_changed(self, number):
74 return (number in self.__members) ^ (self._is_on_list(number))
76 def _is_now_on_list(self, number):
77 return number not in self.__members and self._is_on_list(number)
79 def _was_on_list(self, number):
80 return number in self.__members and not self._is_on_list(number)
82 def _process_refresh(self, addressbook, added, removed, changed):
84 "%s Added: %r, Removed: %r" % (self.__listHandle.get_name(), len(added), len(removed))
86 connection = self._conn
88 # convert changed into added/removed
92 if self._is_now_on_list(number)
97 if self._was_on_list(number)
100 # Merge the added/removed with changed
103 for contactNumber in added
104 if self._is_on_list(contactNumber)
106 added.extend(alsoAdded)
109 handle.create_handle(connection, "contact", contactNumber)
110 for contactNumber in added
112 self.__members.union(added)
114 removed = list(removed)
115 removed.extend(alsoRemoved)
117 handle.create_handle(connection, "contact", contactNumber)
118 for contactNumber in removed
120 self.__members.difference(removed)
124 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
127 handlesAdded, handlesRemoved,
134 class DenyContactsListChannel(AllContactsListChannel):
136 def _is_on_list(self, number):
137 return self._conn.session.addressbook.is_blocked(number)
141 # The group of contacts for whom you receive presence
142 'subscribe': AllContactsListChannel,
143 # The group of contacts who may receive your presence
144 'publish': AllContactsListChannel,
145 # A group of contacts who are on the publish list but are temporarily
146 # disallowed from receiving your presence
147 # This doesn't make sense to support
149 # A group of contacts who may send you messages
150 # Is this meant to serve as a definitive white list for all contact?
152 # A group of contacts who may not send you messages
153 'deny': DenyContactsListChannel,
154 # On protocols where the user's contacts are stored, this contact list
155 # contains all stored contacts regardless of subscription status.
156 'stored': AllContactsListChannel,
160 _SUPPORTED_LISTS = frozenset(
162 for name in _LIST_TO_FACTORY.iterkeys()
167 def create_contact_list_channel(connection, manager, props, h):
168 factory = _LIST_TO_FACTORY.get(h.get_name(), None)
170 raise telepathy.errors.NotCapable("Unsuported type %s" % h.get_name())
171 return factory(connection, manager, props, h)
174 def get_spported_lists():
175 return _SUPPORTED_LISTS