2007-06-12 Murray Cumming <murrayc@murrayc.com>
[modest] / src / modest-account-mgr.c
index a0b1a04..44d7421 100644 (file)
@@ -50,58 +50,86 @@ enum {
 static GObjectClass *parent_class = NULL;
 static guint signals[LAST_SIGNAL] = {0};
 
+/* We signal key changes in batches, every X seconds: */
+static gboolean
+on_timeout_notify_changes (gpointer data)
+{
+       ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
+       ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+               
+       /* TODO: Also store the account names, and notify one list for each account,
+        * if anything uses the account names. */
+       
+       if (priv->changed_conf_keys) {
+               gchar *default_account = 
+                               modest_account_mgr_get_default_account (self);
+               
+               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
+                                default_account, priv->changed_conf_keys, FALSE);
+                       
+               g_free (default_account);
+               
+               g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
+               g_slist_free (priv->changed_conf_keys);
+               priv->changed_conf_keys = NULL;
+       }
+       
+       return TRUE; /* Call this again later. */
+}
+
 static void
 on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, 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);
 
        /* 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) {
-               gchar *default_account =
-                       modest_account_mgr_get_default_account (self);
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
-                              default_account, key, FALSE);
-               g_free (default_account);
-               return;
+               /* Get the default account instead. */
+               
+               /* Store the key for later notification in our timeout callback.
+                * Notifying for every key change would cause unnecessary work: */
+               priv->changed_conf_keys = g_slist_append (priv->changed_conf_keys, 
+                       (gpointer) g_strdup (key));
        }
        
-       account = _modest_account_mgr_account_from_key (key, &is_account_key, &is_server_account);
+       gboolean is_account_key = FALSE;
+       gboolean is_server_account = FALSE;
+       gchar* account = _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)
                return;
 
-       /* account was removed -- emit this, even if the account was disabled */
+       /* account was removed -- emit this, even if the account was
+          disabled. This should not happen unless the user directly
+          does it in gconf */
        if (is_account_key && event == MODEST_CONF_EVENT_KEY_UNSET) {
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0,
+               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, 
                               account, is_server_account);
                g_free (account);
                return;
        }
 
        /* is this account enabled? */
+       gboolean enabled = FALSE;
        if (is_server_account)
                enabled = TRUE;
        else 
                enabled = modest_account_mgr_get_enabled (self, account);
 
-       /* server account was changed, default 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) ||
-           strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0)
-               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
-                              account, key, is_server_account);
+           strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
+               /* Store the key for later notification in our timeout callback.
+                * Notifying for every key change would cause unnecessary work: */
+               priv->changed_conf_keys = g_slist_append (NULL, 
+                       (gpointer) g_strdup (key));
+       }
 
        g_free (account);
 }
@@ -160,8 +188,8 @@ modest_account_mgr_class_init (ModestAccountMgrClass * 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);
+                             modest_marshal_VOID__STRING_POINTER_BOOLEAN,
+                             G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
 }
 
 
@@ -172,18 +200,28 @@ modest_account_mgr_init (ModestAccountMgr * obj)
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
        priv->modest_conf = NULL;
