cd7175ac298bb8188d870dfe3eefbf02bb42d0e9
[theonering] / src / channel / contact_list.py
1 import logging
2
3 import telepathy
4
5 import tp
6 import util.coroutines as coroutines
7 import gtk_toolbox
8 import handle
9
10
11 _moduleLogger = logging.getLogger("channel.contact_list")
12
13
14 class AllContactsListChannel(
15                 tp.ChannelTypeContactList,
16                 tp.ChannelInterfaceGroup,
17         ):
18         """
19         The group of contacts for whom you receive presence
20
21         @bug On Maemo 5 this isn't even being created, I think
22         """
23
24         def __init__(self, connection, manager, props, listHandle):
25                 tp.ChannelTypeContactList.__init__(self, connection, manager, props)
26                 tp.ChannelInterfaceGroup.__init__(self)
27
28                 self.__manager = manager
29                 self.__props = props
30                 self.__session = connection.session
31                 self.__listHandle = listHandle
32
33                 self._callback = coroutines.func_sink(
34                         coroutines.expand_positional(
35                                 self._on_contacts_refreshed
36                         )
37                 )
38                 self.__session.addressbook.updateSignalHandler.register_sink(
39                         self._callback
40                 )
41
42                 self.GroupFlagsChanged(0, 0)
43
44                 addressbook = connection.session.addressbook
45                 contacts = addressbook.get_contact_ids()
46                 self._process_refresh(addressbook, set(contacts), set())
47
48
49         @gtk_toolbox.log_exception(_moduleLogger)
50         def Close(self):
51                 self.close()
52
53         def close(self):
54                 self.__session.addressbook.updateSignalHandler.unregister_sink(
55                         self._callback
56                 )
57                 self._callback = None
58
59                 tp.ChannelTypeContactList.Close(self)
60                 self.remove_from_connection()
61
62         @gtk_toolbox.log_exception(_moduleLogger)
63         def _on_contacts_refreshed(self, addressbook, added, removed, changed):
64                 self._process_refresh(addressbook, added, removed)
65
66         def _process_refresh(self, addressbook, added, removed):
67                 _moduleLogger.info(
68                         "%s Added: %r, Removed: %r" % (self.__listHandle.get_name(), len(added), len(removed))
69                 )
70                 connection = self._conn
71                 handlesAdded = [
72                         handle.create_handle(connection, "contact", contactId, phoneNumber)
73                         for contactId in added
74                         for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
75                 ]
76                 handlesRemoved = [
77                         handle.create_handle(connection, "contact", contactId, phoneNumber)
78                         for contactId in removed
79                         for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
80                 ]
81                 message = ""
82                 actor = 0
83                 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
84                 self.MembersChanged(
85                         message,
86                         handlesAdded, handlesRemoved,
87                         (), (),
88                         actor,
89                         reason,
90                 )
91
92
93 _LIST_TO_FACTORY = {
94         # The group of contacts for whom you receive presence
95         'subscribe': AllContactsListChannel,
96         # The group of contacts who may receive your presence
97         'publish': AllContactsListChannel,
98         # A group of contacts who are on the publish list but are temporarily
99         # disallowed from receiving your presence
100         # This doesn't make sense to support
101         'hide': None,
102         # A group of contacts who may send you messages
103         # @todo Allow-List would be cool to support
104         'allow': None,
105         # A group of contacts who may not send you messages
106         # @todo Deny-List would be cool to support
107         'deny': None,
108         # On protocols where the user's contacts are stored, this contact list
109         # contains all stored contacts regardless of subscription status.
110         'stored': AllContactsListChannel,
111 }
112
113
114 _SUPPORTED_LISTS = frozenset(
115         name
116         for name in _LIST_TO_FACTORY.iterkeys()
117         if name is not None
118 )
119
120
121 def create_contact_list_channel(connection, manager, props, h):
122         factory = _LIST_TO_FACTORY.get(h.get_name(), None)
123         if factory is None:
124                 raise telepathy.errors.NotCapable("Unsuported type %s" % h.get_name())
125         return factory(connection, manager, props, h)
126
127
128 def get_spported_lists():
129         return _SUPPORTED_LISTS