* Fixes NB#91163, removed an invalid g_free
[modest] / src / modest-account-mgr.c
index b9e6727..f77e247 100644 (file)
 
 #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>
+#include <modest-platform.h>
 
 /* 'private'/'protected' functions */
 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
@@ -39,6 +42,13 @@ static void modest_account_mgr_init       (ModestAccountMgr * obj);
 static void modest_account_mgr_finalize   (GObject * obj);
 static void modest_account_mgr_base_init  (gpointer g_class);
 
+static const gchar *_modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, 
+                                                                   const gchar* account_name,
+                                                                   const gchar *name, 
+                                                                   gboolean is_server);
+
+static gboolean modest_account_mgr_unset_default_account (ModestAccountMgr *self);
+
 /* list my signals */
 enum {
        ACCOUNT_INSERTED_SIGNAL,
@@ -46,6 +56,8 @@ enum {
        ACCOUNT_REMOVED_SIGNAL,
        ACCOUNT_BUSY_SIGNAL,
        DEFAULT_ACCOUNT_CHANGED_SIGNAL,
+       DISPLAY_NAME_CHANGED_SIGNAL,
+       ACCOUNT_UPDATED_SIGNAL,
        LAST_SIGNAL
 };
 
@@ -53,166 +65,6 @@ enum {
 static GObjectClass *parent_class = NULL;
 static guint signals[LAST_SIGNAL] = {0};
 
-/* is the account already in the queue? */
-static gboolean
-in_change_queue (GSList *change_queue, const gchar *account)
-{
-       GSList *cursor = change_queue;
-       while (cursor) {
-               const gchar *acc = cursor->data;
-               if (acc && strcmp (acc, account) == 0)
-                       return TRUE;
-               cursor = g_slist_next (cursor);
-       }
-       return FALSE;
-}
-
-static GSList*
-add_to_change_queue (GSList *change_queue, const gchar *account_name)
-{
-       g_return_val_if_fail (account_name, change_queue);      
-       return g_slist_prepend (change_queue, g_strdup (account_name));
-}
-
-
-/* we don't need to track specific accounts, as in our UI case
- * it's impossible to change two accounts within .5 seconds.
- * still, we might want to allow that later, and then this func
- * will come in handy */
-#if 0
-static GSList*
-remove_from_queue (GSList *change_queue, const gchar *account)
-{
-       GSList *cursor = change_queue;
-       while (cursor) {
-               const gchar *acc = cursor->data;
-               if (acc && strcmp (acc, account) == 0) {
-                       g_free (acc);
-                       return g_slist_delete_link (change_queue, cursor);
-               }
-               cursor = g_slist_next (cursor);
-       }
-       return change_queue;
-}
-#endif
-
-static gboolean
-on_timeout_notify_changes (gpointer data)
-{
-       ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
-       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-               
-       GSList *cursor = priv->change_queue;
-       while (cursor) {
-               const gchar *account = cursor->data;
-               if (account)
-                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
-                                      account);
-               cursor = g_slist_next (cursor);
-       }
-       
-       /* free our queue */
-       g_slist_foreach (priv->change_queue, (GFunc)g_free, NULL);
-       g_slist_free (priv->change_queue);
-       priv->change_queue = NULL;
-       priv->timeout = 0; /* hmmm */
-       
-       return FALSE; /* don't call me again */
-}
-
-
-/* little hack to retrieve the account name from a server account name,
- * by relying on the convention for that. Note: this changes the
- * string in-place
- *
- * server accounts look like fooID_transport or fooID_store
- * FIXME: make the suffixes more explicit in the account setup
- */
-static void
-get_account_name_from_server_account (gchar *server_account_name)
-{
-       static const gchar *t = "ID_transport";
-       static const gchar *s = "ID_store";
-       const guint len_t = strlen (t);
-       const guint len_s = strlen (s);
-       
-       guint len_a = strlen (server_account_name);
-               
-       if (g_str_has_suffix (server_account_name, t)) 
-               server_account_name [len_a - len_t] = '\0';
-       else if (g_str_has_suffix (server_account_name, s)) 
-               server_account_name [len_a - len_s] = '\0';
-}
-
-
-
-static void
-on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event,
-              ModestConfNotificationId id, gpointer user_data)
-{
-       ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data);
-       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-
-       gboolean is_account_key;
-       gboolean is_server_account;
-       gchar* account_name = NULL;
-
-       /* Notify that the default account has changed */
-       if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
-               g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
-               return;
-       }
-       
-       is_account_key = FALSE;
-       is_server_account = FALSE;
-       account_name = _modest_account_mgr_account_from_key (key, &is_account_key,
-                                                            &is_server_account);
-
-       /* if this is not an account-related key change, ignore */
-       if (!account_name)
-               return;
-
-       /* account was removed. Do not emit an account removed signal
-          because it was already being done in the remove_account
-          method. Do not notify also the removal of the server
-          account keys for the same reason */
-       if ((is_account_key || is_server_account) &&
-           event == MODEST_CONF_EVENT_KEY_UNSET) {
-               g_free (account_name);
-               return;
-       }
-
-       /* ignore server account changes */
-       if (is_server_account)
-               /* change in place: retrieve the parent account name */
-               get_account_name_from_server_account (account_name);
-
-       /* is this account enabled? */
-       gboolean enabled = FALSE;
-       if (is_server_account)
-               enabled = TRUE;
-       else
-               enabled = modest_account_mgr_get_enabled (self, account_name);
-
-       /* Notify is server account was changed, default account was changed
-        * or when enabled/disabled changes:
-        */
-       if (!is_server_account)
-       if (enabled || g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) ||
-           strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
-               if (!in_change_queue (priv->change_queue, account_name)) {
-                       priv->change_queue = add_to_change_queue (priv->change_queue,
-                                                                 account_name);
-                       /* hmm, small race when this object is destroyed within
-                        * 500ms of the last change, and there are multiple timeouts... */
-                       priv->timeout = g_timeout_add (500, (GSourceFunc) on_timeout_notify_changes,
-                                                      self);
-               }
-       }
-       g_free (account_name);
-}
-
-
 GType
 modest_account_mgr_get_type (void)
 {
@@ -270,8 +122,8 @@ modest_account_mgr_base_init (gpointer g_class)
                                      G_SIGNAL_RUN_FIRST,
                                      G_STRUCT_OFFSET(ModestAccountMgrClass, account_changed),
                                      NULL, NULL,
-                                     g_cclosure_marshal_VOID__STRING,
-                                     G_TYPE_NONE, 1, G_TYPE_STRING);
+                                     modest_marshal_VOID__STRING_INT,
+                                     G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
 
                signals[ACCOUNT_BUSY_SIGNAL] =
                        g_signal_new ("account_busy_changed",
@@ -291,6 +143,25 @@ modest_account_mgr_base_init (gpointer g_class)
                                      g_cclosure_marshal_VOID__VOID,
                                      G_TYPE_NONE, 0);
 
