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