Updated python-purple initial parameters.
[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 set_username(self, username):
226         """
227         Sets the account's username.
228
229         @param username The username
230         @return True if successful, False if account doesn't exists
231         """
232         if self.__exists:
233             account.purple_account_set_username(self._get_structure(), \
234                     username)
235             return True
236         else:
237             return False
238
239     def set_protocol(self, protocol):
240         """
241         Sets the account's protocol.
242
243         @param protocol A Protocol class instance
244         @return True if successful, False if account doesn't exists
245         """
246         if protocol.exists and self.__exists:
247             account.purple_account_set_protocol_id(self._get_structure(), \
248                         protocol.id)
249             self.__protocol = protocol
250             return True
251         else:
252             return False
253
254     def set_protocol_options(self, po):
255         """
256         @param po Dictionary {'setting': value, ...} options to be updated
257         @return True to success or False to failure
258         """
259         cdef glib.GList *iter
260         cdef account.PurpleAccount *c_account
261         cdef plugin.PurplePlugin *c_plugin
262         cdef prpl.PurplePluginProtocolInfo *prpl_info
263         cdef accountopt.PurpleAccountOption *option
264         cdef prefs.PurplePrefType type
265         cdef char *str_value
266         cdef char *setting
267         cdef int int_value
268         cdef glib.gboolean bool_value
269
270         c_account = self._get_structure()
271
272         if c_account == NULL:
273             return False
274
275         c_plugin = plugin.purple_plugins_find_with_id(self.__protocol.id)
276         prpl_info = plugin.PURPLE_PLUGIN_PROTOCOL_INFO(c_plugin)
277         iter = prpl_info.protocol_options
278
279         while iter:
280
281             option = <accountopt.PurpleAccountOption *> iter.data
282             type = accountopt.purple_account_option_get_type(option)
283             setting = <char *> accountopt.purple_account_option_get_setting(option)
284
285             sett = str(<char *> setting)
286
287             if not po.has_key(sett):
288                 iter = iter.next
289                 continue
290
291             if type == prefs.PURPLE_PREF_STRING:
292
293                 str_value = <char *> po[sett]
294                 account.purple_account_set_string(c_account, setting, str_value)
295
296             elif type == prefs.PURPLE_PREF_INT:
297
298                 int_value = int(po[sett])
299                 account.purple_account_set_int(c_account, setting, int_value)
300
301             elif type == prefs.PURPLE_PREF_BOOLEAN:
302
303                 bool_value = bool(po[sett])
304                 account.purple_account_set_bool(c_account, setting, bool_value)
305
306             elif type == prefs.PURPLE_PREF_STRING_LIST:
307
308                 str_value = <char *> po[sett]
309                 account.purple_account_set_string(c_account, setting, str_value)
310
311             iter = iter.next
312
313         return True
314
315     def set_password(self, password):
316         """
317         Sets the account's password.
318
319         @param password The password
320         @return True if successful, False if account doesn't exists
321         """
322         if self.__exists:
323             account.purple_account_set_password(self._get_structure(), \
324                     password)
325             return True
326         else:
327             return False
328
329     def set_alias(self, alias):
330         """
331         Sets the account's alias
332
333         @param alias The alias
334         @return True if successful, False if account doesn't exists
335         """
336         if self.__exists:
337             account.purple_account_set_alias(self._get_structure(), \
338                     alias)
339             return True
340         else:
341             return False
342
343     def set_user_info(self, user_info):
344         """
345         Sets the account's user information
346
347         @param user_info The user information
348         @return True if successful, False if account doesn't exists
349         """
350         if self.__exists:
351             account.purple_account_set_user_info(self._get_structure(), \
352                     user_info)
353             return True
354         else:
355             return False
356
357     def set_remember_password(self, remember_password):
358         """
359         Sets whether or not this account should save its password.
360
361         @param remember_password True if should remember the password,
362                                  or False otherwise
363         @return True if successful, False if account doesn't exists
364         """
365         if self.__exists:
366             account.purple_account_set_remember_password( \
367                 self._get_structure(), remember_password)
368             return True
369         else:
370             return False
371
372     def set_enabled(self, value):
373         """
374         Sets wheter or not this account is enabled.
375
376         @param value True if it is enabled, or False otherwise
377         @return True if successful, False if account doesn't exists
378         """
379         if self.__exists:
380             account.purple_account_set_enabled(self._get_structure(), \
381                     self.__core.ui_name, bool(value))
382             return True
383         else:
384             return False
385
386     def new(self):
387         """
388         Creates a new account.
389
390         @return True if successful, False if account already exists
391         """
392         if self.__exists:
393             return False
394         else:
395             account.purple_accounts_add(account.purple_account_new( \
396                     self.__username, self.__protocol.id))
397
398             self.__exists = True
399             return True
400
401     def remove(self):
402         """
403         Removes an existing account.
404
405         @return True if successful, False if account doesn't exists
406         """
407         if self.__exists:
408             account.purple_accounts_delete(self._get_structure())
409             self__exists = False
410             return True
411         else:
412             return False
413
414     def connect(self):
415         """
416         Connects to an account.
417
418         @return True if successful, False if account doesn't exists
419         """
420         if self.__exists:
421             account.purple_account_connect(self._get_structure())
422             return True
423         else:
424             return False
425
426     def disconnect(self):
427         """
428         Disconnects from an account.
429
430         @return True if successful, False if account doesn't exists
431         """
432         if self.__exists:
433             account.purple_account_disconnect(self._get_structure())
434             return True
435         else:
436             return False
437
438     def add_buddy(self, name, alias=None, group=None):
439         """
440         Adds a buddy to account's buddy list.
441
442         @param name  Buddy name
443         @param alias Buddy alias (optional)
444         @return True if successfull, False otherwise
445         """
446         cdef blist.PurpleBuddy *c_buddy = NULL
447         cdef blist.PurpleGroup *c_group = NULL
448         cdef char *c_alias = NULL
449
450         if alias:
451             c_alias = alias
452         else:
453             c_alias = NULL
454
455         if self.__exists and \
456                 account.purple_account_is_connected(self._get_structure()):
457             if blist.purple_find_buddy(self._get_structure(), name):
458                 return False
459
460             if group:
461                 c_group = blist.purple_find_group(group)
462                 if c_group == NULL:
463                     c_group = blist.purple_group_new(group)
464
465             c_buddy = blist.purple_buddy_new(self._get_structure(), \
466                     name, c_alias)
467             if c_buddy == NULL:
468                 return False
469
470             blist.purple_blist_add_buddy(c_buddy, NULL, c_group, NULL)
471             account.purple_account_add_buddy(self._get_structure(), c_buddy)
472             return True
473
474         else:
475             return None
476
477     def remove_buddy(self, name):
478         """
479         Removes a buddy from account's buddy list.
480
481         @param name Buddy name
482         @return True if successful, False otherwise
483         """
484         cdef blist.PurpleBuddy *c_buddy = NULL
485         cdef blist.PurpleGroup *c_group = NULL
486
487         if self.__exists and \
488                 account.purple_account_is_connected(self._get_structure()):
489             c_buddy = blist.purple_find_buddy(self._get_structure(), name)
490             if c_buddy == NULL:
491                 return False
492
493             c_group = blist.purple_buddy_get_group(c_buddy)
494
495             account.purple_account_remove_buddy(self._get_structure(), \
496                     c_buddy, c_group)
497             blist.purple_blist_remove_buddy(c_buddy)
498             return True
499         else:
500             return None
501
502     def get_buddies_online(self):
503         cdef glib.GSList *iter = NULL
504         cdef blist.PurpleBuddy *c_buddy = NULL
505         cdef char *c_alias = NULL
506
507         if self.__exists and \
508                 account.purple_account_is_connected(self._get_structure()):
509             iter = blist.purple_find_buddies(self._get_structure(), NULL)
510
511             buddies_list = []
512             while iter:
513                 c_alias = NULL
514                 c_buddy = <blist.PurpleBuddy *> iter.data
515                 if <blist.PurpleBuddy *> c_buddy and \
516                         status.purple_presence_is_online( \
517                                 blist.purple_buddy_get_presence(c_buddy)):
518                     name = <char *> blist.purple_buddy_get_name(c_buddy)
519
520                     new_buddy = Buddy(name, self)
521
522                     c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
523                     if c_alias:
524                         new_buddy.set_alias(c_alias)
525
526                     buddies_list.append(new_buddy)
527                 iter = iter.next
528             return buddies_list
529         else:
530             return None
531
532     def request_add_buddy(self, buddy_username, buddy_alias):
533         if buddy_alias:
534             blist.purple_blist_request_add_buddy(self._get_structure(), \
535                     buddy_username, NULL, buddy_alias)
536         else:
537             blist.purple_blist_request_add_buddy(self._get_structure(), \
538                     buddy_username, NULL, NULL)