Fixing a bug where telepathy-theonering wasn't included in packages
[theonering] / src / channel_manager.py
1 import weakref
2 import itertools
3 import logging
4
5 import telepathy
6
7 import channel
8
9
10 _moduleLogger = logging.getLogger("channel_manager")
11
12
13 class ChannelManager(object):
14
15         def __init__(self, connection):
16                 self._connRef = weakref.ref(connection)
17                 self._listChannels = weakref.WeakValueDictionary()
18                 self._textChannels = weakref.WeakValueDictionary()
19                 self._callChannels = weakref.WeakValueDictionary()
20
21         def close(self):
22                 for chan in itertools.chain(
23                         self._listChannels.values(), self._textChannels.values(), self._callChannels.values()
24                 ):
25                         try:
26                                 chan.close()
27                         except Exception:
28                                 _moduleLogger.exception("Shutting down %r" % (chan, ))
29
30         def channel_for_list(self, handle, suppress_handler=False):
31                 try:
32                         chan = self._listChannels[handle]
33                 except KeyError, e:
34                         if handle.get_type() != telepathy.HANDLE_TYPE_LIST:
35                                 raise telepathy.errors.NotImplemented("Only server lists are allowed")
36                         _moduleLogger.debug("Requesting new contact list channel")
37
38                         chan = channel.contact_list.create_contact_list_channel(self._connRef(), handle)
39                         self._listChannels[handle] = chan
40                         self._connRef().add_channel(chan, handle, suppress_handler)
41                 return chan
42
43         def channel_for_text(self, handle, suppress_handler=False):
44                 try:
45                         chan = self._textChannels[handle]
46                 except KeyError, e:
47                         if handle.get_type() != telepathy.HANDLE_TYPE_CONTACT:
48                                 raise telepathy.errors.NotImplemented("Only Contacts are allowed")
49                         _moduleLogger.debug("Requesting new text channel")
50
51                         chan = channel.text.TextChannel(self._connRef(), handle)
52                         self._textChannels[handle] = chan
53                         self._connRef().add_channel(chan, handle, suppress_handler)
54                 return chan
55
56         def channel_for_call(self, handle, suppress_handler=False):
57                 try:
58                         chan = self._callChannels[handle]
59                 except KeyError, e:
60                         if handle.get_type() != telepathy.HANDLE_TYPE_CONTACT:
61                                 _moduleLogger.warning("Using deprecated means to create a call")
62                                 raise telepathy.errors.NotImplemented("Not implementing depcrecated means")
63                         _moduleLogger.debug("Requesting new call channel")
64
65                         chan = channel.call.CallChannel(self._connRef(), handle)
66                         self._callChannels[handle] = chan
67                         self._connRef().add_channel(chan, handle, suppress_handler)
68                 return chan