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>
40 static const gchar * null_means_empty (const gchar * str);
43 null_means_empty (const gchar * str)
45 return str ? str : "";
49 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
52 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
57 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
59 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
62 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
63 const gchar* signature, gboolean use_signature)
65 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
66 use_signature, FALSE);
67 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
68 null_means_empty (signature), FALSE);
73 modest_account_mgr_get_signature (ModestAccountMgr *self,
75 gboolean* use_signature)
78 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
80 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
83 ModestProtocolType modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
85 ModestProtocolType result = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
86 gchar *server_account_name = modest_account_mgr_get_string (self, name,
87 MODEST_ACCOUNT_STORE_ACCOUNT,
89 if (server_account_name) {
90 ModestServerAccountSettings* server_settings =
91 modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
93 /* Easy setup wizard precreates accounts without
94 settings so this could happen */
95 if (server_settings) {
96 result = modest_server_account_settings_get_protocol (server_settings);
97 g_object_unref (server_settings);
99 g_free (server_account_name);
107 modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
108 const gchar* connection_id,
109 const gchar* server_account_name)
111 modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
113 ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
115 gboolean result = TRUE;
117 GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
118 MODEST_CONF_VALUE_STRING, &err);
120 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
125 /* The server account is in the item after the connection name: */
126 list = g_slist_append (list, g_strdup (connection_id));
127 list = g_slist_append (list, g_strdup (server_account_name));
129 /* Reset the changed list: */
130 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
131 MODEST_CONF_VALUE_STRING, &err);
133 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
141 g_slist_foreach (list, (GFunc) g_free, NULL);
149 * modest_account_mgr_remove_connection_specific_smtp
150 * @self: a ModestAccountMgr instance
151 * @name: the account name
152 * @connection_id: A libconic IAP connection id
154 * Disassacoiate a server account to use with the specific connection for this account.
156 * Returns: TRUE if it worked, FALSE otherwise
159 modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
160 const gchar* connection_id)
162 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
164 gboolean result = TRUE;
166 GSList *list = modest_conf_get_list (priv->modest_conf,
167 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
168 MODEST_CONF_VALUE_STRING, &err);
170 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
179 /* The server account is in the item after the connection name: */
180 GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
181 if (list_connection) {
182 GSList *account_node = g_slist_next (list_connection);
183 /* remove both items: */
184 list = g_slist_delete_link(list, list_connection);
185 list = g_slist_delete_link(list, account_node);
188 /* Reset the changed list: */
189 modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
190 MODEST_CONF_VALUE_STRING, &err);
192 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
199 g_slist_foreach (list, (GFunc) g_free, NULL);
207 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
209 return modest_account_mgr_get_bool (self, account_name,
210 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
213 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
216 return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP,
221 * modest_account_mgr_get_connection_specific_smtp
222 * @self: a ModestAccountMgr instance
223 * @connection_id: A libconic IAP connection id
225 * Retrieve a server account to use with this specific connection for this account.
227 * Returns: a server account name to use for this connection, or NULL if none is specified.
229 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* connection_id)
231 gchar *result = NULL;
233 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
236 GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
237 MODEST_CONF_VALUE_STRING, &err);
239 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
247 /* The server account is in the item after the connection name: */
250 const gchar* this_connection_id = (const gchar*)(iter->data);
251 if (strcmp (this_connection_id, connection_id) == 0) {
252 iter = g_slist_next (iter);
255 const gchar* account_name = (const gchar*)(iter->data);
257 result = g_strdup (account_name);
263 /* Skip 2 to go to the next connection in the list: */
264 iter = g_slist_next (iter);
266 iter = g_slist_next (iter);
271 g_slist_foreach (list, (GFunc) g_free, NULL);
279 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
281 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
282 TRUE /* server account */);
286 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name,
287 const gchar* username)
289 /* Note that this won't work properly as long as the gconf cache is broken
291 gchar *existing_username = modest_account_mgr_get_server_account_username(self,
294 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
295 username, TRUE /* server account */);
297 /* We don't know anything about new usernames: */
298 if (strcmp (existing_username, username) != 0)
299 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
301 g_free (existing_username);
305 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
307 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
308 TRUE /* server account */);
312 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self,
313 const gchar* account_name,
316 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
317 succeeded, TRUE /* server account */);
321 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
323 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
324 TRUE /* server account */);
328 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
330 gboolean result = FALSE;
331 gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
332 TRUE /* server account */);
333 if (password && strlen (password)) {
337 bzero (password, strlen (password));
346 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self,
347 const gchar* account_name)
349 return modest_account_mgr_get_string (self,
351 MODEST_ACCOUNT_HOSTNAME,
352 TRUE /* server account */);
356 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self,
357 const gchar *server_account_name,
358 const gchar *hostname)
360 modest_account_mgr_set_string (self,
362 MODEST_ACCOUNT_HOSTNAME,
364 TRUE /* server account */);
370 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self,
371 const gchar* account_name)
373 ModestProtocolRegistry *protocol_registry;
374 ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
377 protocol_registry = modest_runtime_get_protocol_registry ();
378 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
379 TRUE /* server account */);
381 ModestProtocol *protocol;
383 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
387 result = modest_protocol_get_type_id (protocol);
396 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self,
397 const gchar* account_name, ModestProtocolType secure_auth)
399 const gchar* str_value;
400 ModestProtocolRegistry *protocol_registry;
401 ModestProtocol *protocol;
403 /* Get the conf string for the protocol: */
404 protocol_registry = modest_runtime_get_protocol_registry ();
405 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
406 str_value = modest_protocol_get_name (protocol);
408 /* Set it in the configuration: */
409 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
413 modest_account_mgr_get_server_account_security (ModestAccountMgr *self,
414 const gchar* account_name)
416 ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
419 value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
420 TRUE /* server account */);
422 ModestProtocolRegistry *protocol_registry;
423 ModestProtocol *protocol;
425 protocol_registry = modest_runtime_get_protocol_registry ();
426 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
427 MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
432 result = modest_protocol_get_type_id (protocol);
439 modest_account_mgr_set_server_account_security (ModestAccountMgr *self,
440 const gchar* account_name,
441 ModestProtocolType security)
443 const gchar* str_value;
444 ModestProtocolRegistry *protocol_registry;
445 ModestProtocol *protocol;
447 /* Get the conf string for the protocol type: */
448 protocol_registry = modest_runtime_get_protocol_registry ();
449 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
450 str_value = modest_protocol_get_name (protocol);
452 /* Set it in the configuration: */
453 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
456 ModestServerAccountSettings *
457 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
459 ModestServerAccountSettings *settings = NULL;
460 ModestProtocol *protocol;
461 ModestProtocolRegistry *registry;
462 gchar *hostname, *username, *pwd, *uri, *proto, *auth, *sec;
464 if (!modest_account_mgr_account_exists (self, name, TRUE)) {
465 g_warning ("%s account %s does not exist", __FUNCTION__, name);
469 registry = modest_runtime_get_protocol_registry ();
470 settings = modest_server_account_settings_new ();
472 modest_server_account_settings_set_account_name (settings, name);
474 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
477 if (is_transport_and_not_store) {
478 tag = MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS;
480 tag = MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS;
482 protocol = modest_protocol_registry_get_protocol_by_name (registry, tag, proto);
484 modest_server_account_settings_set_protocol (settings,
485 modest_protocol_get_type_id (protocol));
491 modest_server_account_settings_set_port (settings,
492 modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
494 auth = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
496 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, auth);
497 modest_server_account_settings_set_auth_protocol (settings,
498 modest_protocol_get_type_id (protocol));
501 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
504 sec = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
506 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, sec);
507 modest_server_account_settings_set_security_protocol (settings,
508 modest_protocol_get_type_id (protocol));
511 modest_server_account_settings_set_security_protocol (settings,
512 MODEST_PROTOCOLS_CONNECTION_NONE);
515 /* Username, password and URI. Note that the URI could include
516 the former two, so in this case there is no need to have
518 username = modest_account_mgr_get_string (self, name,
519 MODEST_ACCOUNT_USERNAME,TRUE);
521 modest_server_account_settings_set_username (settings, username);
523 pwd = modest_account_mgr_get_string (self, name,
524 MODEST_ACCOUNT_PASSWORD, TRUE);
526 modest_server_account_settings_set_password (settings, pwd);
530 uri = modest_account_mgr_get_string (self, name,
531 MODEST_ACCOUNT_URI, TRUE);
533 modest_server_account_settings_set_uri (settings, uri);
535 hostname = modest_account_mgr_get_string (self, name,
536 MODEST_ACCOUNT_HOSTNAME,TRUE);
538 modest_server_account_settings_set_hostname (settings, hostname);
541 if (!username || !hostname) {
556 g_object_unref (settings);
561 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
562 ModestServerAccountSettings *settings)
564 gboolean has_errors = FALSE;
565 const gchar *account_name;
566 const gchar *protocol_name;
568 ModestProtocolRegistry *protocol_registry;
569 ModestProtocol *protocol;
571 g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
572 protocol_registry = modest_runtime_get_protocol_registry ();
573 account_name = modest_server_account_settings_get_account_name (settings);
575 /* if we don't have a valid account name we cannot save */
576 g_return_val_if_fail (account_name, FALSE);
578 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
579 modest_server_account_settings_get_protocol (settings));
580 protocol_name = modest_protocol_get_name (protocol);
581 uri = modest_server_account_settings_get_uri (settings);
583 const gchar *hostname;
584 const gchar *username;
585 const gchar *password;
587 const gchar *auth_protocol_name;
588 const gchar *security_name;
590 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
591 username = null_means_empty (modest_server_account_settings_get_username (settings));
592 password = null_means_empty (modest_server_account_settings_get_password (settings));
593 port = modest_server_account_settings_get_port (settings);
594 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
595 modest_server_account_settings_get_auth_protocol (settings));
596 auth_protocol_name = modest_protocol_get_name (protocol);
597 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
598 modest_server_account_settings_get_security_protocol (settings));
599 security_name = modest_protocol_get_name (protocol);
601 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
604 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
607 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
610 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
611 protocol_name, TRUE));
613 (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
616 (has_errors = !modest_account_mgr_set_string (self, account_name,
617 MODEST_ACCOUNT_AUTH_MECH,
618 auth_protocol_name, TRUE));
620 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
624 const gchar *uri = modest_server_account_settings_get_uri (settings);
625 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
628 (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
629 protocol_name, TRUE));
637 ModestAccountSettings *
638 modest_account_mgr_load_account_settings (ModestAccountMgr *self,
641 ModestAccountSettings *settings;
643 gchar *server_account;
644 gchar *default_account;
645 gboolean use_signature = FALSE;
647 g_return_val_if_fail (self, NULL);
648 g_return_val_if_fail (name, NULL);
650 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
651 /* For instance, maybe you are mistakenly checking for a server account name? */
652 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
656 settings = modest_account_settings_new ();
658 modest_account_settings_set_account_name (settings, name);
660 string = modest_account_mgr_get_string (self, name,
661 MODEST_ACCOUNT_DISPLAY_NAME,
663 modest_account_settings_set_display_name (settings, string);
666 string = modest_account_mgr_get_string (self, name,
667 MODEST_ACCOUNT_FULLNAME,
669 modest_account_settings_set_fullname (settings, string);
672 string = modest_account_mgr_get_string (self, name,
673 MODEST_ACCOUNT_EMAIL,
675 modest_account_settings_set_email_address (settings, string);
678 modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
679 modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
680 modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
682 default_account = modest_account_mgr_get_default_account (self);
683 modest_account_settings_set_is_default (settings,
684 (default_account && strcmp (default_account, name) == 0));
685 g_free (default_account);
687 string = modest_account_mgr_get_signature (self, name, &use_signature);
688 modest_account_settings_set_use_signature (settings, use_signature);
689 modest_account_settings_set_signature (settings, string);
692 modest_account_settings_set_leave_messages_on_server
693 (settings, modest_account_mgr_get_leave_on_server (self, name));
694 modest_account_settings_set_use_connection_specific_smtp
695 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
698 server_account = modest_account_mgr_get_string (self, name,
699 MODEST_ACCOUNT_STORE_ACCOUNT,
701 if (server_account) {
702 ModestServerAccountSettings *store_settings;
703 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
704 g_free (server_account);
706 /* It could happen that the account data is corrupted
707 so it's not loaded properly */
708 if (store_settings) {
709 modest_account_settings_set_store_settings (settings,
711 g_object_unref (store_settings);
713 g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
714 g_object_unref (settings);
720 server_account = modest_account_mgr_get_string (self, name,
721 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
723 if (server_account) {
724 ModestServerAccountSettings *transport_settings;
725 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
726 g_free (server_account);
728 if (transport_settings) {
729 modest_account_settings_set_transport_settings (settings, transport_settings);
730 g_object_unref (transport_settings);
732 g_warning ("%s can not load server settings. Account corrupted?", __FUNCTION__);
733 g_object_unref (settings);
742 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
743 ModestAccountSettings *settings)
745 const gchar *account_name;
746 ModestServerAccountSettings *store_settings;
747 ModestServerAccountSettings *transport_settings;
749 g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
750 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
752 account_name = modest_account_settings_get_account_name (settings);
753 g_return_if_fail (account_name != NULL);
755 modest_account_mgr_set_display_name (mgr, account_name,
756 modest_account_settings_get_display_name (settings));
757 modest_account_mgr_set_user_fullname (mgr, account_name,
758 modest_account_settings_get_fullname (settings));
759 modest_account_mgr_set_user_email (mgr, account_name,
760 modest_account_settings_get_email_address (settings));
761 modest_account_mgr_set_retrieve_type (mgr, account_name,
762 modest_account_settings_get_retrieve_type (settings));
763 modest_account_mgr_set_retrieve_limit (mgr, account_name,
764 modest_account_settings_get_retrieve_limit (settings));
765 modest_account_mgr_set_leave_on_server (mgr, account_name,
766 modest_account_settings_get_leave_messages_on_server (settings));
767 modest_account_mgr_set_signature (mgr, account_name,
768 modest_account_settings_get_signature (settings),
769 modest_account_settings_get_use_signature (settings));
770 modest_account_mgr_set_use_connection_specific_smtp
772 modest_account_settings_get_use_connection_specific_smtp (settings));
774 store_settings = modest_account_settings_get_store_settings (settings);
775 if (store_settings) {
776 const gchar *store_account_name;
777 store_account_name = modest_server_account_settings_get_account_name (store_settings);
778 if (store_account_name)
779 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT,
780 store_account_name, FALSE);
781 modest_account_mgr_save_server_settings (mgr, store_settings);
782 g_object_unref (store_settings);
785 transport_settings = modest_account_settings_get_transport_settings (settings);
786 if (transport_settings) {
787 const gchar *transport_account_name;
788 transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
789 if (transport_account_name)
790 modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
791 transport_account_name, FALSE);
792 modest_account_mgr_save_server_settings (mgr, transport_settings);
793 g_object_unref (transport_settings);
795 modest_account_mgr_set_enabled (mgr, account_name, TRUE);
800 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
802 return g_utf8_collate((const gchar*)a, (const gchar*)b);
805 /** Get the first one, alphabetically, by title. */
807 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
809 const gchar* account_name = NULL;
810 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
812 /* Return TRUE if there is no account */
816 /* Get the first one, alphabetically, by title: */
817 /* gchar *old_default = modest_account_mgr_get_default_account (self); */
818 GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
820 GSList* iter = list_sorted;
821 gboolean found = FALSE;
822 while (iter && !found) {
823 account_name = (const gchar*)list_sorted->data;
829 iter = g_slist_next (iter);
832 gchar* result = NULL;
834 result = g_strdup (account_name);
836 modest_account_mgr_free_account_names (account_names);
837 account_names = NULL;
843 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
845 gboolean result = FALSE;
847 gchar* account_name = modest_account_mgr_get_first_account_name(self);
849 result = modest_account_mgr_set_default_account (self, account_name);
850 g_free (account_name);
853 result = TRUE; /* If there are no accounts then it's not a failure. */
859 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name, const gchar *mailbox)
862 gchar *transport_account;
865 g_return_val_if_fail (self, NULL);
866 g_return_val_if_fail (name, NULL);
870 transport_account = modest_account_mgr_get_server_account_name (self,
872 TNY_ACCOUNT_TYPE_TRANSPORT);
873 if (transport_account) {
874 proto = modest_account_mgr_get_string (self, transport_account, MODEST_ACCOUNT_PROTO, TRUE);
876 ModestProtocol *protocol =
877 modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
878 MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
880 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
881 from = modest_account_protocol_get_from (MODEST_ACCOUNT_PROTOCOL (protocol),
890 gchar *fullname, *email;
892 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
894 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
896 from = g_strdup_printf ("%s <%s>",
897 fullname ? fullname : "",
906 /* Add a number to the end of the text, or increment a number that is already there.
909 util_increment_name (const gchar* text)
911 g_return_val_if_fail (text, NULL);
913 /* Get the end character,
914 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
916 const gchar* end = NULL;
917 if (!g_utf8_validate (text, -1, &end))
923 --end; /* Go to before the null-termination. */
925 /* Look at each UTF-8 characer, starting at the end: */
926 const gchar* p = end;
927 const gchar* alpha_end = NULL;
930 /* Stop when we reach the first character that is not a numeric digit: */
931 const gunichar ch = g_utf8_get_char (p);
932 if (!g_unichar_isdigit (ch)) {
937 p = g_utf8_find_prev_char (text, p);
941 /* The text must consist completely of numeric digits. */
947 /* Intepret and increment the number, if any: */
948 gint num = atol (alpha_end);
951 /* Get the name part: */
952 gint name_len = alpha_end - text;
953 gchar *name_without_number = g_malloc(name_len + 1);
954 memcpy (name_without_number, text, name_len);
955 name_without_number[name_len] = 0;\
957 /* Concatenate the text part and the new number: */
958 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
959 g_free (name_without_number);
965 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
966 gboolean server_account)
968 gchar *account_name = g_strdup (starting_name);
970 while (modest_account_mgr_account_exists (self,
971 account_name, server_account /* server_account */)) {
973 gchar * account_name2 = util_increment_name (account_name);
974 g_free (account_name);
975 account_name = account_name2;
982 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
984 gchar *account_name = g_strdup (starting_name);
986 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
988 gchar * account_name2 = util_increment_name (account_name);
989 g_free (account_name);
990 account_name = account_name2;
997 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self,
998 const gchar *account_name,
999 gboolean leave_on_server)
1001 modest_account_mgr_set_bool (self,
1003 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1009 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self,
1010 const gchar* account_name)
1012 return modest_account_mgr_get_bool (self,
1014 MODEST_ACCOUNT_LEAVE_ON_SERVER,
1019 modest_account_mgr_get_last_updated (ModestAccountMgr *self,
1020 const gchar* account_name)
1022 return modest_account_mgr_get_int (modest_runtime_get_account_mgr (),
1024 MODEST_ACCOUNT_LAST_UPDATED,
1029 modest_account_mgr_set_last_updated (ModestAccountMgr *self,
1030 const gchar* account_name,
1033 modest_account_mgr_set_int (self,
1035 MODEST_ACCOUNT_LAST_UPDATED,
1039 /* TODO: notify about changes */
1043 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self,
1044 const gchar* account_name)
1046 return modest_account_mgr_get_int (self,
1048 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1053 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self,
1054 const gchar* account_name,
1055 gint limit_retrieve)
1057 modest_account_mgr_set_int (self,
1059 MODEST_ACCOUNT_LIMIT_RETRIEVE,
1061 FALSE /* not server account */);
1065 modest_account_mgr_get_server_account_port (ModestAccountMgr *self,
1066 const gchar* account_name)
1068 return modest_account_mgr_get_int (self,
1070 MODEST_ACCOUNT_PORT,
1075 modest_account_mgr_set_server_account_port (ModestAccountMgr *self,
1076 const gchar *account_name,
1079 modest_account_mgr_set_int (self,
1081 MODEST_ACCOUNT_PORT,
1082 port_num, TRUE /* server account */);
1086 modest_account_mgr_get_server_account_name (ModestAccountMgr *self,
1087 const gchar *account_name,
1088 TnyAccountType account_type)
1090 return modest_account_mgr_get_string (self,
1092 (account_type == TNY_ACCOUNT_TYPE_STORE) ?
1093 MODEST_ACCOUNT_STORE_ACCOUNT :
1094 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
1098 static const gchar *
1099 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1101 switch(retrieve_type) {
1102 case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1103 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1105 case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1106 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1108 case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1109 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1112 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1116 static ModestAccountRetrieveType
1117 get_retrieve_type (const gchar *name)
1119 if (!name || name[0] == 0)
1120 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1121 if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1122 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1123 } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1124 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1126 /* we fall back to headers only */
1127 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1131 ModestAccountRetrieveType
1132 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self,
1133 const gchar *account_name)
1136 ModestAccountRetrieveType result;
1138 string = modest_account_mgr_get_string (self,
1140 MODEST_ACCOUNT_RETRIEVE,
1141 FALSE /* not server account */);
1142 result = get_retrieve_type (string);
1149 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self,
1150 const gchar *account_name,
1151 ModestAccountRetrieveType retrieve_type)
1153 modest_account_mgr_set_string (self,
1155 MODEST_ACCOUNT_RETRIEVE,
1156 get_retrieve_type_name (retrieve_type),
1157 FALSE /* not server account */);
1162 modest_account_mgr_set_user_fullname (ModestAccountMgr *self,
1163 const gchar *account_name,
1164 const gchar *fullname)
1166 modest_account_mgr_set_string (self,
1168 MODEST_ACCOUNT_FULLNAME,
1170 FALSE /* not server account */);
1174 modest_account_mgr_set_user_email (ModestAccountMgr *self,
1175 const gchar *account_name,
1178 modest_account_mgr_set_string (self,
1180 MODEST_ACCOUNT_EMAIL,
1182 FALSE /* not server account */);
1186 modest_account_mgr_account_is_multimailbox (ModestAccountMgr *mgr,
1187 const gchar *account_name,
1188 ModestProtocol **mmb_protocol)
1190 gchar *transport_account;
1191 gboolean result = FALSE;
1194 *mmb_protocol = NULL;
1196 transport_account = modest_account_mgr_get_server_account_name (mgr,
1198 TNY_ACCOUNT_TYPE_TRANSPORT);
1199 if (transport_account) {
1201 ModestProtocolRegistry *registry;
1203 registry = modest_runtime_get_protocol_registry ();
1205 proto = modest_account_mgr_get_string (mgr, transport_account,
1206 MODEST_ACCOUNT_PROTO, TRUE);
1207 if (proto != NULL) {
1208 ModestProtocol *protocol =
1209 modest_protocol_registry_get_protocol_by_name (registry,
1210 MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
1213 modest_protocol_registry_protocol_type_has_tag
1215 modest_protocol_get_type_id (protocol),
1216 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS)) {
1218 *mmb_protocol = protocol;
1229 modest_account_mgr_get_signature_from_recipient (ModestAccountMgr *mgr,
1230 const gchar *current_recipient,
1231 gboolean *has_signature)
1233 gchar *result = NULL;
1234 gchar *mailbox = NULL;
1235 gchar *account_name;
1236 ModestProtocol *protocol = NULL;
1238 *has_signature = FALSE;
1240 account_name = modest_utils_get_account_name_from_recipient (current_recipient, &mailbox);
1241 if (modest_account_mgr_account_is_multimailbox (mgr, account_name, &protocol)) {
1242 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1243 result = modest_account_protocol_get_signature (MODEST_ACCOUNT_PROTOCOL (protocol),
1244 account_name, mailbox,
1249 if (result == NULL) {
1250 result = modest_account_mgr_get_signature (mgr,
1251 account_name, has_signature);
1257 modest_account_mgr_get_branding_from_recipient (ModestAccountMgr *mgr,
1258 const gchar *current_recipient,
1259 gchar **service_name,
1260 const GdkPixbuf **service_icon,
1263 gchar *mailbox = NULL;
1264 gchar *account_name;
1265 ModestProtocol *protocol = NULL;
1267 *service_name = NULL;
1268 *service_icon = NULL;
1270 account_name = modest_utils_get_account_name_from_recipient (current_recipient, &mailbox);
1271 if (modest_account_mgr_account_is_multimailbox (mgr, account_name, &protocol)) {
1272 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1274 const GdkPixbuf *icon;
1275 name = modest_account_protocol_get_service_name (MODEST_ACCOUNT_PROTOCOL (protocol),
1276 account_name, mailbox);
1277 icon = modest_account_protocol_get_service_icon (MODEST_ACCOUNT_PROTOCOL (protocol),
1278 account_name, mailbox, icon_size);
1279 *service_name = name;
1280 *service_icon = icon;