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