Overall, got sending a text to an arbitrary number working
[theonering] / src / connection.py
1 import weakref
2 import logging
3
4 import telepathy
5
6 import constants
7 import gtk_toolbox
8 import gvoice
9 import handle
10 import channel_manager
11
12
13 _moduleLogger = logging.getLogger("connection")
14
15
16 class TheOneRingConnection(telepathy.server.Connection):
17
18         # Overriding a base class variable
19         _mandatory_parameters = {
20                 'username' : 's',
21                 'password' : 's',
22                 'forward' : 's',
23         }
24         # Overriding a base class variable
25         _optional_parameters = {
26         }
27         _parameter_defaults = {
28         }
29
30         def __init__(self, manager, parameters):
31                 try:
32                         self.check_parameters(parameters)
33                         account = unicode(parameters['username'])
34
35                         telepathy.server.Connection.__init__(
36                                 self,
37                                 constants._telepathy_protocol_name_,
38                                 account,
39                                 constants._telepathy_implementation_name_
40                         )
41
42                         self._manager = weakref.proxy(manager)
43                         self._credentials = (
44                                 parameters['username'].encode('utf-8'),
45                                 parameters['password'].encode('utf-8'),
46                         )
47                         self._callbackNumber = parameters['forward'].encode('utf-8')
48                         self._channelManager = channel_manager.ChannelManager(self)
49
50                         cookieFilePath = "%s/cookies.txt" % constants._data_path_
51                         self._session = gvoice.session.Session(cookieFilePath)
52
53                         self.set_self_handle(handle.create_handle(self, 'connection'))
54
55                         _moduleLogger.info("Connection to the account %s created" % account)
56                 except Exception, e:
57                         _moduleLogger.exception("Failed to create Connection")
58                         raise
59
60         @property
61         def manager(self):
62                 return self._manager
63
64         @property
65         def session(self):
66                 return self._session
67
68         @property
69         def username(self):
70                 return self._credentials[0]
71
72         def handle(self, handleType, handleId):
73                 self.check_handle(handleType, handleId)
74                 return self._handles[handleType, handleId]
75
76         @gtk_toolbox.log_exception(_moduleLogger)
77         def Connect(self):
78                 """
79                 For org.freedesktop.telepathy.Connection
80                 """
81                 _moduleLogger.info("Connecting...")
82                 self.StatusChanged(
83                         telepathy.CONNECTION_STATUS_CONNECTING,
84                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
85                 )
86                 try:
87                         self.session.login(*self._credentials)
88                         self.session.backend.set_callback_number(self._callbackNumber)
89                 except gvoice.backend.NetworkError, e:
90                         _moduleLogger.exception("Connection Failed")
91                         self.StatusChanged(
92                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
93                                 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
94                         )
95                 except Exception, e:
96                         _moduleLogger.exception("Connection Failed")
97                         self.StatusChanged(
98                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
99                                 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
100                         )
101                 else:
102                         _moduleLogger.info("Connected")
103                         self.StatusChanged(
104                                 telepathy.CONNECTION_STATUS_CONNECTED,
105                                 telepathy.CONNECTION_STATUS_REASON_REQUESTED
106                         )
107
108         @gtk_toolbox.log_exception(_moduleLogger)
109         def Disconnect(self):
110                 """
111                 For org.freedesktop.telepathy.Connection
112                 @bug Not properly logging out.  Cookie files need to be per connection and removed
113                 """
114                 _moduleLogger.info("Disconnecting")
115                 try:
116                         self.session.logout()
117                         _moduleLogger.info("Disconnected")
118                 except Exception:
119                         _moduleLogger.exception("Disconnecting Failed")
120                 self.StatusChanged(
121                         telepathy.CONNECTION_STATUS_DISCONNECTED,
122                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
123                 )
124
125         @gtk_toolbox.log_exception(_moduleLogger)
126         def RequestChannel(self, type, handleType, handleId, suppressHandler):
127                 """
128                 For org.freedesktop.telepathy.Connection
129
130                 @param type DBus interface name for base channel type
131                 @param handleId represents a contact, list, etc according to handleType
132
133                 @returns DBus object path for the channel created or retrieved
134                 """
135                 self.check_connected()
136                 self.check_handle(handleType, handleId)
137
138                 channel = None
139                 channelManager = self._channelManager
140                 handle = self.handle(handleType, handleId)
141
142                 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
143                         _moduleLogger.info("RequestChannel ContactList")
144                         channel = channelManager.channel_for_list(handle, suppressHandler)
145                 elif type == telepathy.CHANNEL_TYPE_TEXT:
146                         _moduleLogger.info("RequestChannel Text")
147                         channel = channelManager.channel_for_text(handle, suppressHandler)
148                 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
149                         _moduleLogger.info("RequestChannel Media")
150                         channel = channelManager.channel_for_call(handle, suppressHandler)
151                 else:
152                         raise telepathy.NotImplemented("unknown channel type %s" % type)
153
154                 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
155                 return channel._object_path
156
157         @gtk_toolbox.log_exception(_moduleLogger)
158         def RequestHandles(self, handleType, names, sender):
159                 """
160                 For org.freedesktop.telepathy.Connection
161                 Overiding telepathy.server.Connecton to allow custom handles
162                 """
163                 self.check_connected()
164                 self.check_handle_type(handleType)
165
166                 handles = []
167                 for name in names:
168                         name = name.encode('utf-8')
169                         if handleType == telepathy.HANDLE_TYPE_CONTACT:
170                                 _moduleLogger.info("RequestHandles Contact: %s" % name)
171                                 h = self._create_contact_handle(name)
172                         elif handleType == telepathy.HANDLE_TYPE_LIST:
173                                 # Support only server side (immutable) lists
174                                 _moduleLogger.info("RequestHandles List: %s" % name)
175                                 h = handle.create_handle(self, 'list', name)
176                         else:
177                                 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
178                         handles.append(h.id)
179                         self.add_client_handle(h, sender)
180                 return handles
181
182         def _create_contact_handle(self, requestedHandleName):
183                 """
184                 @todo Determine if nay of this is really needed
185                 """
186                 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
187                         requestedHandleName
188                 )
189                 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
190                 return h
191
192         def _on_invite_text(self, contactId):
193                 """
194                 @todo Make this work
195                 """
196                 h = self._create_contact_handle(contactId)
197
198                 channelManager = self._channelManager
199                 channel = channelManager.channel_for_text(handle)