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)
50 self._addressbook = gvoice.addressbook.Addressbook(self._backend)
52 self.set_self_handle(handle.create_handle(self, 'connection'))
54 _moduleLogger.info("Connection to the account %s created" % account)
56 _moduleLogger.exception("Failed to create Connection")
64 def gvoice_backend(self):
68 def addressbook(self):
69 return self._addressbook
75 def handle(self, handleType, handleId):
76 self.check_handle(handleType, handleId)
77 return self._handles[handleType, handleId]
81 For org.freedesktop.telepathy.Connection
84 telepathy.CONNECTION_STATUS_CONNECTING,
85 telepathy.CONNECTION_STATUS_REASON_REQUESTED
88 self._backend.login(*self._credentials)
89 self._backend.set_callback_number(self._callbackNumber)
90 except gvoice.dialer.NetworkError:
92 telepathy.CONNECTION_STATUS_DISCONNECTED,
93 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
97 telepathy.CONNECTION_STATUS_DISCONNECTED,
98 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
102 telepathy.CONNECTION_STATUS_CONNECTED,
103 telepathy.CONNECTION_STATUS_REASON_REQUESTED
106 def Disconnect(self):
108 For org.freedesktop.telepathy.Connection
111 self._backend.logout()
112 _moduleLogger.info("Disconnected")
114 _moduleLogger.exception("Disconnecting Failed")
116 telepathy.CONNECTION_STATUS_DISCONNECTED,
117 telepathy.CONNECTION_STATUS_REASON_REQUESTED
120 def RequestChannel(self, type, handleType, handleId, suppressHandler):
122 For org.freedesktop.telepathy.Connection
124 @param type DBus interface name for base channel type
125 @param handleId represents a contact, list, etc according to handleType
127 @returns DBus object path for the channel created or retrieved
129 self.check_connected()
132 channelManager = self._channelManager
133 handle = self.handle(handleType, handleId)
135 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
136 channel = channelManager.channel_for_list(handle, suppressHandler)
137 elif type == telepathy.CHANNEL_TYPE_TEXT:
138 if handleType != telepathy.HANDLE_TYPE_CONTACT:
139 raise telepathy.NotImplemented("Only Contacts are allowed")
140 channel = channelManager.channel_for_text(handle, None, suppressHandler)
141 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
142 if handleType != telepathy.HANDLE_TYPE_CONTACT:
143 raise telepathy.NotImplemented("Only Contacts are allowed")
144 channel = channelManager.channel_for_text(handle, None, suppressHandler)
146 raise telepathy.NotImplemented("unknown channel type %s" % type)
148 return channel._object_path
150 def RequestHandles(self, handleType, names, sender):
152 For org.freedesktop.telepathy.Connection
154 self.check_connected()
155 self.check_handle_type(handleType)
159 name = name.encode('utf-8')
160 if handleType == telepathy.HANDLE_TYPE_CONTACT:
161 h = self._create_contact_handle(name)
162 elif handleType == telepathy.HANDLE_TYPE_LIST:
163 # Support only server side (immutable) lists
164 h = handle.create_handle(self, 'list', name)
166 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
168 self.add_client_handle(handle, sender)
171 def _create_contact_handle(self, name):
172 requestedContactId = name
174 contacts = self._addressbook.get_contacts()
176 contactId for contactId in contacts
177 if contactId == requestedContactId
180 if 0 < len(contactsFound):
181 contactId = contactsFound[0]
182 if len(contactsFound) != 1:
183 _moduleLogger.error("Contact ID was not unique: %s for %s" % (contactId, ))
185 contactId = requestedContactId
186 h = handle.create_handle(self, 'contact', contactId)