* check for a valid foldername
[modest] / src / modest-account-mgr.c
index 8a05785..56e29ab 100644 (file)
 
 #include <string.h>
 #include <modest-marshal.h>
-#include <modest-account-keys.h>
 #include <modest-account-mgr.h>
+#include <modest-account-mgr-priv.h>
+#include <modest-account-mgr-helpers.h>
 
 /* 'private'/'protected' functions */
 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
-static void modest_account_mgr_init (ModestAccountMgr * obj);
-static void modest_account_mgr_finalize (GObject * obj);
-
-static gchar *get_account_keyname (const gchar * accname, const gchar * name,
-                                  gboolean server_account);
+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);
 
 /* list my signals */
 enum {
+       ACCOUNT_INSERTED_SIGNAL,
        ACCOUNT_CHANGED_SIGNAL,
        ACCOUNT_REMOVED_SIGNAL,
+       ACCOUNT_BUSY_SIGNAL,
        LAST_SIGNAL
 };
 
-typedef struct _ModestAccountMgrPrivate ModestAccountMgrPrivate;
-struct _ModestAccountMgrPrivate {
-       ModestConf        *modest_conf;
-       ModestProtocolMgr *proto_mgr;
-};
-
-#define MODEST_ACCOUNT_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
-                                                MODEST_TYPE_ACCOUNT_MGR, \
-                                                ModestAccountMgrPrivate))
 /* globals */
 static GObjectClass *parent_class = NULL;
-
 static guint signals[LAST_SIGNAL] = {0};
 
-
-static gchar*
-account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
+/* is the account already in the queue? */
+static gboolean
+in_change_queue (GSList *change_queue, const gchar *account)
 {
-       const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
-       const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
-       gchar *cursor;
-       gchar *account = NULL;
+       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;
+}
 
-       /* determine if it's an account or a server account,
-        * based on the prefix */
-       if (g_str_has_prefix (key, account_ns)) {
+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));
+}
 
-               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)) {
+/* 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
 
-               if (is_server_account)
-                       *is_server_account = TRUE;
+static gboolean
+on_timeout_notify_changes (gpointer data)
+{
+       ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
+       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
                
-               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, "/");
+       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);
+       }
        
-       if (is_account_key && cursor)
-               *is_account_key = TRUE;
+       /* 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 */
+}
 
-       /* put a NULL where the first slash was */
-       if (cursor)
-               *cursor = '\0';
+
+/* 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);
                
-       return account;
+       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, gpointer user_data)
+on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event,
+              ModestConfNotificationId id, gpointer user_data)
 {
-       ModestAccountMgr *self;
-       ModestAccountMgrPrivate *priv;
-
-       gchar *account;
-       gboolean is_account_key, is_server_account;
-       gboolean enabled;
-
-       self = MODEST_ACCOUNT_MGR (user_data);
-       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       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;
+
+       /* there is only one not-really-account key which will still emit
+        * a signal: a change in MODEST_CONF_DEFAULT_ACCOUNT */
+       if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
+               /* Get the default account instead. */
+               gchar *default_account =  modest_account_mgr_get_default_account (self);
+               if (!default_account) {
+                       g_warning ("BUG: cannot find default account");
+                       return;
+               } else {
+                       g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, 
+                                      default_account);
+                       g_free(default_account);
+                       return;
+               }       
+       }
        
-       account = account_from_key (key, &is_account_key, &is_server_account);
+       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)
+       if (!account_name)
                return;
 
-       /* account was removed -- emit this, even if the account was disabled */
-       if (is_account_key && event == MODEST_CONF_EVENT_KEY_UNSET) {
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0,
-                              account, is_server_account);
-               g_free (account);
+       /* ignore server account changes */
+       if (is_server_account)
+               /* change in place: retrieve the parent account name */
+               get_account_name_from_server_account (account_name);
+
+       /* 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;
        }
 
        /* is this account enabled? */
+       gboolean enabled = FALSE;
        if (is_server_account)
                enabled = TRUE;
-       else 
-               enabled = modest_account_mgr_account_get_enabled (self, account);
+       else
+               enabled = modest_account_mgr_get_enabled (self, account_name);
 
-       /* account was changed.
-        * and always notify when enabled/disabled changes
+       /* Notify is server account was changed, default account was changed
+        * or when enabled/disabled changes:
         */
