Fix modest_tny_msg_header_get_all_recipients_list (in case from is empty)
[modest] / src / modest-account-mgr.c
index 4a89001..7f0ee82 100644 (file)
@@ -30,6 +30,7 @@
 #include <string.h>
 #include <modest-marshal.h>
 #include <modest-runtime.h>
+#include <modest-defs.h>
 #include <modest-account-mgr.h>
 #include <modest-account-mgr-priv.h>
 #include <modest-account-mgr-helpers.h>
@@ -56,6 +57,7 @@ enum {
        ACCOUNT_BUSY_SIGNAL,
        DEFAULT_ACCOUNT_CHANGED_SIGNAL,
        DISPLAY_NAME_CHANGED_SIGNAL,
+       ACCOUNT_UPDATED_SIGNAL,
        LAST_SIGNAL
 };
 
@@ -149,6 +151,16 @@ modest_account_mgr_base_init (gpointer g_class)
                                      NULL, NULL,
                                      g_cclosure_marshal_VOID__STRING,
                                      G_TYPE_NONE, 1, G_TYPE_STRING);
+               
+               signals[ACCOUNT_UPDATED_SIGNAL] =
+                       g_signal_new ("account_updated",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass, account_updated),
+                                     NULL, NULL,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING);
+
 
                modest_account_mgr_initialized = TRUE;
        }
@@ -207,9 +219,8 @@ modest_account_mgr_finalize (GObject * obj)
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
        if (priv->notification_id_accounts) {
-               /* TODO: forget dirs */
-
                g_hash_table_destroy (priv->notification_id_accounts);
+               priv->notification_id_accounts = NULL;
        }
 
        if (priv->modest_conf) {
@@ -231,6 +242,12 @@ modest_account_mgr_finalize (GObject * obj)
                priv->account_key_hash = NULL;
        }
 
+       if (priv->busy_accounts) {
+               g_slist_foreach (priv->busy_accounts, (GFunc) g_free, NULL);
+               g_slist_free (priv->busy_accounts);
+               priv->busy_accounts = NULL;
+       }
+
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
@@ -259,6 +276,89 @@ null_means_empty (const gchar * str)
        return str ? str : "";
 }
 
+gboolean
+modest_account_mgr_add_account_from_settings (ModestAccountMgr *self,
+                                             ModestAccountSettings *settings)
+{
+       ModestAccountMgrPrivate *priv;
+       const gchar* display_name;
+       gchar *account_name_start, *account_name;
+       gchar *store_name_start, *store_name;
+       gchar *transport_name_start, *transport_name;
+       gchar *default_account;
+       ModestServerAccountSettings *store_settings, *transport_settings;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR (self), FALSE);
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings), FALSE);
+
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       display_name = modest_account_settings_get_display_name (settings);
+
+       /* We should have checked for this already, and changed that name accordingly, 
+        * but let's check again just in case */
+       if (!display_name || 
+           modest_account_mgr_account_with_display_name_exists (self, display_name)) {
+               display_name = _("mcen_ia_emailsetup_defaultname");
+       }
+
+       /* Increment the non-user visible name if necessary, 
+        * based on the display name: */
+       account_name_start = g_strdup_printf ("%sID", display_name);
+       account_name = modest_account_mgr_get_unused_account_name (self,
+                                                                  account_name_start, FALSE /* not a server account */);
+       g_free (account_name_start);
+       
+       /* Add a (incoming) server account, to be used by the account: */
+       store_name_start = g_strconcat (account_name, "_store", NULL);
+       store_name = modest_account_mgr_get_unused_account_name (self, 
+                                                                store_name_start, TRUE /* server account */);
+       g_free (store_name_start);
+       
+       /* Add a (outgoing) server account to be used by the account: */
+       transport_name_start = g_strconcat (account_name, "_transport", NULL);
+       transport_name = modest_account_mgr_get_unused_account_name (self, 
+                                                                    transport_name_start, TRUE /* server account */);
+       g_free (transport_name_start);
+
+       modest_account_settings_set_account_name (settings, account_name);
+       store_settings = modest_account_settings_get_store_settings (settings);
+       modest_server_account_settings_set_account_name (store_settings, store_name);
+       transport_settings = modest_account_settings_get_transport_settings (settings);
+       modest_server_account_settings_set_account_name (transport_settings, transport_name);
+       g_object_unref (store_settings);
+       g_object_unref (transport_settings);
+
+       /* Create the account, which will contain the two "server accounts": */
+       modest_account_mgr_save_account_settings (self, settings);
+       g_free (store_name);
+       g_free (transport_name);
+       
+       /* Sanity check: */
+       /* There must be at least one account now: */
+       /* Note, when this fails is is caused by a Maemo gconf bug that has been 
+        * fixed in versions after 3.1. */
+       if(!modest_account_mgr_has_accounts (self, FALSE))
+               g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
+                               
+       /* Notify the observers */
+       g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, account_name);
+
+       /* if no default account has been defined yet, do so now */
+       default_account = modest_account_mgr_get_default_account (self);
+       if (!default_account) {
+               modest_account_mgr_set_default_account (self, account_name);
+               modest_account_settings_set_is_default (settings, TRUE);
+       }
+       g_free (default_account);
+       g_free (account_name);
+
+       /* (re)set the automatic account update */
+       modest_platform_set_update_interval
+               (modest_conf_get_int (priv->modest_conf, MODEST_CONF_UPDATE_INTERVAL, NULL));
+
+       return TRUE;
+}
+
 
 gboolean
 modest_account_mgr_add_account (ModestAccountMgr *self,
@@ -266,7 +366,7 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
                                const gchar *display_name,
                                const gchar *user_fullname,
                                const gchar *user_email,
-                               const gchar *retrieve_type,
+                               ModestAccountRetrieveType retrieve_type,
                                const gchar *store_account,
                                const gchar *transport_account,
                                gboolean enabled)
