Added detection of missed calls. As part of this I moved some of the connections...
[theonering] / src / tp / channelmanager.py
1 # telepathy-python - Base classes defining the interfaces of the Telepathy framework
2 #
3 # Copyright (C) 2009-2010 Collabora Limited
4 #
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.
9 #
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.
14 #
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
18
19 from telepathy.errors import NotImplemented
20
21 from telepathy.interfaces import (CHANNEL_INTERFACE,
22                                  CHANNEL_TYPE_CONTACT_LIST)
23
24 class ChannelManager(object):
25     def __init__(self, connection):
26         self._conn = connection
27
28         self._requestable_channel_classes = dict()
29         self._channels = dict()
30         self._fixed_properties = dict()
31         self._available_properties = dict()
32
33     def close(self):
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()
40                     else:
41                         channel.Close()
42
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)
49
50     def _get_type_requested_handle(self, props):
51         """Return the type, request and target handle from the requested
52         properties"""
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'])
57
58         handle = self._conn._handles[target_handle_type, target_handle]
59
60         return (type, requested, handle)
61
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
66         handle and type.
67         Connection Manager should subclass this function
68         to implement more appropriate behaviour. """
69
70         type, _, handle = self._get_type_requested_handle(props)
71
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]
76
77         return None
78
79     def channel_exists(self, props):
80         """ Return True if channel exist with theses props, False otherwhise"""
81         return self.existing_channel(props) != None
82
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)
86
87         if type not in self._requestable_channel_classes:
88             raise NotImplemented('Unknown channel type "%s"' % type)
89
90         channel = self._requestable_channel_classes[type](
91             props, **args)
92
93         self._conn.add_channels([channel], signal=signal)
94         if type in self._channels:
95             self._channels[type].setdefault(handle, []).append(channel)
96
97         return channel
98
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"""
103         if channel:
104             return channel
105         else:
106             return self.create_channel_for_props(props, signal, **args)
107
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, {})
112
113         self._fixed_properties[type] = fixed
114         self._available_properties[type] = available
115
116     def get_requestable_channel_classes(self):
117         """Return all the channel types that can be created"""
118         retval = []
119
120         for channel_type in self._requestable_channel_classes:
121             retval.append((self._fixed_properties[channel_type],
122                 self._available_properties[channel_type]))
123
124         return retval