+               signals[DISPLAY_NAME_CHANGED_SIGNAL] =
+                       g_signal_new ("display_name_changed",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass, display_name_changed),
+                                     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;
        }
 }
@@ -317,10 +188,28 @@ modest_account_mgr_init (ModestAccountMgr * obj)
 
        priv->modest_conf   = NULL;
        priv->busy_accounts = NULL;
-       priv->change_queue  = NULL;
        priv->timeout       = 0;
        
        priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
+
+       /* we maintain hashes for the modest-conf keys we build from account name
+        * + key. many seem to be used often, and generating them showed up high
+        * in oprofile */
+       /* both hashes are hashes to hashes;
+        * account-key => keyname ==> account-key-name
+        */     
+       priv->server_account_key_hash = g_hash_table_new_full (g_str_hash,
+                                                              g_str_equal,
+                                                              g_free,
+                                                              (GDestroyNotify)g_hash_table_destroy);
+       priv->account_key_hash        = g_hash_table_new_full (g_str_hash,
+                                                              g_str_equal,
+                                                              g_free,
+                                                              (GDestroyNotify)g_hash_table_destroy);
+
+       /* FALSE means: status is unknown */
+       priv->has_accounts = FALSE;
+       priv->has_enabled_accounts = FALSE;
 }
 
 static void
