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 _moduleLogger.info("Connection to the account %s created" % account)
69 _moduleLogger.exception("Failed to create Connection")
82 return self._credentials[0]
84 def handle(self, handleType, handleId):
85 self.check_handle(handleType, handleId)
86 return self._handles[handleType, handleId]
88 @gtk_toolbox.log_exception(_moduleLogger)
91 For org.freedesktop.telepathy.Connection
93 _moduleLogger.info("Connecting...")
95 telepathy.CONNECTION_STATUS_CONNECTING,
96 telepathy.CONNECTION_STATUS_REASON_REQUESTED
99 self.session.conversations.updateSignalHandler.register_sink(
100 self._on_conversations_updated
102 self.session.login(*self._credentials)
103 self.session.backend.set_callback_number(self._callbackNumber)
104 except gvoice.backend.NetworkError, e:
105 _moduleLogger.exception("Connection Failed")
107 telepathy.CONNECTION_STATUS_DISCONNECTED,
108 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
111 _moduleLogger.exception("Connection Failed")
113 telepathy.CONNECTION_STATUS_DISCONNECTED,
114 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
117 _moduleLogger.info("Connected")
119 telepathy.CONNECTION_STATUS_CONNECTED,
120 telepathy.CONNECTION_STATUS_REASON_REQUESTED
123 @gtk_toolbox.log_exception(_moduleLogger)
124 def Disconnect(self):
126 For org.freedesktop.telepathy.Connection
127 @bug Not properly logging out. Cookie files need to be per connection and removed
129 _moduleLogger.info("Disconnecting")
131 self.session.conversations.updateSignalHandler.unregister_sink(
132 self._on_conversations_updated
134 self._channelManager.close()
135 self.session.logout()
136 _moduleLogger.info("Disconnected")
138 _moduleLogger.exception("Disconnecting Failed")
140 telepathy.CONNECTION_STATUS_DISCONNECTED,
141 telepathy.CONNECTION_STATUS_REASON_REQUESTED
144 @gtk_toolbox.log_exception(_moduleLogger)
145 def RequestChannel(self, type, handleType, handleId, suppressHandler):
147 For org.freedesktop.telepathy.Connection
149 @param type DBus interface name for base channel type
150 @param handleId represents a contact, list, etc according to handleType
152 @returns DBus object path for the channel created or retrieved
154 self.check_connected()
155 self.check_handle(handleType, handleId)
158 channelManager = self._channelManager
159 handle = self.handle(handleType, handleId)
161 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
162 _moduleLogger.info("RequestChannel ContactList")
163 channel = channelManager.channel_for_list(handle, suppressHandler)
164 elif type == telepathy.CHANNEL_TYPE_TEXT:
165 _moduleLogger.info("RequestChannel Text")
166 channel = channelManager.channel_for_text(handle, suppressHandler)
167 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
168 _moduleLogger.info("RequestChannel Media")
169 channel = channelManager.channel_for_call(handle, suppressHandler)
171 raise telepathy.errors.NotImplemented("unknown channel type %s" % type)
173 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
174 return channel._object_path
176 @gtk_toolbox.log_exception(_moduleLogger)
177 def RequestHandles(self, handleType, names, sender):
179 For org.freedesktop.telepathy.Connection
180 Overiding telepathy.server.Connecton to allow custom handles
182 self.check_connected()
183 self.check_handle_type(handleType)
187 name = name.encode('utf-8')
188 if handleType == telepathy.HANDLE_TYPE_CONTACT:
189 _moduleLogger.info("RequestHandles Contact: %s" % name)
190 h = self._create_contact_handle(name)
191 elif handleType == telepathy.HANDLE_TYPE_LIST:
192 # Support only server side (immutable) lists
193 _moduleLogger.info("RequestHandles List: %s" % name)
194 h = handle.create_handle(self, 'list', name)
196 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
198 self.add_client_handle(h, sender)
201 def _create_contact_handle(self, requestedHandleName):
202 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
205 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
208 @coroutines.func_sink
209 @coroutines.expand_positional
211 @gtk_toolbox.log_exception(_moduleLogger)
212 def _on_conversations_updated(self, conversationIds):
213 # @todo get conversations update running
214 # @todo test conversatiuons
215 _moduleLogger.info("Incoming messages from: %r" % (conversationIds, ))
216 channelManager = self._channelManager
217 for contactId, phoneNumber in conversationIds:
218 h = self._create_contact_handle(contactId, phoneNumber)
219 # if its new, __init__ will take care of things
220 # if its old, its own update will take care of it
221 channel = channelManager.channel_for_text(handle)