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>
34 #include <modest-defs.h>
37 static const gchar * null_means_empty (const gchar * str);
40 null_means_empty (const gchar * str)
42 return str ? str : "";
46 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
49 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
54 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
56 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
59 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
60 const gchar* signature, gboolean use_signature)
62 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
63 use_signature, FALSE);
64 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
65 null_means_empty (signature), FALSE);
70 modest_account_mgr_get_signature (ModestAccountMgr *self,
72 gboolean* use_signature)
75 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
77 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
80 ModestProtocolType modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
82 ModestProtocolType result = MODEST_PROTOCOLS_STORE_POP; /* Arbitrary default */
84 gchar *server_account_name = modest_account_mgr_get_string (self, name,
85 MODEST_ACCOUNT_STORE_ACCOUNT,
87 if (server_account_name) {
88 ModestServerAccountSettings* server_settings =
89 modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
90 result = modest_server_account_settings_get_protocol (server_settings);
92 g_object_unref (server_settings);
94 g_free (server_account_name);
101 gboolean modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
102 const gchar* connection_id, const gchar* server_account_name)
104 modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
106 ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
108 gboolean result = TRUE;
110 GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
111 MODEST_CONF_VALUE_STRING, &err);
113 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
118 /* The server account is in the item after the connection name: */
119 list = g_slist_append (list, (gpointer)connection_id);
120 list = g_slist_append (list, (gpointer)server_account_name);
122 /* Reset the changed list: */
123 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
124 MODEST_CONF_VALUE_STRING, &err);
126 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
132 /* TODO: Should we free the items too, or just the list? */
139 * modest_account_mgr_remove_connection_specific_smtp
140 * @self: a ModestAccountMgr instance
141 * @name: the account name
142 * @connection_id: A libconic IAP connection id
144 * Disassacoiate a server account to use with the specific connection for this account.
146 * Returns: TRUE if it worked, FALSE otherwise
148 gboolean modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
149 const gchar* connection_id)
151 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
153 gboolean result = TRUE;
155 GSList *list = modest_conf_get_list (priv->modest_conf,
156 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
157 MODEST_CONF_VALUE_STRING, &err);
159 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
168 /* The server account is in the item after the connection name: */
169 GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
170 if (list_connection) {
171 GSList *account_node = g_slist_next (list_connection);
172 /* remove both items: */
173 list = g_slist_delete_link(list, list_connection);
174 list = g_slist_delete_link(list, account_node);
177 /* Reset the changed list: */
178 modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
179 MODEST_CONF_VALUE_STRING, &err);
181 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
186 /* TODO: Should we free the items too, or just the list? */
193 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
195 return modest_account_mgr_get_bool (self, account_name,
196 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
199 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
202 return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP,
207 * modest_account_mgr_get_connection_specific_smtp
208 * @self: a ModestAccountMgr instance
209 * @connection_id: A libconic IAP connection id
211 * Retrieve a server account to use with this specific connection for this account.
213 * Returns: a server account name to use for this connection, or NULL if none is specified.
215 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* connection_id)
217 gchar *result = NULL;
219 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
222 GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
223 MODEST_CONF_VALUE_STRING, &err);
225 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
233 /* The server account is in the item after the connection name: */
236 const gchar* this_connection_id = (const gchar*)(iter->data);
237 if (strcmp (this_connection_id, connection_id) == 0) {
238 iter = g_slist_next (iter);
241 const gchar* account_name = (const gchar*)(iter->data);
243 result = g_strdup (account_name);
249 /* Skip 2 to go to the next connection in the list: */
250 iter = g_slist_next (iter);
252 iter = g_slist_next (iter);
257 printf (" debug: no server found for connection_id=%s.\n", connection_id);
261 /* TODO: Should we free the items too, or just the list? */
268 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
270 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
271 TRUE /* server account */);
275 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name,
276 const gchar* username)
278 /* Note that this won't work properly as long as the gconf cache is broken
280 gchar *existing_username = modest_account_mgr_get_server_account_username(self,
283 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
284 username, TRUE /* server account */);
286 /* We don't know anything about new usernames: */
287 if (strcmp (existing_username, username) != 0)
288 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
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_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
312 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
313 TRUE /* server account */);
317 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
319 gboolean result = FALSE;
320 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
321 TRUE /* server account */);
322 if (password && strlen (password)) {
332 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
333 const gchar* account_name)
335 return modest_account_mgr_get_string (self,
337 MODEST_ACCOUNT_HOSTNAME,
338 TRUE /* server account */);
342 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
343 const gchar *server_account_name,
344 const gchar *hostname)
346 modest_account_mgr_set_string (self,
348 MODEST_ACCOUNT_HOSTNAME,
350 TRUE /* server account */);
356 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
357 const gchar* account_name)
359 ModestProtocolRegistry *protocol_registry;
360 ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
363 protocol_registry = modest_runtime_get_protocol_registry ();
364 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
365 TRUE /* server account */);
367 ModestProtocol *protocol;
369 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
373 result = modest_protocol_get_type_id (protocol);
382 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
383 const gchar* account_name, ModestProtocolType secure_auth)
385 const gchar* str_value;
386 ModestProtocolRegistry *protocol_registry;
387 ModestProtocol *protocol;
389 /* Get the conf string for the protocol: */
390 protocol_registry = modest_runtime_get_protocol_registry ();
391 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
392 str_value = modest_protocol_get_name (protocol);
394 /* Set it in the configuration: */
395 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
399 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
400 const gchar* account_name)
402 ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
405 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
406 TRUE /* server account */);
408 ModestProtocolRegistry *protocol_registry;
409 ModestProtocol *protocol;
411 protocol_registry = modest_runtime_get_protocol_registry ();
412 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
413 MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
418 result = modest_protocol_get_type_id (protocol);
425 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
426 const gchar* account_name,
427 ModestProtocolType security)
429 const gchar* str_value;
430 ModestProtocolRegistry *protocol_registry;
431 ModestProtocol *protocol;
433 /* Get the conf string for the protocol type: */
434 protocol_registry = modest_runtime_get_protocol_registry ();
435 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
436 str_value = modest_protocol_get_name (protocol);
438 /* Set it in the configuration: */
439 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
442 ModestServerAccountSettings*
443 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
445 ModestServerAccountSettings *settings;
446 ModestProtocol *protocol;
447 ModestProtocolRegistry *registry;
450 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
451 registry = modest_runtime_get_protocol_registry ();
452 settings = modest_server_account_settings_new ();
454 modest_server_account_settings_set_account_name (settings, name);
456 string = modest_account_mgr_get_string (self, name,
457 MODEST_ACCOUNT_HOSTNAME,TRUE);
458 modest_server_account_settings_set_hostname (settings, string);
461 string = modest_account_mgr_get_string (self, name,
462 MODEST_ACCOUNT_USERNAME,TRUE);
463 modest_server_account_settings_set_username (settings, string);
466 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
467 if (is_transport_and_not_store) {
468 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS, string);
470 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS, string);
472 modest_server_account_settings_set_protocol (settings,
473 modest_protocol_get_type_id (protocol));
476 modest_server_account_settings_set_port (settings,
477 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
479 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
481 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, string);
482 modest_server_account_settings_set_auth_protocol (settings,
483 modest_protocol_get_type_id (protocol));
486 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
489 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
491 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, string);
492 modest_server_account_settings_set_security_protocol (settings,
493 modest_protocol_get_type_id (protocol));
496 modest_server_account_settings_set_security_protocol (settings,
497 MODEST_PROTOCOLS_CONNECTION_NONE);
500 string = modest_account_mgr_get_string (self, name,
501 MODEST_ACCOUNT_PASSWORD, TRUE);
502 modest_server_account_settings_set_password (settings, string);
505 string = modest_account_mgr_get_string (self, name,
506 MODEST_ACCOUNT_URI, TRUE);
507 modest_server_account_settings_set_uri (settings, string);
514 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
515 ModestServerAccountSettings *settings)
517 gboolean has_errors = FALSE;
518 const gchar *account_name;
519 const gchar *protocol_name;
521 ModestProtocolRegistry *protocol_registry;
522 ModestProtocol *protocol;
524 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
525 protocol_registry = modest_runtime_get_protocol_registry ();
526 account_name = modest_server_account_settings_get_account_name (settings);
528 /* if we don't have a valid account name we cannot save */
529 g_return_val_if_fail (account_name, FALSE);
531 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
532 modest_server_account_settings_get_protocol (settings));
533 protocol_name = modest_protocol_get_name (protocol);
534 uri = modest_server_account_settings_get_uri (settings);
536 const gchar *hostname;
537 const gchar *username;
538 const gchar *password;
540 const gchar *auth_protocol_name;
541 const gchar *security_name;
543 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
544 username = null_means_empty (modest_server_account_settings_get_username (settings));
545 password = null_means_empty (modest_server_account_settings_get_password (settings));
546 port = modest_server_account_settings_get_port (settings);
547 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
548 modest_server_account_settings_get_auth_protocol (settings));
549 auth_protocol_name = modest_protocol_get_name (protocol);
550 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
551 modest_server_account_settings_get_security_protocol (settings));
552 security_name = modest_protocol_get_name (protocol);
554 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
557 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
560 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
563 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
564 protocol_name, TRUE));
566 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
569 (has_errors = !modest_account_mgr_set_string (self, account_name,
570 MODEST_ACCOUNT_AUTH_MECH,
571 auth_protocol_name, TRUE));
573 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
577 const gchar *uri = modest_server_account_settings_get_uri (settings);
578 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
581 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
582 protocol_name, TRUE));
590 ModestAccountSettings *
591 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
594 ModestAccountSettings *settings;
596 gchar *server_account;
597 gchar *default_account;
598 gboolean use_signature = FALSE;
600 g_return_val_if_fail (self, NULL);
601 g_return_val_if_fail (name, NULL);
603 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
604 /* For instance, maybe you are mistakenly checking for a server account name? */
605 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
609 settings = modest_account_settings_new ();
611 modest_account_settings_set_account_name (settings, name);
613 string = modest_account_mgr_get_string (self, name,
614 MODEST_ACCOUNT_DISPLAY_NAME,
616 modest_account_settings_set_display_name (settings, string);
619 string = modest_account_mgr_get_string (self, name,
620 MODEST_ACCOUNT_FULLNAME,
622 modest_account_settings_set_fullname (settings, string);
625 string = modest_account_mgr_get_string (self, name,
626 MODEST_ACCOUNT_EMAIL,
628 modest_account_settings_set_email_address (settings, string);
631 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
632 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
633 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
635 default_account = modest_account_mgr_get_default_account (self);
636 modest_account_settings_set_is_default (settings,
637 (default_account && strcmp (default_account, name) == 0));
638 g_free (default_account);
640 string = modest_account_mgr_get_signature (self, name, &use_signature);
641 modest_account_settings_set_use_signature (settings, use_signature);
642 modest_account_settings_set_signature (settings, string);
645 modest_account_settings_set_leave_messages_on_server
646 (settings, modest_account_mgr_get_leave_on_server (self, name));
647 modest_account_settings_set_use_connection_specific_smtp
648 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
651 server_account = modest_account_mgr_get_string (self, name,
652 MODEST_ACCOUNT_STORE_ACCOUNT,
654 if (server_account) {
655 ModestServerAccountSettings *store_settings;
656 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
657 modest_account_settings_set_store_settings (settings,
659 g_object_unref (store_settings);
660 g_free (server_account);
664 server_account = modest_account_mgr_get_string (self, name,
665 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
667 if (server_account) {
668 ModestServerAccountSettings *transport_settings;
669 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
670 modest_account_settings_set_transport_settings (settings, transport_settings);
671 g_object_unref (transport_settings);
672 g_free (server_account);
679 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
680 ModestAccountSettings *settings)
682 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
683 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
685 const gchar *account_name;
686 const gchar *store_account_name;
687 const gchar *transport_account_name;
688 ModestServerAccountSettings *store_settings;
689 ModestServerAccountSettings *transport_settings;
691 account_name = modest_account_settings_get_account_name (settings);
692 g_return_if_fail (account_name != NULL);
694 modest_account_mgr_set_display_name (mgr, account_name,
695 modest_account_settings_get_display_name (settings));
696 modest_account_mgr_set_user_fullname (mgr, account_name,
697 modest_account_settings_get_fullname (settings));
698 modest_account_mgr_set_user_email (mgr, account_name,
699 modest_account_settings_get_email_address (settings));
700 modest_account_mgr_set_retrieve_type (mgr, account_name,
701 modest_account_settings_get_retrieve_type (settings));
702 modest_account_mgr_set_retrieve_limit (mgr, account_name,
703 modest_account_settings_get_retrieve_limit (settings));
704 modest_account_mgr_set_leave_on_server (mgr, account_name,
705 modest_account_settings_get_leave_messages_on_server (settings));
706 modest_account_mgr_set_signature (mgr, account_name,
707 modest_account_settings_get_signature (settings),
708 modest_account_settings_get_use_signature (settings));
709 modest_account_mgr_set_use_connection_specific_smtp
711 modest_account_settings_get_use_connection_specific_smtp (settings));
713 store_settings = modest_account_settings_get_store_settings (settings);
714 store_account_name = modest_server_account_settings_get_account_name (store_settings);
715 if (store_settings != NULL) {
716 modest_account_mgr_save_server_settings (mgr, store_settings);
718 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT, store_account_name, FALSE);
719 g_object_unref (store_settings);
721 transport_settings = modest_account_settings_get_transport_settings (settings);
722 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
723 if (transport_settings != NULL) {
724 modest_account_mgr_save_server_settings (mgr, transport_settings);
726 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT, transport_account_name, FALSE);
727 g_object_unref (transport_settings);
728 modest_account_mgr_set_enabled (mgr, account_name, TRUE);
733 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
735 return g_utf8_collate((const gchar*)a, (const gchar*)b);
738 /** Get the first one, alphabetically, by title. */
740 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
742 const gchar* account_name = NULL;
743 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
745 /* Return TRUE if there is no account */
749 /* Get the first one, alphabetically, by title: */
750 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
751 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
753 GSList* iter = list_sorted;
754 gboolean found = FALSE;
755 while (iter && !found) {
756 account_name = (const gchar*)list_sorted->data;
762 iter = g_slist_next (iter);
765 gchar* result = NULL;
767 result = g_strdup (account_name);
769 modest_account_mgr_free_account_names (account_names);
770 account_names = NULL;
776 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
778 gboolean result = FALSE;
780 gchar* account_name = modest_account_mgr_get_first_account_name(self);
782 result = modest_account_mgr_set_default_account (self, account_name);
783 g_free (account_name);
786 result = TRUE; /* If there are no accounts then it's not a failure. */
792 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
794 gchar *fullname, *email, *from;
796 g_return_val_if_fail (self, NULL);
797 g_return_val_if_fail (name, NULL);
799 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
801 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
803 from = g_strdup_printf ("%s <%s>",
804 fullname ? fullname : "",
812 /* Add a number to the end of the text, or increment a number that is already there.
815 util_increment_name (const gchar* text)
817 /* Get the end character,
818 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
820 const gchar* end = NULL;
821 if (!g_utf8_validate (text, -1, &end))
827 --end; /* Go to before the null-termination. */
829 /* Look at each UTF-8 characer, starting at the end: */
830 const gchar* p = end;
831 const gchar* alpha_end = NULL;
834 /* Stop when we reach the first character that is not a numeric digit: */
835 const gunichar ch = g_utf8_get_char (p);
836 if (!g_unichar_isdigit (ch)) {
841 p = g_utf8_prev_char (p);
845 /* The text must consist completely of numeric digits. */
851 /* Intepret and increment the number, if any: */
852 gint num = atol (alpha_end);
855 /* Get the name part: */
856 gint name_len = alpha_end - text;
857 gchar *name_without_number = g_malloc(name_len + 1);
858 memcpy (name_without_number, text, name_len);
859 name_without_number[name_len] = 0;\
861 /* Concatenate the text part and the new number: */
862 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
863 g_free (name_without_number);
869 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
870 gboolean server_account)
872 gchar *account_name = g_strdup (starting_name);
874 while (modest_account_mgr_account_exists (self,
875 account_name, server_account /* server_account */)) {
877 gchar * account_name2 = util_increment_name (account_name);
878 g_free (account_name);
879 account_name = account_name2;
886 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
888 gchar *account_name = g_strdup (starting_name);
890 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
892 gchar * account_name2 = util_increment_name (account_name);
893 g_free (account_name);
894 account_name = account_name2;
901 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
902 const gchar *account_name,
903 gboolean leave_on_server)
905 modest_account_mgr_set_bool (self,
907 MODEST_ACCOUNT_LEAVE_ON_SERVER,
913 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
914 const gchar* account_name)
916 return modest_account_mgr_get_bool (self,
918 MODEST_ACCOUNT_LEAVE_ON_SERVER,
923 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
924 const gchar* account_name)
926 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
928 MODEST_ACCOUNT_LAST_UPDATED,
933 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
934 const gchar* account_name,
937 modest_account_mgr_set_int (self,
939 MODEST_ACCOUNT_LAST_UPDATED,
943 /* TODO: notify about changes */
947 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
948 const gchar* account_name)
950 return modest_account_mgr_get_int (self,
952 MODEST_ACCOUNT_LIMIT_RETRIEVE,
957 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
958 const gchar* account_name,
961 modest_account_mgr_set_int (self,
963 MODEST_ACCOUNT_LIMIT_RETRIEVE,
965 FALSE /* not server account */);
969 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
970 const gchar* account_name)
972 return modest_account_mgr_get_int (self,
979 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
980 const gchar *account_name,
983 modest_account_mgr_set_int (self,
986 port_num, TRUE /* server account */);
990 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
991 const gchar *account_name,
992 TnyAccountType account_type)
994 return modest_account_mgr_get_string (self,
996 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
997 MODEST_ACCOUNT_STORE_ACCOUNT :
998 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
1002 static const gchar *
1003 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1005 switch(retrieve_type) {
1006 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1007 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1009 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1010 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1012 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1013 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1016 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1020 static ModestAccountRetrieveType
1021 get_retrieve_type (const gchar *name)
1023 if (!name || name[0] == 0)
1024 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1025 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1026 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1027 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1028 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1030 /* we fall back to headers only */
1031 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1035 ModestAccountRetrieveType
1036 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
1037 const gchar *account_name)
1040 ModestAccountRetrieveType result;
1042 string = modest_account_mgr_get_string (self,
1044 MODEST_ACCOUNT_RETRIEVE,
1045 FALSE /* not server account */);
1046 result = get_retrieve_type (string);
1053 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
1054 const gchar *account_name,
1055 ModestAccountRetrieveType retrieve_type)
1057 modest_account_mgr_set_string (self,
1059 MODEST_ACCOUNT_RETRIEVE,
1060 get_retrieve_type_name (retrieve_type),
1061 FALSE /* not server account */);
1066 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1067 const gchar *account_name,
1068 const gchar *fullname)
1070 modest_account_mgr_set_string (self,
1072 MODEST_ACCOUNT_FULLNAME,
1074 FALSE /* not server account */);
1078 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1079 const gchar *account_name,
1082 modest_account_mgr_set_string (self,
1084 MODEST_ACCOUNT_EMAIL,
1086 FALSE /* not server account */);