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