@@ -330,15 +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);
-       }
-
-       if (priv->key_changed_handler_uid) {
-               g_signal_handler_disconnect (priv->modest_conf, 
-                                            priv->key_changed_handler_uid);
-               priv->key_changed_handler_uid = 0;
+               priv->notification_id_accounts = NULL;
        }
 
        if (priv->modest_conf) {
@@ -350,6 +232,21 @@ modest_account_mgr_finalize (GObject * obj)
                g_source_remove (priv->timeout);
        priv->timeout = 0;
 
+       if (priv->server_account_key_hash) {
+               g_hash_table_destroy (priv->server_account_key_hash);
+               priv->server_account_key_hash = NULL;
+       }
+       
+       if (priv->account_key_hash) {
+               g_hash_table_destroy (priv->account_key_hash);
+               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);
 }
@@ -369,11 +266,6 @@ modest_account_mgr_new (ModestConf *conf)
        g_object_ref (G_OBJECT(conf));
        priv->modest_conf = conf;
 
-       priv->key_changed_handler_uid = 
-               g_signal_connect (G_OBJECT (conf), "key_changed",
-                                 G_CALLBACK (on_key_change),
-                                 obj);
-       
        return MODEST_ACCOUNT_MGR (obj);
 }
 
@@ -384,16 +276,103 @@ 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,
                                const gchar *name,
+                               const gchar *display_name,
+                               const gchar *user_fullname,
+                               const gchar *user_email,
+                               ModestAccountRetrieveType retrieve_type,
                                const gchar *store_account,
                                const gchar *transport_account,
                                gboolean enabled)
 {
        ModestAccountMgrPrivate *priv;
-       gchar *key;
+       const gchar *key;
        gboolean ok;
        gchar *default_account;
        GError *err = NULL;
@@ -408,15 +387,14 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
         * we create the account by adding an account 'dir', with the name <name>,
         * and in that the 'display_name' string key
         */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
+       key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_DISPLAY_NAME,
+                                                             FALSE);
        if (modest_account_mgr_account_exists (self, key, FALSE)) {
                g_printerr ("modest: account already exists\n");
-               g_free (key);
                return FALSE;
        }
        
        ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
-       g_free (key);
        if (!ok) {
                g_printerr ("modest: cannot set display name\n");
                if (err) {
@@ -427,12 +405,12 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
        }
        
        if (store_account) {
-               key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
+               key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_STORE_ACCOUNT,
+                                                                     FALSE);
                ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
-               g_free (key);
                if (!ok) {
                        g_printerr ("modest: failed to set store account '%s'\n",
-                               store_account);
+                                   store_account);
                        if (err) {
                                g_printerr ("modest: Error adding store account conf: %s\n", err->message);
                                g_error_free (err);
@@ -442,10 +420,10 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
        }
        
        if (transport_account) {
-               key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
-                                                              FALSE);
+               key = _modest_account_mgr_get_account_keyname_cached (priv, name,
+                                                                     MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
+                                                                     FALSE);
                ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
-               g_free (key);
                if (!ok) {
                        g_printerr ("modest: failed to set transport account '%s'\n",
                                    transport_account);
@@ -460,11 +438,21 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
        /* Make sure that leave-messages-on-server is enabled by default, 
         * as per the UI spec, though it is only meaningful for accounts using POP.
         * (possibly this gconf key should be under the server account): */
-       modest_account_mgr_set_bool (self, name,
-               MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE /* not server account */);
-
-
-       modest_account_mgr_set_enabled (self, name, enabled);
+       modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE);
+       modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
+
+       /* Fill other data */
+       modest_account_mgr_set_string (self, name,
+                                      MODEST_ACCOUNT_DISPLAY_NAME, 
+                                      display_name, FALSE);
+       modest_account_mgr_set_string (self, name,
+                                      MODEST_ACCOUNT_FULLNAME, 
+                                      user_fullname, FALSE);
+       modest_account_mgr_set_string (self, name,
+                                      MODEST_ACCOUNT_EMAIL, 
+                                      user_email, FALSE);
+       modest_account_mgr_set_retrieve_type (self, name,
+                                             retrieve_type);
 
        /* Notify the observers */
        g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
@@ -474,36 +462,43 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
        if (!default_account)
                modest_account_mgr_set_default_account (self, name);
        g_free (default_account);
-
+       
+       /* (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_server_account (ModestAccountMgr * self,
-                                      const gchar * name, const gchar *hostname,
+                                      const gchar *name, 
+                                      const gchar *hostname,
                                       guint portnumber,
-                                      const gchar * username, const gchar * password,
-                                      ModestTransportStoreProtocol proto,
-                                      ModestConnectionProtocol security,
-                                      ModestAuthProtocol auth)
+                                      const gchar *username, 
+                                      const gchar *password,
+                                      ModestProtocolType proto,
+                                      ModestProtocolType security,
+                                      ModestProtocolType auth)
 {
        ModestAccountMgrPrivate *priv;
-       gchar *key;
+       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 */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
+       key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_HOSTNAME, TRUE);
        if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
                g_printerr ("modest: server account '%s' already exists\n", name);
