3 @todo Add params for disable/enable state machines
4 @todo Move addressbook to separate state machine
5 @todo Add option to use screen name as callback
15 import util.go_utils as gobject_utils
16 import util.coroutines as coroutines
21 import simple_presence
24 import channel_manager
27 _moduleLogger = logging.getLogger("connection")
30 class TheOneRingConnection(
31 telepathy.server.Connection,
32 aliasing.AliasingMixin,
33 simple_presence.SimplePresenceMixin,
34 presence.PresenceMixin,
35 capabilities.CapabilitiesMixin,
38 # Overriding a base class variable
39 # Should the forwarding number be handled by the alias or by an option?
40 _mandatory_parameters = {
45 # Overriding a base class variable
46 _optional_parameters = {
48 _parameter_defaults = {
51 def __init__(self, manager, parameters):
52 self.check_parameters(parameters)
54 account = unicode(parameters['account'])
56 # Connection init must come first
57 telepathy.server.Connection.__init__(
59 constants._telepathy_protocol_name_,
61 constants._telepathy_implementation_name_
63 aliasing.AliasingMixin.__init__(self)
64 simple_presence.SimplePresenceMixin.__init__(self)
65 presence.PresenceMixin.__init__(self)
66 capabilities.CapabilitiesMixin.__init__(self)
68 self._manager = weakref.proxy(manager)
70 parameters['account'].encode('utf-8'),
71 parameters['password'].encode('utf-8'),
73 self._callbackNumber = parameters['forward'].encode('utf-8')
74 self._channelManager = channel_manager.ChannelManager(self)
76 self._session = gvoice.session.Session(None)
78 self.set_self_handle(handle.create_handle(self, 'connection'))
81 _moduleLogger.info("Connection to the account %s created" % account)
83 _moduleLogger.exception("Failed to create Connection")
96 return self._credentials[0]
99 def userAliasType(self):
100 return self.USER_ALIAS_ACCOUNT
102 def handle(self, handleType, handleId):
103 self.check_handle(handleType, handleId)
104 return self._handles[handleType, handleId]
106 @gtk_toolbox.log_exception(_moduleLogger)
109 For org.freedesktop.telepathy.Connection
111 _moduleLogger.info("Connecting...")
113 telepathy.CONNECTION_STATUS_CONNECTING,
114 telepathy.CONNECTION_STATUS_REASON_REQUESTED
117 cookieFilePath = None
118 self._session = gvoice.session.Session(cookieFilePath)
120 self._callback = coroutines.func_sink(
121 coroutines.expand_positional(
122 self._on_conversations_updated
125 self.session.conversations.updateSignalHandler.register_sink(
128 self.session.login(*self._credentials)
129 self.session.backend.set_callback_number(self._callbackNumber)
130 except gvoice.backend.NetworkError, e:
131 _moduleLogger.exception("Connection Failed")
133 telepathy.CONNECTION_STATUS_DISCONNECTED,
134 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
137 _moduleLogger.exception("Connection Failed")
139 telepathy.CONNECTION_STATUS_DISCONNECTED,
140 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
143 _moduleLogger.info("Connected")
145 telepathy.CONNECTION_STATUS_CONNECTED,
146 telepathy.CONNECTION_STATUS_REASON_REQUESTED
149 @gtk_toolbox.log_exception(_moduleLogger)
150 def Disconnect(self):
152 For org.freedesktop.telepathy.Connection
153 @bug Not properly logging out. Cookie files need to be per connection and removed
155 _moduleLogger.info("Disconnecting")
157 self.session.conversations.updateSignalHandler.unregister_sink(
160 self._callback = None
161 self._channelManager.close()
162 self.session.logout()
165 _moduleLogger.info("Disconnected")
167 _moduleLogger.exception("Disconnecting Failed")
169 telepathy.CONNECTION_STATUS_DISCONNECTED,
170 telepathy.CONNECTION_STATUS_REASON_REQUESTED
172 self.manager.disconnected(self)
174 @gtk_toolbox.log_exception(_moduleLogger)
175 def RequestChannel(self, type, handleType, handleId, suppressHandler):
177 For org.freedesktop.telepathy.Connection
179 @param type DBus interface name for base channel type
180 @param handleId represents a contact, list, etc according to handleType
182 @returns DBus object path for the channel created or retrieved
184 self.check_connected()
185 self.check_handle(handleType, handleId)
187 h = self.handle(handleType, handleId) if handleId != 0 else None
188 props = self._generate_props(type, h, suppressHandler)
189 if hasattr(self, "_validate_handle"):
190 # On newer python-telepathy
191 self._validate_handle(props)
193 chan = self._channelManager.channel_for_props(props, signal=True)
194 path = chan._object_path
195 _moduleLogger.info("RequestChannel Object Path: %s" % path)
198 @gtk_toolbox.log_exception(_moduleLogger)
199 def RequestHandles(self, handleType, names, sender):
201 For org.freedesktop.telepathy.Connection
202 Overiding telepathy.server.Connecton to allow custom handles
204 self.check_connected()
205 self.check_handle_type(handleType)
209 requestedHandleName = name.encode('utf-8')
210 if handleType == telepathy.HANDLE_TYPE_CONTACT:
211 _moduleLogger.info("RequestHandles Contact: %s" % requestedHandleName)
212 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
215 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
216 elif handleType == telepathy.HANDLE_TYPE_LIST:
217 # Support only server side (immutable) lists
218 _moduleLogger.info("RequestHandles List: %s" % requestedHandleName)
219 h = handle.create_handle(self, 'list', requestedHandleName)
221 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
223 self.add_client_handle(h, sender)
226 def _generate_props(self, channelType, handle, suppressHandler, initiatorHandle=None):
227 targetHandle = 0 if handle is None else handle.get_id()
228 targetHandleType = telepathy.HANDLE_TYPE_NONE if handle is None else handle.get_type()
230 telepathy.CHANNEL_INTERFACE + '.ChannelType': channelType,
231 telepathy.CHANNEL_INTERFACE + '.TargetHandle': targetHandle,
232 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': targetHandleType,
233 telepathy.CHANNEL_INTERFACE + '.Requested': suppressHandler
236 if initiatorHandle is not None:
237 props[telepathy.CHANNEL_INTERFACE + '.InitiatorHandle'] = initiatorHandle.id
242 @gtk_toolbox.log_exception(_moduleLogger)
243 def _on_conversations_updated(self, conv, conversationIds):
244 # @todo get conversations update running
245 # @todo test conversatiuons
246 _moduleLogger.info("Incoming messages from: %r" % (conversationIds, ))
247 for contactId, phoneNumber in conversationIds:
248 h = handle.create_handle(self, 'contact', contactId, phoneNumber)
249 # Just let the TextChannel decide whether it should be reported to the user or not
250 props = self._generate_props(telepathy.CHANNEL_TYPE_TEXT, h, False)
251 channel = self._channelManager.channel_for_props(props, signal=True)