@@ -351,9 +451,8 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
        modest_account_mgr_set_string (self, name,
                                       MODEST_ACCOUNT_EMAIL, 
                                       user_email, FALSE);
-       modest_account_mgr_set_string (self, name,
-                                      MODEST_ACCOUNT_RETRIEVE, 
-                                      retrieve_type, FALSE);
+       modest_account_mgr_set_retrieve_type (self, name,
+                                             retrieve_type);
 
        /* Notify the observers */
        g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
@@ -379,19 +478,21 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
                                       guint portnumber,
                                       const gchar *username, 
                                       const gchar *password,
-                                      ModestTransportStoreProtocol proto,
-                                      ModestConnectionProtocol security,
-                                      ModestAuthProtocol auth)
+                                      ModestProtocolType proto,
+                                      ModestProtocolType security,
+                                      ModestProtocolType auth)
 {
        ModestAccountMgrPrivate *priv;
        const gchar *key;
        gboolean ok = TRUE;
        GError *err = NULL;
+       ModestProtocolRegistry *protocol_registry;
 
        g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
-                             
+
+       protocol_registry = modest_runtime_get_protocol_registry ();
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
        /* hostname */
@@ -438,7 +539,7 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        /* proto */
        key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key,
-                                    modest_protocol_info_get_transport_store_protocol_name(proto),
+                                    modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
                                     &err);
        if (err) {
                g_printerr ("modest: failed to set %s: %s\n", key, err->message);
@@ -464,7 +565,7 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        /* auth mechanism */
        key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key,
-                                    modest_protocol_info_get_auth_protocol_name (auth),
+                                    modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, auth)),
                                     &err);
        if (err) {
                g_printerr ("modest: failed to set %s: %s\n", key, err->message);
@@ -492,12 +593,13 @@ cleanup:
 gboolean
 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
                                           const gchar *name, 
-                                          ModestTransportStoreProtocol proto,
+                                          ModestProtocolType proto,
                                           const gchar *uri)
 {
        ModestAccountMgrPrivate *priv;
        const gchar *key;
        gboolean ok;
+       ModestProtocolRegistry *protocol_registry;
        
        g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
@@ -505,11 +607,12 @@ modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
        g_return_val_if_fail (uri, FALSE);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       protocol_registry = modest_runtime_get_protocol_registry ();
        
        /* proto */
        key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key,
-                                    modest_protocol_info_get_transport_store_protocol_name(proto),
+                                    modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
                                     NULL);
 
        if (!ok) {
@@ -597,11 +700,6 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
                   deleted account */
                modest_account_mgr_set_first_account_as_default (self);
        }
-       
-       /* Notify the observers. We do this *after* deleting
-          the keys, because otherwise a call to account_names
-          will retrieve also the deleted account */
-       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
 
        /* if this was the last account, stop any auto-updating */
        /* (re)set the automatic account update */
@@ -615,6 +713,31 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
        } else
                modest_account_mgr_free_account_names (acc_names);
        