+       
+       priv->timeout = g_timeout_add (1000 /* milliseconds */, on_timeout_notify_changes, obj);
 }
 
 static void
 modest_account_mgr_finalize (GObject * obj)
 {
-       ModestAccountMgrPrivate *priv =
+       ModestAccountMgrPrivate *priv = 
                MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
 
        if (priv->modest_conf) {
                g_object_unref (G_OBJECT(priv->modest_conf));
                priv->modest_conf = NULL;
        }
+       
+       if (priv->timeout)
+               g_source_remove (priv->timeout);
+               
+       if (priv->changed_conf_keys) {
+               g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
+               g_slist_free (priv->changed_conf_keys);
+       }
 
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
@@ -222,7 +260,8 @@ gboolean
 modest_account_mgr_add_account (ModestAccountMgr *self,
                                const gchar *name,
                                const gchar *store_account,
-                               const gchar *transport_account)
+                               const gchar *transport_account,
+                               gboolean enabled)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
@@ -252,7 +291,7 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
        if (!ok) {
                g_printerr ("modest: cannot set display name\n");
                if (err) {
-                       g_printerr ("modest: %s\n", err->message);
+                       g_printerr ("modest: Error adding account conf: %s\n", err->message);
                        g_error_free (err);
                }
                return FALSE;
@@ -266,7 +305,7 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
                        g_printerr ("modest: failed to set store account '%s'\n",
                                store_account);
                        if (err) {
-                               g_printerr ("modest: %s\n", err->message);
+                               g_printerr ("modest: Error adding store account conf: %s\n", err->message);
                                g_error_free (err);
                        }
                        
@@ -283,13 +322,21 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
                        g_printerr ("modest: failed to set transport account '%s'\n",
                                    transport_account);
                        if (err) {
-                               g_printerr ("modest: %s\n", err->message);
+                               g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
                                g_error_free (err);
                        }       
                        return FALSE;
                }
        }
-       modest_account_mgr_set_enabled (self, name, 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);
 
        /* if no default account has been defined yet, do so now */
        default_account = modest_account_mgr_get_default_account (self);
@@ -301,29 +348,23 @@ modest_account_mgr_add_account (ModestAccountMgr *self,
 }
 
 
-
-
 gboolean
 modest_account_mgr_add_server_account (ModestAccountMgr * self,
                                       const gchar * name, const gchar *hostname,
+                                      guint portnumber,
                                       const gchar * username, const gchar * password,
-                                      ModestProtocol proto,
-                                      ModestProtocol security,
-                                      ModestProtocol auth)
+                                      ModestTransportStoreProtocol proto,
+                                      ModestConnectionProtocol security,
+                                      ModestAuthProtocol auth)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
-       ModestProtocolType proto_type;
        gboolean ok = TRUE;
        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 (strchr(name, '/') == NULL, FALSE);
-
-       proto_type = modest_protocol_info_get_protocol_type (proto);
-       g_return_val_if_fail (proto_type == MODEST_PROTOCOL_TYPE_TRANSPORT ||
-                             proto_type == MODEST_PROTOCOL_TYPE_STORE, FALSE);
                              
        priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
        
@@ -375,7 +416,21 @@ modest_account_mgr_add_server_account (ModestAccountMgr * 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_protocol_name(proto), &err);
+                                    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);
@@ -385,10 +440,12 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        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_protocol_name (auth), &err);
+                                    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);
@@ -397,54 +454,10 @@ modest_account_mgr_add_server_account (ModestAccountMgr * self,
        g_free (key);
        if (!ok)
                goto cleanup;
-
-       if (proto_type == MODEST_PROTOCOL_TYPE_STORE) {
-
-               GSList *option_list = NULL;
-
-               /* Connection options. Some options are only valid for IMAP
-                  accounts but it's OK for just now since POP is still not
-                  supported */
-               key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_OPTIONS, TRUE);
-               /* Enable subscriptions and check the mails in all folders */
-               option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_USE_LSUB);
-               option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_CHECK_ALL);
-
-               /* TODO: Remove this hack. These are hard-coded camel options to make the connection work.
-                * The regular connection options (set later here) should be interpreted instead 
-                * because in future these camel options will not be in gconf. murrayc.
-                */
-               /* Security options */
-               switch (security) {
-               case MODEST_PROTOCOL_SECURITY_NONE:
-                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_NEVER);
-                       break;
-               case MODEST_PROTOCOL_SECURITY_SSL:
-               case MODEST_PROTOCOL_SECURITY_TLS:
-                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_ALWAYS);
-                       break;
-               case MODEST_PROTOCOL_SECURITY_TLS_OP:
-                       option_list = g_slist_append (option_list, MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE);
-                       break;
-               default:
-                       g_warning ("Invalid security option");
-               }
-               ok = modest_conf_set_list (priv->modest_conf, key, 
-                                          option_list, MODEST_CONF_VALUE_STRING, &err);
-               if (err) {
-                       g_printerr ("modest: failed to set %s: %s\n", key, err->message);
-                       g_error_free (err);
-                       ok = FALSE;
-               }
-               g_slist_free (option_list);
-               g_free (key);
-               
-               
-               /* Add the security and secure-auth settings: */
-               modest_server_account_set_option_security (self, name, security);
-               modest_server_account_set_option_secure_auth (self, name, auth);
-       }
-
+       
+       /* Add the security settings: */
+       modest_server_account_set_security (self, name, security);
+       
 cleanup:
        if (!ok) {
                g_printerr ("modest: failed to add server account\n");
@@ -454,10 +467,12 @@ cleanup:
        return TRUE;
 }
 
