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