Getting start on this here em code
[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
11
12 class TheOneRingConnection(telepathy.server.Connection):
13
14         _mandatory_parameters = {
15                 'account' : 's',
16                 'password' : 's'
17         }
18
19         def __init__(self, manager, parameters):
20                 try:
21                         self.check_parameters(parameters)
22                         account = unicode(parameters['account'])
23
24                         telepathy.server.Connection.__init__(self, 'gvoice', account, 'theonering')
25
26                         self._manager = weakref.proxy(manager)
27                         self._credentials = (
28                                 parameters['account'].encode('utf-8'),
29                                 parameters['password'].encode('utf-8'),
30                         )
31                         self._channelManager = channel_manager.ChannelManager(self)
32
33                         cookieFilePath = "%s/cookies.txt" % constants._data_path_
34                         self._backend = gv_backend.GVDialer(cookieFilePath)
35
36                         self.set_self_handle(handle.create_handle(self, 'self'))
37
38                         self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED
39                         self._initial_presence = None
40                         self._initial_personal_message = None
41
42                         logging.info("Connection to the account %s created" % account)
43                 except Exception, e:
44                         logging.exception("Failed to create Connection")
45                         raise
46
47         @property
48         def manager(self):
49                 return self._manager
50
51         @property
52         def gvoice_backend(self):
53                 return self._backend
54
55         @property
56         def username(self):
57                 self._credentials[0]
58
59         def handle(self, handleType, handleId):
60                 self.check_handle(handleType, handleId)
61                 return self._handles[handleType, handleId]
62
63         def Connect(self):
64                 """
65                 org.freedesktop.telepathy.Connection
66                 """
67                 logging.info("Connecting")
68                 self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED
69                 self._backend.login(*self._credentials)
70
71         def Disconnect(self):
72                 """
73                 org.freedesktop.telepathy.Connection
74                 """
75                 logging.info("Disconnecting")
76                 self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_REQUESTED
77                 self._backend.logout()
78
79         def RequestChannel(self, type, handleType, handleId, suppressHandler):
80                 """
81                 org.freedesktop.telepathy.Connection
82                 @param type DBus interface name for base channel type
83                 @param handleId represents a contact, list, etc according to handleType
84
85                 @returns DBus object path for the channel created or retrieved
86                 """
87                 self.check_connected()
88
89                 channel = None
90                 channelManager = self._channelManager
91                 handle = self.handle(handleType, handleId)
92
93                 if type == telepathy.CHANNEL_TYPE_CONTACT_LIST:
94                         channel = channelManager.channel_for_list(handle, suppressHandler)
95                 elif type == telepathy.CHANNEL_TYPE_TEXT:
96                         if handleType != telepathy.HANDLE_TYPE_CONTACT:
97                                 raise telepathy.NotImplemented("Only Contacts are allowed")
98                         contact = handle.contact
99                         channel = channelManager.channel_for_text(handle, None, suppressHandler)
100                 else:
101                         raise telepathy.NotImplemented("unknown channel type %s" % type)
102
103                 return channel._object_path
104
105         def RequestHandles(self, handleType, names, sender):
106                 """
107                 org.freedesktop.telepathy.Connection
108                 """
109                 self.check_connected()
110                 self.check_handleType(handleType)
111
112                 handles = []
113                 for name in names:
114                         name = name.encode('utf-8')
115                         if handleType == telepathy.HANDLE_TYPE_CONTACT:
116                                 h = self._create_contact_handle(name)
117                         elif handleType == telepathy.HANDLE_TYPE_LIST:
118                                 h = handle.create_handle(self, 'list', name)
119                         elif handleType == telepathy.HANDLE_TYPE_GROUP:
120                                 h = handle.create_handle(self, 'group', name)
121                         else:
122                                 raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
123                         handles.append(h.id)
124                         self.add_client_handle(handle, sender)
125                 return handles
126
127         def _create_contact_handle(self, name):
128                 requestedContactId, requestedContactName = handle.field_split(name)
129
130                 contacts = self._backend.get_contacts()
131                 contactsFound = [
132                         (contactId, contactName) for (contactId, contactName) in contacts
133                         if contactName == name
134                 ]
135
136                 if 0 < len(contactsFound):
137                         contactId, contactName = contactsFound[0]
138                         h = handle.create_handle(self, 'contact', contactId, contactName)
139                 else:
140                         h = handle.create_handle(self, 'contact', requestedContactId, requestedContactName)