-
+/** modest_account_mgr_add_server_account_uri:
+ * Only used for mbox and maildir accounts.
+ */
 gboolean
 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
-                                          const gchar *name, ModestProtocol proto,
+                                          const gchar *name, ModestTransportStoreProtocol proto,
                                           const gchar *uri)
 {
        ModestAccountMgrPrivate *priv;
@@ -475,7 +490,8 @@ modest_account_mgr_add_server_account_uri (ModestAccountMgr * 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_protocol_name(proto), NULL);
+                                    modest_protocol_info_get_transport_store_protocol_name(proto),
+                                    NULL);
        g_free (key);
 
        if (!ok) {
@@ -499,7 +515,7 @@ modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
 
 gboolean
 modest_account_mgr_remove_account (ModestAccountMgr * self,
-                                  const gchar * name,  gboolean server_account)
+                                  const gchar* name,  gboolean server_account)
 {
        ModestAccountMgrPrivate *priv;
        gchar *key;
@@ -510,11 +526,19 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
        g_return_val_if_fail (name, FALSE);
 
        if (!modest_account_mgr_account_exists (self, name, server_account)) {
-               g_printerr ("modest: account '%s' does not exist\n", name);
+               g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
                return FALSE;
        }
 
-       /* in case we're not deleting an account, also delete the dependent store and transport account */
+       /* Notify the observers. We need to do that here because they
+          could need to use the configuration keys before deleting
+          them, i.e., the account store will delete the cache. We
+          only notify about removals of modest accounts */
+       if (!server_account)
+               g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, 
+                              name, server_account);
+
+       /* in case we're deleting an account, also delete the dependent store and transport account */
        if (!server_account) {
                gchar *server_account_name;
                
@@ -549,7 +573,17 @@ modest_account_mgr_remove_account (ModestAccountMgr * self,
                g_printerr ("modest: error removing key: %s\n", err->message);
                g_error_free (err);
        }
-       
+
+       /* If this was the default, then remove that setting: */
+       if (!server_account) {
+               gchar *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);
+               g_free (default_account_name);
+               
+               /* pick another one as the new default account */
+               modest_account_mgr_set_first_account_as_default (self);
+       }
        return retval;
 }
 
@@ -570,12 +604,12 @@ strip_prefix_from_elements (GSList * lst, guint n)
        }
 }
 
-
+#if 0
+/* Not used. */
 GSList*
 modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
                                           const gchar * account_name,
