Lessons learned from looking at telepathy-python to better understand what it was...
[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         @coroutines.func_sink
39         @coroutines.expand_positional
40         @gobject_utils.async
41         def _on_contacts_refreshed(self, addressbook, added, removed, changed):
42                 """
43                 @todo This currently filters out people not yet added to the contact
44                         list.  Something needs to be done about those
45                 @todo This currently does not handle people with multiple phone
46                         numbers, yay that'll be annoying to resolve
47                 """
48                 connection = self._conn
49                 handlesAdded = [
50                         handle.create_handle(connection, "contact", contactId)
51                         for contactId in added
52                         if contactId
53                 ]
54                 handlesRemoved = [
55                         handle.create_handle(connection, "contact", contactId)
56                         for contactId in removed
57                         if contactId
58                 ]
59                 message = ""
60                 actor = 0
61                 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
62                 self.MembersChanged(
63                         message,
64                         handlesAdded, handlesRemoved,
65                         (), (),
66                         actor,
67                         reason,
68                 )
69
70
71 def create_contact_list_channel(connection, h):
72         if h.get_name() == 'subscribe':
73                 # The group of contacts for whom you receive presence
74                 ChannelClass = AllContactsListChannel
75         elif h.get_name() == 'publish':
76                 # The group of contacts who may receive your presence
77                 ChannelClass = AllContactsListChannel
78         elif h.get_name() == 'hide':
79                 # A group of contacts who are on the publish list but are temporarily
80                 # disallowed from receiving your presence
81                 # This doesn't make sense to support
82                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
83         elif h.get_name() == 'allow':
84                 # A group of contacts who may send you messages
85                 # @todo Allow-List would be cool to support
86                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
87         elif h.get_name() == 'deny':
88                 # A group of contacts who may not send you messages
89                 # @todo Deny-List would be cool to support
90                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
91         elif h.get_name() == 'stored':
92                 # On protocols where the user's contacts are stored, this contact list
93                 # contains all stored contacts regardless of subscription status.
94                 ChannelClass = AllContactsListChannel
95         else:
96                 raise TypeError("Unknown list type : " + h.get_name())
97         return ChannelClass(connection, h)
98
99