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)
62 cookieFilePath = "%s/cookies.txt" % constants._data_path_
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.login(*self._credentials)
100 self.session.backend.set_callback_number(self._callbackNumber)
101 except gvoice.backend.NetworkError, e:
102 _moduleLogger.exception("Connection Failed")
104 telepathy.CONNECTION_STATUS_DISCONNECTED,
105 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
108 _moduleLogger.exception("Connection Failed")
110 telepathy.CONNECTION_STATUS_DISCONNECTED,
111 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
114 _moduleLogger.info("Connected")
116 telepathy.CONNECTION_STATUS_CONNECTED,
117 telepathy.CONNECTION_STATUS_REASON_REQUESTED
120 @gtk_toolbox.log_exception(_moduleLogger)
121 def Disconnect(self):
123 For org.freedesktop.telepathy.Connection
124 @bug Not properly logging out. Cookie files need to be per connection and removed
126 _moduleLogger.info("Disconnecting")
128 self.session.logout()
129 _moduleLogger.info("Disconnected")
131 _moduleLogger.exception("Disconnecting Failed")
133 telepathy.CONNECTION_STATUS_DISCONNECTED,
134 telepathy.CONNECTION_STATUS_REASON_REQUESTED
137 @gtk_toolbox.log_exception(_moduleLogger)
138 def RequestChannel(self, type, handleType, handleId, suppressHandler):
140 For org.freedesktop.telepathy.Connection
142 @param type DBus interface name for base channel type
143 @param handleId represents a contact, list, etc according to handleType
145 @returns DBus object path for the channel created or retrieved
147 self.check_connected()
148 self.check_handle(handleType, handleId)
151 channelManager = self._channelManager
152 handle = self.handle(handleType, handleId)
154 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
155 _moduleLogger.info("RequestChannel ContactList")
156 channel = channelManager.channel_for_list(handle, suppressHandler)
157 elif type == telepathy.CHANNEL_TYPE_TEXT:
158 _moduleLogger.info("RequestChannel Text")
159 channel = channelManager.channel_for_text(handle, suppressHandler)
160 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
161 _moduleLogger.info("RequestChannel Media")
162 channel = channelManager.channel_for_call(handle, suppressHandler)
164 raise telepathy.errors.NotImplemented("unknown channel type %s" % type)
166 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
167 return channel._object_path
169 @gtk_toolbox.log_exception(_moduleLogger)
170 def RequestHandles(self, handleType, names, sender):
172 For org.freedesktop.telepathy.Connection
173 Overiding telepathy.server.Connecton to allow custom handles
175 self.check_connected()
176 self.check_handle_type(handleType)
180 name = name.encode('utf-8')
181 if handleType == telepathy.HANDLE_TYPE_CONTACT:
182 _moduleLogger.info("RequestHandles Contact: %s" % name)
183 h = self._create_contact_handle(name)
184 elif handleType == telepathy.HANDLE_TYPE_LIST:
185 # Support only server side (immutable) lists
186 _moduleLogger.info("RequestHandles List: %s" % name)
187 h = handle.create_handle(self, 'list', name)
189 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
191 self.add_client_handle(h, sender)
194 def _create_contact_handle(self, requestedHandleName):
195 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
198 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
201 @coroutines.func_sink
202 @coroutines.expand_positional
204 def _on_conversations_updated(self, conversationIds):
205 # @todo get conversations update running
206 # @todo test conversatiuons
207 channelManager = self._channelManager
208 for contactId, phoneNumber in conversationIds:
209 h = self._create_contact_handle(contactId, phoneNumber)
210 # if its new, __init__ will take care of things
211 # if its old, its own update will take care of it
212 channel = channelManager.channel_for_text(handle)