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>
38 static const gchar * null_means_empty (const gchar * str);
41 null_means_empty (const gchar * str)
43 return str ? str : "";
47 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
50 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
55 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
57 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
60 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
61 const gchar* signature, gboolean use_signature)
63 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
64 use_signature, FALSE);
65 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
66 null_means_empty (signature), FALSE);
71 modest_account_mgr_get_signature (ModestAccountMgr *self,
73 gboolean* use_signature)
76 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
78 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
81 ModestProtocolType modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
83 ModestProtocolType result = MODEST_PROTOCOLS_STORE_POP; /* Arbitrary default */
85 gchar *server_account_name = modest_account_mgr_get_string (self, name,
86 MODEST_ACCOUNT_STORE_ACCOUNT,
88 if (server_account_name) {
89 ModestServerAccountSettings* server_settings =
90 modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
91 result = modest_server_account_settings_get_protocol (server_settings);
93 g_object_unref (server_settings);
95 g_free (server_account_name);
103 modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
104 const gchar* connection_id,
105 const gchar* server_account_name)
107 modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
109 ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
111 gboolean result = TRUE;
113 GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
114 MODEST_CONF_VALUE_STRING, &err);
116 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
121 /* The server account is in the item after the connection name: */
122 list = g_slist_append (list, g_strdup (connection_id));
123 list = g_slist_append (list, g_strdup (server_account_name));
125 /* Reset the changed list: */
126 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
127 MODEST_CONF_VALUE_STRING, &err);
129 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
137 g_slist_foreach (list, (GFunc) g_free, NULL);
145 * modest_account_mgr_remove_connection_specific_smtp
146 * @self: a ModestAccountMgr instance
147 * @name: the account name
148 * @connection_id: A libconic IAP connection id
150 * Disassacoiate a server account to use with the specific connection for this account.
152 * Returns: TRUE if it worked, FALSE otherwise
155 modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
156 const gchar* connection_id)
158 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
160 gboolean result = TRUE;
162 GSList *list = modest_conf_get_list (priv->modest_conf,
163 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
164 MODEST_CONF_VALUE_STRING, &err);
166 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
175 /* The server account is in the item after the connection name: */
176 GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
177 if (list_connection) {
178 GSList *account_node = g_slist_next (list_connection);
179 /* remove both items: */
180 list = g_slist_delete_link(list, list_connection);
181 list = g_slist_delete_link(list, account_node);
184 /* Reset the changed list: */
185 modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
186 MODEST_CONF_VALUE_STRING, &err);
188 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
195 g_slist_foreach (list, (GFunc) g_free, NULL);
203 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
205 return modest_account_mgr_get_bool (self, account_name,
206 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
209 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
212 return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP,
217 * modest_account_mgr_get_connection_specific_smtp
218 * @self: a ModestAccountMgr instance
219 * @connection_id: A libconic IAP connection id
221 * Retrieve a server account to use with this specific connection for this account.
223 * Returns: a server account name to use for this connection, or NULL if none is specified.
225 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* connection_id)
227 gchar *result = NULL;
229 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
232 GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
233 MODEST_CONF_VALUE_STRING, &err);
235 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
243 /* The server account is in the item after the connection name: */
246 const gchar* this_connection_id = (const gchar*)(iter->data);
247 if (strcmp (this_connection_id, connection_id) == 0) {
248 iter = g_slist_next (iter);
251 const gchar* account_name = (const gchar*)(iter->data);
253 result = g_strdup (account_name);
259 /* Skip 2 to go to the next connection in the list: */
260 iter = g_slist_next (iter);
262 iter = g_slist_next (iter);
267 g_slist_foreach (list, (GFunc) g_free, NULL);
275 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
277 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
278 TRUE /* server account */);
282 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name,
283 const gchar* username)
285 /* Note that this won't work properly as long as the gconf cache is broken
287 gchar *existing_username = modest_account_mgr_get_server_account_username(self,
290 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
291 username, TRUE /* server account */);
293 /* We don't know anything about new usernames: */
294 if (strcmp (existing_username, username) != 0)
295 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
297 g_free (existing_username);
301 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
303 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
304 TRUE /* server account */);
308 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self,
309 const gchar* account_name,
312 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
313 succeeded, TRUE /* server account */);
317 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
319 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
320 TRUE /* server account */);
324 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
326 gboolean result = FALSE;
327 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
328 TRUE /* server account */);
329 if (password && strlen (password)) {
333 bzero (password, strlen (password));
342 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
343 const gchar* account_name)
345 return modest_account_mgr_get_string (self,
347 MODEST_ACCOUNT_HOSTNAME,
348 TRUE /* server account */);
352 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
353 const gchar *server_account_name,
354 const gchar *hostname)
356 modest_account_mgr_set_string (self,
358 MODEST_ACCOUNT_HOSTNAME,
360 TRUE /* server account */);
366 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
367 const gchar* account_name)
369 ModestProtocolRegistry *protocol_registry;
370 ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
373 protocol_registry = modest_runtime_get_protocol_registry ();
374 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
375 TRUE /* server account */);
377 ModestProtocol *protocol;
379 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
383 result = modest_protocol_get_type_id (protocol);
392 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
393 const gchar* account_name, ModestProtocolType secure_auth)
395 const gchar* str_value;
396 ModestProtocolRegistry *protocol_registry;
397 ModestProtocol *protocol;
399 /* Get the conf string for the protocol: */
400 protocol_registry = modest_runtime_get_protocol_registry ();
401 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
402 str_value = modest_protocol_get_name (protocol);
404 /* Set it in the configuration: */
405 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
409 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
410 const gchar* account_name)
412 ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
415 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
416 TRUE /* server account */);
418 ModestProtocolRegistry *protocol_registry;
419 ModestProtocol *protocol;
421 protocol_registry = modest_runtime_get_protocol_registry ();
422 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
423 MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
428 result = modest_protocol_get_type_id (protocol);
435 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
436 const gchar* account_name,
437 ModestProtocolType security)
439 const gchar* str_value;
440 ModestProtocolRegistry *protocol_registry;
441 ModestProtocol *protocol;
443 /* Get the conf string for the protocol type: */
444 protocol_registry = modest_runtime_get_protocol_registry ();
445 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
446 str_value = modest_protocol_get_name (protocol);
448 /* Set it in the configuration: */
449 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
452 ModestServerAccountSettings *
453 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
455 ModestServerAccountSettings *settings = NULL;
456 ModestProtocol *protocol;
457 ModestProtocolRegistry *registry;
460 if (!modest_account_mgr_account_exists (self, name, TRUE)) {
461 g_message ("%s account %s does not exist", __FUNCTION__, name);
465 registry = modest_runtime_get_protocol_registry ();
466 settings = modest_server_account_settings_new ();
468 modest_server_account_settings_set_account_name (settings, name);
470 string = modest_account_mgr_get_string (self, name,
471 MODEST_ACCOUNT_HOSTNAME,TRUE);
473 modest_server_account_settings_set_hostname (settings, string);
479 string = modest_account_mgr_get_string (self, name,
480 MODEST_ACCOUNT_USERNAME,TRUE);
482 modest_server_account_settings_set_username (settings, string);
488 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
491 if (is_transport_and_not_store) {
492 tag = MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS;
494 tag = MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS;
496 protocol = modest_protocol_registry_get_protocol_by_name (registry, tag, string);
498 modest_server_account_settings_set_protocol (settings,
499 modest_protocol_get_type_id (protocol));
505 modest_server_account_settings_set_port (settings,
506 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
508 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
510 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, string);
511 modest_server_account_settings_set_auth_protocol (settings,
512 modest_protocol_get_type_id (protocol));
515 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
518 string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
520 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, string);
521 modest_server_account_settings_set_security_protocol (settings,
522 modest_protocol_get_type_id (protocol));
525 modest_server_account_settings_set_security_protocol (settings,
526 MODEST_PROTOCOLS_CONNECTION_NONE);
529 string = modest_account_mgr_get_string (self, name,
530 MODEST_ACCOUNT_PASSWORD, TRUE);
532 modest_server_account_settings_set_password (settings, string);
536 string = modest_account_mgr_get_string (self, name,
537 MODEST_ACCOUNT_URI, TRUE);
539 modest_server_account_settings_set_uri (settings, string);
547 g_object_unref (settings);
552 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
553 ModestServerAccountSettings *settings)
555 gboolean has_errors = FALSE;
556 const gchar *account_name;
557 const gchar *protocol_name;
559 ModestProtocolRegistry *protocol_registry;
560 ModestProtocol *protocol;
562 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
563 protocol_registry = modest_runtime_get_protocol_registry ();
564 account_name = modest_server_account_settings_get_account_name (settings);
566 /* if we don't have a valid account name we cannot save */
567 g_return_val_if_fail (account_name, FALSE);
569 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
570 modest_server_account_settings_get_protocol (settings));
571 protocol_name = modest_protocol_get_name (protocol);
572 uri = modest_server_account_settings_get_uri (settings);
574 const gchar *hostname;
575 const gchar *username;
576 const gchar *password;
578 const gchar *auth_protocol_name;
579 const gchar *security_name;
581 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
582 username = null_means_empty (modest_server_account_settings_get_username (settings));
583 password = null_means_empty (modest_server_account_settings_get_password (settings));
584 port = modest_server_account_settings_get_port (settings);
585 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
586 modest_server_account_settings_get_auth_protocol (settings));
587 auth_protocol_name = modest_protocol_get_name (protocol);
588 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
589 modest_server_account_settings_get_security_protocol (settings));
590 security_name = modest_protocol_get_name (protocol);
592 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
595 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
598 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
601 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
602 protocol_name, TRUE));
604 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
607 (has_errors = !modest_account_mgr_set_string (self, account_name,
608 MODEST_ACCOUNT_AUTH_MECH,
609 auth_protocol_name, TRUE));
611 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
615 const gchar *uri = modest_server_account_settings_get_uri (settings);
616 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
619 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
620 protocol_name, TRUE));
628 ModestAccountSettings *
629 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
632 ModestAccountSettings *settings;
634 gchar *server_account;
635 gchar *default_account;
636 gboolean use_signature = FALSE;
638 g_return_val_if_fail (self, NULL);
639 g_return_val_if_fail (name, NULL);
641 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
642 /* For instance, maybe you are mistakenly checking for a server account name? */
643 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
647 settings = modest_account_settings_new ();
649 modest_account_settings_set_account_name (settings, name);
651 string = modest_account_mgr_get_string (self, name,
652 MODEST_ACCOUNT_DISPLAY_NAME,
654 modest_account_settings_set_display_name (settings, string);
657 string = modest_account_mgr_get_string (self, name,
658 MODEST_ACCOUNT_FULLNAME,
660 modest_account_settings_set_fullname (settings, string);
663 string = modest_account_mgr_get_string (self, name,
664 MODEST_ACCOUNT_EMAIL,
666 modest_account_settings_set_email_address (settings, string);
669 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
670 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
671 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
673 default_account = modest_account_mgr_get_default_account (self);
674 modest_account_settings_set_is_default (settings,
675 (default_account && strcmp (default_account, name) == 0));
676 g_free (default_account);
678 string = modest_account_mgr_get_signature (self, name, &use_signature);
679 modest_account_settings_set_use_signature (settings, use_signature);
680 modest_account_settings_set_signature (settings, string);
683 modest_account_settings_set_leave_messages_on_server
684 (settings, modest_account_mgr_get_leave_on_server (self, name));
685 modest_account_settings_set_use_connection_specific_smtp
686 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
689 server_account = modest_account_mgr_get_string (self, name,
690 MODEST_ACCOUNT_STORE_ACCOUNT,
692 if (server_account) {
693 ModestServerAccountSettings *store_settings;
694 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
695 g_free (server_account);
697 /* It could happen that the account data is corrupted
698 so it's not loaded properly */
699 if (store_settings) {
700 modest_account_settings_set_store_settings (settings,
702 g_object_unref (store_settings);
704 g_message ("%s can not load server settings. Account corrupted?", __FUNCTION__);
705 g_object_unref (settings);
711 server_account = modest_account_mgr_get_string (self, name,
712 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
714 if (server_account) {
715 ModestServerAccountSettings *transport_settings;
716 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
717 g_free (server_account);
719 if (transport_settings) {
720 modest_account_settings_set_transport_settings (settings, transport_settings);
721 g_object_unref (transport_settings);
723 g_message ("%s can not load server settings. Account corrupted?", __FUNCTION__);
724 g_object_unref (settings);
733 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
734 ModestAccountSettings *settings)
736 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
737 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
739 const gchar *account_name;
740 const gchar *store_account_name;
741 const gchar *transport_account_name;
742 ModestServerAccountSettings *store_settings;
743 ModestServerAccountSettings *transport_settings;
745 account_name = modest_account_settings_get_account_name (settings);
746 g_return_if_fail (account_name != NULL);
748 modest_account_mgr_set_display_name (mgr, account_name,
749 modest_account_settings_get_display_name (settings));
750 modest_account_mgr_set_user_fullname (mgr, account_name,
751 modest_account_settings_get_fullname (settings));
752 modest_account_mgr_set_user_email (mgr, account_name,
753 modest_account_settings_get_email_address (settings));
754 modest_account_mgr_set_retrieve_type (mgr, account_name,
755 modest_account_settings_get_retrieve_type (settings));
756 modest_account_mgr_set_retrieve_limit (mgr, account_name,
757 modest_account_settings_get_retrieve_limit (settings));
758 modest_account_mgr_set_leave_on_server (mgr, account_name,
759 modest_account_settings_get_leave_messages_on_server (settings));
760 modest_account_mgr_set_signature (mgr, account_name,
761 modest_account_settings_get_signature (settings),
762 modest_account_settings_get_use_signature (settings));
763 modest_account_mgr_set_use_connection_specific_smtp
765 modest_account_settings_get_use_connection_specific_smtp (settings));
767 store_settings = modest_account_settings_get_store_settings (settings);
768 store_account_name = modest_server_account_settings_get_account_name (store_settings);
769 if (store_settings != NULL) {
770 modest_account_mgr_save_server_settings (mgr, store_settings);
772 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT, store_account_name, FALSE);
773 g_object_unref (store_settings);
775 transport_settings = modest_account_settings_get_transport_settings (settings);
776 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
777 if (transport_settings != NULL) {
778 modest_account_mgr_save_server_settings (mgr, transport_settings);
780 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT, transport_account_name, FALSE);
781 g_object_unref (transport_settings);
782 modest_account_mgr_set_enabled (mgr, account_name, TRUE);
787 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
789 return g_utf8_collate((const gchar*)a, (const gchar*)b);
792 /** Get the first one, alphabetically, by title. */
794 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
796 const gchar* account_name = NULL;
797 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
799 /* Return TRUE if there is no account */
803 /* Get the first one, alphabetically, by title: */
804 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
805 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
807 GSList* iter = list_sorted;
808 gboolean found = FALSE;
809 while (iter && !found) {
810 account_name = (const gchar*)list_sorted->data;
816 iter = g_slist_next (iter);
819 gchar* result = NULL;
821 result = g_strdup (account_name);
823 modest_account_mgr_free_account_names (account_names);
824 account_names = NULL;
830 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
832 gboolean result = FALSE;
834 gchar* account_name = modest_account_mgr_get_first_account_name(self);
836 result = modest_account_mgr_set_default_account (self, account_name);
837 g_free (account_name);
840 result = TRUE; /* If there are no accounts then it's not a failure. */
846 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
848 gchar *fullname, *email, *from;
850 g_return_val_if_fail (self, NULL);
851 g_return_val_if_fail (name, NULL);
853 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
855 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
857 from = g_strdup_printf ("%s <%s>",
858 fullname ? fullname : "",
866 /* Add a number to the end of the text, or increment a number that is already there.
869 util_increment_name (const gchar* text)
871 g_return_val_if_fail (text, NULL);
873 /* Get the end character,
874 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
876 const gchar* end = NULL;
877 if (!g_utf8_validate (text, -1, &end))
883 --end; /* Go to before the null-termination. */
885 /* Look at each UTF-8 characer, starting at the end: */
886 const gchar* p = end;
887 const gchar* alpha_end = NULL;
890 /* Stop when we reach the first character that is not a numeric digit: */
891 const gunichar ch = g_utf8_get_char (p);
892 if (!g_unichar_isdigit (ch)) {
897 p = g_utf8_find_prev_char (text, p);
901 /* The text must consist completely of numeric digits. */
907 /* Intepret and increment the number, if any: */
908 gint num = atol (alpha_end);
911 /* Get the name part: */
912 gint name_len = alpha_end - text;
913 gchar *name_without_number = g_malloc(name_len + 1);
914 memcpy (name_without_number, text, name_len);
915 name_without_number[name_len] = 0;\
917 /* Concatenate the text part and the new number: */
918 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
919 g_free (name_without_number);
925 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
926 gboolean server_account)
928 gchar *account_name = g_strdup (starting_name);
930 while (modest_account_mgr_account_exists (self,
931 account_name, server_account /* server_account */)) {
933 gchar * account_name2 = util_increment_name (account_name);
934 g_free (account_name);
935 account_name = account_name2;
942 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
944 gchar *account_name = g_strdup (starting_name);
946 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
948 gchar * account_name2 = util_increment_name (account_name);
949 g_free (account_name);
950 account_name = account_name2;
957 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
958 const gchar *account_name,
959 gboolean leave_on_server)
961 modest_account_mgr_set_bool (self,
963 MODEST_ACCOUNT_LEAVE_ON_SERVER,
969 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
970 const gchar* account_name)
972 return modest_account_mgr_get_bool (self,
974 MODEST_ACCOUNT_LEAVE_ON_SERVER,
979 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
980 const gchar* account_name)
982 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
984 MODEST_ACCOUNT_LAST_UPDATED,
989 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
990 const gchar* account_name,
993 modest_account_mgr_set_int (self,
995 MODEST_ACCOUNT_LAST_UPDATED,
999 /* TODO: notify about changes */
1003 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
1004 const gchar* account_name)
1006 return modest_account_mgr_get_int (self,
1008 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1013 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
1014 const gchar* account_name,
1015 gint limit_retrieve)
1017 modest_account_mgr_set_int (self,
1019 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1021 FALSE /* not server account */);
1025 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
1026 const gchar* account_name)
1028 return modest_account_mgr_get_int (self,
1030 MODEST_ACCOUNT_PORT,
1035 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
1036 const gchar *account_name,
1039 modest_account_mgr_set_int (self,
1041 MODEST_ACCOUNT_PORT,
1042 port_num, TRUE /* server account */);
1046 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
1047 const gchar *account_name,
1048 TnyAccountType account_type)
1050 return modest_account_mgr_get_string (self,
1052 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
1053 MODEST_ACCOUNT_STORE_ACCOUNT :
1054 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
1058 static const gchar *
1059 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1061 switch(retrieve_type) {
1062 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1063 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1065 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1066 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1068 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1069 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1072 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1076 static ModestAccountRetrieveType
1077 get_retrieve_type (const gchar *name)
1079 if (!name || name[0] == 0)
1080 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1081 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1082 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1083 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1084 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1086 /* we fall back to headers only */
1087 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1091 ModestAccountRetrieveType
1092 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
1093 const gchar *account_name)
1096 ModestAccountRetrieveType result;
1098 string = modest_account_mgr_get_string (self,
1100 MODEST_ACCOUNT_RETRIEVE,
1101 FALSE /* not server account */);
1102 result = get_retrieve_type (string);
1109 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
1110 const gchar *account_name,
1111 ModestAccountRetrieveType retrieve_type)
1113 modest_account_mgr_set_string (self,
1115 MODEST_ACCOUNT_RETRIEVE,
1116 get_retrieve_type_name (retrieve_type),
1117 FALSE /* not server account */);
1122 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1123 const gchar *account_name,
1124 const gchar *fullname)
1126 modest_account_mgr_set_string (self,
1128 MODEST_ACCOUNT_FULLNAME,
1130 FALSE /* not server account */);
1134 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1135 const gchar *account_name,
1138 modest_account_mgr_set_string (self,
1140 MODEST_ACCOUNT_EMAIL,
1142 FALSE /* not server account */);