-       if (enabled || g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED)) 
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
-                              account, key, is_server_account);
-
-       g_free (account);
+       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);
 }
 
 
@@ -158,7 +230,7 @@ modest_account_mgr_get_type (void)
        if (!my_type) {
                static const GTypeInfo my_info = {
                        sizeof (ModestAccountMgrClass),
-                       NULL,   /* base init */
+                       modest_account_mgr_base_init,   /* base init */
                        NULL,   /* base finalize */
                        (GClassInitFunc) modest_account_mgr_class_init,
                        NULL,   /* class finalize */
@@ -176,6 +248,53 @@ modest_account_mgr_get_type (void)
        return my_type;
 }
 
+static void 
+modest_account_mgr_base_init (gpointer g_class)
+{
+       static gboolean modest_account_mgr_initialized = FALSE;
+
+       if (!modest_account_mgr_initialized) {
+               /* signal definitions */
+               signals[ACCOUNT_INSERTED_SIGNAL] =
+                       g_signal_new ("account_inserted",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_inserted),
+                                     NULL, NULL,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING);
+
+               signals[ACCOUNT_REMOVED_SIGNAL] =
+                       g_signal_new ("account_removed",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_removed),
+                                     NULL, NULL,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING);
+
+               signals[ACCOUNT_CHANGED_SIGNAL] =
+                       g_signal_new ("account_changed",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_changed),
+                                     NULL, NULL,
+                                     g_cclosure_marshal_VOID__STRING,
+                                     G_TYPE_NONE, 1, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
+
+               signals[ACCOUNT_BUSY_SIGNAL] =
+                       g_signal_new ("account_busy_changed",
+                                     MODEST_TYPE_ACCOUNT_MGR,
+                                     G_SIGNAL_RUN_FIRST,
+                                     G_STRUCT_OFFSET(ModestAccountMgrClass,account_busy_changed),
+                                     NULL, NULL,
+                                     modest_marshal_VOID__STRING_BOOLEAN,
+                                     G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
+
+               modest_account_mgr_initialized = TRUE;
+       }
+}
+
 static void
 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
 {
@@ -187,24 +306,6 @@ modest_account_mgr_class_init (ModestAccountMgrClass * klass)
 
        g_type_class_add_private (gobject_class,
                                  sizeof (ModestAccountMgrPrivate));
-
-       /* signal definitions */
-       signals[ACCOUNT_REMOVED_SIGNAL] =
-               g_signal_new ("account_removed",
-                             G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_FIRST,
-                             G_STRUCT_OFFSET(ModestAccountMgrClass,account_removed),
-                             NULL, NULL,
-                             modest_marshal_VOID__STRING_BOOLEAN,
-                             G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
-       signals[ACCOUNT_CHANGED_SIGNAL] =
-               g_signal_new ("account_changed",
-                              G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_FIRST,
-                             G_STRUCT_OFFSET(ModestAccountMgrClass,account_changed),
-                             NULL, NULL,
-                             modest_marshal_VOID__STRING_STRING_BOOLEAN,
-                             G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
 }
 
 
@@ -214,29 +315,48 @@ modest_account_mgr_init (ModestAccountMgr * obj)
        ModestAccountMgrPrivate *priv =
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
-       priv->modest_conf = NULL;
-       priv->proto_mgr   = modest_protocol_mgr_new ();
+       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);
 }
 
 static void
 modest_account_mgr_finalize (GObject * obj)
 {
-       ModestAccountMgrPrivate *priv =
+       ModestAccountMgrPrivate *priv = 
                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;
+       }
+
        if (priv->modest_conf) {
                g_object_unref (G_OBJECT(priv->modest_conf));
                priv->modest_conf = NULL;
        }
 
-       if (priv->proto_mgr) {
-               g_object_unref (G_OBJECT(priv->proto_mgr));
-               priv->proto_mgr = NULL;
-       }
+       if (priv->timeout)
+               g_source_remove (priv->timeout);
+       priv->timeout = 0;
+
+
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
+
 ModestAccountMgr *
