8973bfe626dbdda27fe858e6515bc88999ccca26
[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                 # 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)
140
141                 val = str(<char *> str_value)
142
143             elif type == prefs.PURPLE_PREF_INT:
144
145                 int_value = accountopt.purple_account_option_get_default_int(option)
146                 int_value = account.purple_account_get_int(c_account, setting, int_value)
147
148                 val = int(int_value)
149
150             elif type == prefs.PURPLE_PREF_BOOLEAN:
151
152                 bool_value = accountopt.purple_account_option_get_default_bool(option)
153                 bool_value = account.purple_account_get_bool(c_account, setting, bool_value)
154
155                 val = bool(bool_value)
156
157             elif type == prefs.PURPLE_PREF_STRING_LIST:
158
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)
161
162                 val = str(<char *> str_value)
163
164             iter = iter.next
165
166             po[sett] = val
167
168         return po
169     protocol_options = property(_get_protocol_options)
170
171     def __get_password(self):
172         cdef char *password = NULL
173         if self.__exists:
174             password = <char *> account.purple_account_get_password( \
175                     self._get_structure())
176             if password:
177                 return password
178             else:
179                 return None
180         else:
181             return None
182     password = property(__get_password)
183
184     def __get_alias(self):
185         cdef char *alias = NULL
186         if self.__exists:
187             alias = <char *> account.purple_account_get_alias(self._get_structure())
188             if alias:
189                 return alias
190             else:
191                 return None
192         else:
193             return None
194     alias = property(__get_alias)
195
196     def __get_user_info(self):
197         cdef char *user_info = NULL
198         if self.__exists:
199             user_info = <char *> account.purple_account_get_user_info(self._get_structure())
200             if user_info:
201                 return user_info
202             else:
203                 return None
204         else:
205             return None
206     user_info = property(__get_user_info)
207
208     def __get_remember_password(self):
209         if self.__exists:
210             return account.purple_account_get_remember_password( \
211                     self._get_structure())
212         else:
213             return None
214     remember_password = property(__get_remember_password)
215
216     def __get_enabled(self):
217         if self.__exists:
218             return account.purple_account_get_enabled(self._get_structure(), \
219                     self.__core.ui_name)
220         else:
221             return None
222     enabled = property(__get_enabled)
223
224     def set_username(self, username):
225         """
226         Sets the account's username.
227
228         @param username The username
229         @return True if successful, False if account doesn't exists
230         """
231         if self.__exists:
232             account.purple_account_set_username(self._get_structure(), \
233                     username)
234             return True
235         else:
236             return False
237
238     def set_protocol(self, protocol):
239         """
240         Sets the account's protocol.
241
242         @param protocol A Protocol class instance
243         @return True if successful, False if account doesn't exists
244         """
245         if protocol.exists and self.__exists:
246             account.purple_account_set_protocol_id(self._get_structure(), \
247                         protocol.id)
248             self.__protocol = protocol
249             return True
250         else:
251             return False
252
253     def set_protocol_options(self, po):
254         """
255         @param po Dictionary {'setting': value, ...} options to be updated
256         @return True to success or False to failure
257         """
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
264         cdef char *str_value
265         cdef char *setting
266         cdef int int_value
267         cdef glib.gboolean bool_value
268
269         c_account = self._get_structure()
270
271         if c_account == NULL:
272             return False
273
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
277
278         while iter:
279
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)
283
284             sett = str(<char *> setting)
285
286             if type == prefs.PURPLE_PREF_STRING:
287
288                 str_value = <char *> po[sett]
289                 account.purple_account_set_string(c_account, setting, str_value)
290
291             elif type == prefs.PURPLE_PREF_INT:
292
293                 int_value = int(po[sett])
294                 account.purple_account_set_int(c_account, setting, int_value)
295
296             elif type == prefs.PURPLE_PREF_BOOLEAN:
297
298                 bool_value = bool(po[sett])
299                 account.purple_account_set_bool(c_account, setting, bool_value)
300
301             elif type == prefs.PURPLE_PREF_STRING_LIST:
302
303                 str_value = <char *> po[sett]
304                 account.purple_account_set_string(c_account, setting, str_value)
305
306             iter = iter.next
307
308         return True
309
310     def set_password(self, password):
311         """
312         Sets the account's password.
313
314         @param password The password
315         @return True if successful, False if account doesn't exists
316         """
317         if self.__exists:
318             account.purple_account_set_password(self._get_structure(), \
319                     password)
320             return True
321         else:
322             return False
323
324     def set_alias(self, alias):
325         """
326         Sets the account's alias
327
328         @param alias The alias
329         @return True if successful, False if account doesn't exists
330         """
331         if self.__exists:
332             account.purple_account_set_alias(self._get_structure(), \
333                     alias)
334             return True
335         else:
336             return False
337
338     def set_user_info(self, user_info):
339         """
340         Sets the account's user information
341
342         @param user_info The user information
343         @return True if successful, False if account doesn't exists
344         """
345         if self.__exists:
346             account.purple_account_set_user_info(self._get_structure(), \
347                     user_info)
348             return True
349         else:
350             return False
351
352     def set_remember_password(self, remember_password):
353         """
354         Sets whether or not this account should save its password.
355
356         @param remember_password True if should remember the password,
357                                  or False otherwise
358         @return True if successful, False if account doesn't exists
359         """
360         if self.__exists:
361             account.purple_account_set_remember_password( \
362                 self._get_structure(), remember_password)
363             return True
364         else:
365             return False
366
367     def set_enabled(self, value):
368         """
369         Sets wheter or not this account is enabled.
370
371         @param value True if it is enabled, or False otherwise
372         @return True if successful, False if account doesn't exists
373         """
374         if self.__exists:
375             account.purple_account_set_enabled(self._get_structure(), \
376                     self.__core.ui_name, bool(value))
377             return True
378         else:
379             return False
380
381     def new(self):
382         """
383         Creates a new account.
384
385         @return True if successful, False if account already exists
386         """
387         if self.__exists:
388             return False
389         else:
390             account.purple_account_new(self.__username, self.__protocol.id)
391             self.__exists = True
392             return True
393
394     def connect(self):
395         """
396         Connects to an account.
397
398         @return True if successful, False if account doesn't exists
399         """
400         if self.__exists:
401             account.purple_account_connect(self._get_structure())
402             return True
403         else:
404             return False
405
406     def disconnect(self):
407         """
408         Disconnects from an account.
409
410         @return True if successful, False if account doesn't exists
411         """
412         if self.__exists:
413             account.purple_account_disconnect(self._get_structure())
414             return True
415         else:
416             return False
417