10 import channel_manager
13 _moduleLogger = logging.getLogger("connection")
16 class TheOneRingConnection(telepathy.server.Connection):
18 # Overriding a base class variable
19 _mandatory_parameters = {
24 # Overriding a base class variable
25 _optional_parameters = {
27 _parameter_defaults = {
30 def __init__(self, manager, parameters):
32 self.check_parameters(parameters)
33 account = unicode(parameters['username'])
35 telepathy.server.Connection.__init__(
37 constants._telepathy_protocol_name_,
39 constants._telepathy_implementation_name_
42 self._manager = weakref.proxy(manager)
44 parameters['username'].encode('utf-8'),
45 parameters['password'].encode('utf-8'),
47 self._callbackNumber = parameters['forward'].encode('utf-8')
48 self._channelManager = channel_manager.ChannelManager(self)
50 cookieFilePath = "%s/cookies.txt" % constants._data_path_
51 self._session = gvoice.session.Session(cookieFilePath)
53 self.set_self_handle(handle.create_handle(self, 'connection'))
55 _moduleLogger.info("Connection to the account %s created" % account)
57 _moduleLogger.exception("Failed to create Connection")
70 return self._credentials[0]
72 def handle(self, handleType, handleId):
73 self.check_handle(handleType, handleId)
74 return self._handles[handleType, handleId]
76 @gtk_toolbox.log_exception(_moduleLogger)
79 For org.freedesktop.telepathy.Connection
81 _moduleLogger.info("Connecting...")
83 telepathy.CONNECTION_STATUS_CONNECTING,
84 telepathy.CONNECTION_STATUS_REASON_REQUESTED
87 self.session.login(*self._credentials)
88 self.session.backend.set_callback_number(self._callbackNumber)
89 except gvoice.backend.NetworkError, e:
90 _moduleLogger.exception("Connection Failed")
92 telepathy.CONNECTION_STATUS_DISCONNECTED,
93 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
96 _moduleLogger.exception("Connection Failed")
98 telepathy.CONNECTION_STATUS_DISCONNECTED,
99 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
102 _moduleLogger.info("Connected")
104 telepathy.CONNECTION_STATUS_CONNECTED,
105 telepathy.CONNECTION_STATUS_REASON_REQUESTED
108 @gtk_toolbox.log_exception(_moduleLogger)
109 def Disconnect(self):
111 For org.freedesktop.telepathy.Connection
112 @bug Not properly logging out. Cookie files need to be per connection and removed
114 _moduleLogger.info("Disconnecting")
116 self.session.logout()
117 _moduleLogger.info("Disconnected")
119 _moduleLogger.exception("Disconnecting Failed")
121 telepathy.CONNECTION_STATUS_DISCONNECTED,
122 telepathy.CONNECTION_STATUS_REASON_REQUESTED
125 @gtk_toolbox.log_exception(_moduleLogger)
126 def RequestChannel(self, type, handleType, handleId, suppressHandler):
128 For org.freedesktop.telepathy.Connection
130 @param type DBus interface name for base channel type
131 @param handleId represents a contact, list, etc according to handleType
133 @returns DBus object path for the channel created or retrieved
135 self.check_connected()
136 self.check_handle(handleType, handleId)
139 channelManager = self._channelManager
140 handle = self.handle(handleType, handleId)
142 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
143 _moduleLogger.info("RequestChannel ContactList")
144 channel = channelManager.channel_for_list(handle, suppressHandler)
145 elif type == telepathy.CHANNEL_TYPE_TEXT:
146 _moduleLogger.info("RequestChannel Text")
147 channel = channelManager.channel_for_text(handle, suppressHandler)
148 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
149 _moduleLogger.info("RequestChannel Media")
150 channel = channelManager.channel_for_call(handle, suppressHandler)
152 raise telepathy.NotImplemented("unknown channel type %s" % type)
154 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
155 return channel._object_path
157 @gtk_toolbox.log_exception(_moduleLogger)
158 def RequestHandles(self, handleType, names, sender):
160 For org.freedesktop.telepathy.Connection
161 Overiding telepathy.server.Connecton to allow custom handles
163 self.check_connected()
164 self.check_handle_type(handleType)
168 name = name.encode('utf-8')
169 if handleType == telepathy.HANDLE_TYPE_CONTACT:
170 _moduleLogger.info("RequestHandles Contact: %s" % name)
171 h = self._create_contact_handle(name)
172 elif handleType == telepathy.HANDLE_TYPE_LIST:
173 # Support only server side (immutable) lists
174 _moduleLogger.info("RequestHandles List: %s" % name)
175 h = handle.create_handle(self, 'list', name)
177 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
179 self.add_client_handle(h, sender)
182 def _create_contact_handle(self, requestedHandleName):
184 @todo Determine if nay of this is really needed
186 requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
189 h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
192 def _on_invite_text(self, contactId):
196 h = self._create_contact_handle(contactId)
198 channelManager = self._channelManager
199 channel = channelManager.channel_for_text(handle)