3 @todo Add params for disable/enable state machines
4 @todo Separate voicemail/sms into separate conversation instances
5 @todo Setup addressbook, voicemail, sms state machines
6 @todo Add option to use screen name as callback
16 import util.go_utils as gobject_utils
17 import util.coroutines as coroutines
22 import simple_presence
25 import channel_manager
28 _moduleLogger = logging.getLogger("connection")
31 class TheOneRingConnection(
32 telepathy.server.Connection,
33 aliasing.AliasingMixin,
34 simple_presence.SimplePresenceMixin,
35 presence.PresenceMixin,
36 capabilities.CapabilitiesMixin,
39 # Overriding a base class variable
40 # Should the forwarding number be handled by the alias or by an option?
41 _mandatory_parameters = {
46 # Overriding a base class variable
47 _optional_parameters = {
49 _parameter_defaults = {
52 def __init__(self, manager, parameters):
53 self.check_parameters(parameters)
55 account = unicode(parameters['account'])
57 # Connection init must come first
58 telepathy.server.Connection.__init__(
60 constants._telepathy_protocol_name_,
62 constants._telepathy_implementation_name_
64 aliasing.AliasingMixin.__init__(self)
65 simple_presence.SimplePresenceMixin.__init__(self)
66 presence.PresenceMixin.__init__(self)
67 capabilities.CapabilitiesMixin.__init__(self)
69 self._manager = weakref.proxy(manager)
71 parameters['account'].encode('utf-8'),
72 parameters['password'].encode('utf-8'),
74 self._callbackNumber = parameters['forward'].encode('utf-8')
75 self._channelManager = channel_manager.ChannelManager(self)
77 self._session = gvoice.session.Session(None)
79 self.set_self_handle(handle.create_handle(self, 'connection'))
82 _moduleLogger.info("Connection to the account %s created" % account)
84 _moduleLogger.exception("Failed to create Connection")
97 return self._credentials[0]
100 def userAliasType(self):
101 return self.USER_ALIAS_ACCOUNT
103 def handle(self, handleType, handleId):
104 self.check_handle(handleType, handleId)
105 return self._handles[handleType, handleId]
107 @gtk_toolbox.log_exception(_moduleLogger)
110 For org.freedesktop.telepathy.Connection
112 _moduleLogger.info("Connecting...")
114 telepathy.CONNECTION_STATUS_CONNECTING,
115 telepathy.CONNECTION_STATUS_REASON_REQUESTED
118 cookieFilePath = None
119 self._session = gvoice.session.Session(cookieFilePath)
121 self._callback = coroutines.func_sink(
122 coroutines.expand_positional(
123 self._on_conversations_updated
126 self.session.conversations.updateSignalHandler.register_sink(
129 self.session.login(*self._credentials)
130 self.session.backend.set_callback_number(self._callbackNumber)
131 except gvoice.backend.NetworkError, e:
132 _moduleLogger.exception("Connection Failed")
134 telepathy.CONNECTION_STATUS_DISCONNECTED,
135 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
138 _moduleLogger.exception("Connection Failed")
140 telepathy.CONNECTION_STATUS_DISCONNECTED,
141 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
144 _moduleLogger.info("Connected")
146 telepathy.CONNECTION_STATUS_CONNECTED,
147 telepathy.CONNECTION_STATUS_REASON_REQUESTED
150 @gtk_toolbox.log_exception(_moduleLogger)
151 def Disconnect(self):
153 For org.freedesktop.telepathy.Connection
154 @bug Not properly logging out. Cookie files need to be per connection and removed
156 _moduleLogger.info("Disconnecting")
158 self.session.conversations.updateSignalHandler.unregister_sink(
161 self._callback = None
162 self._channelManager.close()
163 self.session.logout()
166 _moduleLogger.info("Disconnected")
168 _moduleLogger.exception("Disconnecting Failed")
170 telepathy.CONNECTION_STATUS_DISCONNECTED,
171 telepathy.CONNECTION_STATUS_REASON_REQUESTED
173 self.manager.disconnected(self)
175 @gtk_toolbox.log_exception(_moduleLogger)
176 def RequestChannel(self, type, handleType, handleId, suppressHandler):
178 For org.freedesktop.telepathy.Connection
180 @param type DBus interface name for base channel type
181 @param handleId represents a contact, list, etc according to handleType
183 @returns DBus object path for the channel created or retrieved
185 self.check_connected()
186 self.check_handle(handleType, handleId)
188 h = self.handle(handleType, handleId) if handleId != 0 else None
189 props = self._generate_props(type, h, suppressHandler)
190 if hasattr(self, "_validate_handle"):
191 # HACK Newer python-telepathy
192 self._validate_handle(props)
194 chan = self._channelManager.channel_for_props(props, signal=True)
195 path = chan._object_path
196 _moduleLogger.info("RequestChannel Object Path: %s" % path)
199 @gtk_toolbox.log_exception(_moduleLogger)
200 def RequestHandles(self, handleType, names, sender):
202 For org.freedesktop.telepathy.Connection
203 Overiding telepathy.server.Connecton to allow custom handles
205 self.check_connected()
206 self.check_handle_type(handleType)
210 requestedHandleName = name.encode('utf-8')
211 if handleType == telepathy.HANDLE_TYPE_CONTACT:
212 _moduleLogger.info("RequestHandles Contact: %s" % requestedHandleName)
213 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
216 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
217 elif handleType == telepathy.HANDLE_TYPE_LIST:
218 # Support only server side (immutable) lists
219 _moduleLogger.info("RequestHandles List: %s" % requestedHandleName)
220 h = handle.create_handle(self, 'list', requestedHandleName)
222 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
224 self.add_client_handle(h, sender)
227 def _generate_props(self, channelType, handle, suppressHandler, initiatorHandle=None):
228 targetHandle = 0 if handle is None else handle.get_id()
229 targetHandleType = telepathy.HANDLE_TYPE_NONE if handle is None else handle.get_type()
231 telepathy.CHANNEL_INTERFACE + '.ChannelType': channelType,
232 telepathy.CHANNEL_INTERFACE + '.TargetHandle': targetHandle,
233 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': targetHandleType,
234 telepathy.CHANNEL_INTERFACE + '.Requested': suppressHandler
237 if initiatorHandle is not None:
238 props[telepathy.CHANNEL_INTERFACE + '.InitiatorHandle'] = initiatorHandle.id
243 @gtk_toolbox.log_exception(_moduleLogger)
244 def _on_conversations_updated(self, conv, conversationIds):
245 # @todo get conversations update running
246 # @todo test conversatiuons
247 _moduleLogger.info("Incoming messages from: %r" % (conversationIds, ))
248 for contactId, phoneNumber in conversationIds:
249 h = handle.create_handle(self, 'contact', contactId, phoneNumber)
250 # Just let the TextChannel decide whether it should be reported to the user or not
251 props = self._generate_props(telepathy.CHANNEL_TYPE_TEXT, h, False)
252 channel = self._channelManager.channel_for_props(props, signal=True)