11 import channel_manager
14 _moduleLogger = logging.getLogger("connection")
17 class TheOneRingConnection(
18 telepathy.server.Connection,
19 aliasing.AliasingMixin
22 # Overriding a base class variable
23 _mandatory_parameters = {
28 # Overriding a base class variable
29 _optional_parameters = {
31 _parameter_defaults = {
34 def __init__(self, manager, parameters):
36 self.check_parameters(parameters)
37 account = unicode(parameters['username'])
39 # Connection init must come first
40 telepathy.server.Connection.__init__(
42 constants._telepathy_protocol_name_,
44 constants._telepathy_implementation_name_
46 aliasing.AliasingMixin.__init__(self)
48 self._manager = weakref.proxy(manager)
50 parameters['username'].encode('utf-8'),
51 parameters['password'].encode('utf-8'),
53 self._callbackNumber = parameters['forward'].encode('utf-8')
54 self._channelManager = channel_manager.ChannelManager(self)
56 cookieFilePath = "%s/cookies.txt" % constants._data_path_
57 self._session = gvoice.session.Session(cookieFilePath)
59 self.set_self_handle(handle.create_handle(self, 'connection'))
61 _moduleLogger.info("Connection to the account %s created" % account)
63 _moduleLogger.exception("Failed to create Connection")
76 return self._credentials[0]
78 def handle(self, handleType, handleId):
79 self.check_handle(handleType, handleId)
80 return self._handles[handleType, handleId]
82 @gtk_toolbox.log_exception(_moduleLogger)
85 For org.freedesktop.telepathy.Connection
87 _moduleLogger.info("Connecting...")
89 telepathy.CONNECTION_STATUS_CONNECTING,
90 telepathy.CONNECTION_STATUS_REASON_REQUESTED
93 self.session.login(*self._credentials)
94 self.session.backend.set_callback_number(self._callbackNumber)
95 except gvoice.backend.NetworkError, e:
96 _moduleLogger.exception("Connection Failed")
98 telepathy.CONNECTION_STATUS_DISCONNECTED,
99 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
102 _moduleLogger.exception("Connection Failed")
104 telepathy.CONNECTION_STATUS_DISCONNECTED,
105 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
108 _moduleLogger.info("Connected")
110 telepathy.CONNECTION_STATUS_CONNECTED,
111 telepathy.CONNECTION_STATUS_REASON_REQUESTED
114 @gtk_toolbox.log_exception(_moduleLogger)
115 def Disconnect(self):
117 For org.freedesktop.telepathy.Connection
118 @bug Not properly logging out. Cookie files need to be per connection and removed
120 _moduleLogger.info("Disconnecting")
122 self.session.logout()
123 _moduleLogger.info("Disconnected")
125 _moduleLogger.exception("Disconnecting Failed")
127 telepathy.CONNECTION_STATUS_DISCONNECTED,
128 telepathy.CONNECTION_STATUS_REASON_REQUESTED
131 @gtk_toolbox.log_exception(_moduleLogger)
132 def RequestChannel(self, type, handleType, handleId, suppressHandler):
134 For org.freedesktop.telepathy.Connection
136 @param type DBus interface name for base channel type
137 @param handleId represents a contact, list, etc according to handleType
139 @returns DBus object path for the channel created or retrieved
141 self.check_connected()
142 self.check_handle(handleType, handleId)
145 channelManager = self._channelManager
146 handle = self.handle(handleType, handleId)
148 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
149 _moduleLogger.info("RequestChannel ContactList")
150 channel = channelManager.channel_for_list(handle, suppressHandler)
151 elif type == telepathy.CHANNEL_TYPE_TEXT:
152 _moduleLogger.info("RequestChannel Text")
153 channel = channelManager.channel_for_text(handle, suppressHandler)
154 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
155 _moduleLogger.info("RequestChannel Media")
156 channel = channelManager.channel_for_call(handle, suppressHandler)
158 raise telepathy.NotImplemented("unknown channel type %s" % type)
160 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
161 return channel._object_path
163 @gtk_toolbox.log_exception(_moduleLogger)
164 def RequestHandles(self, handleType, names, sender):
166 For org.freedesktop.telepathy.Connection
167 Overiding telepathy.server.Connecton to allow custom handles
169 self.check_connected()
170 self.check_handle_type(handleType)
174 name = name.encode('utf-8')
175 if handleType == telepathy.HANDLE_TYPE_CONTACT:
176 _moduleLogger.info("RequestHandles Contact: %s" % name)
177 h = self._create_contact_handle(name)
178 elif handleType == telepathy.HANDLE_TYPE_LIST:
179 # Support only server side (immutable) lists
180 _moduleLogger.info("RequestHandles List: %s" % name)
181 h = handle.create_handle(self, 'list', name)
183 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
185 self.add_client_handle(h, sender)
188 def _create_contact_handle(self, requestedHandleName):
190 @todo Determine if nay of this is really needed
192 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
195 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
198 def _on_invite_text(self, contactId):
202 h = self._create_contact_handle(contactId)
204 channelManager = self._channelManager
205 channel = channelManager.channel_for_text(handle)