Fixing conversation to user account string tuple
[python-purple] / account.pyx
1 #
2 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3 #
4 #  This file is part of python-purple.
5 #
6 #  python-purple is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  python-purple is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 cimport purple
21
22 cdef extern from *:
23     ctypedef char const_char "const char"
24
25 cdef class Account:
26     """ Account class """
27     cdef account.PurpleAccount *c_account
28     cdef plugin.PurplePlugin *c_plugin
29     cdef prpl.PurplePluginProtocolInfo *c_prpl_info
30     cdef plugin.PurplePluginInfo *c_plugin_info
31     cdef savedstatuses.PurpleSavedStatus *__sstatus
32
33     cdef object __proxy
34     cdef object __protocol
35
36     def __init__(self):
37         self.__proxy = purple.ProxyInfo()
38         self.__protocol = purple.Plugin()
39
40     '''
41     def __init__(self, char *username, char *protocol_id):
42         cdef proxy.PurpleProxyInfo *c_proxyinfo
43         cdef account.PurpleAccount *acc = NULL
44
45         acc = account.c_purple_accounts_find(username, protocol_id)
46         if acc:
47             self.c_account = acc
48             c_proxyinfo = account.c_purple_account_get_proxy_info(self.c_account)
49         else:
50             self.c_account = account.c_purple_account_new(username, protocol_id)
51             c_proxyinfo = account.c_purple_account_get_proxy_info(self.c_account)
52             if c_proxyinfo == NULL:
53                 c_proxyinfo = proxy.c_purple_proxy_info_new()
54                 proxy.c_purple_proxy_info_set_type(c_proxyinfo, proxy.PURPLE_PROXY_NONE)
55                 account.c_purple_account_set_proxy_info(self.c_account, c_proxyinfo)
56         self.__proxy = ProxyInfo()
57         self.__proxy.c_proxyinfo = c_proxyinfo
58         acc = NULL
59
60         self.c_plugin = plugin.c_purple_plugins_find_with_id(protocol_id)
61         self.c_prpl_info = plugin.c_PURPLE_PLUGIN_PROTOCOL_INFO(self.c_plugin)
62     '''
63
64     def __get_username(self):
65         if self.c_account:
66             return account.c_purple_account_get_username(self.c_account)
67         else:
68             return None
69     def __set_username(self, username):
70         if self.c_account:
71             account.c_purple_account_set_username(self.c_account, username)
72     username = property(__get_password, __set_username)
73
74     def __get_password(self):
75         if self.c_account:
76             return account.c_purple_account_get_password(self.c_account)
77         else:
78             return None
79     def __set_password(self, password):
80         if self.c_account:
81             account.c_purple_account_set_password(self.c_account, password)
82     password = property(__get_password, __set_password)
83
84     def __get_alias(self):
85         if self.c_account:
86             return account.c_purple_account_get_alias(self.c_account)
87         else:
88             return None
89     def __set_alias(self, alias):
90         if self.c_account:
91             account.c_purple_account_set_alias(self.c_account, alias)
92     alias = property(__get_alias, __set_alias)
93
94     def __get_user_info(self):
95         if self.c_account:
96             return account.c_purple_account_get_user_info(self.c_account)
97         else:
98             return None
99     def __set_user_info(self, user_info):
100         if self.c_account:
101             account.c_purple_account_set_user_info(self.c_account, user_info)
102     user_info = property(__get_user_info, __set_user_info)
103
104     def __get_protocol_id(self):
105         if self.c_account:
106             return account.c_purple_account_get_protocol_id(self.c_account)
107         else:
108             return None
109     def __set_protocol_id(self, protocol_id):
110         if self.c_account:
111             account.c_purple_account_set_protocol_id(self.c_account, protocol_id)
112     protocol_id = property(__get_protocol_id, __set_protocol_id)
113
114     def __get_remember_password(self):
115         if self.c_account:
116             return account.c_purple_account_get_remember_password(self.c_account)
117         else:
118             return None
119     def __set_remember_password(self, value):
120         if self.c_account:
121             account.c_purple_account_set_remember_password(self.c_account, value)
122     remember_password = property(__get_remember_password, __set_remember_password)
123
124     def _get_protocol_options(self):
125         cdef glib.GList *iter
126         cdef accountopt.PurpleAccountOption *option
127         cdef prefs.PurplePrefType type
128         cdef const_char *label_name
129         cdef const_char *str_value
130         cdef const_char *setting
131         cdef int int_value
132         cdef glib.gboolean bool_value
133
134         if self.c_account == NULL:
135             return None
136
137         po = {}
138
139         iter = self.c_prpl_info.protocol_options
140
141         while iter:
142
143             option = <accountopt.PurpleAccountOption *> iter.data
144             type = accountopt.c_purple_account_option_get_type(option)
145             label_name = accountopt.c_purple_account_option_get_text(option)
146             setting = accountopt.c_purple_account_option_get_setting(option)
147
148             sett = str(<char *> setting)
149
150             if type == prefs.PURPLE_PREF_STRING:
151
152                 str_value = accountopt.c_purple_account_option_get_default_string(option)
153
154                 # Google Talk default domain hackery!
155                 if str_value == NULL and str(<char *> label_name) == "Connect server":
156                     str_value = "talk.google.com"
157                 str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
158
159                 val = str(<char *> str_value)
160
161             elif type == prefs.PURPLE_PREF_INT:
162
163                 int_value = accountopt.c_purple_account_option_get_default_int(option)
164                 int_value = account.c_purple_account_get_int(self.c_account, setting, int_value)
165
166                 val = int(int_value)
167
168             elif type == prefs.PURPLE_PREF_BOOLEAN:
169
170                 bool_value = accountopt.c_purple_account_option_get_default_bool(option)
171                 bool_value = account.c_purple_account_get_bool(self.c_account, setting, bool_value)
172
173                 val = bool(bool_value)
174
175             elif type == prefs.PURPLE_PREF_STRING_LIST:
176
177                 str_value = accountopt.c_purple_account_option_get_default_list_value(option)
178                 str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
179
180                 val = str(<char *> str_value)
181
182             iter = iter.next
183
184             po[sett] = val
185
186         return po
187
188     def _set_protocol_options(self, po):
189         cdef glib.GList *iter
190         cdef accountopt.PurpleAccountOption *option
191         cdef prefs.PurplePrefType type
192         cdef const_char *str_value
193         cdef const_char *setting
194         cdef int int_value
195         cdef glib.gboolean bool_value
196
197         if self.c_account == NULL:
198             return
199
200         po = {}
201
202         iter = self.c_prpl_info.protocol_options
203
204         while iter:
205
206             option = <accountopt.PurpleAccountOption *> iter.data
207             type = accountopt.c_purple_account_option_get_type(option)
208             setting = accountopt.c_purple_account_option_get_setting(option)
209
210             sett = str(<char *> setting)
211
212             if type == prefs.PURPLE_PREF_STRING:
213
214                 str_value = <char *> po[sett]
215                 account.c_purple_account_set_string(self.c_account, setting, str_value)
216
217             elif type == prefs.PURPLE_PREF_INT:
218
219                 int_value = int(po[sett])
220                 account.c_purple_account_set_int(self.c_account, setting, int_value)
221
222             elif type == prefs.PURPLE_PREF_BOOLEAN:
223
224                 bool_value = bool(po[sett])
225                 account.c_purple_account_set_bool(self.c_account, setting, bool_value)
226
227             elif type == prefs.PURPLE_PREF_STRING_LIST:
228
229                 str_value = <char *> po[sett]
230                 account.c_purple_account_set_string(self.c_account, setting, str_value)
231
232             iter = iter.next
233
234     protocol_options = property(_get_protocol_options, _set_protocol_options)
235
236     def _get_protocol_labels(self):
237         cdef glib.GList *iter
238         cdef accountopt.PurpleAccountOption *option
239         cdef const_char *label_name
240         cdef const_char *setting
241
242         if self.c_account == NULL:
243             return None
244
245         po = {}
246
247         iter = self.c_prpl_info.protocol_options
248
249         while iter:
250
251             option = <accountopt.PurpleAccountOption *> iter.data
252             label_name = accountopt.c_purple_account_option_get_text(option)
253             setting = accountopt.c_purple_account_option_get_setting(option)
254
255             sett = str(<char *> setting)
256             label = str(<char *> label_name)
257
258             po[sett] = label
259
260         return po
261
262     protocol_labels = property(_get_protocol_labels)
263
264     def __get_proxy(self):
265         return self.__proxy
266     proxy = property(__get_proxy)
267
268     def __get_protocol(self):
269         return self.__protocol
270     protocol = property(__get_protocol)
271
272     def get_protocol_name(self):
273         if self.c_account:
274             return account.c_purple_account_get_protocol_name(self.c_account)
275         else:
276             return None
277
278     def set_status(self):
279         self.__sstatus = savedstatuses.c_purple_savedstatus_new(NULL, status.PURPLE_STATUS_AVAILABLE)
280         savedstatuses.c_purple_savedstatus_activate(self.__sstatus)
281
282     def get_buddies_online(self, acc):
283         cdef account.PurpleAccount *c_account
284         cdef glib.GSList *iter
285         cdef blist.PurpleBuddy *buddy
286         cdef char *c_name = NULL
287         cdef char *c_alias = NULL
288
289         c_account = account.c_purple_accounts_find(acc[0], acc[1])
290         if c_account:
291             iter = blist.c_purple_find_buddies(c_account, NULL)
292         else:
293             return None
294
295         buddies = []
296         while iter:
297             c_name = NULL
298             c_alias = NULL
299             buddy = <blist.PurpleBuddy *> iter.data
300             if <blist.PurpleBuddy *>buddy and \
301                 account.c_purple_account_is_connected(blist.c_purple_buddy_get_account(buddy)) and \
302                 status.c_purple_presence_is_online(blist.c_purple_buddy_get_presence(buddy)):
303                 c_name = <char *> blist.c_purple_buddy_get_name(buddy)
304                 if c_name == NULL:
305                     name = None
306                 else:
307                     name = c_name
308                 c_alias = <char *> blist.c_purple_buddy_get_alias_only(buddy)
309                 if c_alias == NULL:
310                     alias = None
311                 else:
312                     alias = c_alias
313                 buddies.append((name, alias))
314             iter = iter.next
315         return buddies
316
317     def new(self, username, protocol_id):
318         cdef account.PurpleAccount *c_account
319         c_account = account.c_purple_account_new(username, protocol_id)
320
321         if c_account == NULL:
322             return None
323
324         account.c_purple_accounts_add(c_account) 
325
326         return (username, protocol_id)
327
328     def get_all(self):
329         cdef glib.GList *iter
330         cdef account.PurpleAccount *acc
331
332         accounts = []
333
334         iter = account.c_purple_accounts_get_all()
335         while iter:
336             acc = <account.PurpleAccount *> iter.data
337             if <account.PurpleAccount *>acc:
338                 username = account.c_purple_account_get_username(acc)
339                 protocol_id = account.c_purple_account_get_protocol_id(acc)
340
341                 accounts.append((username, protocol_id))
342             iter = iter.next
343
344         return accounts
345
346     def get_password(self, acc):
347         ''' @param acc Tuple (username, protocol id) '''
348         cdef account.PurpleAccount *c_account
349         cdef char *value
350         value = NULL
351
352         c_account = account.c_purple_accounts_find(acc[0], acc[1])
353         if c_account:
354             value = <char *> account.c_purple_account_get_password(c_account)
355
356         if value == NULL:
357             return None
358         else:
359             return value
360
361
362     def set_password(self, acc, password):
363         ''' @param acc Tuple (username, protocol id) '''
364         ''' @param password The account's password '''
365         cdef account.PurpleAccount *c_account
366
367         if not password:
368             return
369
370         c_account = account.c_purple_accounts_find(acc[0], acc[1])
371         if c_account:
372             account.c_purple_account_set_password(c_account, password)
373
374     def get_alias(self, acc):
375         ''' @param acc Tuple (username, protocol id) '''
376         cdef account.PurpleAccount *c_account
377         cdef char *value
378         value = NULL
379
380         c_account = account.c_purple_accounts_find(acc[0], acc[1])
381         if c_account:
382             value = <char *> account.c_purple_account_get_alias(c_account)
383
384         if value == NULL:
385             return None
386         else:
387             return value
388
389     def set_alias(self, acc, alias):
390         ''' @param acc Tuple (username, protocol id) '''
391         ''' @param alias The account's alias '''
392         cdef account.PurpleAccount *c_account
393
394         if not alias:
395             return
396
397         c_account = account.c_purple_accounts_find(acc[0], acc[1])
398         if c_account:
399             account.c_purple_account_set_alias(c_account, alias)
400
401     def set_protocol_id(self, acc, protocol_id):
402         ''' @param acc Tuple (username, protocol id) '''
403         ''' @param protocol_id The new account's protocol id '''
404         cdef account.PurpleAccount *c_account
405
406         if not protocol_id:
407             return
408
409         c_account = account.c_purple_accounts_find(acc[0], acc[1])
410         if c_account:
411             account.c_purple_account_set_protocol_id(c_account, protocol_id)
412
413     def get_protocol_id(self, acc):
414         ''' @param acc Tuple (username, protocol id) '''
415         ''' @return account's protocol id '''
416         cdef account.PurpleAccount *c_account
417         cdef char *value
418         value = NULL
419
420         c_account = account.c_purple_accounts_find(acc[0], acc[1])
421         if c_account:
422             value = <char *> account.c_purple_account_get_protocol_id(c_account)
423
424         if value == NULL:
425             return None
426         else:
427             return value
428
429
430     def set_enabled(self, acc, ui, value):
431         ''' @param acc Tuple (username, protocol id) '''
432         ''' @param ui The UI '''
433         ''' @param value True to enabled or False to disabled '''
434         cdef account.PurpleAccount *c_account
435
436         c_account = account.c_purple_accounts_find(acc[0], acc[1])
437         if c_account:
438             account.c_purple_account_set_enabled(c_account, <char *> ui, bool(value))
439  
440     def get_enabled(self, acc, ui):
441         ''' @param acc Tuple (username, protocol id) '''
442         ''' @param ui The UI '''
443         cdef account.PurpleAccount *c_account
444
445         c_account = account.c_purple_accounts_find(acc[0], acc[1])
446         if c_account:
447             return account.c_purple_account_get_enabled(c_account, ui)
448         else:
449             return False
450
451     def is_connected(self, acc):
452         ''' @param acc Tuple (username, protocol id) '''
453         ''' @param ui The UI '''
454         cdef account.PurpleAccount *c_account
455
456         c_account = account.c_purple_accounts_find(acc[0], acc[1])
457         if c_account:
458             return account.c_purple_account_is_connected(c_account)
459         else:
460             return False
461     
462     def connect(self, acc):
463         ''' @param acc Tuple (username, protocol id) '''
464         ''' @param ui The UI '''
465         cdef account.PurpleAccount *c_account
466
467         c_account = account.c_purple_accounts_find(acc[0], acc[1])
468         if c_account:
469             account.c_purple_account_connect(c_account)
470     
471     def disconnect(self, acc):
472         ''' @param acc Tuple (username, protocol id) '''
473         ''' @param ui The UI '''
474         cdef account.PurpleAccount *c_account
475
476         c_account = account.c_purple_accounts_find(acc[0], acc[1])
477         if c_account:
478             account.c_purple_account_disconnect(c_account)