7 import util.go_utils as gobject_utils
8 import util.coroutines as coroutines
13 import simple_presence
15 import channel_manager
18 _moduleLogger = logging.getLogger("connection")
21 class TheOneRingConnection(
22 telepathy.server.Connection,
23 aliasing.AliasingMixin,
24 simple_presence.SimplePresenceMixin,
25 capabilities.CapabilitiesMixin,
28 # Overriding a base class variable
29 # Should the forwarding number be handled by the alias or by an option?
30 _mandatory_parameters = {
35 # Overriding a base class variable
36 _optional_parameters = {
38 _parameter_defaults = {
41 def __init__(self, manager, parameters):
42 self.check_parameters(parameters)
44 account = unicode(parameters['username'])
46 # Connection init must come first
47 telepathy.server.Connection.__init__(
49 constants._telepathy_protocol_name_,
51 constants._telepathy_implementation_name_
53 aliasing.AliasingMixin.__init__(self)
54 simple_presence.SimplePresenceMixin.__init__(self)
55 capabilities.CapabilitiesMixin.__init__(self)
57 self._manager = weakref.proxy(manager)
59 parameters['username'].encode('utf-8'),
60 parameters['password'].encode('utf-8'),
62 self._callbackNumber = parameters['forward'].encode('utf-8')
63 self._channelManager = channel_manager.ChannelManager(self)
65 self._session = gvoice.session.Session(None)
67 self.set_self_handle(handle.create_handle(self, 'connection'))
70 _moduleLogger.info("Connection to the account %s created" % account)
72 _moduleLogger.exception("Failed to create Connection")
85 return self._credentials[0]
87 def handle(self, handleType, handleId):
88 self.check_handle(handleType, handleId)
89 return self._handles[handleType, handleId]
91 @gtk_toolbox.log_exception(_moduleLogger)
94 For org.freedesktop.telepathy.Connection
96 _moduleLogger.info("Connecting...")
98 telepathy.CONNECTION_STATUS_CONNECTING,
99 telepathy.CONNECTION_STATUS_REASON_REQUESTED
102 cookieFilePath = None
103 self._session = gvoice.session.Session(cookieFilePath)
105 self._callback = coroutines.func_sink(
106 coroutines.expand_positional(
107 self._on_conversations_updated
110 self.session.conversations.updateSignalHandler.register_sink(
113 self.session.login(*self._credentials)
114 self.session.backend.set_callback_number(self._callbackNumber)
115 except gvoice.backend.NetworkError, e:
116 _moduleLogger.exception("Connection Failed")
118 telepathy.CONNECTION_STATUS_DISCONNECTED,
119 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
122 _moduleLogger.exception("Connection Failed")
124 telepathy.CONNECTION_STATUS_DISCONNECTED,
125 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
128 _moduleLogger.info("Connected")
130 telepathy.CONNECTION_STATUS_CONNECTED,
131 telepathy.CONNECTION_STATUS_REASON_REQUESTED
134 @gtk_toolbox.log_exception(_moduleLogger)
135 def Disconnect(self):
137 For org.freedesktop.telepathy.Connection
138 @bug Not properly logging out. Cookie files need to be per connection and removed
140 _moduleLogger.info("Disconnecting")
142 self.session.conversations.updateSignalHandler.unregister_sink(
145 self._callback = None
146 self._channelManager.close()
147 self.session.logout()
150 _moduleLogger.info("Disconnected")
152 _moduleLogger.exception("Disconnecting Failed")
154 telepathy.CONNECTION_STATUS_DISCONNECTED,
155 telepathy.CONNECTION_STATUS_REASON_REQUESTED
157 self.manager.disconnected(self)
159 @gtk_toolbox.log_exception(_moduleLogger)
160 def RequestChannel(self, type, handleType, handleId, suppressHandler):
162 For org.freedesktop.telepathy.Connection
164 @param type DBus interface name for base channel type
165 @param handleId represents a contact, list, etc according to handleType
167 @returns DBus object path for the channel created or retrieved
169 self.check_connected()
170 self.check_handle(handleType, handleId)
173 channelManager = self._channelManager
174 handle = self.handle(handleType, handleId)
176 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
177 _moduleLogger.info("RequestChannel ContactList")
178 channel = channelManager.channel_for_list(handle, suppressHandler)
179 elif type == telepathy.CHANNEL_TYPE_TEXT:
180 _moduleLogger.info("RequestChannel Text")
181 channel = channelManager.channel_for_text(handle, suppressHandler)
182 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
183 _moduleLogger.info("RequestChannel Media")
184 channel = channelManager.channel_for_call(handle, suppressHandler)
186 raise telepathy.errors.NotImplemented("unknown channel type %s" % type)
188 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
189 return channel._object_path
191 @gtk_toolbox.log_exception(_moduleLogger)
192 def RequestHandles(self, handleType, names, sender):
194 For org.freedesktop.telepathy.Connection
195 Overiding telepathy.server.Connecton to allow custom handles
197 self.check_connected()
198 self.check_handle_type(handleType)
202 name = name.encode('utf-8')
203 if handleType == telepathy.HANDLE_TYPE_CONTACT:
204 _moduleLogger.info("RequestHandles Contact: %s" % name)
205 h = self._create_contact_handle(name)
206 elif handleType == telepathy.HANDLE_TYPE_LIST:
207 # Support only server side (immutable) lists
208 _moduleLogger.info("RequestHandles List: %s" % name)
209 h = handle.create_handle(self, 'list', name)
211 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
213 self.add_client_handle(h, sender)
216 def _create_contact_handle(self, requestedHandleName):
217 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
220 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
224 @gtk_toolbox.log_exception(_moduleLogger)
225 def _on_conversations_updated(self, conv, conversationIds):
226 # @todo get conversations update running
227 # @todo test conversatiuons
228 _moduleLogger.info("Incoming messages from: %r" % (conversationIds, ))
229 channelManager = self._channelManager
230 for contactId, phoneNumber in conversationIds:
231 h = handle.create_handle(self, 'contact', contactId, phoneNumber)
232 # Just let the TextChannel decide whether it should be reported to the user or not
233 channel = channelManager.channel_for_text(h)