-               g_free (key);
                ok =  FALSE;
        }
        if (!ok)
@@ -515,80 +510,74 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
                g_error_free (err);
                ok = FALSE;
        }
-       g_free (key);
        if (!ok)
                goto cleanup;
        
        /* username */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_USERNAME, TRUE);
+       key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_USERNAME, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
        if (err) {
                g_printerr ("modest: failed to set %s: %s\n", key, err->message);
                g_error_free (err);
                ok = FALSE;
        }
-       g_free (key);
        if (!ok)
                goto cleanup;
        
        
        /* password */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PASSWORD, TRUE);
+       key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PASSWORD, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
        if (err) {
                g_printerr ("modest: failed to set %s: %s\n", key, err->message);
                g_error_free (err);
                ok = FALSE;
        }
-       g_free (key);
        if (!ok)
                goto cleanup;
 
        /* proto */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
+       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);
                g_error_free (err);
                ok = FALSE;
        }
-       g_free (key);
        if (!ok)
                goto cleanup;
 
 
        /* portnumber */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PORT, TRUE);
+       key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PORT, TRUE);
        ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
        if (err) {
                g_printerr ("modest: failed to set %s: %s\n", key, err->message);
                g_error_free (err);
                ok = FALSE;
        }
-       g_free (key);
        if (!ok)
                goto cleanup;
 
        
        /* auth mechanism */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
+       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);
                g_error_free (err);
                ok = FALSE;
        }
-       g_free (key);
        if (!ok)
                goto cleanup;
        
        /* Add the security settings: */
-       modest_server_account_set_security (self, name, security);
-       
+       modest_account_mgr_set_server_account_security (self, name, security);
+
 cleanup:
        if (!ok) {
                g_printerr ("modest: failed to add server account\n");
@@ -603,12 +592,14 @@ cleanup:
  */
 gboolean
 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
-                                          const gchar *name, ModestTransportStoreProtocol proto,
+                                          const gchar *name, 
+                                          ModestProtocolType proto,
                                           const gchar *uri)
 {
        ModestAccountMgrPrivate *priv;
-       gchar *key;
+       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);
@@ -616,14 +607,13 @@ 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 (name, MODEST_ACCOUNT_PROTO, TRUE);
+       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);
-       g_free (key);
 
        if (!ok) {
                g_printerr ("modest: failed to set proto\n");
@@ -631,9 +621,8 @@ modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
        }
        
        /* uri */
-       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_URI, TRUE);
+       key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_URI, TRUE);
        ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
