7 import util.go_utils as gobject_utils
8 import util.coroutines as coroutines
13 import simple_presence
14 import channel_manager
17 _moduleLogger = logging.getLogger("connection")
20 class TheOneRingConnection(
21 telepathy.server.Connection,
22 aliasing.AliasingMixin,
23 simple_presence.SimplePresenceMixin,
26 # Overriding a base class variable
27 # Should the forwarding number be handled by the alias or by an option?
28 _mandatory_parameters = {
33 # Overriding a base class variable
34 _optional_parameters = {
36 _parameter_defaults = {
39 def __init__(self, manager, parameters):
40 self.check_parameters(parameters)
42 account = unicode(parameters['username'])
44 # Connection init must come first
45 telepathy.server.Connection.__init__(
47 constants._telepathy_protocol_name_,
49 constants._telepathy_implementation_name_
51 aliasing.AliasingMixin.__init__(self)
52 simple_presence.SimplePresenceMixin.__init__(self)
54 self._manager = weakref.proxy(manager)
56 parameters['username'].encode('utf-8'),
57 parameters['password'].encode('utf-8'),
59 self._callbackNumber = parameters['forward'].encode('utf-8')
60 self._channelManager = channel_manager.ChannelManager(self)
63 self._session = gvoice.session.Session(cookieFilePath)
65 self.set_self_handle(handle.create_handle(self, 'connection'))
67 self._callback = coroutines.func_sink(
68 coroutines.expand_positional(
69 self._on_conversations_updated
72 _moduleLogger.info("Connection to the account %s created" % account)
74 _moduleLogger.exception("Failed to create Connection")
87 return self._credentials[0]
89 def handle(self, handleType, handleId):
90 self.check_handle(handleType, handleId)
91 return self._handles[handleType, handleId]
93 @gtk_toolbox.log_exception(_moduleLogger)
96 For org.freedesktop.telepathy.Connection
98 _moduleLogger.info("Connecting...")
100 telepathy.CONNECTION_STATUS_CONNECTING,
101 telepathy.CONNECTION_STATUS_REASON_REQUESTED
104 self.session.conversations.updateSignalHandler.register_sink(
107 self.session.login(*self._credentials)
108 self.session.backend.set_callback_number(self._callbackNumber)
109 except gvoice.backend.NetworkError, e:
110 _moduleLogger.exception("Connection Failed")
112 telepathy.CONNECTION_STATUS_DISCONNECTED,
113 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
116 _moduleLogger.exception("Connection Failed")
118 telepathy.CONNECTION_STATUS_DISCONNECTED,
119 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
122 _moduleLogger.info("Connected")
124 telepathy.CONNECTION_STATUS_CONNECTED,
125 telepathy.CONNECTION_STATUS_REASON_REQUESTED
128 @gtk_toolbox.log_exception(_moduleLogger)
129 def Disconnect(self):
131 For org.freedesktop.telepathy.Connection
132 @bug Not properly logging out. Cookie files need to be per connection and removed
134 _moduleLogger.info("Disconnecting")
136 self.session.conversations.updateSignalHandler.unregister_sink(
139 self._channelManager.close()
140 self.session.logout()
141 _moduleLogger.info("Disconnected")
143 _moduleLogger.exception("Disconnecting Failed")
145 telepathy.CONNECTION_STATUS_DISCONNECTED,
146 telepathy.CONNECTION_STATUS_REASON_REQUESTED
149 @gtk_toolbox.log_exception(_moduleLogger)
150 def RequestChannel(self, type, handleType, handleId, suppressHandler):
152 For org.freedesktop.telepathy.Connection
154 @param type DBus interface name for base channel type
155 @param handleId represents a contact, list, etc according to handleType
157 @returns DBus object path for the channel created or retrieved
159 self.check_connected()
160 self.check_handle(handleType, handleId)
163 channelManager = self._channelManager
164 handle = self.handle(handleType, handleId)
166 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
167 _moduleLogger.info("RequestChannel ContactList")
168 channel = channelManager.channel_for_list(handle, suppressHandler)
169 elif type == telepathy.CHANNEL_TYPE_TEXT:
170 _moduleLogger.info("RequestChannel Text")
171 channel = channelManager.channel_for_text(handle, suppressHandler)
172 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
173 _moduleLogger.info("RequestChannel Media")
174 channel = channelManager.channel_for_call(handle, suppressHandler)
176 raise telepathy.errors.NotImplemented("unknown channel type %s" % type)
178 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
179 return channel._object_path
181 @gtk_toolbox.log_exception(_moduleLogger)
182 def RequestHandles(self, handleType, names, sender):
184 For org.freedesktop.telepathy.Connection
185 Overiding telepathy.server.Connecton to allow custom handles
187 self.check_connected()
188 self.check_handle_type(handleType)
192 name = name.encode('utf-8')
193 if handleType == telepathy.HANDLE_TYPE_CONTACT:
194 _moduleLogger.info("RequestHandles Contact: %s" % name)
195 h = self._create_contact_handle(name)
196 elif handleType == telepathy.HANDLE_TYPE_LIST:
197 # Support only server side (immutable) lists
198 _moduleLogger.info("RequestHandles List: %s" % name)
199 h = handle.create_handle(self, 'list', name)
201 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
203 self.add_client_handle(h, sender)
206 def _create_contact_handle(self, requestedHandleName):
207 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
210 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
214 @gtk_toolbox.log_exception(_moduleLogger)
215 def _on_conversations_updated(self, conv, conversationIds):
216 # @todo get conversations update running
217 # @todo test conversatiuons
218 _moduleLogger.info("Incoming messages from: %r" % (conversationIds, ))
219 channelManager = self._channelManager
220 for contactId, phoneNumber in conversationIds:
221 h = handle.create_handle(self, 'contact', contactId, phoneNumber)
222 # if its new, __init__ will take care of things
223 # if its old, its own update will take care of it
224 channel = channelManager.channel_for_text(h)