Adding function set_options to Plugin class
[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 extern from *:
23     ctypedef char const_char "const char"
24
25 cdef class Account:
26     """ Account class """
27     cdef account.PurpleAccount *c_account
28     cdef plugin.PurplePlugin *c_plugin
29     cdef prpl.PurplePluginProtocolInfo *c_prpl_info
30     cdef plugin.PurplePluginInfo *c_plugin_info
31     cdef savedstatuses.PurpleSavedStatus *__sstatus
32     cdef ProxyInfo __proxy
33
34     def __init__(self, char *username, char *protocol_id):
35         cdef proxy.PurpleProxyInfo *c_proxyinfo
36         cdef account.PurpleAccount *acc = NULL
37
38         acc = account.c_purple_accounts_find(username, protocol_id)
39         if acc:
40             self.c_account = acc
41             c_proxyinfo = account.c_purple_account_get_proxy_info(self.c_account)
42         else:
43             self.c_account = account.c_purple_account_new(username, protocol_id)
44             c_proxyinfo = account.c_purple_account_get_proxy_info(self.c_account)
45             if c_proxyinfo == NULL:
46                 c_proxyinfo = proxy.c_purple_proxy_info_new()
47                 proxy.c_purple_proxy_info_set_type(c_proxyinfo, proxy.PURPLE_PROXY_NONE)
48                 account.c_purple_account_set_proxy_info(self.c_account, c_proxyinfo)
49         self.__proxy = ProxyInfo()
50         self.__proxy.c_proxyinfo = c_proxyinfo
51         acc = NULL
52
53         self.c_plugin = plugin.c_purple_plugins_find_with_id(protocol_id)
54         self.c_prpl_info = plugin.c_PURPLE_PLUGIN_PROTOCOL_INFO(self.c_plugin)
55
56     def __get_username(self):
57         if self.c_account:
58             return account.c_purple_account_get_username(self.c_account)
59         else:
60             return None
61     def __set_username(self, username):
62         if self.c_account:
63             account.c_purple_account_set_username(self.c_account, username)
64     username = property(__get_password, __set_username)
65
66     def __get_password(self):
67         if self.c_account:
68             return account.c_purple_account_get_password(self.c_account)
69         else:
70             return None
71     def __set_password(self, password):
72         if self.c_account:
73             account.c_purple_account_set_password(self.c_account, password)
74     password = property(__get_password, __set_password)
75
76     def __get_alias(self):
77         if self.c_account:
78             return account.c_purple_account_get_alias(self.c_account)
79         else:
80             return None
81     def __set_alias(self, alias):
82         if self.c_account:
83             account.c_purple_account_set_alias(self.c_account, alias)
84     alias = property(__get_alias, __set_alias)
85
86     def __get_user_info(self):
87         if self.c_account:
88             return account.c_purple_account_get_user_info(self.c_account)
89         else:
90             return None
91     def __set_user_info(self, user_info):
92         if self.c_account:
93             account.c_purple_account_set_user_info(self.c_account, user_info)
94     user_info = property(__get_user_info, __set_user_info)
95
96     def __get_protocol_id(self):
97         if self.c_account:
98             return account.c_purple_account_get_protocol_id(self.c_account)
99         else:
100             return None
101     def __set_protocol_id(self, protocol_id):
102         if self.c_account:
103             account.c_purple_account_set_protocol_id(self.c_account, protocol_id)
104     protocol_id = property(__get_protocol_id, __set_protocol_id)
105
106     def __get_remember_password(self):
107         if self.c_account:
108             return account.c_purple_account_get_remember_password(self.c_account)
109         else:
110             return None
111     def __set_remember_password(self, value):
112         if self.c_account:
113             account.c_purple_account_set_remember_password(self.c_account, value)
114     remember_password = property(__get_remember_password, __set_remember_password)
115
116     def _get_protocol_options(self):
117         cdef glib.GList *iter
118         cdef accountopt.PurpleAccountOption *option
119         cdef prefs.PurplePrefType type
120         cdef const_char *label_name
121         cdef const_char *str_value
122         cdef const_char *setting
123         cdef int int_value
124         cdef glib.gboolean bool_value
125
126         if self.c_account == NULL:
127             return None
128
129         po = {}
130
131         iter = self.c_prpl_info.protocol_options
132
133         while iter:
134
135             option = <accountopt.PurpleAccountOption *> iter.data
136             type = accountopt.c_purple_account_option_get_type(option)
137             label_name = accountopt.c_purple_account_option_get_text(option)
138             setting = accountopt.c_purple_account_option_get_setting(option)
139
140             sett = str(<char *> setting)
141
142             if type == prefs.PURPLE_PREF_STRING:
143
144                 str_value = accountopt.c_purple_account_option_get_default_string(option)
145
146                 # Google Talk default domain hackery!
147                 if str_value == NULL and str(<char *> label_name) == "Connect server":
148                     str_value = "talk.google.com"
149                 str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
150
151                 val = str(<char *> str_value)
152
153             elif type == prefs.PURPLE_PREF_INT:
154
155                 int_value = accountopt.c_purple_account_option_get_default_int(option)
156                 int_value = account.c_purple_account_get_int(self.c_account, setting, int_value)
157
158                 val = int(int_value)
159
160             elif type == prefs.PURPLE_PREF_BOOLEAN:
161
162                 bool_value = accountopt.c_purple_account_option_get_default_bool(option)
163                 bool_value = account.c_purple_account_get_bool(self.c_account, setting, bool_value)
164
165                 val = bool(bool_value)
166
167             elif type == prefs.PURPLE_PREF_STRING_LIST:
168
169                 str_value = accountopt.c_purple_account_option_get_default_list_value(option)
170                 str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
171
172                 val = str(<char *> str_value)
173
174             iter = iter.next
175
176             po[sett] = val
177
178         return po
179
180     def _set_protocol_options(self, po):
181         cdef glib.GList *iter
182         cdef accountopt.PurpleAccountOption *option
183         cdef prefs.PurplePrefType type
184         cdef const_char *str_value
185         cdef const_char *setting
186         cdef int int_value
187         cdef glib.gboolean bool_value
188
189         if self.c_account == NULL:
190             return
191
192         po = {}
193
194         iter = self.c_prpl_info.protocol_options
195
196         while iter:
197
198             option = <accountopt.PurpleAccountOption *> iter.data
199             type = accountopt.c_purple_account_option_get_type(option)
200             setting = accountopt.c_purple_account_option_get_setting(option)
201
202             sett = str(<char *> setting)
203
204             if type == prefs.PURPLE_PREF_STRING:
205
206                 str_value = <char *> po[sett]
207                 account.c_purple_account_set_string(self.c_account, setting, str_value)
208
209             elif type == prefs.PURPLE_PREF_INT:
210
211                 int_value = int(po[sett])
212                 account.c_purple_account_set_int(self.c_account, setting, int_value)
213
214             elif type == prefs.PURPLE_PREF_BOOLEAN:
215
216                 bool_value = bool(po[sett])
217                 account.c_purple_account_set_bool(self.c_account, setting, bool_value)
218
219             elif type == prefs.PURPLE_PREF_STRING_LIST:
220
221                 str_value = <char *> po[sett]
222                 account.c_purple_account_set_string(self.c_account, setting, str_value)
223
224             iter = iter.next
225
226     protocol_options = property(_get_protocol_options, _set_protocol_options)
227
228     def _get_protocol_labels(self):
229         cdef glib.GList *iter
230         cdef accountopt.PurpleAccountOption *option
231         cdef const_char *label_name
232         cdef const_char *setting
233
234         if self.c_account == NULL:
235             return None
236
237         po = {}
238
239         iter = self.c_prpl_info.protocol_options
240
241         while iter:
242
243             option = <accountopt.PurpleAccountOption *> iter.data
244             label_name = accountopt.c_purple_account_option_get_text(option)
245             setting = accountopt.c_purple_account_option_get_setting(option)
246
247             sett = str(<char *> setting)
248             label = str(<char *> label_name)
249
250             po[sett] = label
251
252         return po
253
254     protocol_labels = property(_get_protocol_labels)
255
256     def get_protocol_name(self):
257         if self.c_account:
258             return account.c_purple_account_get_protocol_name(self.c_account)
259         else:
260             return None
261
262     def get_enabled(self, ui):
263         if self.c_account:
264             return account.c_purple_account_get_enabled(self.c_account, ui)
265         else:
266             return None
267
268     def set_enabled(self, ui, value):
269         if self.c_account:
270             account.c_purple_account_set_enabled(self.c_account, ui, value)
271
272     def set_status(self):
273         self.__sstatus = savedstatuses.c_purple_savedstatus_new(NULL, status.PURPLE_STATUS_AVAILABLE)
274         savedstatuses.c_purple_savedstatus_activate(self.__sstatus)
275
276     def __get_proxy(self):
277         return self.__proxy
278     proxy = property(__get_proxy)
279
280     def get_buddies_online(self):
281         cdef glib.GSList *iter
282         cdef blist.PurpleBuddy *buddy
283         cdef char *c_name = NULL
284         cdef char *c_alias = NULL
285         buddies = []
286         iter = blist.c_purple_find_buddies(self.c_account, NULL)
287         while iter:
288             c_name = NULL
289             c_alias = NULL
290             buddy = <blist.PurpleBuddy *> iter.data
291             if <blist.PurpleBuddy *>buddy and \
292                 account.c_purple_account_is_connected(blist.c_purple_buddy_get_account(buddy)) and \
293                 status.c_purple_presence_is_online(blist.c_purple_buddy_get_presence(buddy)):
294                 c_name = <char *> blist.c_purple_buddy_get_name(buddy)
295                 if c_name == NULL:
296                     name = None
297                 else:
298                     name = c_name
299                 c_alias = <char *> blist.c_purple_buddy_get_alias_only(buddy)
300                 if c_alias == NULL:
301                     alias = None
302                 else:
303                     alias = c_alias
304                 buddies.append((name, alias))
305             iter = iter.next
306         return buddies
307
308     def new(self, username, protocol_id):
309         cdef account.PurpleAccount *c_account
310         c_account = account.c_purple_account_new(username, protocol_id)
311
312         if c_account == NULL:
313             return None
314
315         return (username, protocol_id)
316
317     def get_all(self):
318         cdef glib.GList *iter
319         cdef account.PurpleAccount *acc
320
321         accounts = []
322
323         iter = account.c_purple_accounts_get_all()
324         while iter:
325             acc = <account.PurpleAccount *> iter.data
326             if <account.PurpleAccount *>acc:
327                 username = account.c_purple_account_get_username(acc)
328                 protocol_id = account.c_purple_account_get_protocol_id(acc)
329
330                 accounts.append((username, protocol_id))
331             iter = iter.next
332
333         return accounts
334