Adding comments to buddy class
[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         status_types = []
232         if self.__exists:
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
241         return status_types
242
243     status_types = property(__get_status_types)
244
245     def __get_active_status(self):
246         cdef status.PurpleStatus* c_status = NULL
247         cdef char *type = NULL
248         cdef char *name = NULL
249         cdef char *msg = NULL
250         if self.__exists:
251             active = {}
252             c_status = <status.PurpleStatus*> account.purple_account_get_active_status(self._get_structure())
253             type = <char *> status.purple_status_get_id(c_status)
254             name = <char *> status.purple_status_get_name(c_status)
255             msg = <char *> status.purple_status_get_attr_string(c_status,
256                 "message")
257
258             active['type'] = type
259             active['name'] = name
260             if msg:
261                 active['message'] = msg
262
263             return active
264         else:
265             return None
266     active_status = property(__get_active_status)
267
268     def set_username(self, username):
269         """
270         Sets the account's username.
271
272         @param username The username
273         @return True if successful, False if account doesn't exists
274         """
275         if self.__exists:
276             account.purple_account_set_username(self._get_structure(), \
277                     username)
278             return True
279         else:
280             return False
281
282     def set_protocol(self, protocol):
283         """
284         Sets the account's protocol.
285
286         @param protocol A Protocol class instance
287         @return True if successful, False if account doesn't exists
288         """
289         if protocol.exists and self.__exists:
290             account.purple_account_set_protocol_id(self._get_structure(), \
291                         protocol.id)
292             self.__protocol = protocol
293             return True
294         else:
295             return False
296
297     def set_protocol_options(self, po):
298         """
299         @param po Dictionary {'setting': value, ...} options to be updated
300         @return True to success or False to failure
301         """
302         cdef glib.GList *iter
303         cdef account.PurpleAccount *c_account
304         cdef plugin.PurplePlugin *c_plugin
305         cdef prpl.PurplePluginProtocolInfo *prpl_info
306         cdef accountopt.PurpleAccountOption *option
307         cdef prefs.PurplePrefType type
308         cdef char *str_value
309         cdef char *setting
310         cdef int int_value
311         cdef glib.gboolean bool_value
312
313         c_account = self._get_structure()
314
315         if c_account == NULL:
316             return False
317
318         c_plugin = plugin.purple_plugins_find_with_id(self.__protocol.id)
319         prpl_info = plugin.PURPLE_PLUGIN_PROTOCOL_INFO(c_plugin)
320         iter = prpl_info.protocol_options
321
322         while iter:
323
324             option = <accountopt.PurpleAccountOption *> iter.data
325             type = accountopt.purple_account_option_get_type(option)
326             setting = <char *> accountopt.purple_account_option_get_setting(option)
327
328             sett = str(<char *> setting)
329
330             if not po.has_key(sett):
331                 iter = iter.next
332                 continue
333
334             if type == prefs.PURPLE_PREF_STRING:
335
336                 str_value = <char *> po[sett]
337                 account.purple_account_set_string(c_account, setting, str_value)
338
339             elif type == prefs.PURPLE_PREF_INT:
340
341                 int_value = int(po[sett])
342                 account.purple_account_set_int(c_account, setting, int_value)
343
344             elif type == prefs.PURPLE_PREF_BOOLEAN:
345
346                 bool_value = bool(po[sett])
347                 account.purple_account_set_bool(c_account, setting, bool_value)
348
349             elif type == prefs.PURPLE_PREF_STRING_LIST:
350
351                 str_value = <char *> po[sett]
352                 account.purple_account_set_string(c_account, setting, str_value)
353
354             iter = iter.next
355
356         return True
357
358     def set_password(self, password):
359         """
360         Sets the account's password.
361
362         @param password The password
363         @return True if successful, False if account doesn't exists
364         """
365         if self.__exists:
366             account.purple_account_set_password(self._get_structure(), \
367                     password)
368             return True
369         else:
370             return False
371
372     def set_alias(self, alias):
373         """
374         Sets the account's alias
375
376         @param alias The alias
377         @return True if successful, False if account doesn't exists
378         """
379         if self.__exists:
380             account.purple_account_set_alias(self._get_structure(), \
381                     alias)
382             return True
383         else:
384             return False
385
386     def set_user_info(self, user_info):
387         """
388         Sets the account's user information
389
390         @param user_info The user information
391         @return True if successful, False if account doesn't exists
392         """
393         if self.__exists:
394             account.purple_account_set_user_info(self._get_structure(), \
395                     user_info)
396             return True
397         else:
398             return False
399
400     def set_remember_password(self, remember_password):
401         """
402         Sets whether or not this account should save its password.
403
404         @param remember_password True if should remember the password,
405                                  or False otherwise
406         @return True if successful, False if account doesn't exists
407         """
408         if self.__exists:
409             account.purple_account_set_remember_password( \
410                 self._get_structure(), remember_password)
411             return True
412         else:
413             return False
414
415     def set_enabled(self, value):
416         """
417         Sets wheter or not this account is enabled.
418
419         @param value True if it is enabled, or False otherwise
420         @return True if successful, False if account doesn't exists
421         """
422         if self.__exists:
423             account.purple_account_set_enabled(self._get_structure(), \
424                     self.__core.ui_name, bool(value))
425             return True
426         else:
427             return False
428
429     def new(self):
430         """
431         Creates a new account.
432
433         @return True if successful, False if account already exists
434         """
435         if self.__exists:
436             return False
437         else:
438             account.purple_accounts_add(account.purple_account_new( \
439                     self.__username, self.__protocol.id))
440
441             self.__exists = True
442             return True
443
444     def remove(self):
445         """
446         Removes an existing account.
447
448         @return True if successful, False if account doesn't exists
449         """
450         if self.__exists:
451             account.purple_accounts_delete(self._get_structure())
452             self__exists = False
453             return True
454         else:
455             return False
456
457     def connect(self):
458         """
459         Connects to an account.
460
461         @return True if successful, False if account doesn't exists
462         """
463         if self.__exists:
464             account.purple_account_connect(self._get_structure())
465             return True
466         else:
467             return False
468
469     def disconnect(self):
470         """
471         Disconnects from an account.
472
473         @return True if successful, False if account doesn't exists
474         """
475         if self.__exists:
476             account.purple_account_disconnect(self._get_structure())
477             return True
478         else:
479             return False
480
481     def add_buddy(self, name, alias=None, group=None):
482         """
483         Adds a buddy to account's buddy list.
484
485         @param name  Buddy name
486         @param alias Buddy alias (optional)
487         @return True if successfull, False otherwise
488         """
489         cdef blist.PurpleBuddy *c_buddy = NULL
490         cdef blist.PurpleGroup *c_group = NULL
491         cdef char *c_alias = NULL
492
493         if alias:
494             c_alias = alias
495         else:
496             c_alias = NULL
497
498         if self.__exists and \
499                 account.purple_account_is_connected(self._get_structure()):
500             if blist.purple_find_buddy(self._get_structure(), name):
501                 return False
502
503             if group:
504                 c_group = blist.purple_find_group(group)
505                 if c_group == NULL:
506                     c_group = blist.purple_group_new(group)
507
508             c_buddy = blist.purple_buddy_new(self._get_structure(), \
509                     name, c_alias)
510             if c_buddy == NULL:
511                 return False
512
513             blist.purple_blist_add_buddy(c_buddy, NULL, c_group, NULL)
514             account.purple_account_add_buddy(self._get_structure(), c_buddy)
515             if c_alias:
516                 blist.purple_blist_alias_buddy(c_buddy, c_alias)
517                 server.serv_alias_buddy(c_buddy)
518
519             return True
520
521         else:
522             return None
523
524     def remove_buddy(self, name):
525         """
526         Removes a buddy from account's buddy list.
527
528         @param name Buddy name
529         @return True if successful, False otherwise
530         """
531         cdef blist.PurpleBuddy *c_buddy = NULL
532         cdef blist.PurpleGroup *c_group = NULL
533
534         if self.__exists and \
535                 account.purple_account_is_connected(self._get_structure()):
536             c_buddy = blist.purple_find_buddy(self._get_structure(), name)
537             if c_buddy == NULL:
538                 return False
539
540             c_group = blist.purple_buddy_get_group(c_buddy)
541
542             account.purple_account_remove_buddy(self._get_structure(), \
543                     c_buddy, c_group)
544             blist.purple_blist_remove_buddy(c_buddy)
545             return True
546         else:
547             return None
548
549     def get_buddies_online(self):
550         cdef glib.GSList *iter = NULL
551         cdef blist.PurpleBuddy *c_buddy = NULL
552         cdef char *c_alias = NULL
553
554         buddies_list = []
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             while iter:
560                 c_alias = NULL
561                 c_buddy = <blist.PurpleBuddy *> iter.data
562                 if <blist.PurpleBuddy *> c_buddy and \
563                         status.purple_presence_is_online( \
564                                 blist.purple_buddy_get_presence(c_buddy)):
565                     name = <char *> blist.purple_buddy_get_name(c_buddy)
566
567                     new_buddy = Buddy(name, self)
568
569                     c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
570                     if c_alias:
571                         new_buddy.set_alias(c_alias)
572
573                     buddies_list.append(new_buddy)
574                 iter = iter.next
575
576         return buddies_list
577
578     def get_buddies(self):
579         """
580         @return Account's buddies list
581         """
582         cdef glib.GSList *iter = NULL
583         cdef blist.PurpleBuddy *c_buddy = NULL
584         cdef char *c_alias = NULL
585
586         buddies_list = []
587         if self.__exists:
588             iter = blist.purple_find_buddies(self._get_structure(), NULL)
589
590             while iter:
591                 c_alias = NULL
592                 c_buddy = <blist.PurpleBuddy *> iter.data
593
594                 name = <char *> blist.purple_buddy_get_name(c_buddy)
595                 new_buddy = Buddy(name, self)
596
597                 c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
598                 if c_alias:
599                     new_buddy.set_alias(c_alias)
600
601                 buddies_list.append(new_buddy)
602                 iter = iter.next
603
604         return buddies_list
605
606     def request_add_buddy(self, buddy_username, buddy_alias):
607         if buddy_alias:
608             blist.purple_blist_request_add_buddy(self._get_structure(), \
609                     buddy_username, NULL, buddy_alias)
610         else:
611             blist.purple_blist_request_add_buddy(self._get_structure(), \
612                     buddy_username, NULL, NULL)
613
614     def set_active_status(self, type, msg=None):
615         cdef status.PurpleStatusType *c_statustype = NULL
616         cdef savedstatuses.PurpleSavedStatus *c_savedstatus = NULL
617
618         if self.__exists:
619             if msg:
620                 account.purple_account_set_status(self._get_structure(),
621                         <char *> type, True, "message", <char *> msg, NULL)
622             else:
623                 account.purple_account_set_status(self._get_structure(),
624                         <char *> type, True, NULL)
625
626             # FIXME: We can create only a savedstatus for each statustype
627             c_savedstatus = savedstatuses.purple_savedstatus_find(type)
628             if c_savedstatus == NULL:
629                 c_statustype = account.purple_account_get_status_type( \
630                         self._get_structure(), type)
631                 c_savedstatus = savedstatuses.purple_savedstatus_new( \
632                         NULL, status.purple_status_type_get_primitive( \
633                                 c_statustype))
634                 savedstatuses.purple_savedstatus_set_title(c_savedstatus,
635                         type)
636
637             savedstatuses.purple_savedstatus_set_message(c_savedstatus, msg)
638             prefs.purple_prefs_set_int("/purple/savedstatus/idleaway",
639                     savedstatuses.purple_savedstatus_get_creation_time(c_savedstatus))
640
641             return True
642         else:
643             return False
644
645     def set_status_message(self, type, msg):
646         cdef status.PurpleStatus* c_status = NULL
647         cdef status.PurpleStatusType *c_statustype = NULL
648         cdef savedstatuses.PurpleSavedStatus *c_savedstatus = NULL
649
650         if self.__exists and msg:
651             c_status = account.purple_account_get_status(self._get_structure(),
652                     type)
653             if c_status == NULL:
654                 return False
655             status.purple_status_set_attr_string(c_status, "message", msg)
656
657             # FIXME: We can create only a savedstatus for each statustype
658             c_savedstatus = savedstatuses.purple_savedstatus_find(type)
659             if c_savedstatus == NULL:
660                 c_statustype = account.purple_account_get_status_type( \
661                         self._get_structure(), type)
662                 c_savedstatus = savedstatuses.purple_savedstatus_new( \
663                         NULL, status.purple_status_type_get_primitive( \
664                                 c_statustype))
665                 savedstatuses.purple_savedstatus_set_title(c_savedstatus,
666                         type)
667
668             savedstatuses.purple_savedstatus_set_message(c_savedstatus, msg)
669             return True
670         else:
671             return False