+gboolean
+modest_account_mgr_unset_default_account (ModestAccountMgr *self)
+{
+ ModestConf *conf;
+
+ g_return_val_if_fail (self, FALSE);
+
+ conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
+
+ return modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
+
+}
+
+gint on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
+{
+ return g_utf8_collate((const gchar*)a, (const gchar*)b);
+}
+
+
+static void
+free_element (gpointer data, gpointer user_data)
+{
+ g_free (data);
+}
+
+gboolean
+modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
+{
+ gboolean result = FALSE;
+ GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
+
+ /* Return TRUE if there is no account */
+ if (!account_names)
+ return TRUE;
+
+ /* Get the first one, alphabetically, by title: */
+ GSList* list_sorted = g_slist_sort (account_names,
+ on_accounts_list_sort_by_title);
+ if(list_sorted) {
+ const gchar* account_name = (const gchar*)list_sorted->data;
+ if (account_name)
+ result = modest_account_mgr_set_default_account (self, account_name);
+ }
+
+ g_slist_foreach (account_names, free_element, NULL);
+ g_slist_free (account_names);
+
+ return result;
+}
+