Updated "connection-error" signal callback.
[python-purple] / plugin.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 Plugin:
23     cdef plugin.PurplePlugin *c_plugin
24     cdef prpl.PurplePluginProtocolInfo *c_prpl_info
25     cdef plugin.PurplePluginInfo *c_plugin_info
26
27     def __init__(self):
28         pass
29
30     '''
31     def __init__(self, id):
32         self.c_plugin = plugin.purple_plugins_find_with_id(id)
33         self.c_prpl_info = plugin.c_PURPLE_PLUGIN_PROTOCOL_INFO(self.c_plugin)
34     '''
35
36     def get_name(self):
37         return self.c_plugin.info.name
38
39     def get_id(self):
40         return self.c_plugin.info.id
41
42     def get_all(self):
43         ''' @return A string list of protocols' (id, name) '''
44         '''    [('prpl-jabber', 'XMPP'), ('foo', 'MSN'), ...] '''
45         cdef glib.GList *iter
46         cdef plugin.PurplePlugin *pp
47
48         protocols = []
49
50         iter = plugin.purple_plugins_get_protocols()
51         while iter:
52             pp = <plugin.PurplePlugin*> iter.data
53             if pp.info and pp.info.name:
54                 protocols.append((pp.info.id, pp.info.name))
55             iter = iter.next
56
57         return protocols
58
59     def get_options(self, id, username=None):
60         ''' @param id The protocol's id '''
61         ''' @param username The account's username '''
62         ''' @return {'setting type': ('UI label', str|int|bool value)} '''
63
64         cdef plugin.PurplePlugin *c_plugin
65         cdef prpl.PurplePluginProtocolInfo *c_prpl_info
66         cdef account.PurpleAccount *c_account
67         cdef glib.GList *iter
68         cdef accountopt.PurpleAccountOption *option
69         cdef prefs.PurplePrefType type
70         cdef const_char *label_name
71         cdef const_char *str_value
72         cdef const_char *setting
73         cdef int int_value
74         cdef glib.gboolean bool_value
75
76         c_account = NULL
77
78         if username:
79             c_account = account.purple_accounts_find(username, id)
80
81         c_plugin = plugin.purple_plugins_find_with_id(id)
82         c_prpl_info = plugin.c_PURPLE_PLUGIN_PROTOCOL_INFO(c_plugin)
83
84         po = {}
85
86         iter = c_prpl_info.protocol_options
87
88         while iter:
89
90             option = <accountopt.PurpleAccountOption *> iter.data
91             type = accountopt.purple_account_option_get_type(option)
92             label_name = accountopt.purple_account_option_get_text(option)
93             setting = accountopt.purple_account_option_get_setting(option)
94
95             sett = str(<char *> setting)
96             label = str(<char *> label_name)
97
98             if type == prefs.PURPLE_PREF_STRING:
99                 str_value = accountopt.purple_account_option_get_default_string(option)
100                 if c_account != NULL:
101                     str_value = account.purple_account_get_string(c_account, setting, str_value)
102
103                 val = str(<char *> str_value)
104
105             elif type == prefs.PURPLE_PREF_INT:
106                 int_value = accountopt.purple_account_option_get_default_int(option)
107                 if c_account != NULL:
108                     int_value = account.purple_account_get_int(c_account, setting, int_value)
109
110                 val = int(int_value)
111
112             elif type == prefs.PURPLE_PREF_BOOLEAN:
113                 bool_value = accountopt.purple_account_option_get_default_bool(option)
114                 if c_account != NULL:
115                     bool_value = account.purple_account_get_bool(c_account, setting, bool_value)
116
117                 val = bool(bool_value)
118
119             elif type == prefs.PURPLE_PREF_STRING_LIST:
120                 str_value = accountopt.purple_account_option_get_default_list_value(option)
121                 if c_account != NULL:
122                     str_value = account.purple_account_get_string(c_account, setting, str_value)
123
124                 val = str(<char *> str_value)
125
126             iter = iter.next
127
128             po[sett] = (label, val)
129
130         return po
131
132     def set_options(self, acc, po):
133         #FIXME: account
134         ''' @param id The protocol's id '''
135         ''' @param username The account's username '''
136         ''' @param po Dictionary {'setting type': str|int|bool value, ...} '''
137         ''' @return True to success or False to failure '''
138
139         cdef plugin.PurplePlugin *c_plugin
140         cdef prpl.PurplePluginProtocolInfo *c_prpl_info
141         cdef account.PurpleAccount *c_account
142         cdef glib.GList *iter
143         cdef accountopt.PurpleAccountOption *option
144         cdef prefs.PurplePrefType type
145         cdef const_char *str_value
146         cdef const_char *setting
147         cdef int int_value
148         cdef glib.gboolean bool_value
149
150         c_account = NULL
151
152         c_account = account.purple_accounts_find(acc[0], acc[1])
153         if c_account == NULL:
154             # FIXME: Message error or call a error handler
155             return False
156
157         c_plugin = plugin.purple_plugins_find_with_id(acc[1])
158         c_prpl_info = plugin.c_PURPLE_PLUGIN_PROTOCOL_INFO(c_plugin)
159
160         iter = c_prpl_info.protocol_options
161
162         while iter:
163
164             option = <accountopt.PurpleAccountOption *> iter.data
165             type = accountopt.purple_account_option_get_type(option)
166             setting = accountopt.purple_account_option_get_setting(option)
167
168             sett = str(<char *> setting)
169
170             iter = iter.next
171
172             if not po.has_key(sett) or not po[sett]:
173                 continue
174
175             if type == prefs.PURPLE_PREF_STRING:
176
177                 str_value = <char *> po[sett]
178                 account.purple_account_set_string(c_account, setting, str_value)
179
180             elif type == prefs.PURPLE_PREF_INT:
181
182                 int_value = int(po[sett])
183                 account.purple_account_set_int(c_account, setting, int_value)
184
185             elif type == prefs.PURPLE_PREF_BOOLEAN:
186
187                 bool_value = bool(po[sett])
188                 account.purple_account_set_bool(c_account, setting, bool_value)
189
190             elif type == prefs.PURPLE_PREF_STRING_LIST:
191
192                 str_value = <char *> po[sett]
193                 account.purple_account_set_string(c_account, setting, str_value)
194
195         return True
196
197
198 cdef class Plugins:
199
200     cdef protocols
201
202     def __init__(self):
203         self.protocols = None
204
205     def get_protocols(self):
206         if self.protocols:
207             return self.protocols
208         cdef glib.GList *iter
209         cdef plugin.PurplePlugin *pp
210         protocols = []
211         iter = plugin.purple_plugins_get_protocols()
212         while iter:
213             pp = <plugin.PurplePlugin*> iter.data
214             if pp.info and pp.info.name:
215                 p = Plugin(pp.info.id)
216                 if p:
217                     protocols += [p]
218             iter = iter.next
219         glib.g_list_free(iter)
220         self.protocols = protocols
221         return protocols