-modest_account_mgr_new (ModestConf * conf)
+modest_account_mgr_new (ModestConf *conf)
 {
        GObject *obj;
        ModestAccountMgrPrivate *priv;
@@ -249,9 +369,10 @@ modest_account_mgr_new (ModestConf * conf)
        g_object_ref (G_OBJECT(conf));
        priv->modest_conf = conf;
 
-       g_signal_connect (G_OBJECT (conf), "key_changed",
-                         G_CALLBACK (on_key_change),
-                         obj);
+       priv->key_changed_handler_uid = 
+               g_signal_connect (G_OBJECT (conf), "key_changed",
+                                 G_CALLBACK (on_key_change),
+                                 obj);
        
        return MODEST_ACCOUNT_MGR (obj);
 }
@@ -265,415 +386,519 @@ null_means_empty (const gchar * str)
 
 
 gboolean
-modest_account_mgr_account_set_enabled (ModestAccountMgr *self, const gchar* name,
-                                       gboolean enabled)
-{
-       return modest_account_mgr_set_bool (self, name,
-                                           MODEST_ACCOUNT_ENABLED, enabled,
-                                           FALSE, NULL);
-}
-
-
-gboolean
-modest_account_mgr_account_get_enabled (ModestAccountMgr *self, const gchar* name)
-{
-       return modest_account_mgr_get_bool (self, name,
-                                           MODEST_ACCOUNT_ENABLED, FALSE,
-                                           NULL);
-}
-
-
-gboolean
 modest_account_mgr_add_account (ModestAccountMgr *self,
                                const gchar *name,
                                const gchar *store_account,
                                const gchar *transport_account,
-                               GError **err)
+                               gboolean enabled)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
        gboolean ok;
-
-       g_return_val_if_fail (self, FALSE);
+       gchar *default_account;
+       GError *err = NULL;
+       
+       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, FALSE, err)) {
-               g_printerr ("modest: account already exists\n");
-               return FALSE;
-       }
+       g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
        
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+
        /*
         * we create the account by adding an account 'dir', with the name <name>,
         * and in that the 'display_name' string key
         */
-       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       key = _modest_account_mgr_get_account_keyname (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;
+       }
        
-       key = get_account_keyname (name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
-       ok = modest_conf_set_string (priv->modest_conf, key, name, err);
+       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) {
+                       g_printerr ("modest: Error adding account conf: %s\n", err->message);
+                       g_error_free (err);
+               }
                return FALSE;
        }
        
        if (store_account) {
-               key = get_account_keyname (name, MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
-               ok = modest_conf_set_string (priv->modest_conf, key, store_account, err);
+               key = _modest_account_mgr_get_account_keyname (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);
+                       if (err) {
+                               g_printerr ("modest: Error adding store account conf: %s\n", err->message);
+                               g_error_free (err);
+                       }
                        return FALSE;
                }
        }
-
+       
        if (transport_account) {
-               key = get_account_keyname (name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
-               ok = modest_conf_set_string (priv->modest_conf, key, transport_account, err);
+               key = _modest_account_mgr_get_account_keyname (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);
+                                   transport_account);
+                       if (err) {
+                               g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
+                               g_error_free (err);
+                       }       
                        return FALSE;
                }
        }
 
-       modest_account_mgr_account_set_enabled (self, name, TRUE);
-       
-       return TRUE;
-}
+       /* 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);
+
+       /* Notify the observers */
+       g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, 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, name);
+       g_free (default_account);
+
+       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,
-                                      const gchar * proto)
+                                      ModestTransportStoreProtocol proto,
+                                      ModestConnectionProtocol security,
+                                      ModestAuthProtocol auth)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
+       gboolean ok = TRUE;
+       GError *err = NULL;
 
-       g_return_val_if_fail (self, FALSE);
+       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);
+                             
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
-       key = get_account_keyname (name, NULL, TRUE);
-       if (modest_conf_key_exists (priv->modest_conf, key, NULL)) {
-               g_printerr ("modest: server account '%s' already exists", name);
+       /* hostname */
+       key = _modest_account_mgr_get_account_keyname (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);
-               return FALSE;
+               ok =  FALSE;
        }
-       g_free (key);
+       if (!ok)
+               goto cleanup;
        
-       /* hostname */
-       key = get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
-       modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), NULL);
+       modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &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;
+       
        /* username */
