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