Making phone numbers as consistent as possible so user started conversations don...
[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, h):
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
32                 self._callback = coroutines.func_sink(
33                         coroutines.expand_positional(
34                                 self._on_contacts_refreshed
35                         )
36                 )
37                 self.__session.addressbook.updateSignalHandler.register_sink(
38                         self._callback
39                 )
40
41                 self.GroupFlagsChanged(0, 0)
42
43                 addressbook = connection.session.addressbook
44                 contacts = addressbook.get_contact_ids()
45                 self._process_refresh(addressbook, set(contacts), set())
46
47
48         @gtk_toolbox.log_exception(_moduleLogger)
49         def Close(self):
50                 self.close()
51
52         def close(self):
53                 self.__session.addressbook.updateSignalHandler.unregister_sink(
54                         self._callback
55                 )
56                 self._callback = None
57
58                 tp.ChannelTypeContactList.Close(self)
59                 self.remove_from_connection()
60
61         @gtk_toolbox.log_exception(_moduleLogger)
62         def _on_contacts_refreshed(self, addressbook, added, removed, changed):
63                 self._process_refresh(addressbook, added, removed)
64
65         def _process_refresh(self, addressbook, added, removed):
66                 _moduleLogger.info("Added: %r, Removed: %r" % (len(added), len(removed)))
67                 connection = self._conn
68                 handlesAdded = [
69                         handle.create_handle(connection, "contact", contactId, phoneNumber)
70                         for contactId in added
71                         for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
72                 ]
73                 handlesRemoved = [
74                         handle.create_handle(connection, "contact", contactId, phoneNumber)
75                         for contactId in removed
76                         for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
77                 ]
78                 message = ""
79                 actor = 0
80                 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
81                 self.MembersChanged(
82                         message,
83                         handlesAdded, handlesRemoved,
84                         (), (),
85                         actor,
86                         reason,
87                 )
88
89
90 def create_contact_list_channel(connection, manager, props, h):
91         if h.get_name() == 'subscribe':
92                 # The group of contacts for whom you receive presence
93                 ChannelClass = AllContactsListChannel
94         elif h.get_name() == 'publish':
95                 # The group of contacts who may receive your presence
96                 ChannelClass = AllContactsListChannel
97         elif h.get_name() == 'hide':
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                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
102         elif h.get_name() == 'allow':
103                 # A group of contacts who may send you messages
104                 # @todo Allow-List would be cool to support
105                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
106         elif h.get_name() == 'deny':
107                 # A group of contacts who may not send you messages
108                 # @todo Deny-List would be cool to support
109                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
110         elif h.get_name() == 'stored':
111                 # On protocols where the user's contacts are stored, this contact list
112                 # contains all stored contacts regardless of subscription status.
113                 ChannelClass = AllContactsListChannel
114         else:
115                 raise TypeError("Unknown list type : " + h.get_name())
116         return ChannelClass(connection, manager, props, h)
117
118