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