2 # Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
4 # This file is part of python-purple.
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.
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.
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/>.
26 @param protocol Protocol class instance
27 @param core Purple class instance
30 cdef object __username
31 cdef object __protocol
35 def __init__(self, username, protocol, core):
36 self.__username = username
37 self.__protocol = protocol
40 if protocol.exists and self._get_structure() != NULL:
45 cdef account.PurpleAccount *_get_structure(self):
46 return account.purple_accounts_find(self.__username, \
49 def __is_connected(self):
51 return account.purple_account_is_connected(self._get_structure())
54 is_connected = property(__is_connected)
56 def __is_connecting(self):
58 return account.purple_account_is_connecting(self._get_structure())
61 is_connecting = property(__is_connecting)
63 def __is_disconnected(self):
65 return account.purple_account_is_disconnected( \
66 self._get_structure())
69 is_disconnected = property(__is_disconnected)
73 core = property(__get_core)
75 def __get_exists(self):
77 exists = property(__get_exists)
79 def __get_username(self):
80 cdef char *username = NULL
82 username = <char *> account.purple_account_get_username( \
83 self._get_structure())
89 return self.__username
90 username = property(__get_username)
92 def __get_protocol(self):
93 return self.__protocol
94 protocol = property(__get_protocol)
96 def _get_protocol_options(self):
98 @return Dictionary {'setting': value, ...}
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
110 cdef glib.gboolean bool_value
112 c_account = self._get_structure()
114 if c_account == NULL:
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
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)
130 sett = str(<char *> setting)
132 if type == prefs.PURPLE_PREF_STRING:
134 str_value = <char *> accountopt.purple_account_option_get_default_string(option)
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:
140 str_value = <char *> account.purple_account_get_string(c_account, setting, str_value)
142 val = str(<char *> str_value)
144 elif type == prefs.PURPLE_PREF_INT:
146 int_value = accountopt.purple_account_option_get_default_int(option)
147 int_value = account.purple_account_get_int(c_account, setting, int_value)
151 elif type == prefs.PURPLE_PREF_BOOLEAN:
153 bool_value = accountopt.purple_account_option_get_default_bool(option)
154 bool_value = account.purple_account_get_bool(c_account, setting, bool_value)
156 val = bool(bool_value)
158 elif type == prefs.PURPLE_PREF_STRING_LIST:
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)
163 val = str(<char *> str_value)
170 protocol_options = property(_get_protocol_options)
172 def __get_password(self):
173 cdef char *password = NULL
175 password = <char *> account.purple_account_get_password( \
176 self._get_structure())
183 password = property(__get_password)
185 def __get_alias(self):
186 cdef char *alias = NULL
188 alias = <char *> account.purple_account_get_alias(self._get_structure())
195 alias = property(__get_alias)
197 def __get_user_info(self):
198 cdef char *user_info = NULL
200 user_info = <char *> account.purple_account_get_user_info(self._get_structure())
207 user_info = property(__get_user_info)
209 def __get_remember_password(self):
211 return account.purple_account_get_remember_password( \
212 self._get_structure())
215 remember_password = property(__get_remember_password)
217 def __get_enabled(self):
219 return account.purple_account_get_enabled(self._get_structure(), \
223 enabled = property(__get_enabled)
225 def __get_status_types(self):
226 cdef glib.GList *iter = NULL
227 cdef status.PurpleStatusType *c_statustype = NULL
229 cdef char *name = NULL
233 iter = account.purple_account_get_status_types(self._get_structure())
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))
243 status_types = property(__get_status_types)
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
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,
258 active['type'] = type
259 active['name'] = name
261 active['message'] = msg
266 active_status = property(__get_active_status)
268 def set_username(self, username):
270 Sets the account's username.
272 @param username The username
273 @return True if successful, False if account doesn't exists
276 account.purple_account_set_username(self._get_structure(), \
282 def set_protocol(self, protocol):
284 Sets the account's protocol.
286 @param protocol A Protocol class instance
287 @return True if successful, False if account doesn't exists
289 if protocol.exists and self.__exists:
290 account.purple_account_set_protocol_id(self._get_structure(), \
292 self.__protocol = protocol
297 def set_protocol_options(self, po):
299 @param po Dictionary {'setting': value, ...} options to be updated
300 @return True to success or False to failure
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
311 cdef glib.gboolean bool_value
313 c_account = self._get_structure()
315 if c_account == NULL:
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
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)
328 sett = str(<char *> setting)
330 if not po.has_key(sett):
334 if type == prefs.PURPLE_PREF_STRING:
336 str_value = <char *> po[sett]
337 account.purple_account_set_string(c_account, setting, str_value)
339 elif type == prefs.PURPLE_PREF_INT:
341 int_value = int(po[sett])
342 account.purple_account_set_int(c_account, setting, int_value)
344 elif type == prefs.PURPLE_PREF_BOOLEAN:
346 bool_value = bool(po[sett])
347 account.purple_account_set_bool(c_account, setting, bool_value)
349 elif type == prefs.PURPLE_PREF_STRING_LIST:
351 str_value = <char *> po[sett]
352 account.purple_account_set_string(c_account, setting, str_value)
358 def set_password(self, password):
360 Sets the account's password.
362 @param password The password
363 @return True if successful, False if account doesn't exists
366 account.purple_account_set_password(self._get_structure(), \
372 def set_alias(self, alias):
374 Sets the account's alias
376 @param alias The alias
377 @return True if successful, False if account doesn't exists
380 account.purple_account_set_alias(self._get_structure(), \
386 def set_user_info(self, user_info):
388 Sets the account's user information
390 @param user_info The user information
391 @return True if successful, False if account doesn't exists
394 account.purple_account_set_user_info(self._get_structure(), \
400 def set_remember_password(self, remember_password):
402 Sets whether or not this account should save its password.
404 @param remember_password True if should remember the password,
406 @return True if successful, False if account doesn't exists
409 account.purple_account_set_remember_password( \
410 self._get_structure(), remember_password)
415 def set_enabled(self, value):
417 Sets wheter or not this account is enabled.
419 @param value True if it is enabled, or False otherwise
420 @return True if successful, False if account doesn't exists
423 account.purple_account_set_enabled(self._get_structure(), \
424 self.__core.ui_name, bool(value))
431 Creates a new account.
433 @return True if successful, False if account already exists
438 account.purple_accounts_add(account.purple_account_new( \
439 self.__username, self.__protocol.id))
446 Removes an existing account.
448 @return True if successful, False if account doesn't exists
451 account.purple_accounts_delete(self._get_structure())
459 Connects to an account.
461 @return True if successful, False if account doesn't exists
464 account.purple_account_connect(self._get_structure())
469 def disconnect(self):
471 Disconnects from an account.
473 @return True if successful, False if account doesn't exists
476 account.purple_account_disconnect(self._get_structure())
481 def add_buddy(self, name, alias=None, group=None):
483 Adds a buddy to account's buddy list.
485 @param name Buddy name
486 @param alias Buddy alias (optional)
487 @return True if successfull, False otherwise
489 cdef blist.PurpleBuddy *c_buddy = NULL
490 cdef blist.PurpleGroup *c_group = NULL
491 cdef char *c_alias = NULL
498 if self.__exists and \
499 account.purple_account_is_connected(self._get_structure()):
500 if blist.purple_find_buddy(self._get_structure(), name):
504 c_group = blist.purple_find_group(group)
506 c_group = blist.purple_group_new(group)
508 c_buddy = blist.purple_buddy_new(self._get_structure(), \
513 blist.purple_blist_add_buddy(c_buddy, NULL, c_group, NULL)
514 account.purple_account_add_buddy(self._get_structure(), c_buddy)
516 blist.purple_blist_alias_buddy(c_buddy, c_alias)
517 server.serv_alias_buddy(c_buddy)
524 def remove_buddy(self, name):
526 Removes a buddy from account's buddy list.
528 @param name Buddy name
529 @return True if successful, False otherwise
531 cdef blist.PurpleBuddy *c_buddy = NULL
532 cdef blist.PurpleGroup *c_group = NULL
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)
540 c_group = blist.purple_buddy_get_group(c_buddy)
542 account.purple_account_remove_buddy(self._get_structure(), \
544 blist.purple_blist_remove_buddy(c_buddy)
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
555 if self.__exists and \
556 account.purple_account_is_connected(self._get_structure()):
557 iter = blist.purple_find_buddies(self._get_structure(), 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)
567 new_buddy = Buddy(name, self)
569 c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
571 new_buddy.set_alias(c_alias)
573 buddies_list.append(new_buddy)
578 def get_buddies(self):
580 @return Account's buddies list
582 cdef glib.GSList *iter = NULL
583 cdef blist.PurpleBuddy *c_buddy = NULL
584 cdef char *c_alias = NULL
588 iter = blist.purple_find_buddies(self._get_structure(), NULL)
592 c_buddy = <blist.PurpleBuddy *> iter.data
594 name = <char *> blist.purple_buddy_get_name(c_buddy)
595 new_buddy = Buddy(name, self)
597 c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
599 new_buddy.set_alias(c_alias)
601 buddies_list.append(new_buddy)
606 def request_add_buddy(self, buddy_username, buddy_alias):
608 blist.purple_blist_request_add_buddy(self._get_structure(), \
609 buddy_username, NULL, buddy_alias)
611 blist.purple_blist_request_add_buddy(self._get_structure(), \
612 buddy_username, NULL, NULL)
614 def set_active_status(self, type, msg=None):
615 cdef status.PurpleStatusType *c_statustype = NULL
616 cdef savedstatuses.PurpleSavedStatus *c_savedstatus = NULL
620 account.purple_account_set_status(self._get_structure(),
621 <char *> type, True, "message", <char *> msg, NULL)
623 account.purple_account_set_status(self._get_structure(),
624 <char *> type, True, NULL)
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( \
634 savedstatuses.purple_savedstatus_set_title(c_savedstatus,
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))
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
650 if self.__exists and msg:
651 c_status = account.purple_account_get_status(self._get_structure(),
655 status.purple_status_set_attr_string(c_status, "message", msg)
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( \
665 savedstatuses.purple_savedstatus_set_title(c_savedstatus,
668 savedstatuses.purple_savedstatus_set_message(c_savedstatus, msg)