+ gchar *modest_account_name = NULL;
+ gchar *from_header;
+
+ /* we cannot edit without a valid account... */
+ if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
+ if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
+ modest_window_mgr_unregister_header (win_mgr,
+ header);
+ goto cleanup;
+ }
+ }
+
+ from_header = tny_header_dup_from (header);
+ if (from_header) {
+ GSList *accounts = modest_account_mgr_account_names (modest_runtime_get_account_mgr (), TRUE);
+ GSList *node = NULL;
+ for (node = accounts; node != NULL; node = g_slist_next (node)) {
+ gchar *from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr (), node->data);
+
+ if (from && (strcmp (from_header, from) == 0)) {
+ g_free (modest_account_name);
+ modest_account_name = g_strdup (node->data);
+ g_free (from);
+ break;
+ }
+ g_free (from);
+ }
+ g_slist_foreach (accounts, (GFunc) g_free, NULL);
+ g_slist_free (accounts);
+ g_free (from_header);
+ }
+
+ if (modest_account_name == NULL) {
+ modest_account_name = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
+ }