10 import simple_presence
13 _moduleLogger = logging.getLogger("connection")
16 class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePresenceMixin):
18 MANDATORY_PARAMETERS = {
23 OPTIONAL_PARAMETERS = {
25 PARAMETER_DEFAULTS = {
28 def __init__(self, manager, parameters):
30 self.check_parameters(parameters)
31 account = unicode(parameters['account'])
33 telepathy.server.Connection.__init__(
35 constants._telepathy_protocol_name_,
37 constants._telepathy_implementation_name_
40 self._manager = weakref.proxy(manager)
42 parameters['account'].encode('utf-8'),
43 parameters['password'].encode('utf-8'),
45 self._callbackNumber = parameters['forward'].encode('utf-8')
46 self._channelManager = channel_manager.ChannelManager(self)
48 cookieFilePath = "%s/cookies.txt" % constants._data_path_
49 self._backend = gvoice.dialer.GVDialer(cookieFilePath)
51 self.set_self_handle(handle.create_handle(self, 'connection'))
53 _moduleLogger.info("Connection to the account %s created" % account)
55 _moduleLogger.exception("Failed to create Connection")
63 def gvoice_backend(self):
70 def handle(self, handleType, handleId):
71 self.check_handle(handleType, handleId)
72 return self._handles[handleType, handleId]
76 For org.freedesktop.telepathy.Connection
79 telepathy.CONNECTION_STATUS_CONNECTING,
80 telepathy.CONNECTION_STATUS_REASON_REQUESTED
83 self._backend.login(*self._credentials)
84 self._backend.set_callback_number(self._callbackNumber)
85 except gvoice.dialer.NetworkError:
87 telepathy.CONNECTION_STATUS_DISCONNECTED,
88 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
92 telepathy.CONNECTION_STATUS_DISCONNECTED,
93 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
97 telepathy.CONNECTION_STATUS_CONNECTED,
98 telepathy.CONNECTION_STATUS_REASON_REQUESTED
101 def Disconnect(self):
103 For org.freedesktop.telepathy.Connection
106 self._backend.logout()
107 _moduleLogger.info("Disconnected")
109 _moduleLogger.exception("Disconnecting Failed")
111 telepathy.CONNECTION_STATUS_DISCONNECTED,
112 telepathy.CONNECTION_STATUS_REASON_REQUESTED
115 def RequestChannel(self, type, handleType, handleId, suppressHandler):
117 For org.freedesktop.telepathy.Connection
119 @param type DBus interface name for base channel type
120 @param handleId represents a contact, list, etc according to handleType
122 @returns DBus object path for the channel created or retrieved
124 self.check_connected()
127 channelManager = self._channelManager
128 handle = self.handle(handleType, handleId)
130 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
131 channel = channelManager.channel_for_list(handle, suppressHandler)
132 elif type == telepathy.CHANNEL_TYPE_TEXT:
133 if handleType != telepathy.HANDLE_TYPE_CONTACT:
134 raise telepathy.NotImplemented("Only Contacts are allowed")
135 channel = channelManager.channel_for_text(handle, None, suppressHandler)
136 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
137 if handleType != telepathy.HANDLE_TYPE_CONTACT:
138 raise telepathy.NotImplemented("Only Contacts are allowed")
139 channel = channelManager.channel_for_text(handle, None, suppressHandler)
141 raise telepathy.NotImplemented("unknown channel type %s" % type)
143 return channel._object_path
145 def RequestHandles(self, handleType, names, sender):
147 For org.freedesktop.telepathy.Connection
149 self.check_connected()
150 self.check_handle_type(handleType)
154 name = name.encode('utf-8')
155 if handleType == telepathy.HANDLE_TYPE_CONTACT:
156 h = self._create_contact_handle(name)
157 elif handleType == telepathy.HANDLE_TYPE_LIST:
158 h = handle.create_handle(self, 'list', name)
159 elif handleType == telepathy.HANDLE_TYPE_GROUP:
160 h = handle.create_handle(self, 'group', name)
162 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
164 self.add_client_handle(handle, sender)
167 def _create_contact_handle(self, name):
168 requestedContactId = name
170 contacts = self._backend.get_contacts()
172 (contactId, contactName) for (contactId, contactName) in contacts
173 if contactId == requestedContactId
176 if 0 < len(contactsFound):
177 contactId, contactName = contactsFound[0]
178 if len(contactsFound) != 1:
179 _moduleLogger.error("Contact ID was not unique: %s for %s" % (contactId, contactName))
181 contactId, contactName = requestedContactId, ""
182 h = handle.create_handle(self, 'contact', contactId, contactName)