9 _moduleLogger = logging.getLogger("channel_manager")
12 class TelepathyChannelManager(object):
14 def __init__(self, connection):
15 self._conn = connection
17 self._requestable_channel_classes = dict()
18 self._channels = dict()
19 self._fixed_properties = dict()
20 self._available_properties = dict()
23 for channel_type in self._requestable_channel_classes:
24 for chan in self._channels[channel_type].values():
26 _moduleLogger.info("Closing %s %s" % (channel_type, chan._object_path))
29 _moduleLogger.exception("Shutting down %r" % (chan, ))
31 def remove_channel(self, chan):
32 for channel_type in self._requestable_channel_classes:
33 for handle, ichan in self._channels[channel_type].items():
35 del self._channels[channel_type][handle]
37 def _get_type_requested_handle(self, props):
38 type = props[telepathy.interfaces.CHANNEL_INTERFACE + '.ChannelType']
39 requested = props[telepathy.interfaces.CHANNEL_INTERFACE + '.Requested']
40 target_handle = props[telepathy.interfaces.CHANNEL_INTERFACE + '.TargetHandle']
41 target_handle_type = props[telepathy.interfaces.CHANNEL_INTERFACE + '.TargetHandleType']
43 handle = self._conn._handles[target_handle_type, target_handle]
45 return (type, requested, handle)
47 def channel_exists(self, props):
48 type, _, handle = self._get_type_requested_handle(props)
50 if type in self._channels:
51 if handle in self._channels[type]:
56 def channel_for_props(self, props, signal=True, **args):
57 type, suppress_handler, handle = self._get_type_requested_handle(props)
59 if type not in self._requestable_channel_classes:
60 raise NotImplemented('Unknown channel type "%s"' % type)
62 if self.channel_exists(props):
63 return self._channels[type][handle]
65 chan = self._requestable_channel_classes[type](props, **args)
67 if hasattr(self._conn, "add_channels"):
68 # HACK Newer python-telepathy
69 self._conn.add_channels([chan], signal=signal)
70 elif hasattr(self._conn, "add_channel"):
71 # HACK Older python-telepathy
72 self._conn.add_channel(chan, handle, suppress_handler)
74 raise RuntimeError("Uhh, what just happened with the connection")
75 self._channels[type][handle] = chan
79 def _implement_channel_class(self, type, make_channel, fixed, available):
80 self._requestable_channel_classes[type] = make_channel
81 self._channels.setdefault(type, {})
83 self._fixed_properties[type] = fixed
84 self._available_properties[type] = available
86 def get_requestable_channel_classes(self):
89 for channel_type in self._requestable_channel_classes:
90 retval.append((self._fixed_properties[channel_type],
91 self._available_properties[channel_type]))
96 class ChannelManager(TelepathyChannelManager):
98 def __init__(self, connection):
99 TelepathyChannelManager.__init__(self, connection)
102 telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_TEXT,
103 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_CONTACT)
105 self._implement_channel_class(
106 telepathy.CHANNEL_TYPE_TEXT,
107 self._get_text_channel,
113 telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_CONTACT_LIST
115 self._implement_channel_class(
116 telepathy.CHANNEL_TYPE_CONTACT_LIST,
117 self._get_list_channel,
123 telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
124 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_CONTACT)
126 self._implement_channel_class(
127 telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
128 self._get_media_channel,
130 [telepathy.CHANNEL_INTERFACE + '.TargetHandle']
133 def _get_list_channel(self, props):
134 _, surpress_handler, handle = self._get_type_requested_handle(props)
136 _moduleLogger.debug('New contact list channel')
137 chan = channel.contact_list.create_contact_list_channel(self._conn, self, props, handle)
140 def _get_text_channel(self, props):
141 _, surpress_handler, handle = self._get_type_requested_handle(props)
143 _moduleLogger.debug('New text channel')
144 chan = channel.text.TextChannel(self._conn, self, props, handle)
147 def _get_media_channel(self, props):
148 _, surpress_handler, handle = self._get_type_requested_handle(props)
150 _moduleLogger.debug('New media channel')
151 chan = channel.call.CallChannel(self._conn, self, props, handle)