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 #include <modest-account-protocol.h>
39 static const gchar * null_means_empty (const gchar * str);
42 null_means_empty (const gchar * str)
44 return str ? str : "";
48 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
51 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
56 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
58 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
61 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
62 const gchar* signature, gboolean use_signature)
64 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
65 use_signature, FALSE);
66 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
67 null_means_empty (signature), FALSE);
72 modest_account_mgr_get_signature (ModestAccountMgr *self,
74 gboolean* use_signature)
77 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
79 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
82 ModestProtocolType modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
84 ModestProtocolType result = MODEST_PROTOCOLS_STORE_POP; /* Arbitrary default */
86 gchar *server_account_name = modest_account_mgr_get_string (self, name,
87 MODEST_ACCOUNT_STORE_ACCOUNT,
89 if (server_account_name) {
90 ModestServerAccountSettings* server_settings =
91 modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
92 result = modest_server_account_settings_get_protocol (server_settings);
94 g_object_unref (server_settings);
96 g_free (server_account_name);
104 modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
105 const gchar* connection_id,
106 const gchar* server_account_name)
108 modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
110 ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
112 gboolean result = TRUE;
114 GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
115 MODEST_CONF_VALUE_STRING, &err);
117 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
122 /* The server account is in the item after the connection name: */
123 list = g_slist_append (list, g_strdup (connection_id));
124 list = g_slist_append (list, g_strdup (server_account_name));
126 /* Reset the changed list: */
127 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
128 MODEST_CONF_VALUE_STRING, &err);
130 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
138 g_slist_foreach (list, (GFunc) g_free, NULL);
146 * modest_account_mgr_remove_connection_specific_smtp
147 * @self: a ModestAccountMgr instance
148 * @name: the account name
149 * @connection_id: A libconic IAP connection id
151 * Disassacoiate a server account to use with the specific connection for this account.
153 * Returns: TRUE if it worked, FALSE otherwise
156 modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
157 const gchar* connection_id)
159 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
161 gboolean result = TRUE;
163 GSList *list = modest_conf_get_list (priv->modest_conf,
164 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
165 MODEST_CONF_VALUE_STRING, &err);
167 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
176 /* The server account is in the item after the connection name: */
177 GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
178 if (list_connection) {
179 GSList *account_node = g_slist_next (list_connection);
180 /* remove both items: */
181 list = g_slist_delete_link(list, list_connection);
182 list = g_slist_delete_link(list, account_node);
185 /* Reset the changed list: */
186 modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
187 MODEST_CONF_VALUE_STRING, &err);
189 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
196 g_slist_foreach (list, (GFunc) g_free, NULL);
204 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
206 return modest_account_mgr_get_bool (self, account_name,
207 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
210 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
213 return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP,
218 * modest_account_mgr_get_connection_specific_smtp
219 * @self: a ModestAccountMgr instance
220 * @connection_id: A libconic IAP connection id
222 * Retrieve a server account to use with this specific connection for this account.
224 * Returns: a server account name to use for this connection, or NULL if none is specified.
226 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* connection_id)
228 gchar *result = NULL;
230 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
233 GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
234 MODEST_CONF_VALUE_STRING, &err);
236 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
244 /* The server account is in the item after the connection name: */
247 const gchar* this_connection_id = (const gchar*)(iter->data);
248 if (strcmp (this_connection_id, connection_id) == 0) {
249 iter = g_slist_next (iter);
252 const gchar* account_name = (const gchar*)(iter->data);
254 result = g_strdup (account_name);
260 /* Skip 2 to go to the next connection in the list: */
261 iter = g_slist_next (iter);
263 iter = g_slist_next (iter);
268 g_slist_foreach (list, (GFunc) g_free, NULL);
276 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
278 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
279 TRUE /* server account */);
283 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name,
284 const gchar* username)
286 /* Note that this won't work properly as long as the gconf cache is broken
288 gchar *existing_username = modest_account_mgr_get_server_account_username(self,
291 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
292 username, TRUE /* server account */);
294 /* We don't know anything about new usernames: */
295 if (strcmp (existing_username, username) != 0)
296 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
298 g_free (existing_username);
302 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
304 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
305 TRUE /* server account */);
309 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self,
310 const gchar* account_name,
313 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
314 succeeded, TRUE /* server account */);
318 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
320 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
321 TRUE /* server account */);
325 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
327 gboolean result = FALSE;
328 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
329 TRUE /* server account */);
330 if (password && strlen (password)) {
334 bzero (password, strlen (password));
343 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
344 const gchar* account_name)
346 return modest_account_mgr_get_string (self,
348 MODEST_ACCOUNT_HOSTNAME,
349 TRUE /* server account */);
353 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
354 const gchar *server_account_name,
355 const gchar *hostname)
357 modest_account_mgr_set_string (self,
359 MODEST_ACCOUNT_HOSTNAME,
361 TRUE /* server account */);
367 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
368 const gchar* account_name)
370 ModestProtocolRegistry *protocol_registry;
371 ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
374 protocol_registry = modest_runtime_get_protocol_registry ();
375 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
376 TRUE /* server account */);
378 ModestProtocol *protocol;
380 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
384 result = modest_protocol_get_type_id (protocol);
393 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
394 const gchar* account_name, ModestProtocolType secure_auth)
396 const gchar* str_value;
397 ModestProtocolRegistry *protocol_registry;
398 ModestProtocol *protocol;
400 /* Get the conf string for the protocol: */
401 protocol_registry = modest_runtime_get_protocol_registry ();
402 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
403 str_value = modest_protocol_get_name (protocol);
405 /* Set it in the configuration: */
406 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
410 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
411 const gchar* account_name)
413 ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
416 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
417 TRUE /* server account */);
419 ModestProtocolRegistry *protocol_registry;
420 ModestProtocol *protocol;
422 protocol_registry = modest_runtime_get_protocol_registry ();
423 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
424 MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
429 result = modest_protocol_get_type_id (protocol);
436 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
437 const gchar* account_name,
438 ModestProtocolType security)
440 const gchar* str_value;
441 ModestProtocolRegistry *protocol_registry;
442 ModestProtocol *protocol;
444 /* Get the conf string for the protocol type: */
445 protocol_registry = modest_runtime_get_protocol_registry ();
446 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
447 str_value = modest_protocol_get_name (protocol);
449 /* Set it in the configuration: */
450 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
453 ModestServerAccountSettings *
454 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
456 ModestServerAccountSettings *settings = NULL;
457 ModestProtocol *protocol;
458 ModestProtocolRegistry *registry;
459 gchar *hostname, *username, *pwd, *uri, *proto, *auth, *sec;
461 if (!modest_account_mgr_account_exists (self, name, TRUE)) {
462 g_message ("%s account %s does not exist", __FUNCTION__, name);
466 registry = modest_runtime_get_protocol_registry ();
467 settings = modest_server_account_settings_new ();
469 modest_server_account_settings_set_account_name (settings, name);
471 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
474 if (is_transport_and_not_store) {
475 tag = MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS;
477 tag = MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS;
479 protocol = modest_protocol_registry_get_protocol_by_name (registry, tag, proto);
481 modest_server_account_settings_set_protocol (settings,
482 modest_protocol_get_type_id (protocol));
488 modest_server_account_settings_set_port (settings,
489 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
491 auth = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
493 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, auth);
494 modest_server_account_settings_set_auth_protocol (settings,
495 modest_protocol_get_type_id (protocol));
498 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
501 sec = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
503 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, sec);
504 modest_server_account_settings_set_security_protocol (settings,
505 modest_protocol_get_type_id (protocol));
508 modest_server_account_settings_set_security_protocol (settings,
509 MODEST_PROTOCOLS_CONNECTION_NONE);
512 /* Username, password and URI. Note that the URI could include
513 the former two, so in this case there is no need to have
515 username = modest_account_mgr_get_string (self, name,
516 MODEST_ACCOUNT_USERNAME,TRUE);
518 modest_server_account_settings_set_username (settings, username);
520 pwd = modest_account_mgr_get_string (self, name,
521 MODEST_ACCOUNT_PASSWORD, TRUE);
523 modest_server_account_settings_set_password (settings, pwd);
527 uri = modest_account_mgr_get_string (self, name,
528 MODEST_ACCOUNT_URI, TRUE);
530 modest_server_account_settings_set_uri (settings, uri);
532 hostname = modest_account_mgr_get_string (self, name,
533 MODEST_ACCOUNT_HOSTNAME,TRUE);
535 modest_server_account_settings_set_hostname (settings, hostname);
538 if (!username || !hostname) {
553 g_object_unref (settings);
558 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
559 ModestServerAccountSettings *settings)
561 gboolean has_errors = FALSE;
562 const gchar *account_name;
563 const gchar *protocol_name;
565 ModestProtocolRegistry *protocol_registry;
566 ModestProtocol *protocol;
568 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
569 protocol_registry = modest_runtime_get_protocol_registry ();
570 account_name = modest_server_account_settings_get_account_name (settings);
572 /* if we don't have a valid account name we cannot save */
573 g_return_val_if_fail (account_name, FALSE);
575 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
576 modest_server_account_settings_get_protocol (settings));
577 protocol_name = modest_protocol_get_name (protocol);
578 uri = modest_server_account_settings_get_uri (settings);
580 const gchar *hostname;
581 const gchar *username;
582 const gchar *password;
584 const gchar *auth_protocol_name;
585 const gchar *security_name;
587 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
588 username = null_means_empty (modest_server_account_settings_get_username (settings));
589 password = null_means_empty (modest_server_account_settings_get_password (settings));
590 port = modest_server_account_settings_get_port (settings);
591 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
592 modest_server_account_settings_get_auth_protocol (settings));
593 auth_protocol_name = modest_protocol_get_name (protocol);
594 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
595 modest_server_account_settings_get_security_protocol (settings));
596 security_name = modest_protocol_get_name (protocol);
598 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
601 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
604 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
607 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
608 protocol_name, TRUE));
610 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
613 (has_errors = !modest_account_mgr_set_string (self, account_name,
614 MODEST_ACCOUNT_AUTH_MECH,
615 auth_protocol_name, TRUE));
617 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
621 const gchar *uri = modest_server_account_settings_get_uri (settings);
622 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
625 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
626 protocol_name, TRUE));
634 ModestAccountSettings *
635 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
638 ModestAccountSettings *settings;
640 gchar *server_account;
641 gchar *default_account;
642 gboolean use_signature = FALSE;
644 g_return_val_if_fail (self, NULL);
645 g_return_val_if_fail (name, NULL);
647 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
648 /* For instance, maybe you are mistakenly checking for a server account name? */
649 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
653 settings = modest_account_settings_new ();
655 modest_account_settings_set_account_name (settings, name);
657 string = modest_account_mgr_get_string (self, name,
658 MODEST_ACCOUNT_DISPLAY_NAME,
660 modest_account_settings_set_display_name (settings, string);
663 string = modest_account_mgr_get_string (self, name,
664 MODEST_ACCOUNT_FULLNAME,
666 modest_account_settings_set_fullname (settings, string);
669 string = modest_account_mgr_get_string (self, name,
670 MODEST_ACCOUNT_EMAIL,
672 modest_account_settings_set_email_address (settings, string);
675 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
676 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
677 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
679 default_account = modest_account_mgr_get_default_account (self);
680 modest_account_settings_set_is_default (settings,
681 (default_account && strcmp (default_account, name) == 0));
682 g_free (default_account);
684 string = modest_account_mgr_get_signature (self, name, &use_signature);
685 modest_account_settings_set_use_signature (settings, use_signature);
686 modest_account_settings_set_signature (settings, string);
689 modest_account_settings_set_leave_messages_on_server
690 (settings, modest_account_mgr_get_leave_on_server (self, name));
691 modest_account_settings_set_use_connection_specific_smtp
692 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
695 server_account = modest_account_mgr_get_string (self, name,
696 MODEST_ACCOUNT_STORE_ACCOUNT,
698 if (server_account) {
699 ModestServerAccountSettings *store_settings;
700 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
701 g_free (server_account);
703 /* It could happen that the account data is corrupted
704 so it's not loaded properly */
705 if (store_settings) {
706 modest_account_settings_set_store_settings (settings,
708 g_object_unref (store_settings);
710 g_message ("%s can not load server settings. Account corrupted?", __FUNCTION__);
711 g_object_unref (settings);
717 server_account = modest_account_mgr_get_string (self, name,
718 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
720 if (server_account) {
721 ModestServerAccountSettings *transport_settings;
722 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
723 g_free (server_account);
725 if (transport_settings) {
726 modest_account_settings_set_transport_settings (settings, transport_settings);
727 g_object_unref (transport_settings);
729 g_message ("%s can not load server settings. Account corrupted?", __FUNCTION__);
730 g_object_unref (settings);
739 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
740 ModestAccountSettings *settings)
742 const gchar *account_name;
743 ModestServerAccountSettings *store_settings;
744 ModestServerAccountSettings *transport_settings;
746 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
747 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
749 account_name = modest_account_settings_get_account_name (settings);
750 g_return_if_fail (account_name != NULL);
752 modest_account_mgr_set_display_name (mgr, account_name,
753 modest_account_settings_get_display_name (settings));
754 modest_account_mgr_set_user_fullname (mgr, account_name,
755 modest_account_settings_get_fullname (settings));
756 modest_account_mgr_set_user_email (mgr, account_name,
757 modest_account_settings_get_email_address (settings));
758 modest_account_mgr_set_retrieve_type (mgr, account_name,
759 modest_account_settings_get_retrieve_type (settings));
760 modest_account_mgr_set_retrieve_limit (mgr, account_name,
761 modest_account_settings_get_retrieve_limit (settings));
762 modest_account_mgr_set_leave_on_server (mgr, account_name,
763 modest_account_settings_get_leave_messages_on_server (settings));
764 modest_account_mgr_set_signature (mgr, account_name,
765 modest_account_settings_get_signature (settings),
766 modest_account_settings_get_use_signature (settings));
767 modest_account_mgr_set_use_connection_specific_smtp
769 modest_account_settings_get_use_connection_specific_smtp (settings));
771 store_settings = modest_account_settings_get_store_settings (settings);
772 if (store_settings) {
773 const gchar *store_account_name;
774 store_account_name = modest_server_account_settings_get_account_name (store_settings);
775 if (store_account_name)
776 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT,
777 store_account_name, FALSE);
778 modest_account_mgr_save_server_settings (mgr, store_settings);
779 g_object_unref (store_settings);
782 transport_settings = modest_account_settings_get_transport_settings (settings);
783 if (transport_settings) {
784 const gchar *transport_account_name;
785 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
786 if (transport_account_name)
787 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
788 transport_account_name, FALSE);
789 modest_account_mgr_save_server_settings (mgr, transport_settings);
790 g_object_unref (transport_settings);
792 modest_account_mgr_set_enabled (mgr, account_name, TRUE);
797 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
799 return g_utf8_collate((const gchar*)a, (const gchar*)b);
802 /** Get the first one, alphabetically, by title. */
804 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
806 const gchar* account_name = NULL;
807 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
809 /* Return TRUE if there is no account */
813 /* Get the first one, alphabetically, by title: */
814 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
815 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
817 GSList* iter = list_sorted;
818 gboolean found = FALSE;
819 while (iter && !found) {
820 account_name = (const gchar*)list_sorted->data;
826 iter = g_slist_next (iter);
829 gchar* result = NULL;
831 result = g_strdup (account_name);
833 modest_account_mgr_free_account_names (account_names);
834 account_names = NULL;
840 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
842 gboolean result = FALSE;
844 gchar* account_name = modest_account_mgr_get_first_account_name(self);
846 result = modest_account_mgr_set_default_account (self, account_name);
847 g_free (account_name);
850 result = TRUE; /* If there are no accounts then it's not a failure. */
856 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name, const gchar *mailbox)
860 g_return_val_if_fail (self, NULL);
861 g_return_val_if_fail (name, NULL);
866 gchar *transport_account;
869 transport_account = modest_account_mgr_get_server_account_name (self,
871 TNY_ACCOUNT_TYPE_TRANSPORT);
872 if (transport_account) {
873 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
875 ModestProtocol *protocol =
876 modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
877 MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
879 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
880 from = modest_account_protocol_get_from (MODEST_ACCOUNT_PROTOCOL (protocol),
891 gchar *fullname, *email;
893 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
895 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
897 from = g_strdup_printf ("%s <%s>",
898 fullname ? fullname : "",
907 /* Add a number to the end of the text, or increment a number that is already there.
910 util_increment_name (const gchar* text)
912 g_return_val_if_fail (text, NULL);
914 /* Get the end character,
915 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
917 const gchar* end = NULL;
918 if (!g_utf8_validate (text, -1, &end))
924 --end; /* Go to before the null-termination. */
926 /* Look at each UTF-8 characer, starting at the end: */
927 const gchar* p = end;
928 const gchar* alpha_end = NULL;
931 /* Stop when we reach the first character that is not a numeric digit: */
932 const gunichar ch = g_utf8_get_char (p);
933 if (!g_unichar_isdigit (ch)) {
938 p = g_utf8_find_prev_char (text, p);
942 /* The text must consist completely of numeric digits. */
948 /* Intepret and increment the number, if any: */
949 gint num = atol (alpha_end);
952 /* Get the name part: */
953 gint name_len = alpha_end - text;
954 gchar *name_without_number = g_malloc(name_len + 1);
955 memcpy (name_without_number, text, name_len);
956 name_without_number[name_len] = 0;\
958 /* Concatenate the text part and the new number: */
959 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
960 g_free (name_without_number);
966 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
967 gboolean server_account)
969 gchar *account_name = g_strdup (starting_name);
971 while (modest_account_mgr_account_exists (self,
972 account_name, server_account /* server_account */)) {
974 gchar * account_name2 = util_increment_name (account_name);
975 g_free (account_name);
976 account_name = account_name2;
983 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
985 gchar *account_name = g_strdup (starting_name);
987 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
989 gchar * account_name2 = util_increment_name (account_name);
990 g_free (account_name);
991 account_name = account_name2;
998 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
999 const gchar *account_name,
1000 gboolean leave_on_server)
1002 modest_account_mgr_set_bool (self,
1004 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1010 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
1011 const gchar* account_name)
1013 return modest_account_mgr_get_bool (self,
1015 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1020 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
1021 const gchar* account_name)
1023 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
1025 MODEST_ACCOUNT_LAST_UPDATED,
1030 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
1031 const gchar* account_name,
1034 modest_account_mgr_set_int (self,
1036 MODEST_ACCOUNT_LAST_UPDATED,
1040 /* TODO: notify about changes */
1044 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
1045 const gchar* account_name)
1047 return modest_account_mgr_get_int (self,
1049 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1054 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
1055 const gchar* account_name,
1056 gint limit_retrieve)
1058 modest_account_mgr_set_int (self,
1060 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1062 FALSE /* not server account */);
1066 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
1067 const gchar* account_name)
1069 return modest_account_mgr_get_int (self,
1071 MODEST_ACCOUNT_PORT,
1076 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
1077 const gchar *account_name,
1080 modest_account_mgr_set_int (self,
1082 MODEST_ACCOUNT_PORT,
1083 port_num, TRUE /* server account */);
1087 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
1088 const gchar *account_name,
1089 TnyAccountType account_type)
1091 return modest_account_mgr_get_string (self,
1093 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
1094 MODEST_ACCOUNT_STORE_ACCOUNT :
1095 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
1099 static const gchar *
1100 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1102 switch(retrieve_type) {
1103 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1104 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1106 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1107 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1109 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1110 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1113 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1117 static ModestAccountRetrieveType
1118 get_retrieve_type (const gchar *name)
1120 if (!name || name[0] == 0)
1121 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1122 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1123 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1124 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1125 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1127 /* we fall back to headers only */
1128 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1132 ModestAccountRetrieveType
1133 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
1134 const gchar *account_name)
1137 ModestAccountRetrieveType result;
1139 string = modest_account_mgr_get_string (self,
1141 MODEST_ACCOUNT_RETRIEVE,
1142 FALSE /* not server account */);
1143 result = get_retrieve_type (string);
1150 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
1151 const gchar *account_name,
1152 ModestAccountRetrieveType retrieve_type)
1154 modest_account_mgr_set_string (self,
1156 MODEST_ACCOUNT_RETRIEVE,
1157 get_retrieve_type_name (retrieve_type),
1158 FALSE /* not server account */);
1163 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1164 const gchar *account_name,
1165 const gchar *fullname)
1167 modest_account_mgr_set_string (self,
1169 MODEST_ACCOUNT_FULLNAME,
1171 FALSE /* not server account */);
1175 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1176 const gchar *account_name,
1179 modest_account_mgr_set_string (self,
1181 MODEST_ACCOUNT_EMAIL,
1183 FALSE /* not server account */);