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_PROTOCOL_REGISTRY_TYPE_INVALID;
96 gchar *server_account_name = modest_account_mgr_get_string (self, name,
97 MODEST_ACCOUNT_STORE_ACCOUNT,
99 if (server_account_name) {
100 ModestServerAccountSettings* server_settings =
101 modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
103 /* Easy setup wizard precreates accounts without
104 settings so this could happen */
105 if (server_settings) {
106 result = modest_server_account_settings_get_protocol (server_settings);
107 g_object_unref (server_settings);
109 g_free (server_account_name);
117 modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
118 const gchar* connection_id,
119 const gchar* server_account_name)
121 modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
123 ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
125 gboolean result = TRUE;
127 GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
128 MODEST_CONF_VALUE_STRING, &err);
130 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
135 /* The server account is in the item after the connection name: */
136 list = g_slist_append (list, g_strdup (connection_id));
137 list = g_slist_append (list, g_strdup (server_account_name));
139 /* Reset the changed list: */
140 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
141 MODEST_CONF_VALUE_STRING, &err);
143 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
151 g_slist_foreach (list, (GFunc) g_free, NULL);
159 * modest_account_mgr_remove_connection_specific_smtp
160 * @self: a ModestAccountMgr instance
161 * @name: the account name
162 * @connection_id: A libconic IAP connection id
164 * Disassacoiate a server account to use with the specific connection for this account.
166 * Returns: TRUE if it worked, FALSE otherwise
169 modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
170 const gchar* connection_id)
172 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
174 gboolean result = TRUE;
176 GSList *list = modest_conf_get_list (priv->modest_conf,
177 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
178 MODEST_CONF_VALUE_STRING, &err);
180 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
189 /* The server account is in the item after the connection name: */
190 GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
191 if (list_connection) {
192 GSList *account_node = g_slist_next (list_connection);
193 /* remove both items: */
194 list = g_slist_delete_link(list, list_connection);
195 list = g_slist_delete_link(list, account_node);
198 /* Reset the changed list: */
199 modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
200 MODEST_CONF_VALUE_STRING, &err);
202 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
209 g_slist_foreach (list, (GFunc) g_free, NULL);
217 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
219 return modest_account_mgr_get_bool (self, account_name,
220 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
223 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
226 return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP,
231 * modest_account_mgr_get_connection_specific_smtp
232 * @self: a ModestAccountMgr instance
233 * @connection_id: A libconic IAP connection id
235 * Retrieve a server account to use with this specific connection for this account.
237 * Returns: a server account name to use for this connection, or NULL if none is specified.
239 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* connection_id)
241 gchar *result = NULL;
243 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
246 GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
247 MODEST_CONF_VALUE_STRING, &err);
249 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
257 /* The server account is in the item after the connection name: */
260 const gchar* this_connection_id = (const gchar*)(iter->data);
261 if (strcmp (this_connection_id, connection_id) == 0) {
262 iter = g_slist_next (iter);
265 const gchar* account_name = (const gchar*)(iter->data);
267 result = g_strdup (account_name);
273 /* Skip 2 to go to the next connection in the list: */
274 iter = g_slist_next (iter);
276 iter = g_slist_next (iter);
281 g_slist_foreach (list, (GFunc) g_free, NULL);
289 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
291 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
292 TRUE /* server account */);
296 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name,
297 const gchar* username)
299 /* Note that this won't work properly as long as the gconf cache is broken
301 gchar *existing_username = modest_account_mgr_get_server_account_username(self,
304 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
305 username, TRUE /* server account */);
307 /* We don't know anything about new usernames: */
308 if (strcmp (existing_username, username) != 0)
309 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
311 g_free (existing_username);
315 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
317 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
318 TRUE /* server account */);
322 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self,
323 const gchar* account_name,
326 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
327 succeeded, TRUE /* server account */);
331 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
333 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
334 TRUE /* server account */);
338 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
340 gboolean result = FALSE;
341 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
342 TRUE /* server account */);
343 if (password && strlen (password)) {
347 bzero (password, strlen (password));
356 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
357 const gchar* account_name)
359 return modest_account_mgr_get_string (self,
361 MODEST_ACCOUNT_HOSTNAME,
362 TRUE /* server account */);
366 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
367 const gchar *server_account_name,
368 const gchar *hostname)
370 modest_account_mgr_set_string (self,
372 MODEST_ACCOUNT_HOSTNAME,
374 TRUE /* server account */);
380 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
381 const gchar* account_name)
383 ModestProtocolRegistry *protocol_registry;
384 ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
387 protocol_registry = modest_runtime_get_protocol_registry ();
388 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
389 TRUE /* server account */);
391 ModestProtocol *protocol;
393 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
397 result = modest_protocol_get_type_id (protocol);
406 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
407 const gchar* account_name, ModestProtocolType secure_auth)
409 const gchar* str_value;
410 ModestProtocolRegistry *protocol_registry;
411 ModestProtocol *protocol;
413 /* Get the conf string for the protocol: */
414 protocol_registry = modest_runtime_get_protocol_registry ();
415 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
416 str_value = modest_protocol_get_name (protocol);
418 /* Set it in the configuration: */
419 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
423 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
424 const gchar* account_name)
426 ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
429 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
430 TRUE /* server account */);
432 ModestProtocolRegistry *protocol_registry;
433 ModestProtocol *protocol;
435 protocol_registry = modest_runtime_get_protocol_registry ();
436 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
437 MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
442 result = modest_protocol_get_type_id (protocol);
449 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
450 const gchar* account_name,
451 ModestProtocolType security)
453 const gchar* str_value;
454 ModestProtocolRegistry *protocol_registry;
455 ModestProtocol *protocol;
457 /* Get the conf string for the protocol type: */
458 protocol_registry = modest_runtime_get_protocol_registry ();
459 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
460 str_value = modest_protocol_get_name (protocol);
462 /* Set it in the configuration: */
463 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
466 ModestServerAccountSettings *
467 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
469 ModestServerAccountSettings *settings = NULL;
470 ModestProtocol *protocol;
471 ModestProtocolRegistry *registry;
472 gchar *hostname, *username, *pwd, *uri, *proto, *auth, *sec;
474 if (!modest_account_mgr_account_exists (self, name, TRUE)) {
475 g_warning ("%s account %s does not exist", __FUNCTION__, name);
479 registry = modest_runtime_get_protocol_registry ();
480 settings = modest_server_account_settings_new ();
482 modest_server_account_settings_set_account_name (settings, name);
484 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
487 if (is_transport_and_not_store) {
488 tag = MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS;
490 tag = MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS;
492 protocol = modest_protocol_registry_get_protocol_by_name (registry, tag, proto);
494 modest_server_account_settings_set_protocol (settings,
495 modest_protocol_get_type_id (protocol));
501 modest_server_account_settings_set_port (settings,
502 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
504 auth = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
506 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, auth);
507 modest_server_account_settings_set_auth_protocol (settings,
508 modest_protocol_get_type_id (protocol));
511 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
514 sec = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
516 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, sec);
517 modest_server_account_settings_set_security_protocol (settings,
518 modest_protocol_get_type_id (protocol));
521 modest_server_account_settings_set_security_protocol (settings,
522 MODEST_PROTOCOLS_CONNECTION_NONE);
525 /* Username, password and URI. Note that the URI could include
526 the former two, so in this case there is no need to have
528 username = modest_account_mgr_get_string (self, name,
529 MODEST_ACCOUNT_USERNAME,TRUE);
531 modest_server_account_settings_set_username (settings, username);
533 pwd = modest_account_mgr_get_string (self, name,
534 MODEST_ACCOUNT_PASSWORD, TRUE);
536 modest_server_account_settings_set_password (settings, pwd);
540 uri = modest_account_mgr_get_string (self, name,
541 MODEST_ACCOUNT_URI, TRUE);
543 modest_server_account_settings_set_uri (settings, uri);
545 hostname = modest_account_mgr_get_string (self, name,
546 MODEST_ACCOUNT_HOSTNAME,TRUE);
548 modest_server_account_settings_set_hostname (settings, hostname);
551 if (!username || !hostname) {
566 g_object_unref (settings);
571 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
572 ModestServerAccountSettings *settings)
574 gboolean has_errors = FALSE;
575 const gchar *account_name;
576 const gchar *protocol_name;
578 ModestProtocolRegistry *protocol_registry;
579 ModestProtocol *protocol;
581 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
582 protocol_registry = modest_runtime_get_protocol_registry ();
583 account_name = modest_server_account_settings_get_account_name (settings);
585 /* if we don't have a valid account name we cannot save */
586 g_return_val_if_fail (account_name, FALSE);
588 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
589 modest_server_account_settings_get_protocol (settings));
590 protocol_name = modest_protocol_get_name (protocol);
591 uri = modest_server_account_settings_get_uri (settings);
593 const gchar *hostname;
594 const gchar *username;
595 const gchar *password;
597 const gchar *auth_protocol_name;
598 const gchar *security_name;
600 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
601 username = null_means_empty (modest_server_account_settings_get_username (settings));
602 password = null_means_empty (modest_server_account_settings_get_password (settings));
603 port = modest_server_account_settings_get_port (settings);
604 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
605 modest_server_account_settings_get_auth_protocol (settings));
606 auth_protocol_name = modest_protocol_get_name (protocol);
607 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
608 modest_server_account_settings_get_security_protocol (settings));
609 security_name = modest_protocol_get_name (protocol);
611 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
614 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
617 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
620 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
621 protocol_name, TRUE));
623 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
626 (has_errors = !modest_account_mgr_set_string (self, account_name,
627 MODEST_ACCOUNT_AUTH_MECH,
628 auth_protocol_name, TRUE));
630 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
634 const gchar *uri = modest_server_account_settings_get_uri (settings);
635 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
638 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
639 protocol_name, TRUE));
647 ModestAccountSettings *
648 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
651 ModestAccountSettings *settings;
653 gchar *server_account;
654 gchar *default_account;
655 gboolean use_signature = FALSE;
657 g_return_val_if_fail (self, NULL);
658 g_return_val_if_fail (name, NULL);
660 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
661 /* For instance, maybe you are mistakenly checking for a server account name? */
662 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
666 settings = modest_account_settings_new ();
668 modest_account_settings_set_account_name (settings, name);
670 string = modest_account_mgr_get_string (self, name,
671 MODEST_ACCOUNT_DISPLAY_NAME,
673 modest_account_settings_set_display_name (settings, string);
676 string = modest_account_mgr_get_string (self, name,
677 MODEST_ACCOUNT_FULLNAME,
679 modest_account_settings_set_fullname (settings, string);
682 string = modest_account_mgr_get_string (self, name,
683 MODEST_ACCOUNT_EMAIL,
685 modest_account_settings_set_email_address (settings, string);
688 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
689 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
690 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
692 default_account = modest_account_mgr_get_default_account (self);
693 modest_account_settings_set_is_default (settings,
694 (default_account && strcmp (default_account, name) == 0));
695 g_free (default_account);
697 string = modest_account_mgr_get_signature (self, name, &use_signature);
698 modest_account_settings_set_use_signature (settings, use_signature);
699 modest_account_settings_set_signature (settings, string);
702 modest_account_settings_set_leave_messages_on_server
703 (settings, modest_account_mgr_get_leave_on_server (self, name));
704 modest_account_settings_set_use_connection_specific_smtp
705 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
708 server_account = modest_account_mgr_get_string (self, name,
709 MODEST_ACCOUNT_STORE_ACCOUNT,
711 if (server_account) {
712 ModestServerAccountSettings *store_settings;
713 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
714 g_free (server_account);
716 /* It could happen that the account data is corrupted
717 so it's not loaded properly */
718 if (store_settings) {
719 modest_account_settings_set_store_settings (settings,
721 g_object_unref (store_settings);
723 g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
724 g_object_unref (settings);
730 server_account = modest_account_mgr_get_string (self, name,
731 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
733 if (server_account) {
734 ModestServerAccountSettings *transport_settings;
735 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
736 g_free (server_account);
738 if (transport_settings) {
739 modest_account_settings_set_transport_settings (settings, transport_settings);
740 g_object_unref (transport_settings);
742 g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
743 g_object_unref (settings);
752 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
753 ModestAccountSettings *settings)
755 const gchar *account_name;
756 ModestServerAccountSettings *store_settings;
757 ModestServerAccountSettings *transport_settings;
759 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
760 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
762 account_name = modest_account_settings_get_account_name (settings);
763 g_return_if_fail (account_name != NULL);
765 modest_account_mgr_set_display_name (mgr, account_name,
766 modest_account_settings_get_display_name (settings));
767 modest_account_mgr_set_user_fullname (mgr, account_name,
768 modest_account_settings_get_fullname (settings));
769 modest_account_mgr_set_user_email (mgr, account_name,
770 modest_account_settings_get_email_address (settings));
771 modest_account_mgr_set_retrieve_type (mgr, account_name,
772 modest_account_settings_get_retrieve_type (settings));
773 modest_account_mgr_set_retrieve_limit (mgr, account_name,
774 modest_account_settings_get_retrieve_limit (settings));
775 modest_account_mgr_set_leave_on_server (mgr, account_name,
776 modest_account_settings_get_leave_messages_on_server (settings));
777 modest_account_mgr_set_signature (mgr, account_name,
778 modest_account_settings_get_signature (settings),
779 modest_account_settings_get_use_signature (settings));
780 modest_account_mgr_set_use_connection_specific_smtp
782 modest_account_settings_get_use_connection_specific_smtp (settings));
784 store_settings = modest_account_settings_get_store_settings (settings);
785 if (store_settings) {
786 const gchar *store_account_name;
787 store_account_name = modest_server_account_settings_get_account_name (store_settings);
788 if (store_account_name)
789 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT,
790 store_account_name, FALSE);
791 modest_account_mgr_save_server_settings (mgr, store_settings);
792 g_object_unref (store_settings);
795 transport_settings = modest_account_settings_get_transport_settings (settings);
796 if (transport_settings) {
797 const gchar *transport_account_name;
798 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
799 if (transport_account_name)
800 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
801 transport_account_name, FALSE);
802 modest_account_mgr_save_server_settings (mgr, transport_settings);
803 g_object_unref (transport_settings);
805 modest_account_mgr_set_bool (mgr, account_name, MODEST_ACCOUNT_ENABLED, TRUE,FALSE);
810 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
812 return g_utf8_collate((const gchar*)a, (const gchar*)b);
815 /** Get the first one, alphabetically, by title. */
817 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
819 const gchar* account_name = NULL;
820 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
822 /* Return TRUE if there is no account */
826 /* Get the first one, alphabetically, by title: */
827 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
828 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
830 GSList* iter = list_sorted;
831 gboolean found = FALSE;
832 while (iter && !found) {
833 account_name = (const gchar*)list_sorted->data;
839 iter = g_slist_next (iter);
842 gchar* result = NULL;
844 result = g_strdup (account_name);
846 modest_account_mgr_free_account_names (account_names);
847 account_names = NULL;
853 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
855 gboolean result = FALSE;
857 gchar* account_name = modest_account_mgr_get_first_account_name(self);
859 result = modest_account_mgr_set_default_account (self, account_name);
860 g_free (account_name);
863 result = TRUE; /* If there are no accounts then it's not a failure. */
869 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name, const gchar *mailbox)
872 gchar *transport_account;
875 g_return_val_if_fail (self, NULL);
876 g_return_val_if_fail (name, NULL);
880 transport_account = modest_account_mgr_get_server_account_name (self,
882 TNY_ACCOUNT_TYPE_TRANSPORT);
883 if (transport_account) {
884 proto = modest_account_mgr_get_string (self, transport_account, MODEST_ACCOUNT_PROTO, TRUE);
886 ModestProtocol *protocol =
887 modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
888 MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
890 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
891 from = modest_account_protocol_get_from (MODEST_ACCOUNT_PROTOCOL (protocol),
900 gchar *fullname, *email;
902 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
904 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
906 from = g_strdup_printf ("%s <%s>",
907 fullname ? fullname : "",
916 /* Add a number to the end of the text, or increment a number that is already there.
919 util_increment_name (const gchar* text)
921 g_return_val_if_fail (text, NULL);
923 /* Get the end character,
924 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
926 const gchar* end = NULL;
927 if (!g_utf8_validate (text, -1, &end))
933 --end; /* Go to before the null-termination. */
935 /* Look at each UTF-8 characer, starting at the end: */
936 const gchar* p = end;
937 const gchar* alpha_end = NULL;
940 /* Stop when we reach the first character that is not a numeric digit: */
941 const gunichar ch = g_utf8_get_char (p);
942 if (!g_unichar_isdigit (ch)) {
947 p = g_utf8_find_prev_char (text, p);
951 /* The text must consist completely of numeric digits. */
957 /* Intepret and increment the number, if any: */
958 gint num = atol (alpha_end);
961 /* Get the name part: */
962 gint name_len = alpha_end - text;
963 gchar *name_without_number = g_malloc(name_len + 1);
964 memcpy (name_without_number, text, name_len);
965 name_without_number[name_len] = 0;\
967 /* Concatenate the text part and the new number: */
968 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
969 g_free (name_without_number);
975 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
976 gboolean server_account)
978 gchar *account_name = g_strdup (starting_name);
980 while (modest_account_mgr_account_exists (self,
981 account_name, server_account /* server_account */)) {
983 gchar * account_name2 = util_increment_name (account_name);
984 g_free (account_name);
985 account_name = account_name2;
992 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
994 gchar *account_name = g_strdup (starting_name);
996 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
998 gchar * account_name2 = util_increment_name (account_name);
999 g_free (account_name);
1000 account_name = account_name2;
1003 return account_name;
1007 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
1008 const gchar *account_name,
1009 gboolean leave_on_server)
1011 modest_account_mgr_set_bool (self,
1013 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1019 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
1020 const gchar* account_name)
1022 return modest_account_mgr_get_bool (self,
1024 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1029 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
1030 const gchar* account_name)
1032 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
1034 MODEST_ACCOUNT_LAST_UPDATED,
1039 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
1040 const gchar* account_name,
1043 modest_account_mgr_set_int (self,
1045 MODEST_ACCOUNT_LAST_UPDATED,
1049 /* TODO: notify about changes */
1053 modest_account_mgr_get_has_new_mails (ModestAccountMgr *self,
1054 const gchar* account_name)
1056 return modest_account_mgr_get_bool (modest_runtime_get_account_mgr (),
1058 MODEST_ACCOUNT_HAS_NEW_MAILS,
1063 modest_account_mgr_set_has_new_mails (ModestAccountMgr *self,
1064 const gchar* account_name,
1065 gboolean has_new_mails)
1067 modest_account_mgr_set_bool (self,
1069 MODEST_ACCOUNT_HAS_NEW_MAILS,
1073 /* TODO: notify about changes */
1077 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
1078 const gchar* account_name)
1080 return modest_account_mgr_get_int (self,
1082 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1087 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
1088 const gchar* account_name,
1089 gint limit_retrieve)
1091 modest_account_mgr_set_int (self,
1093 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1095 FALSE /* not server account */);
1099 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
1100 const gchar* account_name)
1102 return modest_account_mgr_get_int (self,
1104 MODEST_ACCOUNT_PORT,
1109 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
1110 const gchar *account_name,
1113 modest_account_mgr_set_int (self,
1115 MODEST_ACCOUNT_PORT,
1116 port_num, TRUE /* server account */);
1120 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
1121 const gchar *account_name,
1122 TnyAccountType account_type)
1124 return modest_account_mgr_get_string (self,
1126 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
1127 MODEST_ACCOUNT_STORE_ACCOUNT :
1128 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
1132 static const gchar *
1133 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1135 switch(retrieve_type) {
1136 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1137 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1139 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1140 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1142 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1143 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1146 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1150 static ModestAccountRetrieveType
1151 get_retrieve_type (const gchar *name)
1153 if (!name || name[0] == 0)
1154 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1155 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1156 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1157 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1158 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1160 /* we fall back to headers only */
1161 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1165 ModestAccountRetrieveType
1166 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
1167 const gchar *account_name)
1170 ModestAccountRetrieveType result;
1172 string = modest_account_mgr_get_string (self,
1174 MODEST_ACCOUNT_RETRIEVE,
1175 FALSE /* not server account */);
1176 result = get_retrieve_type (string);
1183 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
1184 const gchar *account_name,
1185 ModestAccountRetrieveType retrieve_type)
1187 modest_account_mgr_set_string (self,
1189 MODEST_ACCOUNT_RETRIEVE,
1190 get_retrieve_type_name (retrieve_type),
1191 FALSE /* not server account */);
1196 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1197 const gchar *account_name,
1198 const gchar *fullname)
1200 modest_account_mgr_set_string (self,
1202 MODEST_ACCOUNT_FULLNAME,
1204 FALSE /* not server account */);
1208 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1209 const gchar *account_name,
1212 modest_account_mgr_set_string (self,
1214 MODEST_ACCOUNT_EMAIL,
1216 FALSE /* not server account */);
1220 modest_account_mgr_account_is_multimailbox (ModestAccountMgr *mgr,
1221 const gchar *account_name,
1222 ModestProtocol **mmb_protocol)
1224 gchar *transport_account;
1225 gboolean result = 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;