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