c753f0bb2f277ff5e90b4242aeccb1cc8fe1f85d
[theonering] / src / channel / contact_list.py
1 import logging
2
3 import telepathy
4
5 import util.go_utils as gobject_utils
6 import util.coroutines as coroutines
7 import handle
8
9
10 _moduleLogger = logging.getLogger("channel.contact_list")
11
12
13 class AbstractListChannel(
14                 telepathy.server.ChannelTypeContactList,
15                 telepathy.server.ChannelInterfaceGroup,
16         ):
17         "Abstract Contact List channels"
18
19         def __init__(self, connection, h):
20                 telepathy.server.ChannelTypeContactList.__init__(self, connection, h)
21                 telepathy.server.ChannelInterfaceGroup.__init__(self)
22
23                 self._session = connection.session
24
25
26 class AllContactsListChannel(AbstractListChannel):
27         """
28         The group of contacts for whom you receive presence
29         """
30
31         def __init__(self, connection, h):
32                 AbstractListChannel.__init__(self, connection, h)
33                 self._session.addressbook.updateSignalHandler.register_sink(
34                         self._on_contacts_refreshed
35                 )
36                 self.GroupFlagsChanged(0, 0)
37
38                 addressbook = connection.session.addressbook
39                 contacts = addressbook.get_contacts()
40                 self._process_refresh(addressbook, contacts, [])
41
42         @coroutines.func_sink
43         @coroutines.expand_positional
44         @gobject_utils.async
45         def _on_contacts_refreshed(self, addressbook, added, removed, changed):
46                 """
47                 @todo This currently filters out people not yet added to the contact
48                         list.  Something needs to be done about those
49                 @todo This currently does not handle people with multiple phone
50                         numbers, yay that'll be annoying to resolve
51                 """
52                 self._process_refresh(addressbook, added, removed)
53
54         def _process_refresh(self, addressbook, added, removed):
55                 connection = self._conn
56                 handlesAdded = [
57                         handle.create_handle(connection, "contact", contactId, phoneNumber)
58                         for contactId in added
59                         if contactId
60                         for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
61                 ]
62                 handlesRemoved = [
63                         handle.create_handle(connection, "contact", contactId, phoneNumber)
64                         for contactId in removed
65                         if contactId
66                         for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
67                 ]
68                 message = ""
69                 actor = 0
70                 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
71                 self.MembersChanged(
72                         message,
73                         handlesAdded, handlesRemoved,
74                         (), (),
75                         actor,
76                         reason,
77                 )
78
79
80 def create_contact_list_channel(connection, h):
81         if h.get_name() == 'subscribe':
82                 # The group of contacts for whom you receive presence
83                 ChannelClass = AllContactsListChannel
84         elif h.get_name() == 'publish':
85                 # The group of contacts who may receive your presence
86                 ChannelClass = AllContactsListChannel
87         elif h.get_name() == 'hide':
88                 # A group of contacts who are on the publish list but are temporarily
89                 # disallowed from receiving your presence
90                 # This doesn't make sense to support
91                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
92         elif h.get_name() == 'allow':
93                 # A group of contacts who may send you messages
94                 # @todo Allow-List would be cool to support
95                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
96         elif h.get_name() == 'deny':
97                 # A group of contacts who may not send you messages
98                 # @todo Deny-List would be cool to support
99                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
100         elif h.get_name() == 'stored':
101                 # On protocols where the user's contacts are stored, this contact list
102                 # contains all stored contacts regardless of subscription status.
103                 ChannelClass = AllContactsListChannel
104         else:
105                 raise TypeError("Unknown list type : " + h.get_name())
106         return ChannelClass(connection, h)
107
108