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);
125 gboolean modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
126 const gchar* account_name,
127 const gchar* connection_name, const gchar* server_account_name)
129 modest_account_mgr_remove_connection_specific_smtp (self, account_name, connection_name);
131 GSList *list = modest_account_mgr_get_list (self, account_name,
132 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
133 MODEST_CONF_VALUE_STRING, TRUE);
135 /* The server account is in the item after the connection name: */
136 GSList *list_connection = g_slist_append (list, (gpointer)connection_name);
137 g_slist_append (list_connection, (gpointer)server_account_name);
139 /* Reset the changed list: */
140 modest_account_mgr_set_list (self, account_name,
141 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
142 MODEST_CONF_VALUE_STRING, TRUE);
144 /* TODO: Should we free the items too, or just the list? */
151 * modest_account_mgr_remove_connection_specific_smtp
152 * @self: a ModestAccountMgr instance
153 * @name: the account name
154 * @connection_name: A libconic IAP connection name
156 * Disassacoiate a server account to use with the specific connection for this account.
158 * Returns: TRUE if it worked, FALSE otherwise
160 gboolean modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
161 const gchar* account_name, const gchar* connection_name)
163 GSList *list = modest_account_mgr_get_list (self, account_name,
164 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
165 MODEST_CONF_VALUE_STRING, TRUE);
169 /* The server account is in the item after the connection name: */
170 GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
171 if (list_connection) {
172 /* remove both items: */
173 GSList *temp = g_slist_delete_link(list_connection, list_connection);
174 g_slist_delete_link(temp, g_slist_next(temp));
177 /* Reset the changed list: */
178 modest_account_mgr_set_list (self, account_name,
179 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
180 MODEST_CONF_VALUE_STRING, TRUE);
182 /* TODO: Should we free the items too, or just the list? */
189 * modest_account_mgr_get_connection_specific_smtp
190 * @self: a ModestAccountMgr instance
191 * @name: the account name
192 * @connection_name: A libconic IAP connection name
194 * Retrieve a server account to use with this specific connection for this account.
196 * Returns: a server account name to use for this connection, or NULL if none is specified.
198 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
199 const gchar* connection_name)
201 gchar *result = NULL;
203 GSList *list = modest_account_mgr_get_list (self, account_name,
204 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
205 MODEST_CONF_VALUE_STRING, TRUE);
209 /* The server account is in the item after the connection name: */
210 GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
211 if (list_connection) {
212 GSList * list_server_account = g_slist_next(list_connection);
213 if (list_server_account)
214 result = g_strdup ((gchar*)(list_server_account->data));
217 /* TODO: Should we free the items too, or just the list? */
225 static ModestProtocol
226 get_secure_auth_for_conf_string(const gchar* value)
228 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
230 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
231 result = MODEST_PROTOCOL_AUTH_NONE;
232 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
233 result = MODEST_PROTOCOL_AUTH_PASSWORD;
234 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
235 result = MODEST_PROTOCOL_AUTH_CRAMMD5;
242 modest_server_account_get_secure_auth (ModestAccountMgr *self,
243 const gchar* account_name)
245 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
246 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
247 TRUE /* server account */);
249 result = get_secure_auth_for_conf_string (value);
259 modest_server_account_set_secure_auth (ModestAccountMgr *self,
260 const gchar* account_name, ModestProtocol secure_auth)
262 /* Get the conf string for the enum value: */
263 const gchar* str_value = NULL;
264 if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
265 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
266 else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
267 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
268 else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
269 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
271 /* Set it in the configuration: */
272 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
275 static ModestProtocol
276 get_security_for_conf_string(const gchar* value)
278 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
280 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
281 result = MODEST_PROTOCOL_SECURITY_NONE;
282 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
283 result = MODEST_PROTOCOL_SECURITY_TLS;
284 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
285 result = MODEST_PROTOCOL_SECURITY_SSL;
292 modest_server_account_get_security (ModestAccountMgr *self,
293 const gchar* account_name)
295 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
296 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
297 TRUE /* server account */);
299 result = get_security_for_conf_string (value);
308 modest_server_account_set_security (ModestAccountMgr *self,
309 const gchar* account_name, ModestProtocol security)
311 /* Get the conf string for the enum value: */
312 const gchar* str_value = NULL;
313 if (security == MODEST_PROTOCOL_SECURITY_NONE)
314 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
315 else if (security == MODEST_PROTOCOL_SECURITY_TLS)
316 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
317 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
318 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
320 /* Set it in the configuration: */
321 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
326 modest_account_mgr_get_server_account_option (ModestAccountMgr *self,
327 const gchar* account_name, const gchar* option_name)
329 GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
330 MODEST_CONF_VALUE_STRING, TRUE);
334 gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
336 /* TODO: Should we free the items too, or just the list? */
337 g_slist_free (option_list);
343 static ModestServerAccountData*
344 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
346 ModestServerAccountData *data;
349 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
350 data = g_slice_new0 (ModestServerAccountData);
352 data->account_name = g_strdup (name);
353 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
354 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
355 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
356 data->proto = modest_protocol_info_get_protocol (proto);
359 data->port = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE);
361 gchar *secure_auth_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
362 data->secure_auth = get_secure_auth_for_conf_string(secure_auth_str);
363 g_free (secure_auth_str);
365 gchar *security_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
366 data->security = get_security_for_conf_string(security_str);
367 g_free (security_str);
369 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
371 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
372 data->uri = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
373 data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
374 MODEST_CONF_VALUE_STRING, TRUE);
382 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
383 ModestServerAccountData* data)
385 g_return_if_fail (self);
388 return; /* not an error */
390 g_free (data->account_name);
391 data->account_name = NULL;
393 g_free (data->hostname);
394 data->hostname = NULL;
396 g_free (data->username);
397 data->username = NULL;
399 g_free (data->password);
400 data->password = NULL;
403 GSList *tmp = data->options;
406 tmp = g_slist_next (tmp);
408 g_slist_free (data->options);
411 g_slice_free (ModestServerAccountData, data);
414 /** You must use modest_account_mgr_free_account_data() on the result.
417 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
419 ModestAccountData *data;
420 gchar *server_account;
421 gchar *default_account;
423 g_return_val_if_fail (self, NULL);
424 g_return_val_if_fail (name, NULL);
425 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);
426 data = g_slice_new0 (ModestAccountData);
428 data->account_name = g_strdup (name);
430 data->display_name = modest_account_mgr_get_string (self, name,
431 MODEST_ACCOUNT_DISPLAY_NAME,
433 data->fullname = modest_account_mgr_get_string (self, name,
434 MODEST_ACCOUNT_FULLNAME,
436 data->email = modest_account_mgr_get_string (self, name,
437 MODEST_ACCOUNT_EMAIL,
439 data->is_enabled = modest_account_mgr_get_enabled (self, name);
441 default_account = modest_account_mgr_get_default_account (self);
442 data->is_default = (default_account && strcmp (default_account, name) == 0);
443 g_free (default_account);
446 server_account = modest_account_mgr_get_string (self, name,
447 MODEST_ACCOUNT_STORE_ACCOUNT,
449 if (server_account) {
450 data->store_account =
451 modest_account_mgr_get_server_account_data (self, server_account);
452 g_free (server_account);
456 server_account = modest_account_mgr_get_string (self, name,
457 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
459 if (server_account) {
460 data->transport_account =
461 modest_account_mgr_get_server_account_data (self, server_account);
462 g_free (server_account);
470 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
472 g_return_if_fail (self);
474 if (!data) /* not an error */
477 g_free (data->account_name);
478 g_free (data->display_name);
479 g_free (data->fullname);
480 g_free (data->email);
482 modest_account_mgr_free_server_account_data (self, data->store_account);
483 modest_account_mgr_free_server_account_data (self, data->transport_account);
485 g_slice_free (ModestAccountData, data);
490 modest_account_mgr_get_default_account (ModestAccountMgr *self)
496 g_return_val_if_fail (self, NULL);
498 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
499 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
502 g_printerr ("modest: failed to get '%s': %s\n",
503 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
509 /* it's not really an error if there is no default account */
514 if (!modest_account_mgr_account_exists (self, account, FALSE)) {
515 g_printerr ("modest: default account does not exist\n");
525 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
529 g_return_val_if_fail (self, FALSE);
530 g_return_val_if_fail (account, FALSE);
531 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
534 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
536 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
542 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
544 gchar *fullname, *email, *from;
546 g_return_val_if_fail (self, NULL);
547 g_return_val_if_fail (name, NULL);
549 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
551 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
553 from = g_strdup_printf ("%s <%s>",
554 fullname ? fullname : "",
562 /* Add a number to the end of the text, or increment a number that is already there.
565 util_increment_name (const gchar* text)
567 /* Get the end character,
568 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
570 const gchar* end = NULL;
571 if (!g_utf8_validate (text, -1, &end))
577 --end; /* Go to before the null-termination. */
579 /* Look at each UTF-8 characer, starting at the end: */
580 const gchar* p = end;
581 const gchar* alpha_end = NULL;
584 /* Stop when we reach the first character that is not a numeric digit: */
585 const gunichar ch = g_utf8_get_char (p);
586 if (!g_unichar_isdigit (ch)) {
591 p = g_utf8_prev_char (p);
595 /* The text must consist completely of numeric digits. */
601 /* Intepret and increment the number, if any: */
602 gint num = atol (alpha_end);
605 /* Get the name part: */
606 gint name_len = alpha_end - text;
607 gchar *name_without_number = g_malloc(name_len + 1);
608 memcpy (name_without_number, text, name_len);
609 name_without_number[name_len] = 0;\
611 /* Concatenate the text part and the new number: */
612 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
613 g_free (name_without_number);
619 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
620 gboolean server_account)
622 gchar *account_name = g_strdup (starting_name);
624 while (modest_account_mgr_account_exists (self,
625 account_name, server_account /* server_account */)) {
627 gchar * account_name2 = util_increment_name (account_name);
628 g_free (account_name);
629 account_name = account_name2;
636 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
638 gchar *account_name = g_strdup (starting_name);
640 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
642 gchar * account_name2 = util_increment_name (account_name);
643 g_free (account_name);
644 account_name = account_name2;