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