5 import util.coroutines as coroutines
10 _moduleLogger = logging.getLogger("channel.contact_list")
13 class AllContactsListChannel(
14 telepathy.server.ChannelTypeContactList,
15 telepathy.server.ChannelInterfaceGroup,
18 The group of contacts for whom you receive presence
21 def __init__(self, connection, manager, props, h):
23 # HACK Older python-telepathy way
24 telepathy.server.ChannelTypeContactList.__init__(self, connection, h)
25 self._requested = props[telepathy.interfaces.CHANNEL_INTERFACE + '.Requested']
26 self._implement_property_get(
27 telepathy.interfaces.CHANNEL_INTERFACE,
28 {"Requested": lambda: self._requested}
31 # HACK Newer python-telepathy way
32 telepathy.server.ChannelTypeContactList.__init__(self, connection, manager, props)
33 telepathy.server.ChannelInterfaceGroup.__init__(self)
35 self.__manager = manager
37 self.__session = connection.session
39 # HACK Older python-telepathy doesn't provide this
40 self._immutable_properties = {
41 'ChannelType': telepathy.server.interfaces.CHANNEL_INTERFACE,
42 'TargetHandle': telepathy.server.interfaces.CHANNEL_INTERFACE,
43 'Interfaces': telepathy.server.interfaces.CHANNEL_INTERFACE,
44 'TargetHandleType': telepathy.server.interfaces.CHANNEL_INTERFACE,
45 'TargetID': telepathy.server.interfaces.CHANNEL_INTERFACE,
46 'Requested': telepathy.server.interfaces.CHANNEL_INTERFACE
49 self._callback = coroutines.func_sink(
50 coroutines.expand_positional(
51 self._on_contacts_refreshed
54 self.__session.addressbook.updateSignalHandler.register_sink(
58 self.GroupFlagsChanged(0, 0)
60 addressbook = connection.session.addressbook
61 contacts = addressbook.get_contacts()
62 self._process_refresh(addressbook, contacts, [])
65 # HACK Older python-telepathy doesn't provide this
67 for prop, iface in self._immutable_properties.items():
68 props[iface + '.' + prop] = \
69 self._prop_getters[iface][prop]()
72 @gtk_toolbox.log_exception(_moduleLogger)
77 self.__session.addressbook.updateSignalHandler.unregister_sink(
82 telepathy.server.ChannelTypeContactList.Close(self)
83 if self.__manager.channel_exists(self.__props):
84 # HACK Older python-telepathy requires doing this manually
85 self.__manager.remove_channel(self)
86 self.remove_from_connection()
88 @gtk_toolbox.log_exception(_moduleLogger)
89 def _on_contacts_refreshed(self, addressbook, added, removed, changed):
90 self._process_refresh(addressbook, added, removed)
92 def _process_refresh(self, addressbook, added, removed):
93 connection = self._conn
95 handle.create_handle(connection, "contact", contactId, phoneNumber)
96 for contactId in added
97 for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
100 handle.create_handle(connection, "contact", contactId, phoneNumber)
101 for contactId in removed
102 for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
106 reason = telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
109 handlesAdded, handlesRemoved,
116 def create_contact_list_channel(connection, manager, props, h):
117 if h.get_name() == 'subscribe':
118 # The group of contacts for whom you receive presence
119 ChannelClass = AllContactsListChannel
120 elif h.get_name() == 'publish':
121 # The group of contacts who may receive your presence
122 ChannelClass = AllContactsListChannel
123 elif h.get_name() == 'hide':
124 # A group of contacts who are on the publish list but are temporarily
125 # disallowed from receiving your presence
126 # This doesn't make sense to support
127 _moduleLogger.warn("Unsuported type %s" % h.get_name())
128 elif h.get_name() == 'allow':
129 # A group of contacts who may send you messages
130 # @todo Allow-List would be cool to support
131 _moduleLogger.warn("Unsuported type %s" % h.get_name())
132 elif h.get_name() == 'deny':
133 # A group of contacts who may not send you messages
134 # @todo Deny-List would be cool to support
135 _moduleLogger.warn("Unsuported type %s" % h.get_name())
136 elif h.get_name() == 'stored':
137 # On protocols where the user's contacts are stored, this contact list
138 # contains all stored contacts regardless of subscription status.
139 ChannelClass = AllContactsListChannel
141 raise TypeError("Unknown list type : " + h.get_name())
142 return ChannelClass(connection, manager, props, h)