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);
51 compare_option_strings_for_name (const gchar* a, const gchar* b)
53 /* printf(" debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
54 const gchar* sep = strchr(a, '=');
62 /* Get the part of the string before the =.
63 * Note that this allocation is inefficient just so we can do a strcmp. */
64 gchar* name = g_malloc (len+1);
66 name[len] = 0; /* Null-termination. */
68 /* printf(" debug: name=%s\n", name); */
70 gint result = strcmp (name, b);
78 modest_server_account_data_get_option_value (GSList* options_list, const gchar* option_name)
84 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
86 /* Get the value part of the key=value pair: */
87 const gchar* pair = (const gchar*)option->data;
89 const gchar* sep = strchr(pair, '=');
91 gint len = sep - pair;
93 result = g_strdup(sep+1);
95 /* Avoid returning an empty string instead of NULL. */
96 if(result && strlen(result) == 0) {
108 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
113 gboolean result = FALSE;
114 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
123 modest_server_account_data_get_option_secure_auth (ModestServerAccountData *account_data)
125 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
126 gchar* value = modest_server_account_data_get_option_value (account_data->options,
127 MODEST_ACCOUNT_OPTION_SECURE_AUTH);
129 if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURE_AUTH_VALUE_NONE) == 0)
130 result = MODEST_PROTOCOL_AUTH_NONE;
131 else if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURE_AUTH_VALUE_PASSWORD) == 0)
132 result = MODEST_PROTOCOL_AUTH_PASSWORD;
133 else if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURE_AUTH_VALUE_CRAMMD5) == 0)
134 result = MODEST_PROTOCOL_AUTH_CRAMMD5;
143 modest_server_account_set_option_secure_auth (ModestAccountMgr *self,
144 const gchar* account_name, ModestProtocol secure_auth)
146 GSList *options_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
147 MODEST_CONF_VALUE_STRING, TRUE);
149 /* Remove the item if it exists already: */
150 GSList* option = NULL;
152 option = g_slist_find_custom(options_list, MODEST_ACCOUNT_OPTION_SECURE_AUTH, (GCompareFunc)compare_option_strings_for_name);
154 options_list = g_slist_remove (options_list, option->data);
158 /* Add the new item to the list: */
159 const gchar* str_value = NULL;
160 if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
161 str_value = MODEST_ACCOUNT_OPTION_SECURE_AUTH_VALUE_NONE;
162 else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
163 str_value = MODEST_ACCOUNT_OPTION_SECURE_AUTH_VALUE_PASSWORD;
164 else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
165 str_value = MODEST_ACCOUNT_OPTION_SECURE_AUTH_VALUE_CRAMMD5;
168 gchar* pair = g_strdup_printf(MODEST_ACCOUNT_OPTION_SECURE_AUTH "=%s", str_value);
169 options_list = g_slist_append(options_list, pair);
172 /* Set it in the configuration: */
173 modest_account_mgr_set_list (self, account_name, MODEST_ACCOUNT_OPTIONS, options_list,
174 MODEST_CONF_VALUE_STRING, TRUE);
176 /* TODO: Should we free the items too, or just the list? */
177 g_slist_free (options_list);
181 modest_server_account_data_get_option_security (ModestServerAccountData *account_data)
183 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
184 gchar* value = modest_server_account_data_get_option_value (account_data->options,
185 MODEST_ACCOUNT_OPTION_SECURITY);
187 if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NONE) == 0)
188 result = MODEST_PROTOCOL_SECURITY_NONE;
189 else if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NORMAL) == 0)
190 result = MODEST_PROTOCOL_SECURITY_TLS;
191 else if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURITY_VALUE_SSL) == 0)
192 result = MODEST_PROTOCOL_SECURITY_SSL;
201 modest_server_account_set_option_security (ModestAccountMgr *self,
202 const gchar* account_name, ModestProtocol security)
204 GSList *options_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
205 MODEST_CONF_VALUE_STRING, TRUE);
208 /* Remove the item if it exists already: */
209 GSList* option = NULL;
211 g_slist_find_custom(options_list, MODEST_ACCOUNT_OPTION_SECURITY, (GCompareFunc)compare_option_strings_for_name);
213 options_list = g_slist_remove (options_list, option->data);
217 /* Add the new item to the list: */
218 const gchar* str_value = NULL;
219 if (security == MODEST_PROTOCOL_SECURITY_NONE)
220 str_value = MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NONE;
221 else if (security == MODEST_PROTOCOL_SECURITY_TLS)
222 str_value = MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NORMAL;
223 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
224 str_value = MODEST_ACCOUNT_OPTION_SECURITY_VALUE_SSL;
227 gchar* pair = g_strdup_printf(MODEST_ACCOUNT_OPTION_SECURITY "=%s", str_value);
228 options_list = g_slist_append(options_list, pair);
231 /* Set it in the configuration: */
232 modest_account_mgr_set_list (self, account_name, MODEST_ACCOUNT_OPTIONS, options_list,
233 MODEST_CONF_VALUE_STRING, TRUE);
235 /* TODO: Should we free the items too, or just the list? */
236 g_slist_free (options_list);
240 modest_account_mgr_get_server_account_option (ModestAccountMgr *self,
241 const gchar* account_name, const gchar* option_name)
243 GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
244 MODEST_CONF_VALUE_STRING, TRUE);
248 gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
250 /* TODO: Should we free the items too, or just the list? */
251 g_slist_free (option_list);
256 static ModestServerAccountData*
257 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
259 ModestServerAccountData *data;
262 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
263 data = g_slice_new0 (ModestServerAccountData);
265 data->account_name = g_strdup (name);
266 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
267 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
268 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
269 data->proto = modest_protocol_info_get_protocol (proto);
272 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
274 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
275 data->uri = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
276 data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
277 MODEST_CONF_VALUE_STRING, TRUE);
283 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
284 ModestServerAccountData* data)
286 g_return_if_fail (self);
289 return; /* not an error */
291 g_free (data->account_name);
292 data->account_name = NULL;
294 g_free (data->hostname);
295 data->hostname = NULL;
297 g_free (data->username);
298 data->username = NULL;
300 g_free (data->password);
301 data->password = NULL;
304 GSList *tmp = data->options;
307 tmp = g_slist_next (tmp);
309 g_slist_free (data->options);
312 g_slice_free (ModestServerAccountData, data);
315 /** You must use modest_account_mgr_free_account_data() on the result.
318 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
320 ModestAccountData *data;
321 gchar *server_account;
322 gchar *default_account;
324 g_return_val_if_fail (self, NULL);
325 g_return_val_if_fail (name, NULL);
326 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);
327 data = g_slice_new0 (ModestAccountData);
329 data->account_name = g_strdup (name);
331 data->display_name = modest_account_mgr_get_string (self, name,
332 MODEST_ACCOUNT_DISPLAY_NAME,
334 data->fullname = modest_account_mgr_get_string (self, name,
335 MODEST_ACCOUNT_FULLNAME,
337 data->email = modest_account_mgr_get_string (self, name,
338 MODEST_ACCOUNT_EMAIL,
340 data->is_enabled = modest_account_mgr_get_enabled (self, name);
342 default_account = modest_account_mgr_get_default_account (self);
343 data->is_default = (default_account && strcmp (default_account, name) == 0);
344 g_free (default_account);
347 server_account = modest_account_mgr_get_string (self, name,
348 MODEST_ACCOUNT_STORE_ACCOUNT,
350 if (server_account) {
351 data->store_account =
352 modest_account_mgr_get_server_account_data (self, server_account);
353 g_free (server_account);
357 server_account = modest_account_mgr_get_string (self, name,
358 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
360 if (server_account) {
361 data->transport_account =
362 modest_account_mgr_get_server_account_data (self, server_account);
363 g_free (server_account);
371 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
373 g_return_if_fail (self);
375 if (!data) /* not an error */
378 g_free (data->account_name);
379 g_free (data->display_name);
380 g_free (data->fullname);
381 g_free (data->email);
383 modest_account_mgr_free_server_account_data (self, data->store_account);
384 modest_account_mgr_free_server_account_data (self, data->transport_account);
386 g_slice_free (ModestAccountData, data);
391 modest_account_mgr_get_default_account (ModestAccountMgr *self)
397 g_return_val_if_fail (self, NULL);
399 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
400 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
403 g_printerr ("modest: failed to get '%s': %s\n",
404 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
410 /* it's not really an error if there is no default account */
415 if (!modest_account_mgr_account_exists (self, account, FALSE)) {
416 g_printerr ("modest: default account does not exist\n");
426 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
430 g_return_val_if_fail (self, FALSE);
431 g_return_val_if_fail (account, FALSE);
432 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
435 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
437 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
443 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
445 gchar *fullname, *email, *from;
447 g_return_val_if_fail (self, NULL);
448 g_return_val_if_fail (name, NULL);
450 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
452 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
454 from = g_strdup_printf ("%s <%s>",
455 fullname ? fullname : "",