+       /* Notify the observers. We do this *after* deleting
+          the keys, because otherwise a call to account_names
+          will retrieve also the deleted account */
+       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
+       
+       return TRUE;
+}
+
+gboolean
+modest_account_mgr_remove_server_account (ModestAccountMgr * self,
+                                         const gchar* name)
+{
+       ModestAccountMgrPrivate *priv;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
+       g_return_val_if_fail (name, FALSE);
+
+       if (!modest_account_mgr_account_exists (self, name, TRUE)) {
+               g_printerr ("modest: %s: server account '%s' does not exist\n", __FUNCTION__, name);
+               return FALSE;
+       }
+
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       real_remove_account (priv->modest_conf, name, TRUE);
+
        return TRUE;
 }
 
@@ -643,7 +766,8 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
        ModestAccountMgrPrivate *priv;
        GError *err = NULL;
        
-       const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
+       /* we add 1 for the trailing "/" */
+       const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE) + 1;
 
        g_return_val_if_fail (self, NULL);
 
@@ -665,9 +789,11 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
        /* Unescape the keys to get the account names: */
        GSList *iter = accounts;
        while (iter) {
-               if (!(iter->data))
+               if (!(iter->data)) {
+                       iter = iter->next;
                        continue;
-                       
+               }
+
                const gchar* account_name_key = (const gchar*)iter->data;
                gchar* unescaped_name = account_name_key ? 
                        modest_conf_key_unescape (account_name_key) 
@@ -833,7 +959,7 @@ modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
                             const gchar *key, ModestConfValueType list_type,
                             gboolean server_account)
 {
-       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       ModestAccountMgrPrivate *priv = NULL;
 
        const gchar *keyname;
        GSList *retval;
@@ -853,7 +979,7 @@ modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
                g_printerr ("modest: error getting list '%s': %s\n", keyname,
                            err->message);
                g_error_free (err);
-               retval = FALSE;
+               retval = NULL;
        }
        return retval;
 }
@@ -894,7 +1020,6 @@ modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
                            const gchar * key, int val, gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
-
        const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
@@ -912,6 +1037,13 @@ modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
                g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
                g_error_free (err);
                retval = FALSE;
+       } else {
+               /* check whether this field is one of those interesting for the 
+                * "account-updated" signal */
+               if (strcmp(key, MODEST_ACCOUNT_LAST_UPDATED) == 0) {
+                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATED_SIGNAL], 
+                                       0, name);
+               }
        }
        return retval;
 }
@@ -1017,20 +1149,24 @@ modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self,
        
        /* Look at each non-server account to check their display names; */
        while (cursor) {
-               const gchar * account_name = (gchar*)cursor->data;
+               const gchar *account_name = (gchar*)cursor->data;
+               const gchar *cursor_display_name;
                
-               ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
-               if (!account_data) {
+               ModestAccountSettings *settings = modest_account_mgr_load_account_settings (self, account_name);
+               if (!settings) {
                        g_printerr ("modest: failed to get account data for %s\n", account_name);
+                       cursor = cursor->next;
                        continue;
                }
 
-               if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
+               cursor_display_name = modest_account_settings_get_display_name (settings);
+               if(cursor_display_name && (strcmp (cursor_display_name, display_name) == 0)) {
                        found = TRUE;
+                       g_object_unref (settings);
                        break;
                }
 
-               modest_account_mgr_free_account_data (self, account_data);
+               g_object_unref (settings);
                cursor = cursor->next;
        }
        modest_account_mgr_free_account_names (account_names);
@@ -1039,6 +1175,89 @@ modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self,
        return found;
 }
 