-       key = get_account_keyname (name, MODEST_ACCOUNT_USERNAME, TRUE);
-       modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), NULL);
+       key = _modest_account_mgr_get_account_keyname (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 = get_account_keyname (name, MODEST_ACCOUNT_PASSWORD, TRUE);
-       modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), NULL);
+       key = _modest_account_mgr_get_account_keyname (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 = get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
-       modest_conf_set_string (priv->modest_conf, key, null_means_empty (proto), NULL);
+       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
+       ok = modest_conf_set_string (priv->modest_conf, key,
+                                    modest_protocol_info_get_transport_store_protocol_name(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);
+       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);
+       ok = modest_conf_set_string (priv->modest_conf, key,
+                                    modest_protocol_info_get_auth_protocol_name (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);
+       
+cleanup:
+       if (!ok) {
+               g_printerr ("modest: failed to add server account\n");
+               return FALSE;
+       }
+
        return TRUE;
 }
 
-
-
+/** modest_account_mgr_add_server_account_uri:
+ * Only used for mbox and maildir accounts.
+ */
 gboolean
-modest_account_mgr_remove_account (ModestAccountMgr * self,
-                                  const gchar * name,
-                                  gboolean server_account,
-                                  GError ** err)
+modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
+                                          const gchar *name, ModestTransportStoreProtocol proto,
+                                          const gchar *uri)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
-       gboolean retval;
-
-       g_return_val_if_fail (self, FALSE);
+       gboolean ok;
+       
+       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);
+       g_return_val_if_fail (uri, FALSE);
+       
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       
+       
+       /* proto */
+       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
+       ok = modest_conf_set_string (priv->modest_conf, key,
+                                    modest_protocol_info_get_transport_store_protocol_name(proto),
+                                    NULL);
+       g_free (key);
 
-       if (!modest_account_mgr_account_exists (self, name, server_account, err)) {
-               g_printerr ("modest: account '%s' does not exist\n", name);
+       if (!ok) {
+               g_printerr ("modest: failed to set proto\n");
                return FALSE;
        }
-
-       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       key = get_account_keyname (name, NULL, server_account);
-
-       retval = modest_conf_remove_key (priv->modest_conf, key, NULL);
-
+       
+       /* uri */
+       key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_URI, TRUE);
+       ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
        g_free (key);
-       return retval;
-}
-
 
-
-/* strip the first /n/ character from each element */
-/* caller must make sure all elements are strings with
- * length >= n, and also that data can be freed.
- */
-static GSList*
-strip_prefix_from_elements (GSList * lst, guint n)
-{
-       GSList *cursor = lst;
-
-       while (cursor) {
-               gchar *str = (gchar *) cursor->data;
-               cursor->data = g_strdup (str + n);
-               g_free (str);
-               cursor = cursor->next;
+       if (!ok) {
+               g_printerr ("modest: failed to set uri\n");
+               return FALSE;
        }
-       return lst;
+       return TRUE;
 }
 
-
-GSList *
-modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
-                                          const gchar * account_name,
-                                          ModestProtocolType type,
-                                          const gchar *proto)
+/* 
+ * Utility function used by modest_account_mgr_remove_account
+ */
+static void
+real_remove_account (ModestConf *conf,
+                    const gchar *acc_name,
+                    gboolean server_account)
 {
-       GSList *accounts;
-       GSList *cursor;
-       ModestAccountMgrPrivate *priv;
-       gchar *key;
        GError *err = NULL;
-       
-       g_return_val_if_fail (self, NULL);
-       
-       key      = get_account_keyname (account_name, NULL, TRUE);
-       priv     = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       
-       /* get the list of all server accounts */
-       accounts = modest_conf_list_subkeys (priv->modest_conf, key, &err);
+       gchar *key = NULL;
+
+       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_printerr ("modest: failed to get subkeys for '%s'\n", key);
-               return NULL;
-       }
-       
-       /* no restrictions, return everything */
-       if (type == MODEST_PROTOCOL_TYPE_ANY && !proto)
-               return strip_prefix_from_elements (accounts, strlen(key)+1);
-       /* +1 because we must remove the ending '/' as well */
-       
-       /* otherwise, filter out the none-matching ones */
-       cursor = accounts;
-       while (cursor) {
-               gchar *account;
-               gchar *acc_proto;
-               
-               account = account_from_key ((gchar*)cursor->data, NULL, NULL);
-               acc_proto = modest_account_mgr_get_string (self, account, MODEST_ACCOUNT_PROTO,
-                                                          TRUE, NULL);
-               if ((!acc_proto) ||                                /* proto not defined? */
-                   (type != MODEST_PROTOCOL_TYPE_ANY &&                   /* proto type ...     */
-                    !modest_protocol_mgr_protocol_is_valid (priv->proto_mgr,
-                                                            acc_proto,type)) ||           /* ... matches?       */
-                   (proto && strcmp (proto, acc_proto) != 0)) {  /* proto matches?     */
-                       /* match! remove from the list */
-                       GSList *nxt = cursor->next;
-                       accounts = g_slist_delete_link (accounts, cursor);
-                       cursor = nxt;
-               } else
-                       cursor = cursor->next;
-
-               g_free (account);
-               g_free (acc_proto);
        }