-       g_free (key);
 
        if (!ok) {
                g_printerr ("modest: failed to set uri\n");
@@ -651,16 +640,16 @@ real_remove_account (ModestConf *conf,
                     gboolean server_account)
 {
        GError *err = NULL;
-       gchar *key = NULL;
-
+       gchar *key;
+       
        key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
        modest_conf_remove_key (conf, key, &err);
-       g_free (key);       
 
        if (err) {
                g_printerr ("modest: error removing key: %s\n", err->message);
                g_error_free (err);
        }
+       g_free (key);
 }
 
 gboolean
@@ -711,12 +700,24 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
                   deleted account */
                modest_account_mgr_set_first_account_as_default (self);
        }
+
+       /* if this was the last account, stop any auto-updating */
+       /* (re)set the automatic account update */
+       GSList *acc_names = modest_account_mgr_account_names (self, TRUE);
+       if (!acc_names) {
+               modest_platform_set_update_interval (0);
+               /* it was the last account, the has_account / has_enabled_account
+                * changes
+                */
+               priv->has_accounts = priv->has_enabled_accounts = FALSE; 
+       } 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;
 }
 
@@ -778,9 +779,9 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
                gboolean add = TRUE;
                if (only_enabled) {
                        if (unescaped_name && 
-                               !modest_account_mgr_get_enabled (self, unescaped_name)) {
+                           !modest_account_mgr_get_bool (self, unescaped_name, 
+                                                         MODEST_ACCOUNT_ENABLED, FALSE))
                                add = FALSE;
-                       }
                }
                
                /* Ignore modest accounts whose server accounts don't exist: 
@@ -789,8 +790,9 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
                 * parent modest account already exists.
                 */
                if (add) {
-                       gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
-                                                                           FALSE);
+                       gchar* server_account_name = modest_account_mgr_get_string
+                               (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
+                                FALSE);
                        if (server_account_name) {
                                if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
                                        add = FALSE;
@@ -799,8 +801,9 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
                }
                
                if (add) {
-                       gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
-                                                                           FALSE);
+                       gchar* server_account_name = modest_account_mgr_get_string
+                               (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
+                                FALSE);
                        if (server_account_name) {
                                if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
                                        add = FALSE;
@@ -808,7 +811,7 @@ modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled
                        }
                }
                
-               if (add)        
+               if (add)
                        result = g_slist_append (result, unescaped_name);
                else 
                        g_free (unescaped_name);
@@ -843,7 +846,7 @@ modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
 
        ModestAccountMgrPrivate *priv;
 
-       gchar *keyname;
+       const gchar *keyname;
        gchar *retval;
        GError *err = NULL;
 
@@ -851,7 +854,9 @@ modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
        g_return_val_if_fail (name, NULL);
        g_return_val_if_fail (key, NULL);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
        retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
@@ -860,29 +865,18 @@ modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
                g_error_free (err);
                retval = NULL;
        }
-       g_free (keyname);
 
        return retval;
 }
 
 
-gchar *
-modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
-                              const gchar *key, gboolean server_account)
-{
-       return modest_account_mgr_get_string (self, name, key, server_account);
-
-}
-
-
-
 gint
 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
                            gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
-       gchar *keyname;
+       const gchar *keyname;
        gint retval;
        GError *err = NULL;
        
@@ -890,16 +884,16 @@ modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gch
        g_return_val_if_fail (name, -1);
        g_return_val_if_fail (key, -1);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
-
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
+       
        retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
        if (err) {
                g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
                g_error_free (err);
                retval = -1;
        }
-       g_free (keyname);
 
        return retval;
 }
@@ -912,7 +906,7 @@ modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
 {
        ModestAccountMgrPrivate *priv;
 
-       gchar *keyname;
+       const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
 
@@ -920,16 +914,17 @@ modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
        g_return_val_if_fail (account, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
-       
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       ///keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
+
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, account, key, server_account);
+               
        retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
        if (err) {
                g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
                g_error_free (err);
                retval = FALSE;
        }
-       g_free (keyname);
 
        return retval;
 }
