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 # Google Talk default domain hackery!
137 if str_value == NULL and str(<char *> label_name) == "Connect server":
138 str_value = "talk.google.com"
139 str_value = <char *> account.purple_account_get_string(c_account, setting, str_value)
141 val = str(<char *> str_value)
143 elif type == prefs.PURPLE_PREF_INT:
145 int_value = accountopt.purple_account_option_get_default_int(option)
146 int_value = account.purple_account_get_int(c_account, setting, int_value)
150 elif type == prefs.PURPLE_PREF_BOOLEAN:
152 bool_value = accountopt.purple_account_option_get_default_bool(option)
153 bool_value = account.purple_account_get_bool(c_account, setting, bool_value)
155 val = bool(bool_value)
157 elif type == prefs.PURPLE_PREF_STRING_LIST:
159 str_value = <char *> accountopt.purple_account_option_get_default_list_value(option)
160 str_value = <char *> account.purple_account_get_string(c_account, setting, str_value)
162 val = str(<char *> str_value)
169 protocol_options = property(_get_protocol_options)
171 def __get_password(self):
172 cdef char *password = NULL
174 password = <char *> account.purple_account_get_password( \
175 self._get_structure())
182 password = property(__get_password)
184 def __get_alias(self):
185 cdef char *alias = NULL
187 alias = <char *> account.purple_account_get_alias(self._get_structure())
194 alias = property(__get_alias)
196 def __get_user_info(self):
197 cdef char *user_info = NULL
199 user_info = <char *> account.purple_account_get_user_info(self._get_structure())
206 user_info = property(__get_user_info)
208 def __get_remember_password(self):
210 return account.purple_account_get_remember_password( \
211 self._get_structure())
214 remember_password = property(__get_remember_password)
216 def __get_enabled(self):
218 return account.purple_account_get_enabled(self._get_structure(), \
222 enabled = property(__get_enabled)
224 def set_username(self, username):
226 Sets the account's username.
228 @param username The username
229 @return True if successful, False if account doesn't exists
232 account.purple_account_set_username(self._get_structure(), \
238 def set_protocol(self, protocol):
240 Sets the account's protocol.
242 @param protocol A Protocol class instance
243 @return True if successful, False if account doesn't exists
245 if protocol.exists and self.__exists:
246 account.purple_account_set_protocol_id(self._get_structure(), \
248 self.__protocol = protocol
253 def set_protocol_options(self, po):
255 @param po Dictionary {'setting': value, ...} options to be updated
256 @return True to success or False to failure
258 cdef glib.GList *iter
259 cdef account.PurpleAccount *c_account
260 cdef plugin.PurplePlugin *c_plugin
261 cdef prpl.PurplePluginProtocolInfo *prpl_info
262 cdef accountopt.PurpleAccountOption *option
263 cdef prefs.PurplePrefType type
267 cdef glib.gboolean bool_value
269 c_account = self._get_structure()
271 if c_account == NULL:
274 c_plugin = plugin.purple_plugins_find_with_id(self.__protocol.id)
275 prpl_info = plugin.PURPLE_PLUGIN_PROTOCOL_INFO(c_plugin)
276 iter = prpl_info.protocol_options
280 option = <accountopt.PurpleAccountOption *> iter.data
281 type = accountopt.purple_account_option_get_type(option)
282 setting = <char *> accountopt.purple_account_option_get_setting(option)
284 sett = str(<char *> setting)
286 if not po.has_key(sett):
290 if type == prefs.PURPLE_PREF_STRING:
292 str_value = <char *> po[sett]
293 account.purple_account_set_string(c_account, setting, str_value)
295 elif type == prefs.PURPLE_PREF_INT:
297 int_value = int(po[sett])
298 account.purple_account_set_int(c_account, setting, int_value)
300 elif type == prefs.PURPLE_PREF_BOOLEAN:
302 bool_value = bool(po[sett])
303 account.purple_account_set_bool(c_account, setting, bool_value)
305 elif type == prefs.PURPLE_PREF_STRING_LIST:
307 str_value = <char *> po[sett]
308 account.purple_account_set_string(c_account, setting, str_value)
314 def set_password(self, password):
316 Sets the account's password.
318 @param password The password
319 @return True if successful, False if account doesn't exists
322 account.purple_account_set_password(self._get_structure(), \
328 def set_alias(self, alias):
330 Sets the account's alias
332 @param alias The alias
333 @return True if successful, False if account doesn't exists
336 account.purple_account_set_alias(self._get_structure(), \
342 def set_user_info(self, user_info):
344 Sets the account's user information
346 @param user_info The user information
347 @return True if successful, False if account doesn't exists
350 account.purple_account_set_user_info(self._get_structure(), \
356 def set_remember_password(self, remember_password):
358 Sets whether or not this account should save its password.
360 @param remember_password True if should remember the password,
362 @return True if successful, False if account doesn't exists
365 account.purple_account_set_remember_password( \
366 self._get_structure(), remember_password)
371 def set_enabled(self, value):
373 Sets wheter or not this account is enabled.
375 @param value True if it is enabled, or False otherwise
376 @return True if successful, False if account doesn't exists
379 account.purple_account_set_enabled(self._get_structure(), \
380 self.__core.ui_name, bool(value))
387 Creates a new account.
389 @return True if successful, False if account already exists
394 account.purple_accounts_add(account.purple_account_new( \
395 self.__username, self.__protocol.id))
402 Removes an existing account.
404 @return True if successful, False if account doesn't exists
407 account.purple_accounts_delete(self._get_structure())
415 Connects to an account.
417 @return True if successful, False if account doesn't exists
420 account.purple_account_connect(self._get_structure())
425 def disconnect(self):
427 Disconnects from an account.
429 @return True if successful, False if account doesn't exists
432 account.purple_account_disconnect(self._get_structure())
437 def add_buddy(self, name, alias=None, group=None):
439 Adds a buddy to account's buddy list.
441 @param name Buddy name
442 @param alias Buddy alias (optional)
443 @return True if successfull, False otherwise
445 cdef blist.PurpleBuddy *c_buddy = NULL
446 cdef blist.PurpleGroup *c_group = NULL
447 cdef char *c_alias = NULL
454 if self.__exists and \
455 account.purple_account_is_connected(self._get_structure()):
456 if blist.purple_find_buddy(self._get_structure(), name):
460 c_group = blist.purple_find_group(group)
462 c_group = blist.purple_group_new(group)
464 c_buddy = blist.purple_buddy_new(self._get_structure(), \
469 blist.purple_blist_add_buddy(c_buddy, NULL, c_group, NULL)
470 account.purple_account_add_buddy(self._get_structure(), c_buddy)
476 def remove_buddy(self, name):
478 Removes a buddy from account's buddy list.
480 @param name Buddy name
481 @return True if successful, False otherwise
483 cdef blist.PurpleBuddy *c_buddy = NULL
484 cdef blist.PurpleGroup *c_group = NULL
486 if self.__exists and \
487 account.purple_account_is_connected(self._get_structure()):
488 c_buddy = blist.purple_find_buddy(self._get_structure(), name)
492 c_group = blist.purple_buddy_get_group(c_buddy)
494 account.purple_account_remove_buddy(self._get_structure(), \
496 blist.purple_blist_remove_buddy(c_buddy)
501 def get_buddies_online(self):
502 cdef glib.GSList *iter = NULL
503 cdef blist.PurpleBuddy *c_buddy = NULL
504 cdef char *c_alias = NULL
506 if self.__exists and \
507 account.purple_account_is_connected(self._get_structure()):
508 iter = blist.purple_find_buddies(self._get_structure(), NULL)
513 c_buddy = <blist.PurpleBuddy *> iter.data
514 if <blist.PurpleBuddy *> c_buddy and \
515 status.purple_presence_is_online( \
516 blist.purple_buddy_get_presence(c_buddy)):
517 name = <char *> blist.purple_buddy_get_name(c_buddy)
519 new_buddy = Buddy(name, self)
521 c_alias = <char *> blist.purple_buddy_get_alias_only(c_buddy)
523 new_buddy.set_alias(c_alias)
525 buddies_list.append(new_buddy)