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/>.
34 @param protocol Protocol class instance
35 @param core Purple class instance
38 cdef object __username
39 cdef object __protocol
43 def __init__(self, username, protocol, core):
44 self.__username = username
45 self.__protocol = protocol
48 if protocol.exists and self._get_structure() != NULL:
53 cdef account.PurpleAccount *_get_structure(self):
54 return account.purple_accounts_find(self.__username, \
57 def __is_connected(self):
59 return account.purple_account_is_connected(self._get_structure())
62 is_connected = property(__is_connected)
64 def __is_connecting(self):
66 return account.purple_account_is_connecting(self._get_structure())
69 is_connecting = property(__is_connecting)
71 def __is_disconnected(self):
73 return account.purple_account_is_disconnected( \
74 self._get_structure())
77 is_disconnected = property(__is_disconnected)
81 core = property(__get_core)
83 def __get_exists(self):
85 exists = property(__get_exists)
87 def __get_username(self):
88 cdef char *username = NULL
90 username = <char *> account.purple_account_get_username( \
91 self._get_structure())
97 return self.__username
98 username = property(__get_username)
100 def __get_protocol(self):
101 return self.__protocol
102 protocol = property(__get_protocol)
104 def _get_protocol_options(self):
106 @return Dictionary {'setting': value, ...}
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
118 cdef glib.gboolean bool_value
120 c_account = self._get_structure()
122 if c_account == NULL:
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
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)
138 sett = str(<char *> setting)
140 if type == prefs.PURPLE_PREF_STRING:
142 str_value = <char *> accountopt.purple_account_option_get_default_string(option)
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:
148 str_value = <char *> account.purple_account_get_string(c_account, setting, str_value)
150 val = str(<char *> str_value)
152 elif type == prefs.PURPLE_PREF_INT:
154 int_value = accountopt.purple_account_option_get_default_int(option)
155 int_value = account.purple_account_get_int(c_account, setting, int_value)
159 elif type == prefs.PURPLE_PREF_BOOLEAN:
161 bool_value = accountopt.purple_account_option_get_default_bool(option)
162 bool_value = account.purple_account_get_bool(c_account, setting, bool_value)
164 val = bool(bool_value)
166 elif type == prefs.PURPLE_PREF_STRING_LIST:
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)
171 val = str(<char *> str_value)
178 protocol_options = property(_get_protocol_options)
180 def __get_password(self):
181 cdef char *password = NULL
183 password = <char *> account.purple_account_get_password( \
184 self._get_structure())
191 password = property(__get_password)
193 def __get_alias(self):
194 cdef char *alias = NULL
196 alias = <char *> account.purple_account_get_alias(self._get_structure())
203 alias = property(__get_alias)
205 def __get_user_info(self):
206 cdef char *user_info = NULL
208 user_info = <char *> account.purple_account_get_user_info(self._get_structure())
215 user_info = property(__get_user_info)
217 def __get_remember_password(self):
219 return account.purple_account_get_remember_password( \
220 self._get_structure())
223 remember_password = property(__get_remember_password)
225 def __get_enabled(self):
227 return account.purple_account_get_enabled(self._get_structure(), \
231 enabled = property(__get_enabled)
233 def __get_status_types(self):
234 cdef glib.GList *iter = NULL
235 cdef status.PurpleStatusType *c_statustype = NULL
237 cdef char *name = NULL
241 iter = account.purple_account_get_status_types(self._get_structure())
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))
251 status_types = property(__get_status_types)
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
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,
266 active['type'] = type
267 active['name'] = name
269 active['message'] = msg
274 active_status = property(__get_active_status)
276 def set_username(self, username):
278 Sets the account's username.
280 @param username The username
281 @return True if successful, False if account doesn't exists
284 account.purple_account_set_username(self._get_structure(), \
290 def set_protocol(self, protocol):
292 Sets the account's protocol.
294 @param protocol A Protocol class instance
295 @return True if successful, False if account doesn't exists
297 if protocol.exists and self.__exists:
298 account.purple_account_set_protocol_id(self._get_structure(), \
300 self.__protocol = protocol
305 def set_protocol_options(self, po):
307 @param po Dictionary {'setting': value, ...} options to be updated
308 @return True to success or False to failure
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
319 cdef glib.gboolean bool_value
321 c_account = self._get_structure()
323 if c_account == NULL:
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
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)
336 sett = str(<char *> setting)
342 if type == prefs.PURPLE_PREF_STRING:
344 str_value = <char *> po[sett]
345 account.purple_account_set_string(c_account, setting, str_value)
347 elif type == prefs.PURPLE_PREF_INT:
349 int_value = int(po[sett])
350 account.purple_account_set_int(c_account, setting, int_value)
352 elif type == prefs.PURPLE_PREF_BOOLEAN:
354 bool_value = bool(po[sett])
355 account.purple_account_set_bool(c_account, setting, bool_value)
357 elif type == prefs.PURPLE_PREF_STRING_LIST:
359 str_value = <char *> po[sett]
360 account.purple_account_set_string(c_account, setting, str_value)
366 def set_password(self, password):
368 Sets the account's password.
370 @param password The password
371 @return True if successful, False if account doesn't exists
374 account.purple_account_set_password(self._get_structure(), \
380 def set_alias(self, alias):
382 Sets the account's alias
384 @param alias The alias
385 @return True if successful, False if account doesn't exists
388 account.purple_account_set_alias(self._get_structure(), \
394 def set_user_info(self, user_info):
396 Sets the account's user information
398 @param user_info The user information
399 @return True if successful, False if account doesn't exists
402 account.purple_account_set_user_info(self._get_structure(), \
408 def set_remember_password(self, remember_password):
410 Sets whether or not this account should save its password.
412 @param remember_password True if should remember the password,
414 @return True if successful, False if account doesn't exists
417 account.purple_account_set_remember_password( \
418 self._get_structure(), remember_password)
423 def set_enabled(self, value):
425 Sets wheter or not this account is enabled.
427 @param value True if it is enabled, or False otherwise
428 @return True if successful, False if account doesn't exists
431 account.purple_account_set_enabled(self._get_structure(), \
432 self.__core.ui_name, bool(value))
439 Creates a new account.
441 @return True if successful, False if account already exists
446 account.purple_accounts_add(account.purple_account_new( \
447 self.__username, self.__protocol.id))
454 Removes an existing account.
456 @return True if successful, False if account doesn't exists
459 account.purple_accounts_delete(self._get_structure())
467 Connects to an account.
469 @return True if successful, False if account doesn't exists
472 account.purple_account_connect(self._get_structure())
477 def disconnect(self):
479 Disconnects from an account.
481 @return True if successful, False if account doesn't exists
484 account.purple_account_disconnect(self._get_structure())
489 def add_buddy(self, name, alias=None, group=None):
491 Adds a buddy to account's buddy list.
493 @param name Buddy name
494 @param alias Buddy alias (optional)
495 @return True if successfull, False otherwise
497 cdef blist.PurpleBuddy *c_buddy = NULL
498 cdef blist.PurpleGroup *c_group = NULL
499 cdef char *c_alias = NULL
506 if self.__exists and \
507 account.purple_account_is_connected(self._get_structure()):
508 if blist.purple_find_buddy(self._get_structure(), name):
512 c_group = blist.purple_find_group(group)
514 c_group = blist.purple_group_new(group)
516 c_buddy = blist.purple_buddy_new(self._get_structure(), \
521 blist.purple_blist_add_buddy(c_buddy, NULL, c_group, NULL)
522 account.purple_account_add_buddy(self._get_structure(), c_buddy)
524 blist.purple_blist_alias_buddy(c_buddy, c_alias)
525 server.serv_alias_buddy(c_buddy)
532 def remove_buddy(self, name):
534 Removes a buddy from account's buddy list.
536 @param name Buddy name
537 @return True if successful, False otherwise
539 cdef blist.PurpleBuddy *c_buddy = NULL
540 cdef blist.PurpleGroup *c_group = NULL
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)
548 c_group = blist.purple_buddy_get_group(c_buddy)
550 account.purple_account_remove_buddy(self._get_structure(), \
552 blist.purple_blist_remove_buddy(c_buddy)
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
563 if self.__exists and \
564 account.purple_account_is_connected(self._get_structure()):
565 iter = blist.purple_find_buddies(self._get_structure(), 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)
575 new_buddy = Buddy(name, self)
577 c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
579 new_buddy.set_alias(c_alias)
581 buddies_list.append(new_buddy)
586 def get_buddies(self):
588 @return Account's buddies list
590 cdef glib.GSList *iter = NULL
591 cdef blist.PurpleBuddy *c_buddy = NULL
592 cdef char *c_alias = NULL
596 iter = blist.purple_find_buddies(self._get_structure(), NULL)
600 c_buddy = <blist.PurpleBuddy *> iter.data
602 name = <char *> blist.purple_buddy_get_name(c_buddy)
603 new_buddy = Buddy(name, self)
605 c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
607 new_buddy.set_alias(c_alias)
609 buddies_list.append(new_buddy)
614 def request_add_buddy(self, buddy_username, buddy_alias):
616 blist.purple_blist_request_add_buddy(self._get_structure(), \
617 buddy_username, NULL, buddy_alias)
619 blist.purple_blist_request_add_buddy(self._get_structure(), \
620 buddy_username, NULL, NULL)
622 def set_active_status(self, type, msg=None):
623 cdef status.PurpleStatusType *c_statustype = NULL
624 cdef savedstatuses.PurpleSavedStatus *c_savedstatus = NULL
628 account.purple_account_set_status(self._get_structure(),
629 <char *> type, True, "message", <char *> msg, NULL)
631 account.purple_account_set_status(self._get_structure(),
632 <char *> type, True, NULL)
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( \
642 savedstatuses.purple_savedstatus_set_title(c_savedstatus,
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))
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
658 if self.__exists and msg:
659 c_status = account.purple_account_get_status(self._get_structure(),
663 status.purple_status_set_attr_string(c_status, "message", msg)
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( \
673 savedstatuses.purple_savedstatus_set_title(c_savedstatus,
676 savedstatuses.purple_savedstatus_set_message(c_savedstatus, msg)