-
-       return strip_prefix_from_elements (accounts, strlen(key)+1);
-       /* +1 because we must remove the ending '/' as well */
 }
 
-
-GSList *
-modest_account_mgr_account_names (ModestAccountMgr * self, GError ** err)
+gboolean
+modest_account_mgr_remove_account (ModestAccountMgr * self,
+                                  const gchar* name)
 {
-       GSList *accounts;
        ModestAccountMgrPrivate *priv;
-       const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
-
-       g_return_val_if_fail (self, NULL);
+       gchar *default_account_name, *store_acc_name, *transport_acc_name;
+       gboolean default_account_deleted;
 
-       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
+       g_return_val_if_fail (name, FALSE);
 
-       accounts = modest_conf_list_subkeys (priv->modest_conf,
-                                             MODEST_ACCOUNT_NAMESPACE, err);
-       
-       return strip_prefix_from_elements (accounts, prefix_len);
-}
+       if (!modest_account_mgr_account_exists (self, name, FALSE)) {
+               g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
+               return FALSE;
+       }
 
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       default_account_deleted = FALSE;
 
-static ModestServerAccountData*
-modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
-{
-       ModestServerAccountData *data;
+       /* If this was the default, then remove that setting: */
+       default_account_name = modest_account_mgr_get_default_account (self);
+       if (default_account_name && (strcmp (default_account_name, name) == 0)) {
+               modest_account_mgr_unset_default_account (self);
+               default_account_deleted = TRUE;
+       }
+       g_free (default_account_name);
+
+       /* Delete transport and store accounts */
+       store_acc_name = modest_account_mgr_get_string (self, name, 
+                                                       MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
+       if (store_acc_name)
+               real_remove_account (priv->modest_conf, store_acc_name, TRUE);
+
+       transport_acc_name = modest_account_mgr_get_string (self, name, 
+                                                           MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
+       if (transport_acc_name)
+               real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
+                       
+       /* Remove the modest account */
+       real_remove_account (priv->modest_conf, name, FALSE);
+
+       if (default_account_deleted) {  
+               /* pick another one as the new default account. We do
+                  this *after* deleting the keys, because otherwise a
+                  call to account_names will retrieve also the
+                  deleted account */
+               modest_account_mgr_set_first_account_as_default (self);
+       }
        
-       g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
-                                                                TRUE, NULL), NULL);    
-       data = g_new0 (ModestServerAccountData, 1);
+       /* 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);
 
-       data->account_name = g_strdup (name);
-       data->hostname     = modest_account_mgr_get_string (self, name,
-                                                           MODEST_ACCOUNT_HOSTNAME,
-                                                           TRUE, NULL);
-       data->username     = modest_account_mgr_get_string (self, name,
-                                                           MODEST_ACCOUNT_USERNAME,
-                                                           TRUE, NULL);
-       data->proto        = modest_account_mgr_get_string (self, name,
-                                                           MODEST_ACCOUNT_PROTO,
-                                                           TRUE, NULL);
-       data->password     = modest_account_mgr_get_string (self, name,
-                                                           MODEST_ACCOUNT_PASSWORD,
-                                                           TRUE, NULL);
-       return data;
+       return TRUE;
 }
 
 
+
+/* strip the first /n/ character from each element
+ * caller must make sure all elements are strings with
+ * length >= n, and also that data can be freed.
+ * change is in-place
+ */
 static void
-modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
-                                            ModestServerAccountData* data)
+strip_prefix_from_elements (GSList * lst, guint n)
 {
-       g_return_if_fail (self);
-
-       if (!data)
-               return; /* not an error */
-
-       g_free (data->account_name);
-       g_free (data->hostname);
-       g_free (data->username);
-       g_free (data->proto);
-       g_free (data->password);
-
-       g_free (data);
+       while (lst) {
+               memmove (lst->data, lst->data + n,
+                        strlen(lst->data) - n + 1);
+               lst = lst->next;
+       }
 }
 
 
