12 _moduleLogger = logging.getLogger("connection")
15 class TheOneRingConnection(telepathy.server.Connection):
17 # Overriding a base class variable
18 _mandatory_parameters = {
23 # Overriding a base class variable
24 _optional_parameters = {
26 _parameter_defaults = {
29 def __init__(self, manager, parameters):
31 self.check_parameters(parameters)
32 account = unicode(parameters['username'])
34 telepathy.server.Connection.__init__(
36 constants._telepathy_protocol_name_,
38 constants._telepathy_implementation_name_
41 self._manager = weakref.proxy(manager)
43 parameters['username'].encode('utf-8'),
44 parameters['password'].encode('utf-8'),
46 self._callbackNumber = parameters['forward'].encode('utf-8')
47 self._channelManager = channel_manager.ChannelManager(self)
49 cookieFilePath = "%s/cookies.txt" % constants._data_path_
50 self._session = gvoice.session.Session(cookieFilePath)
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")
69 return self._credentials[0]
71 def handle(self, handleType, handleId):
72 self.check_handle(handleType, handleId)
73 return self._handles[handleType, handleId]
77 For org.freedesktop.telepathy.Connection
79 _moduleLogger.info("Connecting...")
81 telepathy.CONNECTION_STATUS_CONNECTING,
82 telepathy.CONNECTION_STATUS_REASON_REQUESTED
85 self.session.login(*self._credentials)
86 self.session.backend.set_callback_number(self._callbackNumber)
87 except gvoice.backend.NetworkError, e:
88 _moduleLogger.exception("Connection Failed")
90 telepathy.CONNECTION_STATUS_DISCONNECTED,
91 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
94 _moduleLogger.exception("Connection Failed")
96 telepathy.CONNECTION_STATUS_DISCONNECTED,
97 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
100 _moduleLogger.info("Connected")
102 telepathy.CONNECTION_STATUS_CONNECTED,
103 telepathy.CONNECTION_STATUS_REASON_REQUESTED
106 def Disconnect(self):
108 For org.freedesktop.telepathy.Connection
109 @bug Not properly logging out. Cookie files need to be per connection and removed
111 _moduleLogger.info("Disconnecting")
113 self.session.logout()
114 _moduleLogger.info("Disconnected")
116 _moduleLogger.exception("Disconnecting Failed")
118 telepathy.CONNECTION_STATUS_DISCONNECTED,
119 telepathy.CONNECTION_STATUS_REASON_REQUESTED
122 def RequestChannel(self, type, handleType, handleId, suppressHandler):
124 For org.freedesktop.telepathy.Connection
126 @param type DBus interface name for base channel type
127 @param handleId represents a contact, list, etc according to handleType
129 @returns DBus object path for the channel created or retrieved
131 self.check_connected()
134 channelManager = self._channelManager
135 handle = self.handle(handleType, handleId)
137 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
138 _moduleLogger.info("RequestChannel ContactList")
139 channel = channelManager.channel_for_list(handle, suppressHandler)
140 elif type == telepathy.CHANNEL_TYPE_TEXT:
141 _moduleLogger.info("RequestChannel Text")
142 channel = channelManager.channel_for_text(handle, None, suppressHandler)
143 elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
144 _moduleLogger.info("RequestChannel Media")
145 channel = channelManager.channel_for_text(handle, None, suppressHandler)
147 raise telepathy.NotImplemented("unknown channel type %s" % type)
149 _moduleLogger.info("RequestChannel Object Path: %s" % channel._object_path)
150 return channel._object_path
152 def RequestHandles(self, handleType, names, sender):
154 For org.freedesktop.telepathy.Connection
156 self.check_connected()
157 self.check_handle_type(handleType)
161 name = name.encode('utf-8')
162 if handleType == telepathy.HANDLE_TYPE_CONTACT:
163 _moduleLogger.info("RequestHandles Contact: %s" % name)
164 h = self._create_contact_handle(name)
165 elif handleType == telepathy.HANDLE_TYPE_LIST:
166 # Support only server side (immutable) lists
167 _moduleLogger.info("RequestHandles List: %s" % name)
168 h = handle.create_handle(self, 'list', name)
170 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
172 self.add_client_handle(h, sender)
175 def _create_contact_handle(self, name):
176 requestedContactId = name
178 contacts = self.session.addressbook.get_contacts()
180 contactId for contactId in contacts
181 if contactId == requestedContactId
184 if 0 < len(contactsFound):
185 contactId = contactsFound[0]
186 if len(contactsFound) != 1:
187 _moduleLogger.error("Contact ID was not unique: %s for %s" % (contactId, ))
189 contactId = requestedContactId
190 h = handle.create_handle(self, 'contact', contactId)