1 # telepathy-python - Base classes defining the interfaces of the Telepathy framework
3 # Copyright (C) 2009-2010 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)
24 class ChannelManager(object):
25 def __init__(self, connection):
26 self._conn = connection
28 self._requestable_channel_classes = dict()
29 self._channels = dict()
30 self._fixed_properties = dict()
31 self._available_properties = dict()
34 """Close channel manager and all the existing channels."""
35 for channel_type in self._requestable_channel_classes:
36 for channels in self._channels[channel_type].values():
37 for channel in channels:
38 if channel._type == CHANNEL_TYPE_CONTACT_LIST:
39 channel.remove_from_connection()
43 def remove_channel(self, channel):
44 "Remove channel from the channel manager"
45 for channel_type in self._requestable_channel_classes:
46 for handle, channels in self._channels[channel_type].items():
47 if channel in channels :
48 channels.remove(channel)
50 def _get_type_requested_handle(self, props):
51 """Return the type, request and target handle from the requested
53 type = props[CHANNEL_INTERFACE + '.ChannelType']
54 requested = props[CHANNEL_INTERFACE + '.Requested']
55 target_handle = int(props[CHANNEL_INTERFACE + '.TargetHandle'])
56 target_handle_type = int(props[CHANNEL_INTERFACE + '.TargetHandleType'])
58 handle = self._conn._handles[target_handle_type, target_handle]
60 return (type, requested, handle)
62 def existing_channel(self, props):
63 """ Return a channel corresponding to theses properties if such
64 one exists, otherwhise return None. Default implementation will
65 return the last created channel of the same kind identified by
67 Connection Manager should subclass this function
68 to implement more appropriate behaviour. """
70 type, _, handle = self._get_type_requested_handle(props)
72 if type in self._channels:
73 if handle in self._channels[type]:
74 if len(self._channels[type][handle]) > 0:
75 return self._channels[type][handle][-1]
79 def channel_exists(self, props):
80 """ Return True if channel exist with theses props, False otherwhise"""
81 return self.existing_channel(props) != None
83 def create_channel_for_props(self, props, signal=True, **args):
84 """Create a new channel with theses properties"""
85 type, _, handle = self._get_type_requested_handle(props)
87 if type not in self._requestable_channel_classes:
88 raise NotImplemented('Unknown channel type "%s"' % type)
90 channel = self._requestable_channel_classes[type](
93 self._conn.add_channels([channel], signal=signal)
94 if type in self._channels:
95 self._channels[type].setdefault(handle, []).append(channel)
99 def channel_for_props(self, props, signal=True, **args):
100 channel = self.existing_channel(props)
101 """Return an existing channel with theses properties if it already
102 exists, otherwhise return a new one"""
106 return self.create_channel_for_props(props, signal, **args)
108 def _implement_channel_class(self, type, make_channel, fixed, available):
109 """Notify channel manager a channel with these properties can be created"""
110 self._requestable_channel_classes[type] = make_channel
111 self._channels.setdefault(type, {})
113 self._fixed_properties[type] = fixed
114 self._available_properties[type] = available
116 def get_requestable_channel_classes(self):
117 """Return all the channel types that can be created"""
120 for channel_type in self._requestable_channel_classes:
121 retval.append((self._fixed_properties[channel_type],
122 self._available_properties[channel_type]))