-ModestAccountData*
-modest_account_mgr_get_account_data     (ModestAccountMgr *self,
-                                        const gchar* name)
+GSList*
+modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
 {
-       ModestAccountData *data;
-       gchar *server_account;
+       GSList *accounts;
+       ModestAccountMgrPrivate *priv;
+       GError *err = NULL;
        
+       const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
+
        g_return_val_if_fail (self, NULL);
-       g_return_val_if_fail (name, NULL);
-       g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
-                                                                FALSE, NULL), NULL);   
-       data = g_new0 (ModestAccountData, 1);
 
-       data->account_name = g_strdup (name);
-       data->full_name    = modest_account_mgr_get_string (self, name,
-                                                           MODEST_ACCOUNT_FULLNAME,
-                                                           FALSE, NULL);
-       data->email        = modest_account_mgr_get_string (self, name,
-                                                           MODEST_ACCOUNT_EMAIL,
-                                                           FALSE, NULL);
-       data->enabled      = modest_account_mgr_account_get_enabled (self, name);
+       priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       accounts = modest_conf_list_subkeys (priv->modest_conf,
+                                             MODEST_ACCOUNT_NAMESPACE, &err);
 
-       /* store */
-       server_account = modest_account_mgr_get_string (self, name,
-                                                       MODEST_ACCOUNT_STORE_ACCOUNT,
-                                                       FALSE, NULL);
-       if (server_account) {
-               data->store_account =
-                       modest_account_mgr_get_server_account_data (self,
-                                                                   server_account);
-               g_free (server_account);
+       if (err) {
+               g_printerr ("modest: failed to get subkeys (%s): %s\n",
+                           MODEST_ACCOUNT_NAMESPACE, err->message);
+               g_error_free (err);
+               return NULL; /* assume accounts did not get value when err is set...*/
        }
+       
+       strip_prefix_from_elements (accounts, prefix_len);
+               
+       GSList *result = NULL;
+       
+       /* Unescape the keys to get the account names: */
+       GSList *iter = accounts;
+       while (iter) {
+               if (!(iter->data))
+                       continue;
+                       
+               const gchar* account_name_key = (const gchar*)iter->data;
+               gchar* unescaped_name = account_name_key ? 
+                       modest_conf_key_unescape (account_name_key) 
+                       : NULL;
+               
+               gboolean add = TRUE;
+               if (only_enabled) {
+                       if (unescaped_name && 
+                               !modest_account_mgr_get_enabled (self, unescaped_name)) {
+                               add = FALSE;
+                       }
+               }
+               
+               /* Ignore modest accounts whose server accounts don't exist: 
+                * (We could be getting this list while the account is being deleted, 
+                * while the child server accounts have already been deleted, but the 
+                * 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);
+                       if (server_account_name) {
+                               if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
+                                       add = FALSE;
+                               g_free (server_account_name);
+                       }
+               }
+               
+               if (add) {
+                       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;
+                               g_free (server_account_name);
+                       }
+               }
+               
+               if (add)        
+                       result = g_slist_append (result, unescaped_name);
+               else 
+                       g_free (unescaped_name);
 
-       /* transport */
-       server_account = modest_account_mgr_get_string (self, name,
-                                                       MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
-                                                       FALSE, NULL);
-       if (server_account) {
-               data->transport_account =
-                       modest_account_mgr_get_server_account_data (self,
-                                                                   server_account);
-               g_free (server_account);
+               g_free (iter->data);
+               iter->data = NULL;
+               
+               iter = g_slist_next (iter);     
        }
+       
 
-       return data;
+       /* we already freed the strings in the loop */
+       g_slist_free (accounts);
+       
+       accounts = NULL;
+       return result;
 }
 
 
 void
-modest_account_mgr_free_account_data     (ModestAccountMgr *self,
-                                         ModestAccountData *data)
+modest_account_mgr_free_account_names (GSList *account_names)
 {
-       g_return_if_fail (self);
-
-       if (!data)
-               return;
-
-       g_free (data->account_name);
-       g_free (data->full_name);
-       g_free (data->email);
-
-       modest_account_mgr_free_server_account_data (self, data->store_account);
-       modest_account_mgr_free_server_account_data (self, data->transport_account);
-       
-       g_free (data);
+       g_slist_foreach (account_names, (GFunc)g_free, NULL);
+       g_slist_free (account_names);
 }
 
 
 
 gchar *
 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
-                              const gchar *key, gboolean server_account, GError **err) {
+                              const gchar *key, gboolean server_account) {
 
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
        gchar *retval;
+       GError *err = NULL;
 
        g_return_val_if_fail (self, NULL);
        g_return_val_if_fail (name, NULL);
        g_return_val_if_fail (key, NULL);
 
-       keyname = get_account_keyname (name, key, server_account);
+       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       retval = modest_conf_get_string (priv->modest_conf, keyname, err);
+       retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
+       if (err) {
+               g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
+               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, GError **err)
+modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
+                           gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
        gint retval;
-
-       g_return_val_if_fail (self, -1);
+       GError *err = NULL;
+       
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
        g_return_val_if_fail (name, -1);
        g_return_val_if_fail (key, -1);
 
-       keyname = get_account_keyname (name, key, server_account);
+       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
 
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       retval = modest_conf_get_int (priv->modest_conf, keyname, err);
+       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;
@@ -683,22 +908,59 @@ modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name,
 
 gboolean
 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
-                            const gchar * key, gboolean server_account, GError ** err)
+                            const gchar * key, gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
        gboolean retval;