-                                          ModestProtocolType type,
-                                          ModestProtocol proto)
+                                          ModestTransportStoreProtocol proto)
 {
        GSList *accounts;
        GSList *cursor;
@@ -584,13 +618,6 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
        GError *err = NULL;
        
        g_return_val_if_fail (self, NULL);
-
-       if (proto != MODEST_PROTOCOL_UNKNOWN) {
-               ModestProtocolType proto_type;
-               proto_type = modest_protocol_info_get_protocol_type (proto);
-               g_return_val_if_fail (proto_type == MODEST_PROTOCOL_TYPE_TRANSPORT ||
-                                     proto_type == MODEST_PROTOCOL_TYPE_STORE, NULL);
-       }
        
        key      = _modest_account_mgr_get_account_keyname (account_name, NULL, TRUE);
        priv     = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
@@ -611,11 +638,9 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
        while (cursor) { 
                gchar *account   = _modest_account_mgr_account_from_key ((gchar*)cursor->data, NULL, NULL);
                gchar *acc_proto = modest_account_mgr_get_string (self, account, MODEST_ACCOUNT_PROTO,TRUE);
-               ModestProtocol     this_proto = modest_protocol_info_get_protocol (acc_proto);
-               ModestProtocolType this_type  = modest_protocol_info_get_protocol_type (this_proto);
-
-               if ((this_type  != MODEST_PROTOCOL_TYPE_UNKNOWN && this_type  != type) ||
-                   (this_proto != MODEST_PROTOCOL_UNKNOWN      && this_proto != proto)) {
+               ModestTransportStoreProtocol this_proto = 
+                       modest_protocol_info_get_transport_store_protocol (acc_proto);
+               if (this_proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN && this_proto != proto) {
                        GSList *nxt = cursor->next;
                        accounts = g_slist_delete_link (accounts, cursor);
                        cursor = nxt;
@@ -630,10 +655,10 @@ modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
        strip_prefix_from_elements (accounts, strlen(key)+1);
        return accounts;        
 }
-
+#endif
 
 GSList*
-modest_account_mgr_account_names (ModestAccountMgr * self)
+modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
 {
        GSList *accounts;
        ModestAccountMgrPrivate *priv;
@@ -655,7 +680,45 @@ modest_account_mgr_account_names (ModestAccountMgr * self)
        }
        
        strip_prefix_from_elements (accounts, prefix_len);
-       return accounts;
+               
+       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;
+               /* printf ("DEBUG: %s: account_name_key=%s\n", __FUNCTION__, account_name_key); */
+               gchar* unescaped_name = account_name_key ? 
+                       modest_conf_key_unescape (account_name_key) 
+                       : NULL;
+               /* printf ("  DEBUG: %s: unescaped name=%s\n", __FUNCTION__, unescaped_name); */
+               
+               gboolean add = TRUE;
+               if (only_enabled) {
+                       if (unescaped_name && 
+                               !modest_account_mgr_get_enabled (self, unescaped_name)) {
+                               add = FALSE;
+                       }
+               }
+               
+               if (add) {      
+                       result = g_slist_append (result, unescaped_name);
+               }
+               else {
+                       g_free (unescaped_name);
+               }
+                       
+               iter = g_slist_next (iter);     
+       }
+       
+       /* TODO: Free the strings too? */
+       g_slist_free (accounts);
+       accounts = NULL;
+
+       return result;
 }
 
 
@@ -932,7 +995,6 @@ modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
         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);
        retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
        if (err) {
@@ -945,13 +1007,14 @@ modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
        return retval;
 }
 
-gboolean       modest_account_mgr_account_with_display_name_exists       (ModestAccountMgr *self,
-                                                          const gchar *display_name)
+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);
+       cursor = account_names = modest_account_mgr_account_names (self, 
+               TRUE /* enabled accounts, because disabled accounts are not user visible. */);
 
        gboolean found = FALSE;
        
@@ -1008,3 +1071,104 @@ modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
        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;
+}
+
+
+
+/* must be freed by caller */
+gchar *
+_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;
+       
+       if (!account_name)
+               return g_strdup (namespace);
+       
+       /* Always escape the conf keys, so that it is acceptable to gconf: */
+       gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
+       gchar *escaped_name =  name ? modest_conf_key_escape (name) : NULL;
+
+       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 (escaped_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;
+       g_slist_free (account_names);
+       
+       return accounts_exist;
+}