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 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
51 const gchar* signature, gboolean use_signature)
53 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
54 use_signature, FALSE);
55 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
60 gchar* modest_account_mgr_get_display_name (ModestAccountMgr *self,
63 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
68 gchar* modest_account_mgr_get_signature (ModestAccountMgr *self, const gchar* name,
69 gboolean* use_signature)
73 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
76 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
81 #if 0 /* Not needed, but works. */
83 compare_option_strings_for_name (const gchar* a, const gchar* b)
85 /* printf(" debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
86 const gchar* sep = strchr(a, '=');
94 /* Get the part of the string before the =.
95 * Note that this allocation is inefficient just so we can do a strcmp. */
96 gchar* name = g_malloc (len+1);
98 name[len] = 0; /* Null-termination. */
100 /* printf(" debug: name=%s\n", name); */
102 gint result = strcmp (name, b);
110 modest_server_account_data_get_option_string (GSList* options_list, const gchar* option_name)
115 gchar *result = NULL;
116 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
118 /* Get the value part of the key=value pair: */
119 const gchar* pair = (const gchar*)option->data;
121 const gchar* sep = strchr(pair, '=');
123 gint len = sep - pair;
125 result = g_strdup(sep+1);
127 /* Avoid returning an empty string instead of NULL. */
128 if(result && strlen(result) == 0) {
140 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
145 gboolean result = FALSE;
146 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
156 gboolean modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
157 const gchar* account_name,
158 const gchar* connection_name, const gchar* server_account_name)
160 modest_account_mgr_remove_connection_specific_smtp (self, account_name, connection_name);
162 GSList *list = modest_account_mgr_get_list (self, account_name,
163 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
164 MODEST_CONF_VALUE_STRING, TRUE);
166 /* The server account is in the item after the connection name: */
167 GSList *list_connection = g_slist_append (list, (gpointer)connection_name);
168 list_connection = g_slist_append (list_connection, (gpointer)server_account_name);
170 /* Reset the changed list: */
171 modest_account_mgr_set_list (self, account_name,
172 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
173 MODEST_CONF_VALUE_STRING, TRUE);
175 /* TODO: Should we free the items too, or just the list? */
182 * modest_account_mgr_remove_connection_specific_smtp
183 * @self: a ModestAccountMgr instance
184 * @name: the account name
185 * @connection_name: A libconic IAP connection name
187 * Disassacoiate a server account to use with the specific connection for this account.
189 * Returns: TRUE if it worked, FALSE otherwise
191 gboolean modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
192 const gchar* account_name, const gchar* connection_name)
194 GSList *list = modest_account_mgr_get_list (self, account_name,
195 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
196 MODEST_CONF_VALUE_STRING, TRUE);
200 /* The server account is in the item after the connection name: */
201 GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
202 if (list_connection) {
203 /* remove both items: */
204 GSList *temp = g_slist_delete_link(list_connection, list_connection);
205 temp = g_slist_delete_link(temp, g_slist_next(temp));
208 /* Reset the changed list: */
209 modest_account_mgr_set_list (self, account_name,
210 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
211 MODEST_CONF_VALUE_STRING, TRUE);
213 /* TODO: Should we free the items too, or just the list? */
220 * modest_account_mgr_get_connection_specific_smtp
221 * @self: a ModestAccountMgr instance
222 * @name: the account name
223 * @connection_name: A libconic IAP connection name
225 * Retrieve a server account to use with this specific connection for this account.
227 * Returns: a server account name to use for this connection, or NULL if none is specified.
229 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
230 const gchar* connection_name)
232 gchar *result = NULL;
234 GSList *list = modest_account_mgr_get_list (self, account_name,
235 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
236 MODEST_CONF_VALUE_STRING, TRUE);
240 /* The server account is in the item after the connection name: */
241 GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
242 if (list_connection) {
243 GSList * list_server_account = g_slist_next(list_connection);
244 if (list_server_account)
245 result = g_strdup ((gchar*)(list_server_account->data));
248 /* TODO: Should we free the items too, or just the list? */
255 modest_server_account_get_username (ModestAccountMgr *self, const gchar* account_name)
257 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
258 TRUE /* server account */);
262 modest_server_account_set_username (ModestAccountMgr *self, const gchar* account_name,
263 const gchar* username)
265 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
266 username, TRUE /* server account */);
270 modest_server_account_set_password (ModestAccountMgr *self, const gchar* account_name,
271 const gchar* password)
273 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
274 password, TRUE /* server account */);
278 modest_server_account_get_hostname (ModestAccountMgr *self, const gchar* account_name)
280 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
281 TRUE /* server account */);
285 static ModestProtocol
286 get_secure_auth_for_conf_string(const gchar* value)
288 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
290 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
291 result = MODEST_PROTOCOL_AUTH_NONE;
292 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
293 result = MODEST_PROTOCOL_AUTH_PASSWORD;
294 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
295 result = MODEST_PROTOCOL_AUTH_CRAMMD5;
302 modest_server_account_get_secure_auth (ModestAccountMgr *self,
303 const gchar* account_name)
305 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
306 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
307 TRUE /* server account */);
309 result = get_secure_auth_for_conf_string (value);
319 modest_server_account_set_secure_auth (ModestAccountMgr *self,
320 const gchar* account_name, ModestProtocol secure_auth)
322 /* Get the conf string for the enum value: */
323 const gchar* str_value = NULL;
324 if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
325 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
326 else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
327 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
328 else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
329 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
331 /* Set it in the configuration: */
332 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
335 static ModestProtocol
336 get_security_for_conf_string(const gchar* value)
338 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
340 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
341 result = MODEST_PROTOCOL_SECURITY_NONE;
342 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
343 result = MODEST_PROTOCOL_SECURITY_TLS;
344 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
345 result = MODEST_PROTOCOL_SECURITY_SSL;
352 modest_server_account_get_security (ModestAccountMgr *self,
353 const gchar* account_name)
355 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
356 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
357 TRUE /* server account */);
359 result = get_security_for_conf_string (value);
368 modest_server_account_set_security (ModestAccountMgr *self,
369 const gchar* account_name, ModestProtocol security)
371 /* Get the conf string for the enum value: */
372 const gchar* str_value = NULL;
373 if (security == MODEST_PROTOCOL_SECURITY_NONE)
374 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
375 else if (security == MODEST_PROTOCOL_SECURITY_TLS)
376 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
377 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
378 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
380 /* Set it in the configuration: */
381 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
386 modest_account_mgr_get_server_account_option (ModestAccountMgr *self,
387 const gchar* account_name, const gchar* option_name)
389 GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
390 MODEST_CONF_VALUE_STRING, TRUE);
394 gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
396 /* TODO: Should we free the items too, or just the list? */
397 g_slist_free (option_list);
403 ModestServerAccountData*
404 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
406 ModestServerAccountData *data;
409 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
410 data = g_slice_new0 (ModestServerAccountData);
412 data->account_name = g_strdup (name);
413 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
414 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
415 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
416 data->proto = modest_protocol_info_get_protocol (proto);
419 data->port = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE);
421 gchar *secure_auth_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
422 data->secure_auth = get_secure_auth_for_conf_string(secure_auth_str);
423 g_free (secure_auth_str);
425 gchar *security_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
426 data->security = get_security_for_conf_string(security_str);
427 g_free (security_str);
429 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
431 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
432 data->uri = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
433 data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
434 MODEST_CONF_VALUE_STRING, TRUE);
442 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
443 ModestServerAccountData* data)
445 g_return_if_fail (self);
448 return; /* not an error */
450 g_free (data->account_name);
451 data->account_name = NULL;
453 g_free (data->hostname);
454 data->hostname = NULL;
456 g_free (data->username);
457 data->username = NULL;
459 g_free (data->password);
460 data->password = NULL;
463 GSList *tmp = data->options;
466 tmp = g_slist_next (tmp);
468 g_slist_free (data->options);
471 g_slice_free (ModestServerAccountData, data);
474 /** You must use modest_account_mgr_free_account_data() on the result.
477 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
479 ModestAccountData *data;
480 gchar *server_account;
481 gchar *default_account;
483 g_return_val_if_fail (self, NULL);
484 g_return_val_if_fail (name, NULL);
486 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
487 /* For instance, maybe you are mistakenly checking for a server account name? */
488 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
492 data = g_slice_new0 (ModestAccountData);
494 data->account_name = g_strdup (name);
496 data->display_name = modest_account_mgr_get_string (self, name,
497 MODEST_ACCOUNT_DISPLAY_NAME,
499 data->fullname = modest_account_mgr_get_string (self, name,
500 MODEST_ACCOUNT_FULLNAME,
502 data->email = modest_account_mgr_get_string (self, name,
503 MODEST_ACCOUNT_EMAIL,
505 data->is_enabled = modest_account_mgr_get_enabled (self, name);
507 default_account = modest_account_mgr_get_default_account (self);
508 data->is_default = (default_account && strcmp (default_account, name) == 0);
509 g_free (default_account);
512 server_account = modest_account_mgr_get_string (self, name,
513 MODEST_ACCOUNT_STORE_ACCOUNT,
515 if (server_account) {
516 data->store_account =
517 modest_account_mgr_get_server_account_data (self, server_account);
518 g_free (server_account);
522 server_account = modest_account_mgr_get_string (self, name,
523 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
525 if (server_account) {
526 data->transport_account =
527 modest_account_mgr_get_server_account_data (self, server_account);
528 g_free (server_account);
536 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
538 g_return_if_fail (self);
540 if (!data) /* not an error */
543 g_free (data->account_name);
544 g_free (data->display_name);
545 g_free (data->fullname);
546 g_free (data->email);
548 modest_account_mgr_free_server_account_data (self, data->store_account);
549 modest_account_mgr_free_server_account_data (self, data->transport_account);
551 g_slice_free (ModestAccountData, data);
556 modest_account_mgr_get_default_account (ModestAccountMgr *self)
562 g_return_val_if_fail (self, NULL);
564 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
565 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
568 g_printerr ("modest: failed to get '%s': %s\n",
569 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
575 /* it's not really an error if there is no default account */
580 if (!modest_account_mgr_account_exists (self, account, FALSE)) {
581 g_printerr ("modest: default account does not exist\n");
591 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
595 g_return_val_if_fail (self, FALSE);
596 g_return_val_if_fail (account, FALSE);
597 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
600 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
602 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
608 modest_account_mgr_unset_default_account (ModestAccountMgr *self)
612 g_return_val_if_fail (self, FALSE);
614 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
616 return modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
621 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
623 gboolean result = FALSE;
624 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
627 const gchar* account_name = (const gchar*)account_names->data;
629 result = modest_account_mgr_set_default_account (self, account_name);
632 /* TODO: Free the strings too? */
633 g_slist_free (account_names);
639 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
641 gchar *fullname, *email, *from;
643 g_return_val_if_fail (self, NULL);
644 g_return_val_if_fail (name, NULL);
646 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
648 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
650 from = g_strdup_printf ("%s <%s>",
651 fullname ? fullname : "",
659 /* Add a number to the end of the text, or increment a number that is already there.
662 util_increment_name (const gchar* text)
664 /* Get the end character,
665 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
667 const gchar* end = NULL;
668 if (!g_utf8_validate (text, -1, &end))
674 --end; /* Go to before the null-termination. */
676 /* Look at each UTF-8 characer, starting at the end: */
677 const gchar* p = end;
678 const gchar* alpha_end = NULL;
681 /* Stop when we reach the first character that is not a numeric digit: */
682 const gunichar ch = g_utf8_get_char (p);
683 if (!g_unichar_isdigit (ch)) {
688 p = g_utf8_prev_char (p);
692 /* The text must consist completely of numeric digits. */
698 /* Intepret and increment the number, if any: */
699 gint num = atol (alpha_end);
702 /* Get the name part: */
703 gint name_len = alpha_end - text;
704 gchar *name_without_number = g_malloc(name_len + 1);
705 memcpy (name_without_number, text, name_len);
706 name_without_number[name_len] = 0;\
708 /* Concatenate the text part and the new number: */
709 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
710 g_free (name_without_number);
716 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
717 gboolean server_account)
719 gchar *account_name = g_strdup (starting_name);
721 while (modest_account_mgr_account_exists (self,
722 account_name, server_account /* server_account */)) {
724 gchar * account_name2 = util_increment_name (account_name);
725 g_free (account_name);
726 account_name = account_name2;
733 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
735 gchar *account_name = g_strdup (starting_name);
737 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
739 gchar * account_name2 = util_increment_name (account_name);
740 g_free (account_name);
741 account_name = account_name2;