12 _moduleLogger = logging.getLogger("connection")
15 class TheOneRingConnection(telepathy.server.Connection):
17 MANDATORY_PARAMETERS = {
22 OPTIONAL_PARAMETERS = {
24 PARAMETER_DEFAULTS = {
27 def __init__(self, manager, parameters):
29 self.check_parameters(parameters)
30 account = unicode(parameters['account'])
32 telepathy.server.Connection.__init__(
34 constants._telepathy_protocol_name_,
36 constants._telepathy_implementation_name_
39 self._manager = weakref.proxy(manager)
41 parameters['account'].encode('utf-8'),
42 parameters['password'].encode('utf-8'),
44 self._callbackNumber = parameters['forward'].encode('utf-8')
45 self._channelManager = channel_manager.ChannelManager(self)
47 cookieFilePath = "%s/cookies.txt" % constants._data_path_
48 self._session = gvoice.session.Session(cookieFilePath)
50 self.set_self_handle(handle.create_handle(self, 'connection'))
52 _moduleLogger.info("Connection to the account %s created" % account)
54 _moduleLogger.exception("Failed to create Connection")
69 def handle(self, handleType, handleId):
70 self.check_handle(handleType, handleId)
71 return self._handles[handleType, handleId]
75 For org.freedesktop.telepathy.Connection
78 telepathy.CONNECTION_STATUS_CONNECTING,
79 telepathy.CONNECTION_STATUS_REASON_REQUESTED
82 self.session.login(*self._credentials)
83 self.session.backend.set_callback_number(self._callbackNumber)
84 except gvoice.backend.NetworkError:
86 telepathy.CONNECTION_STATUS_DISCONNECTED,
87 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
91 telepathy.CONNECTION_STATUS_DISCONNECTED,
92 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
96 telepathy.CONNECTION_STATUS_CONNECTED,
97 telepathy.CONNECTION_STATUS_REASON_REQUESTED
100 def Disconnect(self):
102 For org.freedesktop.telepathy.Connection
105 self.session.logout()
106 _moduleLogger.info("Disconnected")
108 _moduleLogger.exception("Disconnecting Failed")
110 telepathy.CONNECTION_STATUS_DISCONNECTED,
111 telepathy.CONNECTION_STATUS_REASON_REQUESTED
114 def RequestChannel(self, type, handleType, handleId, suppressHandler):
116 For org.freedesktop.telepathy.Connection
118 @param type DBus interface name for base channel type
119 @param handleId represents a contact, list, etc according to handleType
121 @returns DBus object path for the channel created or retrieved
123 self.check_connected()
126 channelManager = self._channelManager
127 handle = self.handle(handleType, handleId)
129 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
130 channel = channelManager.channel_for_list(handle, suppressHandler)
131 elif type == telepathy.CHANNEL_TYPE_TEXT:
132 if handleType != telepathy.HANDLE_TYPE_CONTACT:
133 raise telepathy.NotImplemented("Only Contacts are allowed")
134 channel = channelManager.channel_for_text(handle, None, suppressHandler)
135 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
136 if handleType != telepathy.HANDLE_TYPE_CONTACT:
137 raise telepathy.NotImplemented("Only Contacts are allowed")
138 channel = channelManager.channel_for_text(handle, None, suppressHandler)
140 raise telepathy.NotImplemented("unknown channel type %s" % type)
142 return channel._object_path
144 def RequestHandles(self, handleType, names, sender):
146 For org.freedesktop.telepathy.Connection
148 self.check_connected()
149 self.check_handle_type(handleType)
153 name = name.encode('utf-8')
154 if handleType == telepathy.HANDLE_TYPE_CONTACT:
155 h = self._create_contact_handle(name)
156 elif handleType == telepathy.HANDLE_TYPE_LIST:
157 # Support only server side (immutable) lists
158 h = handle.create_handle(self, 'list', name)
160 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
162 self.add_client_handle(handle, sender)
165 def _create_contact_handle(self, name):
166 requestedContactId = name
168 contacts = self.session.addressbook.get_contacts()
170 contactId for contactId in contacts
171 if contactId == requestedContactId
174 if 0 < len(contactsFound):
175 contactId = contactsFound[0]
176 if len(contactsFound) != 1:
177 _moduleLogger.error("Contact ID was not unique: %s for %s" % (contactId, ))
179 contactId = requestedContactId
180 h = handle.create_handle(self, 'contact', contactId)