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