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