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);
69 modest_account_mgr_get_signature (ModestAccountMgr *self,
71 gboolean* use_signature)
74 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, FALSE);
289 g_free (existing_username);
293 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
295 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
296 TRUE /* server account */);
300 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self,
301 const gchar* account_name,
304 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
305 succeeded, TRUE /* server account */);
309 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
311 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
312 TRUE /* server account */);
316 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
318 gboolean result = FALSE;
319 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
320 TRUE /* server account */);
321 if (password && strlen (password)) {
331 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
332 const gchar* account_name)
334 return modest_account_mgr_get_string (self,
336 MODEST_ACCOUNT_HOSTNAME,
337 TRUE /* server account */);
341 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
342 const gchar *server_account_name,
343 const gchar *hostname)
345 modest_account_mgr_set_string (self,
347 MODEST_ACCOUNT_HOSTNAME,
349 TRUE /* server account */);
355 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
356 const gchar* account_name)
358 ModestAuthProtocol result = MODEST_PROTOCOL_AUTH_NONE;
359 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
360 TRUE /* server account */);
362 result = modest_protocol_info_get_auth_protocol (value);
372 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
373 const gchar* account_name, ModestAuthProtocol secure_auth)
375 /* Get the conf string for the enum value: */
376 const gchar* str_value = NULL;
378 str_value = modest_protocol_info_get_auth_protocol_name (secure_auth);
380 /* Set it in the configuration: */
381 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
384 ModestConnectionProtocol
385 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
386 const gchar* account_name)
388 ModestConnectionProtocol result = MODEST_PROTOCOL_CONNECTION_NORMAL;
389 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
390 TRUE /* server account */);
392 result = modest_protocol_info_get_connection_protocol (value);
401 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
402 const gchar* account_name, ModestConnectionProtocol security)
404 /* Get the conf string for the enum value: */
405 const gchar* str_value = NULL;
406 str_value = modest_protocol_info_get_connection_protocol_name (security);
408 /* Set it in the configuration: */
409 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
412 ModestServerAccountSettings*
413 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name)
415 ModestServerAccountSettings *settings;
418 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
419 settings = modest_server_account_settings_new ();
421 modest_server_account_settings_set_account_name (settings, name);
423 string = modest_account_mgr_get_string (self, name,
424 MODEST_ACCOUNT_HOSTNAME,TRUE);
425 modest_server_account_settings_set_hostname (settings, string);
428 string = modest_account_mgr_get_string (self, name,
429 MODEST_ACCOUNT_USERNAME,TRUE);
430 modest_server_account_settings_set_username (settings, string);
433 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
434 modest_server_account_settings_set_protocol (settings,
435 modest_protocol_info_get_transport_store_protocol (string));
438 modest_server_account_settings_set_port (settings,
439 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
441 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
442 modest_server_account_settings_set_auth_protocol (settings,
443 modest_protocol_info_get_auth_protocol(string));
446 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
447 modest_server_account_settings_set_security (settings,
448 modest_protocol_info_get_connection_protocol(string));
451 string = modest_account_mgr_get_string (self, name,
452 MODEST_ACCOUNT_PASSWORD, TRUE);
453 modest_server_account_settings_set_password (settings, string);
460 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
461 ModestServerAccountSettings *settings)
463 gboolean has_errors = FALSE;
464 const gchar *account_name;
465 const gchar *protocol;
468 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
469 account_name = modest_server_account_settings_get_account_name (settings);
471 /* if we don't have a valid account name we cannot save */
472 g_return_val_if_fail (account_name, FALSE);
474 protocol = modest_protocol_info_get_transport_store_protocol_name (
475 modest_server_account_settings_get_protocol (settings));
476 uri = modest_server_account_settings_get_uri (settings);
478 const gchar *hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
479 const gchar *username = null_means_empty (modest_server_account_settings_get_username (settings));
480 const gchar *password = null_means_empty (modest_server_account_settings_get_password (settings));
481 gint port = modest_server_account_settings_get_port (settings);
482 const gchar *auth_protocol = modest_protocol_info_get_auth_protocol_name (
483 modest_server_account_settings_get_auth_protocol (settings));
484 const gchar *security = modest_protocol_info_get_connection_protocol_name (
485 modest_server_account_settings_get_security (settings));
487 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
490 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
493 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
496 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
499 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
502 (has_errors = !modest_account_mgr_set_string (self, account_name,
503 MODEST_ACCOUNT_AUTH_MECH,
504 auth_protocol, TRUE));
506 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
510 const gchar *uri = modest_server_account_settings_get_uri (settings);
511 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
514 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
519 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
527 ModestAccountSettings *
528 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
531 ModestAccountSettings *settings;
533 gchar *server_account;
534 gchar *default_account;
535 gboolean use_signature = FALSE;
537 g_return_val_if_fail (self, NULL);
538 g_return_val_if_fail (name, NULL);
540 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
541 /* For instance, maybe you are mistakenly checking for a server account name? */
542 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
546 settings = modest_account_settings_new ();
548 modest_account_settings_set_account_name (settings, name);
550 string = modest_account_mgr_get_string (self, name,
551 MODEST_ACCOUNT_DISPLAY_NAME,
553 modest_account_settings_set_display_name (settings, string);
556 string = modest_account_mgr_get_string (self, name,
557 MODEST_ACCOUNT_FULLNAME,
559 modest_account_settings_set_fullname (settings, string);
562 string = modest_account_mgr_get_string (self, name,
563 MODEST_ACCOUNT_EMAIL,
565 modest_account_settings_set_email_address (settings, string);
568 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
569 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
570 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
572 default_account = modest_account_mgr_get_default_account (self);
573 modest_account_settings_set_is_default (settings,
574 (default_account && strcmp (default_account, name) == 0));
575 g_free (default_account);
577 string = modest_account_mgr_get_signature (self, name, &use_signature);
578 modest_account_settings_set_use_signature (settings, use_signature);
579 modest_account_settings_set_signature (settings, string);
582 modest_account_settings_set_leave_messages_on_server
583 (settings, modest_account_mgr_get_leave_on_server (self, name));
584 modest_account_settings_set_use_connection_specific_smtp
585 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
588 server_account = modest_account_mgr_get_string (self, name,
589 MODEST_ACCOUNT_STORE_ACCOUNT,
591 if (server_account) {
592 ModestServerAccountSettings *store_settings;
593 store_settings = modest_account_mgr_load_server_settings (self, server_account);
594 modest_account_settings_set_store_settings (settings,
596 g_object_unref (store_settings);
597 g_free (server_account);
601 server_account = modest_account_mgr_get_string (self, name,
602 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
604 if (server_account) {
605 ModestServerAccountSettings *transport_settings;
606 transport_settings = modest_account_mgr_load_server_settings (self, server_account);
607 modest_account_settings_set_transport_settings (settings, transport_settings);
608 g_object_unref (transport_settings);
609 g_free (server_account);
616 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
617 ModestAccountSettings *settings)
619 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
620 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
622 const gchar *account_name;
623 const gchar *store_account_name;
624 const gchar *transport_account_name;
625 ModestServerAccountSettings *store_settings;
626 ModestServerAccountSettings *transport_settings;
628 account_name = modest_account_settings_get_account_name (settings);
629 g_return_if_fail (account_name != NULL);
631 modest_account_mgr_set_display_name (mgr, account_name,
632 modest_account_settings_get_display_name (settings));
633 modest_account_mgr_set_user_fullname (mgr, account_name,
634 modest_account_settings_get_fullname (settings));
635 modest_account_mgr_set_user_email (mgr, account_name,
636 modest_account_settings_get_email_address (settings));
637 modest_account_mgr_set_retrieve_type (mgr, account_name,
638 modest_account_settings_get_retrieve_type (settings));
639 modest_account_mgr_set_retrieve_limit (mgr, account_name,
640 modest_account_settings_get_retrieve_limit (settings));
641 modest_account_mgr_set_leave_on_server (mgr, account_name,
642 modest_account_settings_get_leave_messages_on_server (settings));
643 modest_account_mgr_set_signature (mgr, account_name,
644 modest_account_settings_get_signature (settings),
645 modest_account_settings_get_use_signature (settings));
646 modest_account_mgr_set_use_connection_specific_smtp
648 modest_account_settings_get_use_connection_specific_smtp (settings));
650 store_settings = modest_account_settings_get_store_settings (settings);
651 store_account_name = modest_server_account_settings_get_account_name (store_settings);
652 if (store_settings != NULL) {
653 modest_account_mgr_save_server_settings (mgr, store_settings);
655 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT, store_account_name, FALSE);
656 g_object_unref (store_settings);
658 transport_settings = modest_account_settings_get_transport_settings (settings);
659 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
660 if (transport_settings != NULL) {
661 modest_account_mgr_save_server_settings (mgr, transport_settings);
663 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT, transport_account_name, FALSE);
664 g_object_unref (transport_settings);
665 modest_account_mgr_set_enabled (mgr, account_name, TRUE);
670 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
672 return g_utf8_collate((const gchar*)a, (const gchar*)b);
675 /** Get the first one, alphabetically, by title. */
677 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
679 const gchar* account_name = NULL;
680 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
682 /* Return TRUE if there is no account */
686 /* Get the first one, alphabetically, by title: */
687 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
688 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
690 GSList* iter = list_sorted;
691 gboolean found = FALSE;
692 while (iter && !found) {
693 account_name = (const gchar*)list_sorted->data;
699 iter = g_slist_next (iter);
702 gchar* result = NULL;
704 result = g_strdup (account_name);
706 modest_account_mgr_free_account_names (account_names);
707 account_names = NULL;
713 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
715 gboolean result = FALSE;
717 gchar* account_name = modest_account_mgr_get_first_account_name(self);
719 result = modest_account_mgr_set_default_account (self, account_name);
720 g_free (account_name);
723 result = TRUE; /* If there are no accounts then it's not a failure. */
729 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
731 gchar *fullname, *email, *from;
733 g_return_val_if_fail (self, NULL);
734 g_return_val_if_fail (name, NULL);
736 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
738 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
740 from = g_strdup_printf ("%s <%s>",
741 fullname ? fullname : "",
749 /* Add a number to the end of the text, or increment a number that is already there.
752 util_increment_name (const gchar* text)
754 /* Get the end character,
755 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
757 const gchar* end = NULL;
758 if (!g_utf8_validate (text, -1, &end))
764 --end; /* Go to before the null-termination. */
766 /* Look at each UTF-8 characer, starting at the end: */
767 const gchar* p = end;
768 const gchar* alpha_end = NULL;
771 /* Stop when we reach the first character that is not a numeric digit: */
772 const gunichar ch = g_utf8_get_char (p);
773 if (!g_unichar_isdigit (ch)) {
778 p = g_utf8_prev_char (p);
782 /* The text must consist completely of numeric digits. */
788 /* Intepret and increment the number, if any: */
789 gint num = atol (alpha_end);
792 /* Get the name part: */
793 gint name_len = alpha_end - text;
794 gchar *name_without_number = g_malloc(name_len + 1);
795 memcpy (name_without_number, text, name_len);
796 name_without_number[name_len] = 0;\
798 /* Concatenate the text part and the new number: */
799 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
800 g_free (name_without_number);
806 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
807 gboolean server_account)
809 gchar *account_name = g_strdup (starting_name);
811 while (modest_account_mgr_account_exists (self,
812 account_name, server_account /* server_account */)) {
814 gchar * account_name2 = util_increment_name (account_name);
815 g_free (account_name);
816 account_name = account_name2;
823 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
825 gchar *account_name = g_strdup (starting_name);
827 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
829 gchar * account_name2 = util_increment_name (account_name);
830 g_free (account_name);
831 account_name = account_name2;
838 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
839 const gchar *account_name,
840 gboolean leave_on_server)
842 modest_account_mgr_set_bool (self,
844 MODEST_ACCOUNT_LEAVE_ON_SERVER,
850 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
851 const gchar* account_name)
853 return modest_account_mgr_get_bool (self,
855 MODEST_ACCOUNT_LEAVE_ON_SERVER,
860 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
861 const gchar* account_name)
863 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
865 MODEST_ACCOUNT_LAST_UPDATED,
870 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
871 const gchar* account_name,
874 modest_account_mgr_set_int (self,
876 MODEST_ACCOUNT_LAST_UPDATED,
880 /* TODO: notify about changes */
884 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
885 const gchar* account_name)
887 return modest_account_mgr_get_int (self,
889 MODEST_ACCOUNT_LIMIT_RETRIEVE,
894 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
895 const gchar* account_name,
898 modest_account_mgr_set_int (self,
900 MODEST_ACCOUNT_LIMIT_RETRIEVE,
902 FALSE /* not server account */);
906 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
907 const gchar* account_name)
909 return modest_account_mgr_get_int (self,
916 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
917 const gchar *account_name,
920 modest_account_mgr_set_int (self,
923 port_num, TRUE /* server account */);
927 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
928 const gchar *account_name,
929 TnyAccountType account_type)
931 return modest_account_mgr_get_string (self,
933 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
934 MODEST_ACCOUNT_STORE_ACCOUNT :
935 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
940 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
942 switch(retrieve_type) {
943 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
944 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
946 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
947 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
949 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
950 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
953 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
957 static ModestAccountRetrieveType
958 get_retrieve_type (const gchar *name)
960 if (!name || name[0] == 0)
961 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
962 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
963 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
964 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
965 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
967 /* we fall back to headers only */
968 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
972 ModestAccountRetrieveType
973 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
974 const gchar *account_name)
977 ModestAccountRetrieveType result;
979 string = modest_account_mgr_get_string (self,
981 MODEST_ACCOUNT_RETRIEVE,
982 FALSE /* not server account */);
983 result = get_retrieve_type (string);
990 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
991 const gchar *account_name,
992 ModestAccountRetrieveType retrieve_type)
994 modest_account_mgr_set_string (self,
996 MODEST_ACCOUNT_RETRIEVE,
997 get_retrieve_type_name (retrieve_type),
998 FALSE /* not server account */);
1003 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1004 const gchar *account_name,
1005 const gchar *fullname)
1007 modest_account_mgr_set_string (self,
1009 MODEST_ACCOUNT_FULLNAME,
1011 FALSE /* not server account */);
1015 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1016 const gchar *account_name,
1019 modest_account_mgr_set_string (self,
1021 MODEST_ACCOUNT_EMAIL,
1023 FALSE /* not server account */);