f70873ed9e9e223a26c07b635cc2080231f8ed68
[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 class Account:
23     """
24     Account class
25     @param username
26     @param protocol Protocol class instance
27     @param core Purple class instance
28     """
29
30     cdef object __username
31     cdef object __protocol
32     cdef object __core
33     cdef object __exists
34
35     def __init__(self, username, protocol, core):
36         self.__username = username
37         self.__protocol = protocol
38         self.__core = core
39
40         if protocol.exists and self._get_structure() != NULL:
41             self.__exists = True
42         else:
43             self.__exists = False
44
45     cdef account.PurpleAccount *_get_structure(self):
46         return account.purple_accounts_find(self.__username, \
47                 self.__protocol.id)
48
49     def __is_connected(self):
50         if self.__exists:
51             return account.purple_account_is_connected(self._get_structure())
52         else:
53             return None
54     is_connected = property(__is_connected)
55
56     def __is_connecting(self):
57         if self.__exists:
58             return account.purple_account_is_connecting(self._get_structure())
59         else:
60             return None
61     is_connecting = property(__is_connecting)
62
63     def __is_disconnected(self):
64         if self.__exists:
65             return account.purple_account_is_disconnected( \
66                     self._get_structure())
67         else:
68             return None
69     is_disconnected = property(__is_disconnected)
70
71     def __get_core(self):
72         return self.__core
73     core = property(__get_core)
74
75     def __get_exists(self):
76         return self.__exists
77     exists = property(__get_exists)
78
79     def __get_username(self):
80         cdef char *username = NULL
81         if self.__exists:
82             username = <char *> account.purple_account_get_username( \
83                     self._get_structure())
84             if username:
85                 return username
86             else:
87                 return None
88         else:
89             return self.__username
90     username = property(__get_username)
91
92     def __get_protocol(self):
93         return self.__protocol
94     protocol = property(__get_protocol)
95
96     def _get_protocol_options(self):
97         """
98         @return Dictionary {'setting': value, ...}
99         """
100         cdef glib.GList *iter
101         cdef account.PurpleAccount *c_account
102         cdef plugin.PurplePlugin *c_plugin
103         cdef prpl.PurplePluginProtocolInfo *prpl_info
104         cdef accountopt.PurpleAccountOption *option
105         cdef prefs.PurplePrefType type
106         cdef char *label_name
107         cdef char *str_value
108         cdef char *setting
109         cdef int int_value
110         cdef glib.gboolean bool_value
111
112         c_account = self._get_structure()
113
114         if c_account == NULL:
115             return None
116
117         po = {}
118
119         c_plugin = plugin.purple_plugins_find_with_id(self.__protocol.id)
120         prpl_info = plugin.PURPLE_PLUGIN_PROTOCOL_INFO(c_plugin)
121         iter = prpl_info.protocol_options
122
123         while iter:
124
125             option = <accountopt.PurpleAccountOption *> iter.data
126             type = accountopt.purple_account_option_get_type(option)
127             label_name = <char *> accountopt.purple_account_option_get_text(option)
128             setting = <char *> accountopt.purple_account_option_get_setting(option)
129
130             sett = str(<char *> setting)
131
132             if type == prefs.PURPLE_PREF_STRING:
133
134                 str_value = <char *> accountopt.purple_account_option_get_default_string(option)
135
136                 # Hack to set string "" as default value to Account options when
137                 # the default value of the protocol is NULL
138                 if str_value == NULL:
139                     str_value = ""
140                 str_value = <char *> account.purple_account_get_string(c_account, setting, str_value)
141
142                 val = str(<char *> str_value)
143
144             elif type == prefs.PURPLE_PREF_INT:
145
146                 int_value = accountopt.purple_account_option_get_default_int(option)
147                 int_value = account.purple_account_get_int(c_account, setting, int_value)
148
149                 val = int(int_value)
150
151             elif type == prefs.PURPLE_PREF_BOOLEAN:
152
153                 bool_value = accountopt.purple_account_option_get_default_bool(option)
154                 bool_value = account.purple_account_get_bool(c_account, setting, bool_value)
155
156                 val = bool(bool_value)
157
158             elif type == prefs.PURPLE_PREF_STRING_LIST:
159
160                 str_value = <char *> accountopt.purple_account_option_get_default_list_value(option)
161                 str_value = <char *> account.purple_account_get_string(c_account, setting, str_value)
162
163                 val = str(<char *> str_value)
164
165             iter = iter.next
166
167             po[sett] = val
168
169         return po
170     protocol_options = property(_get_protocol_options)
171
172     def __get_password(self):
173         cdef char *password = NULL
174         if self.__exists:
175             password = <char *> account.purple_account_get_password( \
176                     self._get_structure())
177             if password:
178                 return password
179             else:
180                 return None
181         else:
182             return None
183     password = property(__get_password)
184
185     def __get_alias(self):
186         cdef char *alias = NULL
187         if self.__exists:
188             alias = <char *> account.purple_account_get_alias(self._get_structure())
189             if alias:
190                 return alias
191             else:
192                 return None
193         else:
194             return None
195     alias = property(__get_alias)
196
197     def __get_user_info(self):
198         cdef char *user_info = NULL
199         if self.__exists:
200             user_info = <char *> account.purple_account_get_user_info(self._get_structure())
201             if user_info:
202                 return user_info
203             else:
204                 return None
205         else:
206             return None
207     user_info = property(__get_user_info)
208
209     def __get_remember_password(self):
210         if self.__exists:
211             return account.purple_account_get_remember_password( \
212                     self._get_structure())
213         else:
214             return None
215     remember_password = property(__get_remember_password)
216
217     def __get_enabled(self):
218         if self.__exists:
219             return account.purple_account_get_enabled(self._get_structure(), \
220                     self.__core.ui_name)
221         else:
222             return None
223     enabled = property(__get_enabled)
224
225     def __get_status_types(self):
226         cdef glib.GList *iter = NULL
227         cdef status.PurpleStatusType *c_statustype = NULL
228         cdef char *id = NULL
229         cdef char *name = NULL
230
231         if self.__exists:
232             status_types = []
233             iter = account.purple_account_get_status_types(self._get_structure())
234             while iter:
235                 c_statustype = <status.PurpleStatusType *> iter.data
236                 id = <char *> status.purple_status_type_get_id(c_statustype)
237                 name = <char *> status.purple_status_type_get_name(c_statustype)
238                 status_types.append((id, name))
239                 iter = iter.next
240             return status_types
241         else:
242              return None
243
244     status_types = property(__get_status_types)
245
246     def __get_active_status(self):
247         cdef status.PurpleStatus* c_status = NULL
248         cdef char *type = NULL
249         cdef char *name = NULL
250         cdef char *msg = NULL
251         if self.__exists:
252             active = {}
253             c_status = <status.PurpleStatus*> account.purple_account_get_active_status(self._get_structure())
254             type = <char *> status.purple_status_get_id(c_status)
255             name = <char *> status.purple_status_get_name(c_status)
256             msg = <char *> status.purple_status_get_attr_string(c_status,
257                 "message")
258
259             active['type'] = type
260             active['name'] = name
261             if msg:
262                 active['message'] = msg
263
264             return active
265         else:
266             return None
267     active_status = property(__get_active_status)
268
269     def set_username(self, username):
270         """
271         Sets the account's username.
272
273         @param username The username
274         @return True if successful, False if account doesn't exists
275         """
276         if self.__exists:
277             account.purple_account_set_username(self._get_structure(), \
278                     username)
279             return True
280         else:
281             return False
282
283     def set_protocol(self, protocol):
284         """
285         Sets the account's protocol.
286
287         @param protocol A Protocol class instance
288         @return True if successful, False if account doesn't exists
289         """
290         if protocol.exists and self.__exists:
291             account.purple_account_set_protocol_id(self._get_structure(), \
292                         protocol.id)
293             self.__protocol = protocol
294             return True
295         else:
296             return False
297
298     def set_protocol_options(self, po):
299         """
300         @param po Dictionary {'setting': value, ...} options to be updated
301         @return True to success or False to failure
302         """
303         cdef glib.GList *iter
304         cdef account.PurpleAccount *c_account
305         cdef plugin.PurplePlugin *c_plugin
306         cdef prpl.PurplePluginProtocolInfo *prpl_info
307         cdef accountopt.PurpleAccountOption *option
308         cdef prefs.PurplePrefType type
309         cdef char *str_value
310         cdef char *setting
311         cdef int int_value
312         cdef glib.gboolean bool_value
313
314         c_account = self._get_structure()
315
316         if c_account == NULL:
317             return False
318
319         c_plugin = plugin.purple_plugins_find_with_id(self.__protocol.id)
320         prpl_info = plugin.PURPLE_PLUGIN_PROTOCOL_INFO(c_plugin)
321         iter = prpl_info.protocol_options
322
323         while iter:
324
325             option = <accountopt.PurpleAccountOption *> iter.data
326             type = accountopt.purple_account_option_get_type(option)
327             setting = <char *> accountopt.purple_account_option_get_setting(option)
328
329             sett = str(<char *> setting)
330
331             if not po.has_key(sett):
332                 iter = iter.next
333                 continue
334
335             if type == prefs.PURPLE_PREF_STRING:
336
337                 str_value = <char *> po[sett]
338                 account.purple_account_set_string(c_account, setting, str_value)
339
340             elif type == prefs.PURPLE_PREF_INT:
341
342                 int_value = int(po[sett])
343                 account.purple_account_set_int(c_account, setting, int_value)
344
345             elif type == prefs.PURPLE_PREF_BOOLEAN:
346
347                 bool_value = bool(po[sett])
348                 account.purple_account_set_bool(c_account, setting, bool_value)
349
350             elif type == prefs.PURPLE_PREF_STRING_LIST:
351
352                 str_value = <char *> po[sett]
353                 account.purple_account_set_string(c_account, setting, str_value)
354
355             iter = iter.next
356
357         return True
358
359     def set_password(self, password):
360         """
361         Sets the account's password.
362
363         @param password The password
364         @return True if successful, False if account doesn't exists
365         """
366         if self.__exists:
367             account.purple_account_set_password(self._get_structure(), \
368                     password)
369             return True
370         else:
371             return False
372
373     def set_alias(self, alias):
374         """
375         Sets the account's alias
376
377         @param alias The alias
378         @return True if successful, False if account doesn't exists
379         """
380         if self.__exists:
381             account.purple_account_set_alias(self._get_structure(), \
382                     alias)
383             return True
384         else:
385             return False
386
387     def set_user_info(self, user_info):
388         """
389         Sets the account's user information
390
391         @param user_info The user information
392         @return True if successful, False if account doesn't exists
393         """
394         if self.__exists:
395             account.purple_account_set_user_info(self._get_structure(), \
396                     user_info)
397             return True
398         else:
399             return False
400
401     def set_remember_password(self, remember_password):
402         """
403         Sets whether or not this account should save its password.
404
405         @param remember_password True if should remember the password,
406                                  or False otherwise
407         @return True if successful, False if account doesn't exists
408         """
409         if self.__exists:
410             account.purple_account_set_remember_password( \
411                 self._get_structure(), remember_password)
412             return True
413         else:
414             return False
415
416     def set_enabled(self, value):
417         """
418         Sets wheter or not this account is enabled.
419
420         @param value True if it is enabled, or False otherwise
421         @return True if successful, False if account doesn't exists
422         """
423         if self.__exists:
424             account.purple_account_set_enabled(self._get_structure(), \
425                     self.__core.ui_name, bool(value))
426             return True
427         else:
428             return False
429
430     def new(self):
431         """
432         Creates a new account.
433
434         @return True if successful, False if account already exists
435         """
436         if self.__exists:
437             return False
438         else:
439             account.purple_accounts_add(account.purple_account_new( \
440                     self.__username, self.__protocol.id))
441
442             self.__exists = True
443             return True
444
445     def remove(self):
446         """
447         Removes an existing account.
448
449         @return True if successful, False if account doesn't exists
450         """
451         if self.__exists:
452             account.purple_accounts_delete(self._get_structure())
453             self__exists = False
454             return True
455         else:
456             return False
457
458     def connect(self):
459         """
460         Connects to an account.
461
462         @return True if successful, False if account doesn't exists
463         """
464         if self.__exists:
465             account.purple_account_connect(self._get_structure())
466             return True
467         else:
468             return False
469
470     def disconnect(self):
471         """
472         Disconnects from an account.
473
474         @return True if successful, False if account doesn't exists
475         """
476         if self.__exists:
477             account.purple_account_disconnect(self._get_structure())
478             return True
479         else:
480             return False
481
482     def add_buddy(self, name, alias=None, group=None):
483         """
484         Adds a buddy to account's buddy list.
485
486         @param name  Buddy name
487         @param alias Buddy alias (optional)
488         @return True if successfull, False otherwise
489         """
490         cdef blist.PurpleBuddy *c_buddy = NULL
491         cdef blist.PurpleGroup *c_group = NULL
492         cdef char *c_alias = NULL
493
494         if alias:
495             c_alias = alias
496         else:
497             c_alias = NULL
498
499         if self.__exists and \
500                 account.purple_account_is_connected(self._get_structure()):
501             if blist.purple_find_buddy(self._get_structure(), name):
502                 return False
503
504             if group:
505                 c_group = blist.purple_find_group(group)
506                 if c_group == NULL:
507                     c_group = blist.purple_group_new(group)
508
509             c_buddy = blist.purple_buddy_new(self._get_structure(), \
510                     name, c_alias)
511             if c_buddy == NULL:
512                 return False
513
514             blist.purple_blist_add_buddy(c_buddy, NULL, c_group, NULL)
515             account.purple_account_add_buddy(self._get_structure(), c_buddy)
516             if c_alias:
517                 blist.purple_blist_alias_buddy(c_buddy, c_alias)
518                 server.serv_alias_buddy(c_buddy)
519
520             return True
521
522         else:
523             return None
524
525     def remove_buddy(self, name):
526         """
527         Removes a buddy from account's buddy list.
528
529         @param name Buddy name
530         @return True if successful, False otherwise
531         """
532         cdef blist.PurpleBuddy *c_buddy = NULL
533         cdef blist.PurpleGroup *c_group = NULL
534
535         if self.__exists and \
536                 account.purple_account_is_connected(self._get_structure()):
537             c_buddy = blist.purple_find_buddy(self._get_structure(), name)
538             if c_buddy == NULL:
539                 return False
540
541             c_group = blist.purple_buddy_get_group(c_buddy)
542
543             account.purple_account_remove_buddy(self._get_structure(), \
544                     c_buddy, c_group)
545             blist.purple_blist_remove_buddy(c_buddy)
546             return True
547         else:
548             return None
549
550     def get_buddies_online(self):
551         cdef glib.GSList *iter = NULL
552         cdef blist.PurpleBuddy *c_buddy = NULL
553         cdef char *c_alias = NULL
554
555         if self.__exists and \
556                 account.purple_account_is_connected(self._get_structure()):
557             iter = blist.purple_find_buddies(self._get_structure(), NULL)
558
559             buddies_list = []
560             while iter:
561                 c_alias = NULL
562                 c_buddy = <blist.PurpleBuddy *> iter.data
563                 if <blist.PurpleBuddy *> c_buddy and \
564                         status.purple_presence_is_online( \
565                                 blist.purple_buddy_get_presence(c_buddy)):
566                     name = <char *> blist.purple_buddy_get_name(c_buddy)
567
568                     new_buddy = Buddy(name, self)
569
570                     c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
571                     if c_alias:
572                         new_buddy.set_alias(c_alias)
573
574                     buddies_list.append(new_buddy)
575                 iter = iter.next
576             return buddies_list
577         else:
578             return None
579
580     def request_add_buddy(self, buddy_username, buddy_alias):
581         if buddy_alias:
582             blist.purple_blist_request_add_buddy(self._get_structure(), \
583                     buddy_username, NULL, buddy_alias)
584         else:
585             blist.purple_blist_request_add_buddy(self._get_structure(), \
586                     buddy_username, NULL, NULL)
587
588     def set_active_status(self, type, msg=None):
589         cdef status.PurpleStatusType *c_statustype = NULL
590
591         if self.__exists:
592             if msg:
593                 account.purple_account_set_status(self._get_structure(),
594                         <char *> type, True, "message", <char *> msg, NULL)
595             else:
596                 account.purple_account_set_status(self._get_structure(),
597                         <char *> type, True, NULL)
598             return True
599         else:
600             return False
601
602     def set_status_message(self, type, msg):
603         cdef status.PurpleStatus* c_status = NULL
604         cdef status.PurpleStatusType *c_statustype = NULL
605
606         if self.__exists and msg:
607             c_status = account.purple_account_get_status(self._get_structure(),
608                     type)
609             status.purple_status_set_attr_string(c_status, "message", msg)
610             return True
611         else:
612             return False