1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <modest-account-mgr-helpers.h>
31 #include <modest-account-mgr-priv.h>
32 #include <tny-simple-list.h>
33 #include <modest-runtime.h>
37 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
40 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
45 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
47 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
50 #if 0 /* Not needed, but works. */
52 compare_option_strings_for_name (const gchar* a, const gchar* b)
54 /* printf(" debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
55 const gchar* sep = strchr(a, '=');
63 /* Get the part of the string before the =.
64 * Note that this allocation is inefficient just so we can do a strcmp. */
65 gchar* name = g_malloc (len+1);
67 name[len] = 0; /* Null-termination. */
69 /* printf(" debug: name=%s\n", name); */
71 gint result = strcmp (name, b);
79 modest_server_account_data_get_option_string (GSList* options_list, const gchar* option_name)
85 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
87 /* Get the value part of the key=value pair: */
88 const gchar* pair = (const gchar*)option->data;
90 const gchar* sep = strchr(pair, '=');
92 gint len = sep - pair;
94 result = g_strdup(sep+1);
96 /* Avoid returning an empty string instead of NULL. */
97 if(result && strlen(result) == 0) {
109 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
114 gboolean result = FALSE;
115 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
124 static ModestProtocol
125 get_secure_auth_for_conf_string(const gchar* value)
127 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
129 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
130 result = MODEST_PROTOCOL_AUTH_NONE;
131 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
132 result = MODEST_PROTOCOL_AUTH_PASSWORD;
133 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
134 result = MODEST_PROTOCOL_AUTH_CRAMMD5;
141 modest_server_account_get_secure_auth (ModestAccountMgr *self,
142 const gchar* account_name)
144 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
145 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
146 TRUE /* server account */);
148 result = get_secure_auth_for_conf_string (value);
158 modest_server_account_set_secure_auth (ModestAccountMgr *self,
159 const gchar* account_name, ModestProtocol secure_auth)
161 /* Get the conf string for the enum value: */
162 const gchar* str_value = NULL;
163 if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
164 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
165 else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
166 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
167 else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
168 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
170 /* Set it in the configuration: */
171 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
174 static ModestProtocol
175 get_security_for_conf_string(const gchar* value)
177 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
179 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
180 result = MODEST_PROTOCOL_SECURITY_NONE;
181 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
182 result = MODEST_PROTOCOL_SECURITY_TLS;
183 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
184 result = MODEST_PROTOCOL_SECURITY_SSL;
191 modest_server_account_get_security (ModestAccountMgr *self,
192 const gchar* account_name)
194 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
195 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
196 TRUE /* server account */);
198 result = get_security_for_conf_string (value);
207 modest_server_account_set_security (ModestAccountMgr *self,
208 const gchar* account_name, ModestProtocol security)
210 /* Get the conf string for the enum value: */
211 const gchar* str_value = NULL;
212 if (security == MODEST_PROTOCOL_SECURITY_NONE)
213 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
214 else if (security == MODEST_PROTOCOL_SECURITY_TLS)
215 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
216 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
217 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
219 /* Set it in the configuration: */
220 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
225 modest_account_mgr_get_server_account_option (ModestAccountMgr *self,
226 const gchar* account_name, const gchar* option_name)
228 GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
229 MODEST_CONF_VALUE_STRING, TRUE);
233 gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
235 /* TODO: Should we free the items too, or just the list? */
236 g_slist_free (option_list);
242 static ModestServerAccountData*
243 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
245 ModestServerAccountData *data;
248 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
249 data = g_slice_new0 (ModestServerAccountData);
251 data->account_name = g_strdup (name);
252 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
253 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
254 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
255 data->proto = modest_protocol_info_get_protocol (proto);
258 data->port = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE);
260 gchar *secure_auth_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
261 data->secure_auth = get_secure_auth_for_conf_string(secure_auth_str);
262 g_free (secure_auth_str);
264 gchar *security_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
265 data->security = get_security_for_conf_string(security_str);
266 g_free (security_str);
268 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
270 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
271 data->uri = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
272 data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
273 MODEST_CONF_VALUE_STRING, TRUE);
281 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
282 ModestServerAccountData* data)
284 g_return_if_fail (self);
287 return; /* not an error */
289 g_free (data->account_name);
290 data->account_name = NULL;
292 g_free (data->hostname);
293 data->hostname = NULL;
295 g_free (data->username);
296 data->username = NULL;
298 g_free (data->password);
299 data->password = NULL;
302 GSList *tmp = data->options;
305 tmp = g_slist_next (tmp);
307 g_slist_free (data->options);
310 g_slice_free (ModestServerAccountData, data);
313 /** You must use modest_account_mgr_free_account_data() on the result.
316 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
318 ModestAccountData *data;
319 gchar *server_account;
320 gchar *default_account;
322 g_return_val_if_fail (self, NULL);
323 g_return_val_if_fail (name, NULL);
324 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);
325 data = g_slice_new0 (ModestAccountData);
327 data->account_name = g_strdup (name);
329 data->display_name = modest_account_mgr_get_string (self, name,
330 MODEST_ACCOUNT_DISPLAY_NAME,
332 data->fullname = modest_account_mgr_get_string (self, name,
333 MODEST_ACCOUNT_FULLNAME,
335 data->email = modest_account_mgr_get_string (self, name,
336 MODEST_ACCOUNT_EMAIL,
338 data->is_enabled = modest_account_mgr_get_enabled (self, name);
340 default_account = modest_account_mgr_get_default_account (self);
341 data->is_default = (default_account && strcmp (default_account, name) == 0);
342 g_free (default_account);
345 server_account = modest_account_mgr_get_string (self, name,
346 MODEST_ACCOUNT_STORE_ACCOUNT,
348 if (server_account) {
349 data->store_account =
350 modest_account_mgr_get_server_account_data (self, server_account);
351 g_free (server_account);
355 server_account = modest_account_mgr_get_string (self, name,
356 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
358 if (server_account) {
359 data->transport_account =
360 modest_account_mgr_get_server_account_data (self, server_account);
361 g_free (server_account);
369 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
371 g_return_if_fail (self);
373 if (!data) /* not an error */
376 g_free (data->account_name);
377 g_free (data->display_name);
378 g_free (data->fullname);
379 g_free (data->email);
381 modest_account_mgr_free_server_account_data (self, data->store_account);
382 modest_account_mgr_free_server_account_data (self, data->transport_account);
384 g_slice_free (ModestAccountData, data);
389 modest_account_mgr_get_default_account (ModestAccountMgr *self)
395 g_return_val_if_fail (self, NULL);
397 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
398 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
401 g_printerr ("modest: failed to get '%s': %s\n",
402 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
408 /* it's not really an error if there is no default account */
413 if (!modest_account_mgr_account_exists (self, account, FALSE)) {
414 g_printerr ("modest: default account does not exist\n");
424 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
428 g_return_val_if_fail (self, FALSE);
429 g_return_val_if_fail (account, FALSE);
430 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
433 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
435 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
441 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
443 gchar *fullname, *email, *from;
445 g_return_val_if_fail (self, NULL);
446 g_return_val_if_fail (name, NULL);
448 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
450 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
452 from = g_strdup_printf ("%s <%s>",
453 fullname ? fullname : "",