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>
38 #include <modest-utils.h>
39 #include <modest-platform.h>
41 static const gchar * null_means_empty (const gchar * str);
44 null_means_empty (const gchar * str)
46 return str ? str : "";
50 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
54 result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
57 modest_platform_emit_account_created_signal (name);
59 modest_platform_emit_account_removed_signal (name);
67 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
69 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
72 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
73 const gchar* signature, gboolean use_signature)
75 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
76 use_signature, FALSE);
77 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
78 null_means_empty (signature), FALSE);
83 modest_account_mgr_get_signature (ModestAccountMgr *self,
85 gboolean* use_signature)
88 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
90 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
93 ModestProtocolType modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
95 ModestProtocolType result = MODEST_PROTOCOLS_STORE_POP; /* Arbitrary default */
97 gchar *server_account_name = modest_account_mgr_get_string (self, name,
98 MODEST_ACCOUNT_STORE_ACCOUNT,
100 if (server_account_name) {
101 ModestServerAccountSettings* server_settings =
102 modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
103 result = modest_server_account_settings_get_protocol (server_settings);
105 g_object_unref (server_settings);
107 g_free (server_account_name);
115 modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
116 const gchar* connection_id,
117 const gchar* server_account_name)
119 modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
121 ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
123 gboolean result = TRUE;
125 GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
126 MODEST_CONF_VALUE_STRING, &err);
128 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
133 /* The server account is in the item after the connection name: */
134 list = g_slist_append (list, g_strdup (connection_id));
135 list = g_slist_append (list, g_strdup (server_account_name));
137 /* Reset the changed list: */
138 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
139 MODEST_CONF_VALUE_STRING, &err);
141 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
149 g_slist_foreach (list, (GFunc) g_free, NULL);
157 * modest_account_mgr_remove_connection_specific_smtp
158 * @self: a ModestAccountMgr instance
159 * @name: the account name
160 * @connection_id: A libconic IAP connection id
162 * Disassacoiate a server account to use with the specific connection for this account.
164 * Returns: TRUE if it worked, FALSE otherwise
167 modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
168 const gchar* connection_id)
170 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
172 gboolean result = TRUE;
174 GSList *list = modest_conf_get_list (priv->modest_conf,
175 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
176 MODEST_CONF_VALUE_STRING, &err);
178 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
187 /* The server account is in the item after the connection name: */
188 GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
189 if (list_connection) {
190 GSList *account_node = g_slist_next (list_connection);
191 /* remove both items: */
192 list = g_slist_delete_link(list, list_connection);
193 list = g_slist_delete_link(list, account_node);
196 /* Reset the changed list: */
197 modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
198 MODEST_CONF_VALUE_STRING, &err);
200 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
207 g_slist_foreach (list, (GFunc) g_free, NULL);
215 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
217 return modest_account_mgr_get_bool (self, account_name,
218 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
221 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
224 return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP,
229 * modest_account_mgr_get_connection_specific_smtp
230 * @self: a ModestAccountMgr instance
231 * @connection_id: A libconic IAP connection id
233 * Retrieve a server account to use with this specific connection for this account.
235 * Returns: a server account name to use for this connection, or NULL if none is specified.
237 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* connection_id)
239 gchar *result = NULL;
241 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
244 GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
245 MODEST_CONF_VALUE_STRING, &err);
247 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
255 /* The server account is in the item after the connection name: */
258 const gchar* this_connection_id = (const gchar*)(iter->data);
259 if (strcmp (this_connection_id, connection_id) == 0) {
260 iter = g_slist_next (iter);
263 const gchar* account_name = (const gchar*)(iter->data);
265 result = g_strdup (account_name);
271 /* Skip 2 to go to the next connection in the list: */
272 iter = g_slist_next (iter);
274 iter = g_slist_next (iter);
279 g_slist_foreach (list, (GFunc) g_free, NULL);
287 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
289 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
290 TRUE /* server account */);
294 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name,
295 const gchar* username)
297 /* Note that this won't work properly as long as the gconf cache is broken
299 gchar *existing_username = modest_account_mgr_get_server_account_username(self,
302 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
303 username, TRUE /* server account */);
305 /* We don't know anything about new usernames: */
306 if (strcmp (existing_username, username) != 0)
307 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
309 g_free (existing_username);
313 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
315 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
316 TRUE /* server account */);
320 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self,
321 const gchar* account_name,
324 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
325 succeeded, TRUE /* server account */);
329 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
331 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
332 TRUE /* server account */);
336 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
338 gboolean result = FALSE;
339 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
340 TRUE /* server account */);
341 if (password && strlen (password)) {
345 bzero (password, strlen (password));
354 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
355 const gchar* account_name)
357 return modest_account_mgr_get_string (self,
359 MODEST_ACCOUNT_HOSTNAME,
360 TRUE /* server account */);
364 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
365 const gchar *server_account_name,
366 const gchar *hostname)
368 modest_account_mgr_set_string (self,
370 MODEST_ACCOUNT_HOSTNAME,
372 TRUE /* server account */);
378 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
379 const gchar* account_name)
381 ModestProtocolRegistry *protocol_registry;
382 ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
385 protocol_registry = modest_runtime_get_protocol_registry ();
386 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
387 TRUE /* server account */);
389 ModestProtocol *protocol;
391 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
395 result = modest_protocol_get_type_id (protocol);
404 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
405 const gchar* account_name, ModestProtocolType secure_auth)
407 const gchar* str_value;
408 ModestProtocolRegistry *protocol_registry;
409 ModestProtocol *protocol;
411 /* Get the conf string for the protocol: */
412 protocol_registry = modest_runtime_get_protocol_registry ();
413 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
414 str_value = modest_protocol_get_name (protocol);
416 /* Set it in the configuration: */
417 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
421 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
422 const gchar* account_name)
424 ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
427 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
428 TRUE /* server account */);
430 ModestProtocolRegistry *protocol_registry;
431 ModestProtocol *protocol;
433 protocol_registry = modest_runtime_get_protocol_registry ();
434 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
435 MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
440 result = modest_protocol_get_type_id (protocol);
447 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
448 const gchar* account_name,
449 ModestProtocolType security)
451 const gchar* str_value;
452 ModestProtocolRegistry *protocol_registry;
453 ModestProtocol *protocol;
455 /* Get the conf string for the protocol type: */
456 protocol_registry = modest_runtime_get_protocol_registry ();
457 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
458 str_value = modest_protocol_get_name (protocol);
460 /* Set it in the configuration: */
461 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
464 ModestServerAccountSettings *
465 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
467 ModestServerAccountSettings *settings = NULL;
468 ModestProtocol *protocol;
469 ModestProtocolRegistry *registry;
470 gchar *hostname, *username, *pwd, *uri, *proto, *auth, *sec;
472 if (!modest_account_mgr_account_exists (self, name, TRUE)) {
473 g_warning ("%s account %s does not exist", __FUNCTION__, name);
477 registry = modest_runtime_get_protocol_registry ();
478 settings = modest_server_account_settings_new ();
480 modest_server_account_settings_set_account_name (settings, name);
482 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
485 if (is_transport_and_not_store) {
486 tag = MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS;
488 tag = MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS;
490 protocol = modest_protocol_registry_get_protocol_by_name (registry, tag, proto);
492 modest_server_account_settings_set_protocol (settings,
493 modest_protocol_get_type_id (protocol));
499 modest_server_account_settings_set_port (settings,
500 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
502 auth = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
504 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, auth);
505 modest_server_account_settings_set_auth_protocol (settings,
506 modest_protocol_get_type_id (protocol));
509 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
512 sec = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
514 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, sec);
515 modest_server_account_settings_set_security_protocol (settings,
516 modest_protocol_get_type_id (protocol));
519 modest_server_account_settings_set_security_protocol (settings,
520 MODEST_PROTOCOLS_CONNECTION_NONE);
523 /* Username, password and URI. Note that the URI could include
524 the former two, so in this case there is no need to have
526 username = modest_account_mgr_get_string (self, name,
527 MODEST_ACCOUNT_USERNAME,TRUE);
529 modest_server_account_settings_set_username (settings, username);
531 pwd = modest_account_mgr_get_string (self, name,
532 MODEST_ACCOUNT_PASSWORD, TRUE);
534 modest_server_account_settings_set_password (settings, pwd);
538 uri = modest_account_mgr_get_string (self, name,
539 MODEST_ACCOUNT_URI, TRUE);
541 modest_server_account_settings_set_uri (settings, uri);
543 hostname = modest_account_mgr_get_string (self, name,
544 MODEST_ACCOUNT_HOSTNAME,TRUE);
546 modest_server_account_settings_set_hostname (settings, hostname);
549 if (!username || !hostname) {
564 g_object_unref (settings);
569 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
570 ModestServerAccountSettings *settings)
572 gboolean has_errors = FALSE;
573 const gchar *account_name;
574 const gchar *protocol_name;
576 ModestProtocolRegistry *protocol_registry;
577 ModestProtocol *protocol;
579 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
580 protocol_registry = modest_runtime_get_protocol_registry ();
581 account_name = modest_server_account_settings_get_account_name (settings);
583 /* if we don't have a valid account name we cannot save */
584 g_return_val_if_fail (account_name, FALSE);
586 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
587 modest_server_account_settings_get_protocol (settings));
588 protocol_name = modest_protocol_get_name (protocol);
589 uri = modest_server_account_settings_get_uri (settings);
591 const gchar *hostname;
592 const gchar *username;
593 const gchar *password;
595 const gchar *auth_protocol_name;
596 const gchar *security_name;
598 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
599 username = null_means_empty (modest_server_account_settings_get_username (settings));
600 password = null_means_empty (modest_server_account_settings_get_password (settings));
601 port = modest_server_account_settings_get_port (settings);
602 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
603 modest_server_account_settings_get_auth_protocol (settings));
604 auth_protocol_name = modest_protocol_get_name (protocol);
605 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
606 modest_server_account_settings_get_security_protocol (settings));
607 security_name = modest_protocol_get_name (protocol);
609 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
612 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
615 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
618 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
619 protocol_name, TRUE));
621 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
624 (has_errors = !modest_account_mgr_set_string (self, account_name,
625 MODEST_ACCOUNT_AUTH_MECH,
626 auth_protocol_name, TRUE));
628 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
632 const gchar *uri = modest_server_account_settings_get_uri (settings);
633 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
636 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
637 protocol_name, TRUE));
645 ModestAccountSettings *
646 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
649 ModestAccountSettings *settings;
651 gchar *server_account;
652 gchar *default_account;
653 gboolean use_signature = FALSE;
655 g_return_val_if_fail (self, NULL);
656 g_return_val_if_fail (name, NULL);
658 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
659 /* For instance, maybe you are mistakenly checking for a server account name? */
660 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
664 settings = modest_account_settings_new ();
666 modest_account_settings_set_account_name (settings, name);
668 string = modest_account_mgr_get_string (self, name,
669 MODEST_ACCOUNT_DISPLAY_NAME,
671 modest_account_settings_set_display_name (settings, string);
674 string = modest_account_mgr_get_string (self, name,
675 MODEST_ACCOUNT_FULLNAME,
677 modest_account_settings_set_fullname (settings, string);
680 string = modest_account_mgr_get_string (self, name,
681 MODEST_ACCOUNT_EMAIL,
683 modest_account_settings_set_email_address (settings, string);
686 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
687 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
688 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
690 default_account = modest_account_mgr_get_default_account (self);
691 modest_account_settings_set_is_default (settings,
692 (default_account && strcmp (default_account, name) == 0));
693 g_free (default_account);
695 string = modest_account_mgr_get_signature (self, name, &use_signature);
696 modest_account_settings_set_use_signature (settings, use_signature);
697 modest_account_settings_set_signature (settings, string);
700 modest_account_settings_set_leave_messages_on_server
701 (settings, modest_account_mgr_get_leave_on_server (self, name));
702 modest_account_settings_set_use_connection_specific_smtp
703 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
706 server_account = modest_account_mgr_get_string (self, name,
707 MODEST_ACCOUNT_STORE_ACCOUNT,
709 if (server_account) {
710 ModestServerAccountSettings *store_settings;
711 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
712 g_free (server_account);
714 /* It could happen that the account data is corrupted
715 so it's not loaded properly */
716 if (store_settings) {
717 modest_account_settings_set_store_settings (settings,
719 g_object_unref (store_settings);
721 g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
722 g_object_unref (settings);
728 server_account = modest_account_mgr_get_string (self, name,
729 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
731 if (server_account) {
732 ModestServerAccountSettings *transport_settings;
733 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
734 g_free (server_account);
736 if (transport_settings) {
737 modest_account_settings_set_transport_settings (settings, transport_settings);
738 g_object_unref (transport_settings);
740 g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
741 g_object_unref (settings);
750 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
751 ModestAccountSettings *settings)
753 const gchar *account_name;
754 ModestServerAccountSettings *store_settings;
755 ModestServerAccountSettings *transport_settings;
757 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
758 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
760 account_name = modest_account_settings_get_account_name (settings);
761 g_return_if_fail (account_name != NULL);
763 modest_account_mgr_set_display_name (mgr, account_name,
764 modest_account_settings_get_display_name (settings));
765 modest_account_mgr_set_user_fullname (mgr, account_name,
766 modest_account_settings_get_fullname (settings));
767 modest_account_mgr_set_user_email (mgr, account_name,
768 modest_account_settings_get_email_address (settings));
769 modest_account_mgr_set_retrieve_type (mgr, account_name,
770 modest_account_settings_get_retrieve_type (settings));
771 modest_account_mgr_set_retrieve_limit (mgr, account_name,
772 modest_account_settings_get_retrieve_limit (settings));
773 modest_account_mgr_set_leave_on_server (mgr, account_name,
774 modest_account_settings_get_leave_messages_on_server (settings));
775 modest_account_mgr_set_signature (mgr, account_name,
776 modest_account_settings_get_signature (settings),
777 modest_account_settings_get_use_signature (settings));
778 modest_account_mgr_set_use_connection_specific_smtp
780 modest_account_settings_get_use_connection_specific_smtp (settings));
782 store_settings = modest_account_settings_get_store_settings (settings);
783 if (store_settings) {
784 const gchar *store_account_name;
785 store_account_name = modest_server_account_settings_get_account_name (store_settings);
786 if (store_account_name)
787 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT,
788 store_account_name, FALSE);
789 modest_account_mgr_save_server_settings (mgr, store_settings);
790 g_object_unref (store_settings);
793 transport_settings = modest_account_settings_get_transport_settings (settings);
794 if (transport_settings) {
795 const gchar *transport_account_name;
796 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
797 if (transport_account_name)
798 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
799 transport_account_name, FALSE);
800 modest_account_mgr_save_server_settings (mgr, transport_settings);
801 g_object_unref (transport_settings);
803 modest_account_mgr_set_bool (mgr, account_name, MODEST_ACCOUNT_ENABLED, TRUE,FALSE);
808 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
810 return g_utf8_collate((const gchar*)a, (const gchar*)b);
813 /** Get the first one, alphabetically, by title. */
815 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
817 const gchar* account_name = NULL;
818 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
820 /* Return TRUE if there is no account */
824 /* Get the first one, alphabetically, by title: */
825 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
826 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
828 GSList* iter = list_sorted;
829 gboolean found = FALSE;
830 while (iter && !found) {
831 account_name = (const gchar*)list_sorted->data;
837 iter = g_slist_next (iter);
840 gchar* result = NULL;
842 result = g_strdup (account_name);
844 modest_account_mgr_free_account_names (account_names);
845 account_names = NULL;
851 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
853 gboolean result = FALSE;
855 gchar* account_name = modest_account_mgr_get_first_account_name(self);
857 result = modest_account_mgr_set_default_account (self, account_name);
858 g_free (account_name);
861 result = TRUE; /* If there are no accounts then it's not a failure. */
867 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name, const gchar *mailbox)
870 gchar *transport_account;
873 g_return_val_if_fail (self, NULL);
874 g_return_val_if_fail (name, NULL);
878 transport_account = modest_account_mgr_get_server_account_name (self,
880 TNY_ACCOUNT_TYPE_TRANSPORT);
881 if (transport_account) {
882 proto = modest_account_mgr_get_string (self, transport_account, MODEST_ACCOUNT_PROTO, TRUE);
884 ModestProtocol *protocol =
885 modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
886 MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
888 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
889 from = modest_account_protocol_get_from (MODEST_ACCOUNT_PROTOCOL (protocol),
898 gchar *fullname, *email;
900 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
902 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
904 from = g_strdup_printf ("%s <%s>",
905 fullname ? fullname : "",
914 /* Add a number to the end of the text, or increment a number that is already there.
917 util_increment_name (const gchar* text)
919 g_return_val_if_fail (text, NULL);
921 /* Get the end character,
922 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
924 const gchar* end = NULL;
925 if (!g_utf8_validate (text, -1, &end))
931 --end; /* Go to before the null-termination. */
933 /* Look at each UTF-8 characer, starting at the end: */
934 const gchar* p = end;
935 const gchar* alpha_end = NULL;
938 /* Stop when we reach the first character that is not a numeric digit: */
939 const gunichar ch = g_utf8_get_char (p);
940 if (!g_unichar_isdigit (ch)) {
945 p = g_utf8_find_prev_char (text, p);
949 /* The text must consist completely of numeric digits. */
955 /* Intepret and increment the number, if any: */
956 gint num = atol (alpha_end);
959 /* Get the name part: */
960 gint name_len = alpha_end - text;
961 gchar *name_without_number = g_malloc(name_len + 1);
962 memcpy (name_without_number, text, name_len);
963 name_without_number[name_len] = 0;\
965 /* Concatenate the text part and the new number: */
966 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
967 g_free (name_without_number);
973 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
974 gboolean server_account)
976 gchar *account_name = g_strdup (starting_name);
978 while (modest_account_mgr_account_exists (self,
979 account_name, server_account /* server_account */)) {
981 gchar * account_name2 = util_increment_name (account_name);
982 g_free (account_name);
983 account_name = account_name2;
990 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
992 gchar *account_name = g_strdup (starting_name);
994 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
996 gchar * account_name2 = util_increment_name (account_name);
997 g_free (account_name);
998 account_name = account_name2;
1001 return account_name;
1005 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
1006 const gchar *account_name,
1007 gboolean leave_on_server)
1009 modest_account_mgr_set_bool (self,
1011 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1017 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
1018 const gchar* account_name)
1020 return modest_account_mgr_get_bool (self,
1022 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1027 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
1028 const gchar* account_name)
1030 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
1032 MODEST_ACCOUNT_LAST_UPDATED,
1037 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
1038 const gchar* account_name,
1041 modest_account_mgr_set_int (self,
1043 MODEST_ACCOUNT_LAST_UPDATED,
1047 /* TODO: notify about changes */
1051 modest_account_mgr_get_has_new_mails (ModestAccountMgr *self,
1052 const gchar* account_name)
1054 return modest_account_mgr_get_bool (modest_runtime_get_account_mgr (),
1056 MODEST_ACCOUNT_HAS_NEW_MAILS,
1061 modest_account_mgr_set_has_new_mails (ModestAccountMgr *self,
1062 const gchar* account_name,
1063 gboolean has_new_mails)
1065 modest_account_mgr_set_bool (self,
1067 MODEST_ACCOUNT_HAS_NEW_MAILS,
1071 /* TODO: notify about changes */
1075 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
1076 const gchar* account_name)
1078 return modest_account_mgr_get_int (self,
1080 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1085 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
1086 const gchar* account_name,
1087 gint limit_retrieve)
1089 modest_account_mgr_set_int (self,
1091 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1093 FALSE /* not server account */);
1097 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
1098 const gchar* account_name)
1100 return modest_account_mgr_get_int (self,
1102 MODEST_ACCOUNT_PORT,
1107 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
1108 const gchar *account_name,
1111 modest_account_mgr_set_int (self,
1113 MODEST_ACCOUNT_PORT,
1114 port_num, TRUE /* server account */);
1118 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
1119 const gchar *account_name,
1120 TnyAccountType account_type)
1122 return modest_account_mgr_get_string (self,
1124 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
1125 MODEST_ACCOUNT_STORE_ACCOUNT :
1126 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
1130 static const gchar *
1131 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1133 switch(retrieve_type) {
1134 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1135 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1137 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1138 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1140 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1141 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1144 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1148 static ModestAccountRetrieveType
1149 get_retrieve_type (const gchar *name)
1151 if (!name || name[0] == 0)
1152 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1153 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1154 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1155 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1156 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1158 /* we fall back to headers only */
1159 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1163 ModestAccountRetrieveType
1164 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
1165 const gchar *account_name)
1168 ModestAccountRetrieveType result;
1170 string = modest_account_mgr_get_string (self,
1172 MODEST_ACCOUNT_RETRIEVE,
1173 FALSE /* not server account */);
1174 result = get_retrieve_type (string);
1181 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
1182 const gchar *account_name,
1183 ModestAccountRetrieveType retrieve_type)
1185 modest_account_mgr_set_string (self,
1187 MODEST_ACCOUNT_RETRIEVE,
1188 get_retrieve_type_name (retrieve_type),
1189 FALSE /* not server account */);
1194 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1195 const gchar *account_name,
1196 const gchar *fullname)
1198 modest_account_mgr_set_string (self,
1200 MODEST_ACCOUNT_FULLNAME,
1202 FALSE /* not server account */);
1206 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1207 const gchar *account_name,
1210 modest_account_mgr_set_string (self,
1212 MODEST_ACCOUNT_EMAIL,
1214 FALSE /* not server account */);
1218 modest_account_mgr_account_is_multimailbox (ModestAccountMgr *mgr,
1219 const gchar *account_name,
1220 ModestProtocol **mmb_protocol)
1222 gchar *transport_account;
1223 gboolean result = FALSE;
1225 g_return_val_if_fail (account_name, FALSE);
1228 *mmb_protocol = NULL;
1230 transport_account = modest_account_mgr_get_server_account_name (mgr,
1232 TNY_ACCOUNT_TYPE_TRANSPORT);
1233 if (transport_account) {
1235 ModestProtocolRegistry *registry;
1237 registry = modest_runtime_get_protocol_registry ();
1239 proto = modest_account_mgr_get_string (mgr, transport_account,
1240 MODEST_ACCOUNT_PROTO, TRUE);
1241 if (proto != NULL) {
1242 ModestProtocol *protocol =
1243 modest_protocol_registry_get_protocol_by_name (registry,
1244 MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
1247 modest_protocol_registry_protocol_type_has_tag
1249 modest_protocol_get_type_id (protocol),
1250 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS)) {
1252 *mmb_protocol = protocol;
1263 modest_account_mgr_get_signature_from_recipient (ModestAccountMgr *mgr,
1264 const gchar *current_recipient,
1265 gboolean *has_signature)
1267 gchar *result = NULL;
1268 gchar *mailbox = NULL;
1269 gchar *account_name;
1270 ModestProtocol *protocol = NULL;
1272 *has_signature = FALSE;
1274 account_name = modest_utils_get_account_name_from_recipient (current_recipient, &mailbox);
1275 if (modest_account_mgr_account_is_multimailbox (mgr, account_name, &protocol)) {
1276 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1277 result = modest_account_protocol_get_signature (MODEST_ACCOUNT_PROTOCOL (protocol),
1278 account_name, mailbox,
1283 if (result == NULL) {
1284 result = modest_account_mgr_get_signature (mgr,
1285 account_name, has_signature);
1291 modest_account_mgr_get_branding_from_recipient (ModestAccountMgr *mgr,
1292 const gchar *current_recipient,
1293 gchar **service_name,
1294 const GdkPixbuf **service_icon,
1297 gchar *mailbox = NULL;
1298 gchar *account_name;
1299 ModestProtocol *protocol = NULL;
1301 *service_name = NULL;
1302 *service_icon = NULL;
1304 account_name = modest_utils_get_account_name_from_recipient (current_recipient, &mailbox);
1305 if (modest_account_mgr_account_is_multimailbox (mgr, account_name, &protocol)) {
1306 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1308 const GdkPixbuf *icon;
1309 name = modest_account_protocol_get_service_name (MODEST_ACCOUNT_PROTOCOL (protocol),
1310 account_name, mailbox);
1311 icon = modest_account_protocol_get_service_icon (MODEST_ACCOUNT_PROTOCOL (protocol),
1312 account_name, mailbox, icon_size);
1313 *service_name = name;
1314 *service_icon = icon;