Fixing typo bugs, weak dictionary bugs, adding logging, handle type enforcement, etc
[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.remove_from_connection()# so that dbus lets it die.
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 RuntimeError("Unsupported channel type %r" % handle.get_type())
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.NotImplemented("Only Contacts are allowed")
47                         _moduleLogger.debug("Requesting new text channel")
48
49                         contact = handle.contact
50                         chan = channel.text.TextChannel(self._connRef())
51                         self._textChannels[handle] = chan
52                         self._connRef().add_channel(chan, handle, suppress_handler)
53                 return chan
54
55         def channel_for_call(self, handle, suppress_handler=False):
56                 try:
57                         chan = self._callChannels[handle]
58                 except KeyError, e:
59                         if handle.get_type() != telepathy.HANDLE_TYPE_CONTACT:
60                                 raise telepathy.NotImplemented("Only Contacts are allowed")
61                         _moduleLogger.debug("Requesting new call channel")
62
63                         contact = handle.contact
64                         chan = channel.call.CallChannel(self._connRef())
65                         self._callChannels[handle] = chan
66                         self._connRef().add_channel(chan, handle, suppress_handler)
67                 return chan