+static gboolean
+server_accounts_equal (ModestServerAccountSettings *s1,
+                      ModestServerAccountSettings *s2)
+{
+       const gchar *str1, *str2;
+
+       if (modest_server_account_settings_get_protocol (s1) !=
+           modest_server_account_settings_get_protocol (s2))
+               return FALSE;
+
+       str1 = modest_server_account_settings_get_username (s1);
+       str2 = modest_server_account_settings_get_username (s2);
+       if (str1 && str2 && (str1 != str2) &&
+           strcmp (str1, str2) != 0)
+               return FALSE;
+
+       str1 = modest_server_account_settings_get_hostname (s1);
+       str2 = modest_server_account_settings_get_hostname (s2);
+       if (str1 && str2 && (str1 != str2) &&
+           strcmp (str1, str2) != 0)
+               return FALSE;
+
+       if (modest_server_account_settings_get_port (s1) !=
+           modest_server_account_settings_get_port (s2))
+               return FALSE;
+
+       return TRUE;
+}
+
+gboolean
+modest_account_mgr_check_already_configured_account  (ModestAccountMgr *self, 
+                                                     ModestAccountSettings *settings)
+{
+       GSList *account_names = NULL;
+       GSList *cursor = NULL;
+       ModestServerAccountSettings *server_settings;
+       
+       cursor = account_names = modest_account_mgr_account_names (self, 
+                                                                  TRUE /* enabled accounts, because disabled accounts are not user visible. */);
+
+       gboolean found = FALSE;
+
+       server_settings = modest_account_settings_get_store_settings (settings);
+       if (!server_settings) {
+               g_printerr ("modest: couldn't get store settings from settings");
+               modest_account_mgr_free_account_names (account_names);
+               return FALSE;
+       }
+       
+       /* Look at each non-server account to check their display names; */
+       while (cursor && !found) {
+               const gchar *account_name;
+               ModestAccountSettings *from_mgr_settings;
+               ModestServerAccountSettings *from_mgr_server_settings;
+
+               account_name = (gchar*)cursor->data;            
+               from_mgr_settings = modest_account_mgr_load_account_settings (self, account_name);
+               if (!settings) {
+                       g_printerr ("modest: failed to get account data for %s\n", account_name);
+                       cursor = cursor->next;
+                       continue;
+               }
+
+               from_mgr_server_settings = modest_account_settings_get_store_settings (from_mgr_settings);
+               if (server_settings) {
+                       if (server_accounts_equal (from_mgr_server_settings, server_settings)) {
+                               found = TRUE;
+                       }
+                       g_object_unref (from_mgr_server_settings);
+               } else {
+                       g_printerr ("modest: couldn't get store settings from account %s", account_name);
+               }
+               g_object_unref (from_mgr_settings);
+               cursor = cursor->next;
+       }
+
+       g_object_unref (server_settings);
+       modest_account_mgr_free_account_names (account_names);
+       account_names = NULL;
+       
+       return found;
+}
+
 
 
 
@@ -1072,6 +1291,11 @@ modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
 gchar*
 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
 {
+       const gchar* account_ns;
+       const gchar* server_account_ns;
+       gchar *account = NULL;
+       gchar *unescaped_name = NULL;
+
        /* Initialize input parameters: */
        if (is_account_key)
                *is_account_key = FALSE;
@@ -1079,10 +1303,8 @@ _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key
        if (is_server_account)
                *is_server_account = FALSE;
 
-       const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
-       const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
-       gchar *cursor;
-       gchar *account = NULL;
+       account_ns        = modest_defs_namespace (MODEST_ACCOUNT_SUBNAMESPACE "/");
+       server_account_ns = modest_defs_namespace (MODEST_SERVER_ACCOUNT_SUBNAMESPACE "/");
 
        /* determine whether it's an account or a server account,
         * based on the prefix */
@@ -1090,37 +1312,40 @@ _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key
 
                if (is_server_account)
                        *is_server_account = FALSE;
-               
+
                account = g_strdup (key + strlen (account_ns));
 
        } else if (g_str_has_prefix (key, server_account_ns)) {
 
                if (is_server_account)
                        *is_server_account = TRUE;
-               
-               account = g_strdup (key + strlen (server_account_ns));  
+
+               account = g_strdup (key + strlen (server_account_ns));
        } else
                return NULL;
 
-       /* if there are any slashes left in the key, it's not
-        * the toplevel entry for an account
-        */
-       cursor = strstr(account, "/");
-       
-       if (is_account_key && cursor)
-               *is_account_key = TRUE;
+       if (account) {
+               gchar *cursor;
 
-       /* put a NULL where the first slash was */
-       if (cursor)
-               *cursor = '\0';
+               /* if there are any slashes left in the key, it's not
+                * the toplevel entry for an account
+                */
+               cursor = strstr(account, "/");
 
-       if (account) {
-               /* The key is an escaped string, so unescape it to get the actual account name: */
-               gchar *unescaped_name = modest_conf_key_unescape (account);
+               if (cursor) {
+                       if (is_account_key)
+                               *is_account_key = TRUE;
+
+                       /* put a NULL where the first slash was */
+                       *cursor = '\0';
+               }
+
+               /* The key is an escaped string, so unescape it to get the actual account name */
+               unescaped_name = modest_conf_key_unescape (account);
                g_free (account);
-               return unescaped_name;
-       } else
-               return NULL;
+       }
+
+       return unescaped_name;
 }
 
 
