7 import util.go_utils as gobject_utils
8 import util.coroutines as coroutines
13 import simple_presence
16 import channel_manager
19 _moduleLogger = logging.getLogger("connection")
22 class TheOneRingConnection(
23 telepathy.server.Connection,
24 aliasing.AliasingMixin,
25 simple_presence.SimplePresenceMixin,
26 presence.PresenceMixin,
27 capabilities.CapabilitiesMixin,
30 # Overriding a base class variable
31 # Should the forwarding number be handled by the alias or by an option?
32 _mandatory_parameters = {
37 # Overriding a base class variable
38 _optional_parameters = {
40 _parameter_defaults = {
43 def __init__(self, manager, parameters):
44 self.check_parameters(parameters)
46 account = unicode(parameters['username'])
48 # Connection init must come first
49 telepathy.server.Connection.__init__(
51 constants._telepathy_protocol_name_,
53 constants._telepathy_implementation_name_
55 aliasing.AliasingMixin.__init__(self)
56 simple_presence.SimplePresenceMixin.__init__(self)
57 presence.PresenceMixin.__init__(self)
58 capabilities.CapabilitiesMixin.__init__(self)
60 self._manager = weakref.proxy(manager)
62 parameters['username'].encode('utf-8'),
63 parameters['password'].encode('utf-8'),
65 self._callbackNumber = parameters['forward'].encode('utf-8')
66 self._channelManager = channel_manager.ChannelManager(self)
68 self._session = gvoice.session.Session(None)
70 self.set_self_handle(handle.create_handle(self, 'connection'))
73 _moduleLogger.info("Connection to the account %s created" % account)
75 _moduleLogger.exception("Failed to create Connection")
88 return self._credentials[0]
90 def handle(self, handleType, handleId):
91 self.check_handle(handleType, handleId)
92 return self._handles[handleType, handleId]
94 @gtk_toolbox.log_exception(_moduleLogger)
97 For org.freedesktop.telepathy.Connection
99 _moduleLogger.info("Connecting...")
101 telepathy.CONNECTION_STATUS_CONNECTING,
102 telepathy.CONNECTION_STATUS_REASON_REQUESTED
105 cookieFilePath = None
106 self._session = gvoice.session.Session(cookieFilePath)
108 self._callback = coroutines.func_sink(
109 coroutines.expand_positional(
110 self._on_conversations_updated
113 self.session.conversations.updateSignalHandler.register_sink(
116 self.session.login(*self._credentials)
117 self.session.backend.set_callback_number(self._callbackNumber)
118 except gvoice.backend.NetworkError, e:
119 _moduleLogger.exception("Connection Failed")
121 telepathy.CONNECTION_STATUS_DISCONNECTED,
122 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
125 _moduleLogger.exception("Connection Failed")
127 telepathy.CONNECTION_STATUS_DISCONNECTED,
128 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
131 _moduleLogger.info("Connected")
133 telepathy.CONNECTION_STATUS_CONNECTED,
134 telepathy.CONNECTION_STATUS_REASON_REQUESTED
137 @gtk_toolbox.log_exception(_moduleLogger)
138 def Disconnect(self):
140 For org.freedesktop.telepathy.Connection
141 @bug Not properly logging out. Cookie files need to be per connection and removed
143 _moduleLogger.info("Disconnecting")
145 self.session.conversations.updateSignalHandler.unregister_sink(
148 self._callback = None
149 self._channelManager.close()
150 self.session.logout()
153 _moduleLogger.info("Disconnected")
155 _moduleLogger.exception("Disconnecting Failed")
157 telepathy.CONNECTION_STATUS_DISCONNECTED,
158 telepathy.CONNECTION_STATUS_REASON_REQUESTED
160 self.manager.disconnected(self)
162 @gtk_toolbox.log_exception(_moduleLogger)
163 def RequestChannel(self, type, handleType, handleId, suppressHandler):
165 For org.freedesktop.telepathy.Connection
167 @param type DBus interface name for base channel type
168 @param handleId represents a contact, list, etc according to handleType
170 @returns DBus object path for the channel created or retrieved
172 self.check_connected()
173 self.check_handle(handleType, handleId)
176 channelManager = self._channelManager
177 handle = self.handle(handleType, handleId)
179 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
180 _moduleLogger.info("RequestChannel ContactList")
181 channel = channelManager.channel_for_list(handle, suppressHandler)
182 elif type == telepathy.CHANNEL_TYPE_TEXT:
183 _moduleLogger.info("RequestChannel Text")
184 channel = channelManager.channel_for_text(handle, suppressHandler)
185 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
186 _moduleLogger.info("RequestChannel Media")
187 channel = channelManager.channel_for_call(handle, suppressHandler)
189 raise telepathy.errors.NotImplemented("unknown channel type %s" % type)
191 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
192 return channel._object_path
194 @gtk_toolbox.log_exception(_moduleLogger)
195 def RequestHandles(self, handleType, names, sender):
197 For org.freedesktop.telepathy.Connection
198 Overiding telepathy.server.Connecton to allow custom handles
200 self.check_connected()
201 self.check_handle_type(handleType)
205 name = name.encode('utf-8')
206 if handleType == telepathy.HANDLE_TYPE_CONTACT:
207 _moduleLogger.info("RequestHandles Contact: %s" % name)
208 h = self._create_contact_handle(name)
209 elif handleType == telepathy.HANDLE_TYPE_LIST:
210 # Support only server side (immutable) lists
211 _moduleLogger.info("RequestHandles List: %s" % name)
212 h = handle.create_handle(self, 'list', name)
214 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
216 self.add_client_handle(h, sender)
219 def _create_contact_handle(self, requestedHandleName):
220 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
223 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
227 @gtk_toolbox.log_exception(_moduleLogger)
228 def _on_conversations_updated(self, conv, conversationIds):
229 # @todo get conversations update running
230 # @todo test conversatiuons
231 _moduleLogger.info("Incoming messages from: %r" % (conversationIds, ))
232 channelManager = self._channelManager
233 for contactId, phoneNumber in conversationIds:
234 h = handle.create_handle(self, 'contact', contactId, phoneNumber)
235 # Just let the TextChannel decide whether it should be reported to the user or not
236 channel = channelManager.channel_for_text(h)