+ retval = NULL;
+ }
+
+ /* g_free is only needed if we actually allocated anything */
+ if (name != escaped_name)
+ g_free (escaped_name);
+ if (account_name != escaped_account_name)
+ g_free (escaped_account_name);
+
+ return retval;
+}
+
+static const gchar *
+_modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv,
+ const gchar* account_name,
+ const gchar *name,
+ gboolean is_server)
+{
+ GHashTable *hash = is_server ? priv->server_account_key_hash : priv->account_key_hash;
+ GHashTable *account_hash;
+ gchar *key = NULL;
+ const gchar *search_name;
+
+ if (!account_name)
+ return is_server ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
+
+ search_name = name ? name : "<dummy>";
+
+ account_hash = g_hash_table_lookup (hash, account_name);
+ if (!account_hash) { /* no hash for this account yet? create it */
+ account_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+ key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
+ g_hash_table_insert (account_hash, g_strdup(search_name), key);
+ g_hash_table_insert (hash, g_strdup(account_name), account_hash);
+ return key;
+ }
+
+ /* we have a hash for this account, but do we have the key? */
+ key = g_hash_table_lookup (account_hash, search_name);
+ if (!key) {
+ key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
+ g_hash_table_insert (account_hash, g_strdup(search_name), key);
+ }
+
+ return key;
+}
+
+
+gboolean
+modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
+{
+ /* Check that at least one account exists: */
+ GSList *account_names = modest_account_mgr_account_names (self,
+ enabled);
+ gboolean accounts_exist = account_names != NULL;
+
+ 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;
+
+ g_return_if_fail (MODEST_IS_ACCOUNT_MGR(self));
+ g_return_if_fail (account_name);
+
+ priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+ if (busy) {
+ GSList *account_names = modest_account_mgr_account_names (self, TRUE);
+ GSList* account = g_slist_find_custom(account_names, account_name,
+ (GCompareFunc) compare_account_name);
+
+ if (account && !modest_account_mgr_account_is_busy(self, account_name)) {
+ priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
+ g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL],
+ 0, account_name, TRUE);
+ }
+ modest_account_mgr_free_account_names (account_names);
+ account_names = NULL;
+ } else {
+ GSList* account =
+ g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
+
+ if (account) {
+ g_free(account->data);
+ priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
+ g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL],
+ 0, account_name, FALSE);
+ }
+ }
+}
+
+gboolean
+modest_account_mgr_account_is_busy (ModestAccountMgr* self, const gchar* account_name)
+{
+ ModestAccountMgrPrivate* priv;
+
+ g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
+ g_return_val_if_fail (account_name, FALSE);
+
+ priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);