7 import util.misc as util_misc
10 _moduleLogger = logging.getLogger("channel_manager")
13 class TelepathyChannelManager(object):
15 def __init__(self, connection):
16 self._conn = connection
18 self._requestable_channel_classes = dict()
19 self._channels = dict()
20 self._fixed_properties = dict()
21 self._available_properties = dict()
24 for channel_type in self._requestable_channel_classes:
25 for chan in self._channels[channel_type].values():
27 _moduleLogger.info("Closing %s %s" % (channel_type, chan._object_path))
30 _moduleLogger.exception("Shutting down %r" % (chan, ))
32 def remove_channel(self, chan):
33 for channel_type in self._requestable_channel_classes:
34 for handle, ichan in self._channels[channel_type].items():
36 del self._channels[channel_type][handle]
38 def _get_type_requested_handle(self, props):
39 type = props[telepathy.interfaces.CHANNEL_INTERFACE + '.ChannelType']
40 requested = props[telepathy.interfaces.CHANNEL_INTERFACE + '.Requested']
41 target_handle = props[telepathy.interfaces.CHANNEL_INTERFACE + '.TargetHandle']
42 target_handle_type = props[telepathy.interfaces.CHANNEL_INTERFACE + '.TargetHandleType']
44 handle = self._conn._handles[target_handle_type, target_handle]
46 return (type, requested, handle)
48 def channel_exists(self, props):
49 type, _, handle = self._get_type_requested_handle(props)
51 if type in self._channels:
52 if handle in self._channels[type]:
57 def channel_for_props(self, props, signal=True, **args):
58 type, suppress_handler, handle = self._get_type_requested_handle(props)
60 if type not in self._requestable_channel_classes:
61 raise NotImplemented('Unknown channel type "%s"' % type)
63 if self.channel_exists(props):
64 return self._channels[type][handle]
66 chan = self._requestable_channel_classes[type](props, **args)
68 if hasattr(self._conn, "add_channels"):
69 # HACK Newer python-telepathy
70 self._conn.add_channels([chan], signal=signal)
71 elif hasattr(self._conn, "add_channel"):
72 # HACK Older python-telepathy
73 self._conn.add_channel(chan, handle, suppress_handler)
75 raise RuntimeError("Uhh, what just happened with the connection")
76 self._channels[type][handle] = chan
80 def _implement_channel_class(self, type, make_channel, fixed, available):
81 self._requestable_channel_classes[type] = make_channel
82 self._channels.setdefault(type, {})
84 self._fixed_properties[type] = fixed
85 self._available_properties[type] = available
87 def get_requestable_channel_classes(self):
90 for channel_type in self._requestable_channel_classes:
91 retval.append((self._fixed_properties[channel_type],
92 self._available_properties[channel_type]))
97 class ChannelManager(TelepathyChannelManager):
99 def __init__(self, connection):
100 TelepathyChannelManager.__init__(self, connection)
103 telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_TEXT,
104 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_CONTACT)
106 self._implement_channel_class(
107 telepathy.CHANNEL_TYPE_TEXT,
108 self._get_text_channel,
114 telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_CONTACT_LIST
116 self._implement_channel_class(
117 telepathy.CHANNEL_TYPE_CONTACT_LIST,
118 self._get_list_channel,
124 telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
125 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_CONTACT)
127 self._implement_channel_class(
128 telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
129 self._get_media_channel,
131 [telepathy.CHANNEL_INTERFACE + '.TargetHandle']
134 def _get_list_channel(self, props):
135 _, surpress_handler, h = self._get_type_requested_handle(props)
137 _moduleLogger.debug('New contact list channel')
138 chan = channel.contact_list.create_contact_list_channel(self._conn, self, props, h)
141 def _get_text_channel(self, props):
142 _, surpress_handler, h = self._get_type_requested_handle(props)
144 accountNumber = util_misc.strip_number(self._conn.session.backend.get_account_number())
145 if h.phoneNumber == accountNumber:
146 _moduleLogger.debug('New Debug channel')
147 chan = channel.debug_prompt.DebugPromptChannel(self._conn, self, props, h)
149 _moduleLogger.debug('New text channel')
150 chan = channel.text.TextChannel(self._conn, self, props, h)
153 def _get_media_channel(self, props):
154 _, surpress_handler, h = self._get_type_requested_handle(props)
156 _moduleLogger.debug('New media channel')
157 chan = channel.call.CallChannel(self._conn, self, props, h)