@@ -941,39 +936,42 @@ modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
                             const gchar *key, ModestConfValueType list_type,
                             gboolean server_account)
 {
-       ModestAccountMgrPrivate *priv;
+       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
-       gchar *keyname;
+       const gchar *keyname;
        GSList *retval;
        GError *err = NULL;
        
        g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
        g_return_val_if_fail (name, NULL);
        g_return_val_if_fail (key, NULL);
-
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key,
+                                                                 server_account);
+       
        retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
        if (err) {
                g_printerr ("modest: error getting list '%s': %s\n", keyname,
                            err->message);
                g_error_free (err);
-               retval = FALSE;
+               retval = NULL;
        }
-       g_free (keyname);
-
        return retval;
 }
 
 
 gboolean
-modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
-                              const gchar * key, const gchar * val, gboolean server_account)
+modest_account_mgr_set_string (ModestAccountMgr * self, 
+                              const gchar * name,
+                              const gchar * key, 
+                              const gchar * val, 
+                              gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
-       gchar *keyname;
+       const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
 
@@ -981,37 +979,25 @@ modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
-       
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
+       
        retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
        if (err) {
                g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
                g_error_free (err);
                retval = FALSE;
        }
-       g_free (keyname);       
        return retval;
 }
 
-
-gboolean
-modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
-                                const gchar * key, const gchar * val, gboolean server_account)
-{
-       return modest_account_mgr_set_password (self, name, key, val, server_account);
-}
-
-
-
 gboolean
 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
                            const gchar * key, int val, gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
-
-       gchar *keyname;
+       const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
        
@@ -1019,17 +1005,23 @@ modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
-       
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
+       
        retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
        if (err) {
                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);
+               }
        }
-       g_free (keyname);
        return retval;
 }
 
@@ -1041,7 +1033,7 @@ modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
 {
        ModestAccountMgrPrivate *priv;
 
-       gchar *keyname;
+       const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
 
@@ -1049,17 +1041,16 @@ modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
-
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
+       
        retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
        if (err) {
                g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
                g_error_free (err);
                retval = FALSE;
        }
-       g_free (keyname);
+
        return retval;
 }
 
@@ -1073,7 +1064,7 @@ modest_account_mgr_set_list (ModestAccountMgr *self,
                             gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
-       gchar *keyname;
+       const gchar *keyname;
        GError *err = NULL;
        gboolean retval;
        
@@ -1082,16 +1073,16 @@ modest_account_mgr_set_list (ModestAccountMgr *self,
        g_return_val_if_fail (key,  FALSE);
        g_return_val_if_fail (val,  FALSE);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
-       
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
+       
        retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
        if (err) {
                g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
                g_error_free (err);
                retval = FALSE;
        }
-       g_free (keyname);
 
        return retval;
 }
@@ -1102,15 +1093,15 @@ modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
 {
        ModestAccountMgrPrivate *priv;
 
-       gchar *keyname;
+       const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
 
        g_return_val_if_fail (self, FALSE);
         g_return_val_if_fail (name, FALSE);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, NULL, server_account);
        retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
        if (err) {
                g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
@@ -1118,12 +1109,12 @@ modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
                g_error_free (err);
                retval = FALSE;
        }
-       g_free (keyname);
        return retval;
 }
 
 gboolean
-modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, const gchar *display_name)
+modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, 
+                                                     const gchar *display_name)
 {
        GSList *account_names = NULL;
        GSList *cursor = NULL;
@@ -1135,20 +1126,23 @@ modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, co
        
        /* 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);
                        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);
@@ -1157,6 +1151,88 @@ modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, co
        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);
+                       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;
+}
+
 
 
 
@@ -1166,7 +1242,7 @@ modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
 {
        ModestAccountMgrPrivate *priv;
        
-       gchar *keyname;
+       const gchar *keyname;
        gboolean retval;
        GError *err = NULL;
        
@@ -1174,9 +1250,9 @@ modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
         g_return_val_if_fail (name, FALSE);
         g_return_val_if_fail (key, FALSE);
 
-       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
-
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
+
        retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
        if (err) {
                g_printerr ("modest: error unsetting'%s': %s\n", keyname,
@@ -1184,7 +1260,6 @@ modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
                g_error_free (err);
                retval = FALSE;
        }
-       g_free (keyname);
        return retval;
 }
 
@@ -1245,6 +1320,7 @@ _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key
 
 
 
+
 /* optimization: only with non-alphanum chars, escaping is needed */
 inline static gboolean
 is_alphanum (const gchar* str)
@@ -1261,9 +1337,12 @@ is_alphanum (const gchar* str)
 }
                
 
