1 # telepathy-python - Base classes defining the interfaces of the Telepathy framework
3 # Copyright (C) 2009 Collabora Limited
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 from telepathy.errors import NotImplemented
21 from telepathy.interfaces import (CHANNEL_INTERFACE,
22 CHANNEL_TYPE_CONTACT_LIST,
25 class ChannelManager(object):
27 def __init__(self, connection):
28 self._conn = connection
30 self._requestable_channel_classes = dict()
31 self._channels = dict()
32 self._fixed_properties = dict()
33 self._available_properties = dict()
36 for channel_type in self._requestable_channel_classes:
37 for channel in self._channels[channel_type].values():
38 if channel._type == CHANNEL_TYPE_CONTACT_LIST:
39 channel.remove_from_connection()
43 def remove_channel(self, channel):
44 for channel_type in self._requestable_channel_classes:
45 for handle, chan in self._channels[channel_type].items():
47 del self._channels[channel_type][handle]
49 def _get_type_requested_handle(self, props):
50 type = props[CHANNEL_INTERFACE + '.ChannelType']
51 requested = props[CHANNEL_INTERFACE + '.Requested']
52 target_handle = int(props[CHANNEL_INTERFACE + '.TargetHandle'])
53 target_handle_type = int(props[CHANNEL_INTERFACE + '.TargetHandleType'])
55 handle = self._conn._handles[target_handle_type, target_handle]
57 return (type, requested, handle)
59 def channel_exists(self, props):
60 type, _, handle = self._get_type_requested_handle(props)
62 if type in self._channels:
63 if handle in self._channels[type]:
68 def channel_for_props(self, props, signal=True, **args):
69 type, _, handle = self._get_type_requested_handle(props)
71 if type not in self._requestable_channel_classes:
72 raise NotImplemented('Unknown channel type "%s"' % type)
74 if self.channel_exists(props):
75 return self._channels[type][handle]
77 channel = self._requestable_channel_classes[type](
80 self._conn.add_channels([channel], signal=signal)
81 self._channels[type][handle] = channel
85 def _implement_channel_class(self, type, make_channel, fixed, available):
86 self._requestable_channel_classes[type] = make_channel
87 self._channels.setdefault(type, {})
89 self._fixed_properties[type] = fixed
90 self._available_properties[type] = available
92 def get_requestable_channel_classes(self):
95 for channel_type in self._requestable_channel_classes:
96 retval.append((self._fixed_properties[channel_type],
97 self._available_properties[channel_type]))