2fd3632d153acaaadf2160cf8cb0937b8e438c88
[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                         self._addressbook = gvoice.addressbook.Addressbook(self._backend)
51
52                         self.set_self_handle(handle.create_handle(self, 'connection'))
53
54                         _moduleLogger.info("Connection to the account %s created" % account)
55                 except Exception, e:
56                         _moduleLogger.exception("Failed to create Connection")
57                         raise
58
59         @property
60         def manager(self):
61                 return self._manager
62
63         @property
64         def gvoice_backend(self):
65                 return self._backend
66
67         @property
68         def addressbook(self):
69                 return self._addressbook
70
71         @property
72         def username(self):
73                 self._credentials[0]
74
75         def handle(self, handleType, handleId):
76                 self.check_handle(handleType, handleId)
77                 return self._handles[handleType, handleId]
78
79         def Connect(self):
80                 """
81                 For org.freedesktop.telepathy.Connection
82                 """
83                 self.StatusChanged(
84                         telepathy.CONNECTION_STATUS_CONNECTING,
85                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
86                 )
87                 try:
88                         self._backend.login(*self._credentials)
89                         self._backend.set_callback_number(self._callbackNumber)
90                 except gvoice.dialer.NetworkError:
91                         self.StatusChanged(
92                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
93                                 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
94                         )
95                 except Exception:
96                         self.StatusChanged(
97                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
98                                 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
99                         )
100                 else:
101                         self.StatusChanged(
102                                 telepathy.CONNECTION_STATUS_CONNECTED,
103                                 telepathy.CONNECTION_STATUS_REASON_REQUESTED
104                         )
105
106         def Disconnect(self):
107                 """
108                 For org.freedesktop.telepathy.Connection
109                 """
110                 try:
111                         self._backend.logout()
112                         _moduleLogger.info("Disconnected")
113                 except Exception:
114                         _moduleLogger.exception("Disconnecting Failed")
115                 self.StatusChanged(
116                         telepathy.CONNECTION_STATUS_DISCONNECTED,
117                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
118                 )
119
120         def RequestChannel(self, type, handleType, handleId, suppressHandler):
121                 """
122                 For org.freedesktop.telepathy.Connection
123
124                 @param type DBus interface name for base channel type
125                 @param handleId represents a contact, list, etc according to handleType
126
127                 @returns DBus object path for the channel created or retrieved
128                 """
129                 self.check_connected()
130
131                 channel = None
132                 channelManager = self._channelManager
133                 handle = self.handle(handleType, handleId)
134
135                 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
136                         channel = channelManager.channel_for_list(handle, suppressHandler)
137                 elif type == telepathy.CHANNEL_TYPE_TEXT:
138                         if handleType != telepathy.HANDLE_TYPE_CONTACT:
139                                 raise telepathy.NotImplemented("Only Contacts are allowed")
140                         channel = channelManager.channel_for_text(handle, None, suppressHandler)
141                 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
142                         if handleType != telepathy.HANDLE_TYPE_CONTACT:
143                                 raise telepathy.NotImplemented("Only Contacts are allowed")
144                         channel = channelManager.channel_for_text(handle, None, suppressHandler)
145                 else:
146                         raise telepathy.NotImplemented("unknown channel type %s" % type)
147
148                 return channel._object_path
149
150         def RequestHandles(self, handleType, names, sender):
151                 """
152                 For org.freedesktop.telepathy.Connection
153                 """
154                 self.check_connected()
155                 self.check_handle_type(handleType)
156
157                 handles = []
158                 for name in names:
159                         name = name.encode('utf-8')
160                         if handleType == telepathy.HANDLE_TYPE_CONTACT:
161                                 h = self._create_contact_handle(name)
162                         elif handleType == telepathy.HANDLE_TYPE_LIST:
163                                 # Support only server side (immutable) lists
164                                 h = handle.create_handle(self, 'list', name)
165                         else:
166                                 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
167                         handles.append(h.id)
168                         self.add_client_handle(handle, sender)
169                 return handles
170
171         def _create_contact_handle(self, name):
172                 requestedContactId = name
173
174                 contacts = self._addressbook.get_contacts()
175                 contactsFound = [
176                         contactId for contactId in contacts
177                         if contactId == requestedContactId
178                 ]
179
180                 if 0 < len(contactsFound):
181                         contactId = contactsFound[0]
182                         if len(contactsFound) != 1:
183                                 _moduleLogger.error("Contact ID was not unique: %s for %s" % (contactId, ))
184                 else:
185                         contactId = requestedContactId
186                 h = handle.create_handle(self, 'contact', contactId)