Random cleanup/implementation fairy of fun
[theonering] / src / connection.py
1 import weakref
2 import logging
3
4 import telepathy
5
6 import constants
7 import gv_backend
8 import handle
9 import channel_manager
10
11
12 class TheOneRingConnection(telepathy.server.Connection):
13
14         MANDATORY_PARAMETERS = {
15                 'account' : 's',
16                 'password' : 's'
17         }
18         OPTIONAL_PARAMETERS = {
19         }
20         PARAMETER_DEFAULTS = {
21         }
22
23         def __init__(self, manager, parameters):
24                 try:
25                         self.check_parameters(parameters)
26                         account = unicode(parameters['account'])
27
28                         telepathy.server.Connection.__init__(
29                                 self,
30                                 constants._telepathy_protocol_name_,
31                                 account,
32                                 constants._telepathy_implementation_name_
33                         )
34
35                         self._manager = weakref.proxy(manager)
36                         self._credentials = (
37                                 parameters['account'].encode('utf-8'),
38                                 parameters['password'].encode('utf-8'),
39                         )
40                         self._channelManager = channel_manager.ChannelManager(self)
41
42                         cookieFilePath = "%s/cookies.txt" % constants._data_path_
43                         self._backend = gv_backend.GVDialer(cookieFilePath)
44
45                         self.set_self_handle(handle.create_handle(self, 'self'))
46
47                         self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED
48
49                         logging.info("Connection to the account %s created" % account)
50                 except Exception, e:
51                         logging.exception("Failed to create Connection")
52                         raise
53
54         @property
55         def manager(self):
56                 return self._manager
57
58         @property
59         def gvoice_backend(self):
60                 return self._backend
61
62         @property
63         def username(self):
64                 self._credentials[0]
65
66         def handle(self, handleType, handleId):
67                 self.check_handle(handleType, handleId)
68                 return self._handles[handleType, handleId]
69
70         def Connect(self):
71                 """
72                 org.freedesktop.telepathy.Connection
73                 """
74                 logging.info("Connecting")
75                 self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED
76                 try:
77                         self._backend.login(*self._credentials)
78                 except RuntimeError:
79                         self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
80
81         def Disconnect(self):
82                 """
83                 org.freedesktop.telepathy.Connection
84                 """
85                 logging.info("Disconnecting")
86                 self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_REQUESTED
87                 self._backend.logout()
88
89         def RequestChannel(self, type, handleType, handleId, suppressHandler):
90                 """
91                 org.freedesktop.telepathy.Connection
92                 @param type DBus interface name for base channel type
93                 @param handleId represents a contact, list, etc according to handleType
94
95                 @returns DBus object path for the channel created or retrieved
96                 """
97                 self.check_connected()
98
99                 channel = None
100                 channelManager = self._channelManager
101                 handle = self.handle(handleType, handleId)
102
103                 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
104                         channel = channelManager.channel_for_list(handle, suppressHandler)
105                 elif type == telepathy.CHANNEL_TYPE_TEXT:
106                         if handleType != telepathy.HANDLE_TYPE_CONTACT:
107                                 raise telepathy.NotImplemented("Only Contacts are allowed")
108                         contact = handle.contact
109                         channel = channelManager.channel_for_text(handle, None, suppressHandler)
110                 else:
111                         raise telepathy.NotImplemented("unknown channel type %s" % type)
112
113                 return channel._object_path
114
115         def RequestHandles(self, handleType, names, sender):
116                 """
117                 org.freedesktop.telepathy.Connection
118                 """
119                 self.check_connected()
120                 self.check_handleType(handleType)
121
122                 handles = []
123                 for name in names:
124                         name = name.encode('utf-8')
125                         if handleType == telepathy.HANDLE_TYPE_CONTACT:
126                                 h = self._create_contact_handle(name)
127                         elif handleType == telepathy.HANDLE_TYPE_LIST:
128                                 h = handle.create_handle(self, 'list', name)
129                         elif handleType == telepathy.HANDLE_TYPE_GROUP:
130                                 h = handle.create_handle(self, 'group', name)
131                         else:
132                                 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
133                         handles.append(h.id)
134                         self.add_client_handle(handle, sender)
135                 return handles
136
137         def _create_contact_handle(self, name):
138                 requestedContactId, requestedContactName = handle.field_split(name)
139
140                 contacts = self._backend.get_contacts()
141                 contactsFound = [
142                         (contactId, contactName) for (contactId, contactName) in contacts
143                         if contactName == name
144                 ]
145
146                 if 0 < len(contactsFound):
147                         contactId, contactName = contactsFound[0]
148                         h = handle.create_handle(self, 'contact', contactId, contactName)
149                 else:
150                         h = handle.create_handle(self, 'contact', requestedContactId, requestedContactName)