+       GError *err = NULL;
 
-       g_return_val_if_fail (self, FALSE);
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (account, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = get_account_keyname (account, key, server_account);
+       keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       retval = modest_conf_get_bool (priv->modest_conf, keyname, err);
-               
+       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;
+}
+
+
+
+GSList * 
+modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
+                            const gchar *key, ModestConfValueType list_type,
+                            gboolean server_account)
+{
+       ModestAccountMgrPrivate *priv;
+
+       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);
+       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;
+       }
        g_free (keyname);
 
        return retval;
@@ -707,50 +969,66 @@ modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
 
 gboolean
 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
-                              const gchar * key, const gchar * val,
-                              gboolean server_account, GError ** err)
+                              const gchar * key, const gchar * val, gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
        gboolean retval;
+       GError *err = NULL;
 
-       g_return_val_if_fail (self, FALSE);
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = get_account_keyname (name, key, server_account);
+       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
-       retval = modest_conf_set_string (priv->modest_conf, keyname, val,
-                                        err);
-
-       g_free (keyname);
+       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,
-                           GError ** err)
+                           const gchar * key, int val, gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
        gboolean retval;
-
-       g_return_val_if_fail (self, FALSE);
+       GError *err = NULL;
+       
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = get_account_keyname (name, key, server_account);
+       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
        
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
-       retval = modest_conf_set_int (priv->modest_conf, keyname, val, err);
-
+       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;
+       }
        g_free (keyname);
        return retval;
 }
@@ -759,69 +1037,331 @@ modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
 
 gboolean
 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
-                            const gchar * key, gboolean val, gboolean server_account, 
-                            GError ** err)
+                            const gchar * key, gboolean val, gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
        gboolean retval;
+       GError *err = NULL;
 
-       g_return_val_if_fail (self, FALSE);
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
        g_return_val_if_fail (name, FALSE);
        g_return_val_if_fail (key, FALSE);
 
-       keyname = get_account_keyname (name, key, server_account);
+       keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
 
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
 
-       retval = modest_conf_set_bool (priv->modest_conf, keyname, val, err);
-
+       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;
 }
 
 
 gboolean
-modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
-                                  gboolean server_account, GError ** err)
+modest_account_mgr_set_list (ModestAccountMgr *self,
+                            const gchar *name,
+                            const gchar *key,
+                            GSList *val,
+                            ModestConfValueType list_type,
+                            gboolean server_account)
+{
+       ModestAccountMgrPrivate *priv;
+       gchar *keyname;
+       GError *err = NULL;
+       gboolean retval;
+       
+       g_return_val_if_fail (self, FALSE);
+       g_return_val_if_fail (name, FALSE);
+       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);
+       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;
+}
+
+gboolean
+modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
+                                  gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
 
        gchar *keyname;
        gboolean retval;
+       GError *err = NULL;
 
        g_return_val_if_fail (self, FALSE);
         g_return_val_if_fail (name, FALSE);
 