@@ -1151,7 +1376,7 @@ _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar*
                                         gboolean server_account)
 {
        gchar *retval = NULL;   
-       gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
+       gchar *namespace = server_account ? (gchar *) MODEST_SERVER_ACCOUNT_NAMESPACE : (gchar *) MODEST_ACCOUNT_NAMESPACE;
        gchar *escaped_account_name, *escaped_name;
        
        if (!account_name)
@@ -1320,23 +1545,27 @@ void
 modest_account_mgr_notify_account_update (ModestAccountMgr* self, 
                                          const gchar *server_account_name)
 {
-       ModestTransportStoreProtocol proto;
+       ModestProtocolType proto;
        ModestAccountMgrPrivate* priv;
+       ModestProtocolRegistry *protocol_registry;
        gchar *proto_name = NULL;
 
        g_return_if_fail (self);
        g_return_if_fail (server_account_name);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       protocol_registry = modest_runtime_get_protocol_registry ();
        
        /* Get protocol */
        proto_name = modest_account_mgr_get_string (self, server_account_name, 
                                                    MODEST_ACCOUNT_PROTO, TRUE);
        if (!proto_name) {
-               g_free (proto_name);
                g_return_if_reached ();
+               return;
        }
-       proto = modest_protocol_info_get_transport_store_protocol (proto_name);
+       proto = modest_protocol_get_type_id (modest_protocol_registry_get_protocol_by_name (protocol_registry,
+                                                                                           MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
+                                                                                           proto_name));
        g_free (proto_name);
 
        /* there is some update in the account, so we can't
@@ -1349,7 +1578,7 @@ modest_account_mgr_notify_account_update (ModestAccountMgr* self,
        g_signal_emit (G_OBJECT(self), 
                       signals[ACCOUNT_CHANGED_SIGNAL], 0, 
                       server_account_name, 
-                      (modest_protocol_info_protocol_is_store (proto)) ? 
+                      (modest_protocol_registry_protocol_type_has_tag (protocol_registry, proto, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) ? 
                       TNY_ACCOUNT_TYPE_STORE : 
                       TNY_ACCOUNT_TYPE_TRANSPORT);
 }
@@ -1393,7 +1622,6 @@ modest_account_mgr_get_default_account  (ModestAccountMgr *self)
                g_printerr ("modest: failed to get '%s': %s\n",
                            MODEST_CONF_DEFAULT_ACCOUNT, err->message);
                g_error_free (err);
-               g_free (account);
                return  NULL;
        }
        
@@ -1438,6 +1666,11 @@ modest_account_mgr_set_display_name (ModestAccountMgr *self,
                                     const gchar *account_name,
                                     const gchar *display_name)
 {
+       gboolean notify = TRUE;
+
+       if (!modest_account_mgr_get_display_name (self, account_name))
+               notify = FALSE;
+
        modest_account_mgr_set_string (self, 
                                       account_name,
                                       MODEST_ACCOUNT_DISPLAY_NAME, 
@@ -1445,5 +1678,31 @@ modest_account_mgr_set_display_name (ModestAccountMgr *self,
                                       FALSE /* not server account */);
 
        /* Notify about the change in the display name */
-       g_signal_emit (self, signals[DISPLAY_NAME_CHANGED_SIGNAL], 0, account_name);
+       if (notify)
+               g_signal_emit (self, signals[DISPLAY_NAME_CHANGED_SIGNAL], 0, account_name);
+}
+
+gboolean 
+modest_account_mgr_singleton_protocol_exists (ModestAccountMgr *mgr,
+                                             ModestProtocolType protocol_type)
+{
+       GSList *account_names, *node;
+       gboolean found = FALSE;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR (mgr), FALSE);
+       account_names = modest_account_mgr_account_names (mgr, TRUE);
+
+       for (node = account_names; node != NULL; node = g_slist_next (node)) {
+               ModestProtocolType current_protocol;
+
+               current_protocol = modest_account_mgr_get_store_protocol (mgr, (gchar *) node->data);
+               if (current_protocol == protocol_type) {
+                       found = TRUE;
+                       break;
+               }
+       }
+
+       modest_account_mgr_free_account_names (account_names);
+
+       return found;
 }