Removing @todo/@bugs because we are now on bugs.maemo.org
[theonering] / src / channel / contact_list.py
1 import logging
2
3 import telepathy
4
5 import tp
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 AllContactsListChannel(
15                 tp.ChannelTypeContactList,
16                 tp.ChannelInterfaceGroup,
17         ):
18         """
19         The group of contacts for whom you receive presence
20         """
21
22         def __init__(self, connection, manager, props, listHandle):
23                 tp.ChannelTypeContactList.__init__(self, connection, manager, props)
24                 tp.ChannelInterfaceGroup.__init__(self)
25
26                 self.__manager = manager
27                 self.__props = props
28                 self.__session = connection.session
29                 self.__listHandle = listHandle
30
31                 self._callback = coroutines.func_sink(
32                         coroutines.expand_positional(
33                                 self._on_contacts_refreshed
34                         )
35                 )
36                 self.__session.addressbook.updateSignalHandler.register_sink(
37                         self._callback
38                 )
39
40                 self.GroupFlagsChanged(0, 0)
41
42                 addressbook = connection.session.addressbook
43                 contacts = addressbook.get_numbers()
44                 self._process_refresh(addressbook, set(contacts), set())
45
46
47         @gtk_toolbox.log_exception(_moduleLogger)
48         def Close(self):
49                 self.close()
50
51         def close(self):
52                 _moduleLogger.debug("Closing contact list")
53                 self.__session.addressbook.updateSignalHandler.unregister_sink(
54                         self._callback
55                 )
56                 self._callback = None
57
58                 tp.ChannelTypeContactList.Close(self)
59                 self.remove_from_connection()
60
61         @gtk_toolbox.log_exception(_moduleLogger)
62         def GetLocalPendingMembersWithInfo(self):
63                 return []
64
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                 _moduleLogger.info(
71                         "%s Added: %r, Removed: %r" % (self.__listHandle.get_name(), len(added), len(removed))
72                 )
73                 connection = self._conn
74                 handlesAdded = [
75                         handle.create_handle(connection, "contact", contactNumber)
76                         for contactNumber in added
77                 ]
78                 handlesRemoved = [
79                         handle.create_handle(connection, "contact", contactNumber)
80                         for contactNumber in removed
81                 ]
82                 message = ""
83                 actor = 0
84                 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
85                 self.MembersChanged(
86                         message,
87                         handlesAdded, handlesRemoved,
88                         (), (),
89                         actor,
90                         reason,
91                 )
92
93
94 _LIST_TO_FACTORY = {
95         # The group of contacts for whom you receive presence
96         'subscribe': AllContactsListChannel,
97         # The group of contacts who may receive your presence
98         'publish': AllContactsListChannel,
99         # A group of contacts who are on the publish list but are temporarily
100         # disallowed from receiving your presence
101         # This doesn't make sense to support
102         'hide': None,
103         # A group of contacts who may send you messages
104         'allow': None,
105         # A group of contacts who may not send you messages
106         'deny': None,
107         # On protocols where the user's contacts are stored, this contact list
108         # contains all stored contacts regardless of subscription status.
109         'stored': AllContactsListChannel,
110 }
111
112
113 _SUPPORTED_LISTS = frozenset(
114         name
115         for name in _LIST_TO_FACTORY.iterkeys()
116         if name is not None
117 )
118
119
120 def create_contact_list_channel(connection, manager, props, h):
121         factory = _LIST_TO_FACTORY.get(h.get_name(), None)
122         if factory is None:
123                 raise telepathy.errors.NotCapable("Unsuported type %s" % h.get_name())
124         return factory(connection, manager, props, h)
125
126
127 def get_spported_lists():
128         return _SUPPORTED_LISTS