Switching back to module level loggers
[theonering] / src / connection.py
1 import weakref
2 import logging
3
4 import telepathy
5
6 import constants
7 import gv_backend
8 import handle
9 import channel_manager
10 import simple_presence
11
12
13 _moduleLogger = logging.getLogger("connection")
14
15
16 class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePresenceMixin):
17
18         MANDATORY_PARAMETERS = {
19                 'account' : 's',
20                 'password' : 's'
21         }
22         OPTIONAL_PARAMETERS = {
23         }
24         PARAMETER_DEFAULTS = {
25         }
26
27         def __init__(self, manager, parameters):
28                 try:
29                         self.check_parameters(parameters)
30                         account = unicode(parameters['account'])
31
32                         telepathy.server.Connection.__init__(
33                                 self,
34                                 constants._telepathy_protocol_name_,
35                                 account,
36                                 constants._telepathy_implementation_name_
37                         )
38
39                         self._manager = weakref.proxy(manager)
40                         self._credentials = (
41                                 parameters['account'].encode('utf-8'),
42                                 parameters['password'].encode('utf-8'),
43                         )
44                         self._channelManager = channel_manager.ChannelManager(self)
45
46                         cookieFilePath = "%s/cookies.txt" % constants._data_path_
47                         self._backend = gv_backend.GVDialer(cookieFilePath)
48
49                         self.set_self_handle(handle.create_handle(self, 'connection'))
50
51                         _moduleLogger.info("Connection to the account %s created" % account)
52                 except Exception, e:
53                         _moduleLogger.exception("Failed to create Connection")
54                         raise
55
56         @property
57         def manager(self):
58                 return self._manager
59
60         @property
61         def gvoice_backend(self):
62                 return self._backend
63
64         @property
65         def username(self):
66                 self._credentials[0]
67
68         def handle(self, handleType, handleId):
69                 self.check_handle(handleType, handleId)
70                 return self._handles[handleType, handleId]
71
72         def Connect(self):
73                 """
74                 For org.freedesktop.telepathy.Connection
75                 """
76                 self.StatusChanged(
77                         telepathy.CONNECTION_STATUS_CONNECTING,
78                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
79                 )
80                 try:
81                         self._backend.login(*self._credentials)
82                 except gv_backend.NetworkError:
83                         self.StatusChanged(
84                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
85                                 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
86                         )
87                 except Exception:
88                         self.StatusChanged(
89                                 telepathy.CONNECTION_STATUS_DISCONNECTED,
90                                 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
91                         )
92                 else:
93                         self.StatusChanged(
94                                 telepathy.CONNECTION_STATUS_CONNECTED,
95                                 telepathy.CONNECTION_STATUS_REASON_REQUESTED
96                         )
97
98         def Disconnect(self):
99                 """
100                 For org.freedesktop.telepathy.Connection
101                 """
102                 try:
103                         self._backend.logout()
104                         _moduleLogger.info("Disconnected")
105                 except Exception:
106                         _moduleLogger.exception("Disconnecting Failed")
107                 self.StatusChanged(
108                         telepathy.CONNECTION_STATUS_DISCONNECTED,
109                         telepathy.CONNECTION_STATUS_REASON_REQUESTED
110                 )
111
112         def RequestChannel(self, type, handleType, handleId, suppressHandler):
113                 """
114                 For org.freedesktop.telepathy.Connection
115
116                 @param type DBus interface name for base channel type
117                 @param handleId represents a contact, list, etc according to handleType
118
119                 @returns DBus object path for the channel created or retrieved
120                 """
121                 self.check_connected()
122
123                 channel = None
124                 channelManager = self._channelManager
125                 handle = self.handle(handleType, handleId)
126
127                 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
128                         channel = channelManager.channel_for_list(handle, suppressHandler)
129                 elif type == telepathy.CHANNEL_TYPE_TEXT:
130                         if handleType != telepathy.HANDLE_TYPE_CONTACT:
131                                 raise telepathy.NotImplemented("Only Contacts are allowed")
132                         contact = handle.contact
133                         channel = channelManager.channel_for_text(handle, None, suppressHandler)
134                 else:
135                         raise telepathy.NotImplemented("unknown channel type %s" % type)
136
137                 return channel._object_path
138
139         def RequestHandles(self, handleType, names, sender):
140                 """
141                 For org.freedesktop.telepathy.Connection
142                 """
143                 self.check_connected()
144                 self.check_handleType(handleType)
145
146                 handles = []
147                 for name in names:
148                         name = name.encode('utf-8')
149                         if handleType == telepathy.HANDLE_TYPE_CONTACT:
150                                 h = self._create_contact_handle(name)
151                         elif handleType == telepathy.HANDLE_TYPE_LIST:
152                                 h = handle.create_handle(self, 'list', name)
153                         elif handleType == telepathy.HANDLE_TYPE_GROUP:
154                                 h = handle.create_handle(self, 'group', name)
155                         else:
156                                 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
157                         handles.append(h.id)
158                         self.add_client_handle(handle, sender)
159                 return handles
160
161         def _create_contact_handle(self, name):
162                 requestedContactId, requestedContactName = handle.field_split(name)
163
164                 contacts = self._backend.get_contacts()
165                 contactsFound = [
166                         (contactId, contactName) for (contactId, contactName) in contacts
167                         if contactName == name
168                 ]
169
170                 if 0 < len(contactsFound):
171                         contactId, contactName = contactsFound[0]
172                         h = handle.create_handle(self, 'contact', contactId, contactName)
173                 else:
174                         h = handle.create_handle(self, 'contact', requestedContactId, requestedContactName)