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