-       keyname = get_account_keyname (name, NULL, server_account);
-
+       keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
-       retval = modest_conf_key_exists (priv->modest_conf, keyname, err);
+       retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
+       if (err) {
+               g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
+                           err->message);
+               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)
+{
+       GSList *account_names = NULL;
+       GSList *cursor = NULL;
+       
+       cursor = account_names = modest_account_mgr_account_names (self, 
+                                                                  TRUE /* enabled accounts, because disabled accounts are not user visible. */);
+
+       gboolean found = FALSE;
+       
+       /* Look at each non-server account to check their display names; */
+       while (cursor) {
+               const gchar * account_name = (gchar*)cursor->data;
+               
+               ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
+               if (!account_data) {
+                       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)) {
+                       found = TRUE;
+                       break;
+               }
+
+               modest_account_mgr_free_account_data (self, account_data);
+               cursor = cursor->next;
+       }
+       modest_account_mgr_free_account_names (account_names);
+       account_names = NULL;
+       
+       return found;
+}
+
+
+
+
+gboolean 
+modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
+                         const gchar *key, gboolean server_account)
+{
+       ModestAccountMgrPrivate *priv;
+       
+       gchar *keyname;
+       gboolean retval;
+       GError *err = NULL;
+       
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
+        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);
+       retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
+       if (err) {
+               g_printerr ("modest: error unsetting'%s': %s\n", keyname,
+                           err->message);
+               g_error_free (err);
+               retval = FALSE;
+       }
        g_free (keyname);
        return retval;
 }
 
+gchar*
+_modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
+{
+       /* Initialize input parameters: */
+       if (is_account_key)
+               *is_account_key = FALSE;
+
+       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;
+
+       /* determine whether it's an account or a server account,
+        * based on the prefix */
+       if (g_str_has_prefix (key, account_ns)) {
+
+               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));  
+       } 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;
+
+       /* put a NULL where the first slash was */
+       if (cursor)
+               *cursor = '\0';
+
+       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);
+               g_free (account);
+               return unescaped_name;
+       } else
+               return NULL;
+}
+
+
+
+
+/* optimization: only with non-alphanum chars, escaping is needed */
+inline static gboolean
+is_alphanum (const gchar* str)
+{
+       const gchar *cursor;
+       for (cursor = str; cursor && *cursor; ++cursor) {
+               const char c = *cursor;
+               /* we cannot trust isalnum(3), because it might consider locales */
+               /*       numbers            ALPHA            alpha       */
+               if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
+                       return FALSE;
+       }
+       return TRUE;
+}
+               
 
 /* must be freed by caller */
-static gchar *
-get_account_keyname (const gchar * accname, const gchar * name, gboolean server_account)
+gchar *
+_modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
 {
-       gchar *namespace, *account_name, *retval;
+       gchar *retval = NULL;   
+       gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
+       gchar *escaped_account_name, *escaped_name;
        
-       namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
-
-       if (!accname)
+       if (!account_name)
                return g_strdup (namespace);
-
-       account_name = modest_conf_key_escape (NULL, accname);
        
-       if (name)
-               retval = g_strconcat (namespace, "/", accname, "/", name, NULL);
+       /* optimization: only escape names when need to be escaped */
+       if (is_alphanum (account_name))
+               escaped_account_name = (gchar*)account_name;
+       else
+               escaped_account_name = modest_conf_key_escape (account_name);
+       
+       if (is_alphanum (name))
+               escaped_name = (gchar*)name;
        else
-               retval = g_strconcat (namespace, "/", accname, NULL);
+               escaped_name = modest_conf_key_escape (name);
+       //////////////////////////////////////////////////////////////
+
+       if (escaped_account_name && escaped_name)
+               retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
+       else if (escaped_account_name)
+               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);
+               g_free (retval);
+               retval = NULL;
+       }
 
-       g_free (account_name);
+       /* 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;
 }
+
+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;
+       
+       modest_account_mgr_free_account_names (account_names);
+       account_names = NULL;
+       
+       return accounts_exist;
+}
+
+static int
+compare_account_name(gconstpointer a, gconstpointer b)
+{
+       const gchar* account_name = (const gchar*) a;
+       const gchar* account_name2 = (const gchar*) b;
+       return strcmp(account_name, account_name2);
+}
+
+void 
+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))
+               {
+                       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);
+               }
+               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)
+               {
+                       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);
+               }
+       }
+}
+
+gboolean
+modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
+{
+       ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+       return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
+                                       != NULL);
+}
+