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>
36 static const gchar * null_means_empty (const gchar * str);
39 null_means_empty (const gchar * str)
41 return str ? str : "";
45 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
48 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
53 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
55 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
58 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
59 const gchar* signature, gboolean use_signature)
61 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
62 use_signature, FALSE);
63 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
64 null_means_empty (signature), 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);
79 ModestTransportStoreProtocol modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
81 ModestTransportStoreProtocol result = MODEST_PROTOCOL_STORE_POP; /* Arbitrary default */
83 gchar *server_account_name = modest_account_mgr_get_string (self, name,
84 MODEST_ACCOUNT_STORE_ACCOUNT,
86 if (server_account_name) {
87 ModestServerAccountSettings* server_settings =
88 modest_account_mgr_load_server_settings (self, server_account_name);
89 result = modest_server_account_settings_get_protocol (server_settings);
91 g_object_unref (server_settings);
93 g_free (server_account_name);
100 gboolean modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
101 const gchar* connection_id, const gchar* server_account_name)
103 modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
105 ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
107 gboolean result = TRUE;
109 GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
110 MODEST_CONF_VALUE_STRING, &err);
112 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
117 /* The server account is in the item after the connection name: */
118 list = g_slist_append (list, (gpointer)connection_id);
119 list = g_slist_append (list, (gpointer)server_account_name);
121 /* Reset the changed list: */
122 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
123 MODEST_CONF_VALUE_STRING, &err);
125 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
131 /* TODO: Should we free the items too, or just the list? */
138 * modest_account_mgr_remove_connection_specific_smtp
139 * @self: a ModestAccountMgr instance
140 * @name: the account name
141 * @connection_id: A libconic IAP connection id
143 * Disassacoiate a server account to use with the specific connection for this account.
145 * Returns: TRUE if it worked, FALSE otherwise
147 gboolean modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
148 const gchar* connection_id)
150 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
152 gboolean result = TRUE;
154 GSList *list = modest_conf_get_list (priv->modest_conf,
155 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
156 MODEST_CONF_VALUE_STRING, &err);
158 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
167 /* The server account is in the item after the connection name: */
168 GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
169 if (list_connection) {
170 GSList *account_node = g_slist_next (list_connection);
171 /* remove both items: */
172 list = g_slist_delete_link(list, list_connection);
173 list = g_slist_delete_link(list, account_node);
176 /* Reset the changed list: */
177 modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
178 MODEST_CONF_VALUE_STRING, &err);
180 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
185 /* TODO: Should we free the items too, or just the list? */
192 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
194 return modest_account_mgr_get_bool (self, account_name,
195 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
198 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
201 return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP,
206 * modest_account_mgr_get_connection_specific_smtp
207 * @self: a ModestAccountMgr instance
208 * @connection_id: A libconic IAP connection id
210 * Retrieve a server account to use with this specific connection for this account.
212 * Returns: a server account name to use for this connection, or NULL if none is specified.
214 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* connection_id)
216 gchar *result = NULL;
218 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
221 GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
222 MODEST_CONF_VALUE_STRING, &err);
224 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
232 /* The server account is in the item after the connection name: */
235 const gchar* this_connection_id = (const gchar*)(iter->data);
236 if (strcmp (this_connection_id, connection_id) == 0) {
237 iter = g_slist_next (iter);
240 const gchar* account_name = (const gchar*)(iter->data);
242 result = g_strdup (account_name);
248 /* Skip 2 to go to the next connection in the list: */
249 iter = g_slist_next (iter);
251 iter = g_slist_next (iter);
256 printf (" debug: no server found for connection_id=%s.\n", connection_id);
260 /* TODO: Should we free the items too, or just the list? */
267 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
269 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
270 TRUE /* server account */);
274 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name,
275 const gchar* username)
277 /* Note that this won't work properly as long as the gconf cache is broken
279 gchar *existing_username = modest_account_mgr_get_server_account_username(self,
282 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
283 username, TRUE /* server account */);
285 /* We don't know anything about new usernames: */
286 if (strcmp (existing_username, username) != 0)
287 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name,
290 g_free (existing_username);
294 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
296 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
297 TRUE /* server account */);
301 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self,
302 const gchar* account_name,
305 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
306 succeeded, TRUE /* server account */);
310 modest_account_mgr_set_server_account_password (ModestAccountMgr *self, const gchar* account_name,
311 const gchar* password)
313 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
314 password, TRUE /* server account */);
319 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
321 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
322 TRUE /* server account */);
326 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
328 gboolean result = FALSE;
329 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
330 TRUE /* server account */);
331 if (password && strlen (password)) {
341 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
342 const gchar* account_name)
344 return modest_account_mgr_get_string (self,
346 MODEST_ACCOUNT_HOSTNAME,
347 TRUE /* server account */);
351 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
352 const gchar *server_account_name,
353 const gchar *hostname)
355 modest_account_mgr_set_string (self,
357 MODEST_ACCOUNT_HOSTNAME,
359 TRUE /* server account */);
365 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
366 const gchar* account_name)
368 ModestAuthProtocol result = MODEST_PROTOCOL_AUTH_NONE;
369 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
370 TRUE /* server account */);
372 result = modest_protocol_info_get_auth_protocol (value);
382 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
383 const gchar* account_name, ModestAuthProtocol secure_auth)
385 /* Get the conf string for the enum value: */
386 const gchar* str_value = NULL;
388 str_value = modest_protocol_info_get_auth_protocol_name (secure_auth);
390 /* Set it in the configuration: */
391 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
394 ModestConnectionProtocol
395 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
396 const gchar* account_name)
398 ModestConnectionProtocol result = MODEST_PROTOCOL_CONNECTION_NORMAL;
399 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
400 TRUE /* server account */);
402 result = modest_protocol_info_get_connection_protocol (value);
411 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
412 const gchar* account_name, ModestConnectionProtocol security)
414 /* Get the conf string for the enum value: */
415 const gchar* str_value = NULL;
416 str_value = modest_protocol_info_get_connection_protocol_name (security);
418 /* Set it in the configuration: */
419 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
422 ModestServerAccountSettings*
423 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name)
425 ModestServerAccountSettings *settings;
428 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
429 settings = modest_server_account_settings_new ();
431 modest_server_account_settings_set_account_name (settings, name);
433 string = modest_account_mgr_get_string (self, name,
434 MODEST_ACCOUNT_HOSTNAME,TRUE);
435 modest_server_account_settings_set_hostname (settings, string);
438 string = modest_account_mgr_get_string (self, name,
439 MODEST_ACCOUNT_USERNAME,TRUE);
440 modest_server_account_settings_set_username (settings, string);
443 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
444 modest_server_account_settings_set_protocol (settings,
445 modest_protocol_info_get_transport_store_protocol (string));
448 modest_server_account_settings_set_port (settings,
449 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
451 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
452 modest_server_account_settings_set_auth_protocol (settings,
453 modest_protocol_info_get_auth_protocol(string));
456 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
457 modest_server_account_settings_set_security (settings,
458 modest_protocol_info_get_connection_protocol(string));
461 string = modest_account_mgr_get_string (self, name,
462 MODEST_ACCOUNT_PASSWORD, TRUE);
463 modest_server_account_settings_set_password (settings, string);
470 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
471 ModestServerAccountSettings *settings)
473 gboolean has_errors = FALSE;
474 const gchar *account_name;
475 const gchar *protocol;
478 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
479 account_name = modest_server_account_settings_get_account_name (settings);
481 /* if we don't have a valid account name we cannot save */
482 g_return_val_if_fail (account_name, FALSE);
484 protocol = modest_protocol_info_get_transport_store_protocol_name (
485 modest_server_account_settings_get_protocol (settings));
486 uri = modest_server_account_settings_get_uri (settings);
488 const gchar *hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
489 const gchar *username = null_means_empty (modest_server_account_settings_get_username (settings));
490 const gchar *password = null_means_empty (modest_server_account_settings_get_password (settings));
491 gint port = modest_server_account_settings_get_port (settings);
492 const gchar *auth_protocol = modest_protocol_info_get_auth_protocol_name (
493 modest_server_account_settings_get_auth_protocol (settings));
494 const gchar *security = modest_protocol_info_get_connection_protocol_name (
495 modest_server_account_settings_get_security (settings));
497 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
500 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
503 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
506 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
509 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
512 (has_errors = !modest_account_mgr_set_string (self, account_name,
513 MODEST_ACCOUNT_AUTH_MECH,
514 auth_protocol, TRUE));
516 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
520 const gchar *uri = modest_server_account_settings_get_uri (settings);
521 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
524 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
533 ModestAccountSettings *
534 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
537 ModestAccountSettings *settings;
539 gchar *server_account;
540 gchar *default_account;
541 gboolean use_signature = FALSE;
543 g_return_val_if_fail (self, NULL);
544 g_return_val_if_fail (name, NULL);
546 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
547 /* For instance, maybe you are mistakenly checking for a server account name? */
548 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
552 settings = modest_account_settings_new ();
554 modest_account_settings_set_account_name (settings, name);
556 string = modest_account_mgr_get_string (self, name,
557 MODEST_ACCOUNT_DISPLAY_NAME,
559 modest_account_settings_set_display_name (settings, string);
562 string = modest_account_mgr_get_string (self, name,
563 MODEST_ACCOUNT_FULLNAME,
565 modest_account_settings_set_fullname (settings, string);
568 string = modest_account_mgr_get_string (self, name,
569 MODEST_ACCOUNT_EMAIL,
571 modest_account_settings_set_email_address (settings, string);
574 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
575 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
576 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
578 default_account = modest_account_mgr_get_default_account (self);
579 modest_account_settings_set_is_default (settings,
580 (default_account && strcmp (default_account, name) == 0));
581 g_free (default_account);
583 string = modest_account_mgr_get_signature (self, name, &use_signature);
584 modest_account_settings_set_use_signature (settings, use_signature);
585 modest_account_settings_set_signature (settings, string);
588 modest_account_settings_set_leave_messages_on_server
589 (settings, modest_account_mgr_get_leave_on_server (self, name));
590 modest_account_settings_set_use_connection_specific_smtp
591 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
594 server_account = modest_account_mgr_get_string (self, name,
595 MODEST_ACCOUNT_STORE_ACCOUNT,
597 if (server_account) {
598 ModestServerAccountSettings *store_settings;
599 store_settings = modest_account_mgr_load_server_settings (self, server_account);
600 modest_account_settings_set_store_settings (settings,
602 g_object_unref (store_settings);
603 g_free (server_account);
607 server_account = modest_account_mgr_get_string (self, name,
608 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
610 if (server_account) {
611 ModestServerAccountSettings *transport_settings;
612 transport_settings = modest_account_mgr_load_server_settings (self, server_account);
613 modest_account_settings_set_transport_settings (settings, transport_settings);
614 g_object_unref (transport_settings);
615 g_free (server_account);
622 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
623 ModestAccountSettings *settings)
625 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
626 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
628 const gchar *account_name;
629 const gchar *store_account_name;
630 const gchar *transport_account_name;
631 ModestServerAccountSettings *store_settings;
632 ModestServerAccountSettings *transport_settings;
634 account_name = modest_account_settings_get_account_name (settings);
635 g_return_if_fail (account_name != NULL);
637 modest_account_mgr_set_display_name (mgr, account_name,
638 modest_account_settings_get_display_name (settings));
639 modest_account_mgr_set_user_fullname (mgr, account_name,
640 modest_account_settings_get_fullname (settings));
641 modest_account_mgr_set_user_email (mgr, account_name,
642 modest_account_settings_get_email_address (settings));
643 modest_account_mgr_set_retrieve_type (mgr, account_name,
644 modest_account_settings_get_retrieve_type (settings));
645 modest_account_mgr_set_retrieve_limit (mgr, account_name,
646 modest_account_settings_get_retrieve_limit (settings));
647 modest_account_mgr_set_leave_on_server (mgr, account_name,
648 modest_account_settings_get_leave_messages_on_server (settings));
649 modest_account_mgr_set_signature (mgr, account_name,
650 modest_account_settings_get_signature (settings),
651 modest_account_settings_get_use_signature (settings));
652 modest_account_mgr_set_use_connection_specific_smtp
654 modest_account_settings_get_use_connection_specific_smtp (settings));
656 store_settings = modest_account_settings_get_store_settings (settings);
657 store_account_name = modest_server_account_settings_get_account_name (store_settings);
658 if (store_settings != NULL) {
659 modest_account_mgr_save_server_settings (mgr, store_settings);
661 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT, store_account_name, FALSE);
662 g_object_unref (store_settings);
664 transport_settings = modest_account_settings_get_transport_settings (settings);
665 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
666 if (transport_settings != NULL) {
667 modest_account_mgr_save_server_settings (mgr, transport_settings);
669 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT, transport_account_name, FALSE);
670 g_object_unref (transport_settings);
671 modest_account_mgr_set_enabled (mgr, account_name, TRUE);
676 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
678 return g_utf8_collate((const gchar*)a, (const gchar*)b);
681 /** Get the first one, alphabetically, by title. */
683 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
685 const gchar* account_name = NULL;
686 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
688 /* Return TRUE if there is no account */
692 /* Get the first one, alphabetically, by title: */
693 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
694 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
696 GSList* iter = list_sorted;
697 gboolean found = FALSE;
698 while (iter && !found) {
699 account_name = (const gchar*)list_sorted->data;
705 iter = g_slist_next (iter);
708 gchar* result = NULL;
710 result = g_strdup (account_name);
712 modest_account_mgr_free_account_names (account_names);
713 account_names = NULL;
719 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
721 gboolean result = FALSE;
723 gchar* account_name = modest_account_mgr_get_first_account_name(self);
725 result = modest_account_mgr_set_default_account (self, account_name);
726 g_free (account_name);
729 result = TRUE; /* If there are no accounts then it's not a failure. */
735 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
737 gchar *fullname, *email, *from;
739 g_return_val_if_fail (self, NULL);
740 g_return_val_if_fail (name, NULL);
742 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
744 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
746 from = g_strdup_printf ("%s <%s>",
747 fullname ? fullname : "",
755 /* Add a number to the end of the text, or increment a number that is already there.
758 util_increment_name (const gchar* text)
760 /* Get the end character,
761 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
763 const gchar* end = NULL;
764 if (!g_utf8_validate (text, -1, &end))
770 --end; /* Go to before the null-termination. */
772 /* Look at each UTF-8 characer, starting at the end: */
773 const gchar* p = end;
774 const gchar* alpha_end = NULL;
777 /* Stop when we reach the first character that is not a numeric digit: */
778 const gunichar ch = g_utf8_get_char (p);
779 if (!g_unichar_isdigit (ch)) {
784 p = g_utf8_prev_char (p);
788 /* The text must consist completely of numeric digits. */
794 /* Intepret and increment the number, if any: */
795 gint num = atol (alpha_end);
798 /* Get the name part: */
799 gint name_len = alpha_end - text;
800 gchar *name_without_number = g_malloc(name_len + 1);
801 memcpy (name_without_number, text, name_len);
802 name_without_number[name_len] = 0;\
804 /* Concatenate the text part and the new number: */
805 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
806 g_free (name_without_number);
812 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
813 gboolean server_account)
815 gchar *account_name = g_strdup (starting_name);
817 while (modest_account_mgr_account_exists (self,
818 account_name, server_account /* server_account */)) {
820 gchar * account_name2 = util_increment_name (account_name);
821 g_free (account_name);
822 account_name = account_name2;
829 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
831 gchar *account_name = g_strdup (starting_name);
833 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
835 gchar * account_name2 = util_increment_name (account_name);
836 g_free (account_name);
837 account_name = account_name2;
844 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
845 const gchar *account_name,
846 gboolean leave_on_server)
848 modest_account_mgr_set_bool (self,
850 MODEST_ACCOUNT_LEAVE_ON_SERVER,
856 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
857 const gchar* account_name)
859 return modest_account_mgr_get_bool (self,
861 MODEST_ACCOUNT_LEAVE_ON_SERVER,
866 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
867 const gchar* account_name)
869 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
871 MODEST_ACCOUNT_LAST_UPDATED,
876 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
877 const gchar* account_name,
880 modest_account_mgr_set_int (self,
882 MODEST_ACCOUNT_LAST_UPDATED,
886 /* TODO: notify about changes */
890 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
891 const gchar* account_name)
893 return modest_account_mgr_get_int (self,
895 MODEST_ACCOUNT_LIMIT_RETRIEVE,
900 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
901 const gchar* account_name,
904 modest_account_mgr_set_int (self,
906 MODEST_ACCOUNT_LIMIT_RETRIEVE,
908 FALSE /* not server account */);
912 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
913 const gchar* account_name)
915 return modest_account_mgr_get_int (self,
922 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
923 const gchar *account_name,
926 modest_account_mgr_set_int (self,
929 port_num, TRUE /* server account */);
933 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
934 const gchar *account_name,
935 TnyAccountType account_type)
937 return modest_account_mgr_get_string (self,
939 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
940 MODEST_ACCOUNT_STORE_ACCOUNT :
941 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
946 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
948 switch(retrieve_type) {
949 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
950 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
952 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
953 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
955 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
956 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
959 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
963 static ModestAccountRetrieveType
964 get_retrieve_type (const gchar *name)
966 if (!name || name[0] == 0)
967 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
968 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
969 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
970 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
971 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
973 /* we fall back to headers only */
974 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
978 ModestAccountRetrieveType
979 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
980 const gchar *account_name)
983 ModestAccountRetrieveType result;
985 string = modest_account_mgr_get_string (self,
987 MODEST_ACCOUNT_RETRIEVE,
988 FALSE /* not server account */);
989 result = get_retrieve_type (string);
996 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
997 const gchar *account_name,
998 ModestAccountRetrieveType retrieve_type)
1000 modest_account_mgr_set_string (self,
1002 MODEST_ACCOUNT_RETRIEVE,
1003 get_retrieve_type_name (retrieve_type),
1004 FALSE /* not server account */);
1009 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1010 const gchar *account_name,
1011 const gchar *fullname)
1013 modest_account_mgr_set_string (self,
1015 MODEST_ACCOUNT_FULLNAME,
1017 FALSE /* not server account */);
1021 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1022 const gchar *account_name,
1025 modest_account_mgr_set_string (self,
1027 MODEST_ACCOUNT_EMAIL,
1029 FALSE /* not server account */);