+
+
 /* must be freed by caller */
 gchar *
-_modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
+_modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar* name,
+                                        gboolean server_account)
 {
        gchar *retval = NULL;   
        gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
@@ -1271,7 +1350,7 @@ _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar
        
        if (!account_name)
                return g_strdup (namespace);
-       
+
        /* optimization: only escape names when need to be escaped */
        if (is_alphanum (account_name))
                escaped_account_name = (gchar*)account_name;
@@ -1290,31 +1369,85 @@ _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar
                retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
 
        /* Sanity check: */
-       if (!modest_conf_key_is_valid (retval)) {
-               g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
+       if (!retval || !modest_conf_key_is_valid (retval)) {
+               g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__,
+                          retval ? retval: "<empty>");
                g_free (retval);
                retval = NULL;
        }
-
+       
        /* g_free is only needed if we actually allocated anything */
        if (name != escaped_name)
                g_free (escaped_name);
        if (account_name != escaped_account_name)
                g_free (escaped_account_name);
-       
+
        return retval;
 }
 
+static const gchar *
+_modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, 
+                                               const gchar* account_name,
+                                               const gchar *name, 
+                                               gboolean is_server)
+{
+       GHashTable *hash = is_server ? priv->server_account_key_hash : priv->account_key_hash;
+       GHashTable *account_hash;
+       gchar *key = NULL;
+       const gchar *search_name;
+
+       if (!account_name)
+               return is_server ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
+
+       search_name = name ? name : "<dummy>";
+       
+       account_hash = g_hash_table_lookup (hash, account_name);        
+       if (!account_hash) { /* no hash for this account yet? create it */
+               account_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);         
+               key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
+               g_hash_table_insert (account_hash, g_strdup(search_name), key);
+               g_hash_table_insert (hash, g_strdup(account_name), account_hash);
+               return key;
+       }
+       
+       /* we have a hash for this account, but do we have the key? */  
+       key = g_hash_table_lookup (account_hash, search_name);
+       if (!key) {
+               key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
+               g_hash_table_insert (account_hash, g_strdup(search_name), key);
+       }
+       
+       return key;
+}
+
+
 gboolean
 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
 {
-       /* Check that at least one account exists: */
-       GSList *account_names = modest_account_mgr_account_names (self,
-                                                                 enabled);
-       gboolean accounts_exist = account_names != NULL;
+       ModestAccountMgrPrivate* priv;
+       GSList *account_names;
+       gboolean accounts_exist;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
+       
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
        
+       if (enabled && priv->has_enabled_accounts)
+               return TRUE;
+       else if (priv->has_accounts)
+               return TRUE;
+               
+       /* Check that at least one account exists: */
+       account_names = modest_account_mgr_account_names (self,enabled);
+       accounts_exist = account_names != NULL;
        modest_account_mgr_free_account_names (account_names);
        account_names = NULL;
+
+       /* cache it. */
+       if (enabled)
+               priv->has_enabled_accounts = accounts_exist;
+       else
+               priv->has_accounts = accounts_exist;
        
        return accounts_exist;
 }
@@ -1328,40 +1461,192 @@ compare_account_name(gconstpointer a, gconstpointer b)
 }
 
 void 
-modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
+modest_account_mgr_set_account_busy(ModestAccountMgr* self, 
+                                   const gchar* account_name, 
                                    gboolean busy)
 {
-       ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       if (busy)
-       {
-               GSList *account_names = modest_account_mgr_account_names (self,
-                               TRUE);
-               GSList* account = 
-                       g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
-               if (account && !modest_account_mgr_account_is_busy(self, account_name))
-               {
+       ModestAccountMgrPrivate* priv;
+
+       g_return_if_fail (MODEST_IS_ACCOUNT_MGR(self));
+       g_return_if_fail (account_name);
+
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       if (busy) {
+               GSList *account_names = modest_account_mgr_account_names (self, TRUE);
+               GSList* account = g_slist_find_custom(account_names, account_name, 
+                                                     (GCompareFunc) compare_account_name);
+
+               if (account && !modest_account_mgr_account_is_busy(self, account_name)) {
                        priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
-                       g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
+                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
+                                      0, account_name, TRUE);
                }
                modest_account_mgr_free_account_names (account_names);
                account_names = NULL;
        } else {
                GSList* account = 
                        g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
-               if (account)
-               {
+
+               if (account) {
                        g_free(account->data);
                        priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
-                       g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
+                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
+                                      0, account_name, FALSE);
                }
        }
 }
 
 gboolean
-modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
+modest_account_mgr_account_is_busy (ModestAccountMgr* self, const gchar* account_name)
 {
-       ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       ModestAccountMgrPrivate* priv;
+       
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
+       g_return_val_if_fail (account_name, FALSE);
+
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+               
        return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
-                                       != NULL);
+               != NULL);
+}
+
+void
+modest_account_mgr_notify_account_update (ModestAccountMgr* self, 
+                                         const gchar *server_account_name)
+{
+       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_return_if_reached ();
+               return;
+       }
+       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
+        * be sure about whether there are still enabled accounts...
+        */
+       priv->has_enabled_accounts = FALSE;
+       priv->has_accounts         = FALSE;
+       
+       /* Emit "update-account" */
+       g_signal_emit (G_OBJECT(self), 
+                      signals[ACCOUNT_CHANGED_SIGNAL], 0, 
+                      server_account_name, 
+                      (modest_protocol_registry_protocol_type_has_tag (protocol_registry, proto, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) ? 
+                      TNY_ACCOUNT_TYPE_STORE : 
+                      TNY_ACCOUNT_TYPE_TRANSPORT);
 }
+
+
+gboolean
+modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
+{
+       ModestConf *conf;
+       gboolean retval;
        
+       g_return_val_if_fail (self,    FALSE);
+       g_return_val_if_fail (account, FALSE);
+       g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
+                             FALSE);
+       
+       conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
+
+       /* Change the default account and notify */
+       retval = modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, account, NULL);
+       if (retval)
+               g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
+
+       return retval;
+}
+
+
+gchar*
+modest_account_mgr_get_default_account  (ModestAccountMgr *self)
+{
+       gchar *account; 
+       ModestConf *conf;
+       GError *err = NULL;
+       
+       g_return_val_if_fail (self, NULL);
+
+       conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
+       account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
+       
+       if (err) {
+               g_printerr ("modest: failed to get '%s': %s\n",
+                           MODEST_CONF_DEFAULT_ACCOUNT, err->message);
+               g_error_free (err);
+               return  NULL;
+       }
+       
+       /* sanity check */
+       if (account && !modest_account_mgr_account_exists (self, account, FALSE)) {
+               g_printerr ("modest: default account does not exist\n");
+               g_free (account);
+               return NULL;
+       }
+
+       return account;
+}
+
+static gboolean
+modest_account_mgr_unset_default_account (ModestAccountMgr *self)
+{
+       ModestConf *conf;
+       gboolean retval;
+       
+       g_return_val_if_fail (self,    FALSE);
+
+       conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
+               
+       retval = modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
+
+       if (retval)
+               g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
+
+       return retval;
+}
+
+
+gchar* 
+modest_account_mgr_get_display_name (ModestAccountMgr *self, 
+                                    const gchar* name)
+{
+       return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
+}
+
+void 
+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, 
+                                      display_name, 
+                                      FALSE /* not server account */);
+
+       /* Notify about the change in the display name */
+       if (notify)
+               g_signal_emit (self, signals[DISPLAY_NAME_CHANGED_SIGNAL], 0, account_name);
+}