* Fix some detected memory leaks
[modest] / src / modest-ui-actions.c
index 3bb0d8f..90a4928 100644 (file)
 #include "modest-tny-platform-factory.h"
 #include "modest-platform.h"
 
+#ifdef MODEST_PLATFORM_MAEMO
+#include "maemo/modest-osso-state-saving.h"
+#endif /* MODEST_PLATFORM_MAEMO */
+
+
 #include <widgets/modest-main-window.h>
 #include <widgets/modest-msg-view-window.h>
 #include <widgets/modest-account-view-window.h>
@@ -55,7 +60,7 @@
 
 #ifdef MODEST_HAVE_EASYSETUP
 #include "easysetup/modest-easysetup-wizard.h"
-#endif /*MODEST_HAVE_EASYSETUP*/
+#endif /* MODEST_HAVE_EASYSETUP */
 
 #include <modest-widget-memory.h>
 #include <tny-error.h>
 #include <tny-msg-view.h>
 #include <tny-device.h>
 
-typedef struct _GetMsgAsyncHelper {
+typedef struct _GetMsgAsyncHelper {    
        ModestWindow *window;
+       ModestMailOperation *mail_op;
        TnyIterator *iter;
-       GFunc func;
+       guint num_ops;
+       GFunc func;     
        gpointer user_data;
 } GetMsgAsyncHelper;
 
@@ -80,18 +87,35 @@ typedef struct _ReplyForwardHelper {
 guint reply_forward_type;
        ReplyForwardAction action;
        gchar *account_name;
+       guint pending_ops;
 } ReplyForwardHelper;
 
+typedef struct _HeaderActivatedHelper {
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+       TnyFolder *folder;
+} HeaderActivatedHelper;
+
+/*
+ * The do_headers_action uses this kind of functions to perform some
+ * action to each member of a list of headers
+ */
+typedef void (*HeadersFunc) (TnyHeader *header, ModestWindow *win, gpointer user_data);
+
+static void
+do_headers_action (ModestWindow *win, 
+                  HeadersFunc func,
+                  gpointer user_data);
+
+
+static void     open_msg_func          (const GObject *obj, const TnyMsg *msg, gpointer user_data);
+
+static void     reply_forward_func     (const GObject *obj, const TnyMsg *msg, gpointer user_data);
 
-static void     reply_forward_func     (gpointer data, gpointer user_data);
-static void     read_msg_func          (gpointer data, gpointer user_data);
-static void     get_msg_cb             (TnyFolder *folder, TnyMsg *msg,        GError **err, 
-                                       gpointer user_data);
 static void     reply_forward          (ReplyForwardAction action, ModestWindow *win);
 
 static gchar*   ask_for_folder_name    (GtkWindow *parent_window, const gchar *title);
 
-
 void   
 modest_ui_actions_on_about (GtkAction *action, ModestWindow *win)
 {
@@ -148,44 +172,36 @@ get_selected_headers (ModestWindow *win)
                return NULL;
 }
 
-void
-modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
+static void
+headers_action_delete (TnyHeader *header,
+                      ModestWindow *win,
+                      gpointer user_data)
 {
-       TnyList *header_list;
-       TnyIterator *iter;
-
-       g_return_if_fail (MODEST_IS_WINDOW(win));
-               
-       header_list = get_selected_headers (win);
-       
-       if (header_list) {
-               iter = tny_list_create_iterator (header_list);
-               do {
-                       TnyHeader *header;
-                       ModestMailOperation *mail_op;
-
-                       header = TNY_HEADER (tny_iterator_get_current (iter));
-                       /* TODO: thick grain mail operation involving
-                          a list of objects. Composite pattern ??? */
-                       /* TODO: add confirmation dialog */
-                       mail_op = modest_mail_operation_new ();
-                       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
-                                                        mail_op);
+       ModestMailOperation *mail_op;
 
-                       /* Always delete. TODO: Move to trash still not supported */
-                       modest_mail_operation_remove_msg (mail_op, header, FALSE);
+       /* TODO: add confirmation dialog */
+       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_DELETE, G_OBJECT(win));
+       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
+                                        mail_op);
 
-                       /* Frees */
-                       g_object_unref (G_OBJECT (mail_op));
-                       g_object_unref (G_OBJECT (header));
-
-                       tny_iterator_next (iter);
+       /* Always delete. TODO: Move to trash still not supported */
+       modest_mail_operation_remove_msg (mail_op, header, FALSE);
+       g_object_unref (G_OBJECT (mail_op));
+}
 
-               } while (!tny_iterator_is_done (iter));
+void
+modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
+{
+       g_return_if_fail (MODEST_IS_WINDOW(win));
 
-               /* Free iter */
-               g_object_unref (G_OBJECT (iter));
+       if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
+               gboolean ret_value;
+               g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);
+               return;
        }
+               
+       /* Remove each header */
+       do_headers_action (win, headers_action_delete, NULL);
 
        if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
                gtk_widget_destroy (GTK_WIDGET(win));
@@ -196,6 +212,10 @@ modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
 void
 modest_ui_actions_on_quit (GtkAction *action, ModestWindow *win)
 {
+       #ifdef MODEST_PLATFORM_MAEMO
+       modest_osso_save_state();
+       #endif /* MODEST_PLATFORM_MAEMO */
+       
        gtk_main_quit ();
 }
 
@@ -204,6 +224,9 @@ modest_ui_actions_on_close_window (GtkAction *action, ModestWindow *win)
 {
        if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
                gtk_widget_destroy (GTK_WIDGET (win));
+       } else if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
+               gboolean ret_value;
+               g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);
        } else if (MODEST_IS_WINDOW (win)) {
                gtk_widget_destroy (GTK_WIDGET (win));
        } else {
@@ -232,11 +255,11 @@ modest_ui_actions_on_accounts (GtkAction *action, ModestWindow *win)
         * because it requires a providers preset file which is not publically available.
         */
 #ifdef MODEST_PLATFORM_MAEMO /* Defined in config.h */
-       GSList *account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr());
+       GSList *account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
+                               TRUE /* enabled accounts only */);
        gboolean accounts_exist = account_names != NULL;
        g_slist_free (account_names);
        
-/* To test, while modest_account_mgr_account_names() is broken: accounts_exist = TRUE; */
        if (!accounts_exist) {
                /* If there are no accounts yet, just show the easy-setup wizard, as per the UI spec: */
                ModestEasysetupWizardDialog *wizard = modest_easysetup_wizard_dialog_new ();
@@ -285,9 +308,10 @@ modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
        TnyFolder *folder = NULL;
        gchar *account_name = NULL;
        gchar *from_str = NULL;
-       GError *err = NULL;
+/*     GError *err = NULL; */
        TnyAccount *account = NULL;
        ModestWindowMgr *mgr;
+       gchar *signature = NULL;
        
        account_name = g_strdup(modest_window_get_active_account (win));
        if (!account_name)
@@ -311,7 +335,15 @@ modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
                goto cleanup;
        }
 
-       msg = modest_tny_msg_new ("", from_str, "", "", "", "", NULL);
+       if (modest_account_mgr_get_bool (modest_runtime_get_account_mgr (), account_name,
+                                        MODEST_ACCOUNT_USE_SIGNATURE, FALSE)) {
+               signature = modest_account_mgr_get_string (modest_runtime_get_account_mgr (), account_name,
+                                                          MODEST_ACCOUNT_SIGNATURE, FALSE);
+       } else {
+               signature = g_strdup ("");
+       }
+
+       msg = modest_tny_msg_new ("", from_str, "", "", "", signature, NULL);
        if (!msg) {
                g_printerr ("modest: failed to create new msg\n");
                goto cleanup;
@@ -323,13 +355,13 @@ modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
                goto cleanup;
        }
        
-       tny_folder_add_msg (folder, msg, &err);
-       if (err) {
-               g_printerr ("modest: error adding msg to Drafts folder: %s",
-                           err->message);
-               g_error_free (err);
-               goto cleanup;
-       }
+/*     tny_folder_add_msg (folder, msg, &err); */
+/*     if (err) { */
+/*             g_printerr ("modest: error adding msg to Drafts folder: %s", */
+/*                         err->message); */
+/*             g_error_free (err); */
+/*             goto cleanup; */
+/*     } */
 
        /* Create and register edit window */
        msg_win = modest_msg_edit_window_new (msg, account_name);
@@ -344,6 +376,7 @@ modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
 cleanup:
        g_free (account_name);
        g_free (from_str);
+       g_free (signature);
        if (account)
                g_object_unref (G_OBJECT(account));
        if (msg)
@@ -352,20 +385,85 @@ cleanup:
                g_object_unref (G_OBJECT(folder));
 }
 
+static void
+headers_action_open (TnyHeader *header, 
+                    ModestWindow *win, 
+                    gpointer user_data)
+{
+       modest_ui_actions_on_header_activated (MODEST_HEADER_VIEW (user_data), 
+                                              header,
+                                              MODEST_MAIN_WINDOW (win));
+}
 
 void
 modest_ui_actions_on_open (GtkAction *action, ModestWindow *win)
 {
-       modest_runtime_not_implemented (GTK_WINDOW(win)); /* FIXME */
+       GtkWidget *header_view;
+
+       /* Get header view */
+       header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                          MODEST_WIDGET_TYPE_HEADER_VIEW);
+
+       /* Open each message */
+       do_headers_action (win, headers_action_open, header_view);
 }
 
 
+static void
+open_msg_func (const GObject *obj, const TnyMsg *msg, gpointer user_data)
+{
+       ModestWindowMgr *mgr = NULL;
+       ModestWindow *parent_win = NULL;
+       ModestWindow *win = NULL;
+       HeaderActivatedHelper *helper = NULL;
+       TnyFolderType folder_type = TNY_FOLDER_TYPE_UNKNOWN;
+       gchar *account = NULL;
+       
+       g_return_if_fail (MODEST_IS_WINDOW(obj));
+       g_return_if_fail (user_data != NULL);
+       parent_win = MODEST_WINDOW(obj);
+       helper = (HeaderActivatedHelper *) user_data;
+
+       /* Get account */
+       account =  g_strdup(modest_window_get_active_account(MODEST_WINDOW(parent_win)));
+       if (!account)
+               account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
+       
+       /* Gets foldert type (OUTBOX headers will be opened in edit window */
+/*     folder_type = modest_tny_folder_guess_folder_type (helper->folder); */
+       if (modest_tny_folder_is_local_folder (helper->folder))
+               folder_type = modest_tny_folder_get_local_folder_type (helper->folder);
+
+       switch (folder_type) {
+       case TNY_FOLDER_TYPE_DRAFTS:
+               win = modest_msg_edit_window_new ((TnyMsg *) msg, account);
+               break;
+       default:
+               if (helper->model != NULL)
+                       win = modest_msg_view_window_new_with_header_model ((TnyMsg *) msg, account, helper->model, helper->iter);
+               else
+                       win = modest_msg_view_window_new ((TnyMsg *) msg, account);
+       }
+               
+       /* Register and show new window */
+       if (win != NULL) {
+               mgr = modest_runtime_get_window_mgr ();
+               modest_window_mgr_register_window (mgr, win);
+               gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent_win));
+               gtk_widget_show_all (GTK_WIDGET(win));
+       }
+
+       /* Free */
+       g_free(account);
+/*     g_object_unref (G_OBJECT(msg)); */
+       g_object_unref (G_OBJECT(helper->folder));
+       g_slice_free (HeaderActivatedHelper, helper);
+}
 
 static void
-reply_forward_func (gpointer data, gpointer user_data)
+reply_forward_func (const GObject *obj, const TnyMsg *msg, gpointer user_data)
 {
-       TnyMsg *msg, *new_msg;
-       GetMsgAsyncHelper *helper;
+       TnyMsg *new_msg;
        ReplyForwardHelper *rf_helper;
        ModestWindow *msg_win;
        ModestEditType edit_type;
@@ -374,30 +472,40 @@ reply_forward_func (gpointer data, gpointer user_data)
        TnyFolder *folder = NULL;
        TnyAccount *account = NULL;
        ModestWindowMgr *mgr;
-       
-       msg = TNY_MSG (data);
-       helper = (GetMsgAsyncHelper *) user_data;
-       rf_helper = (ReplyForwardHelper *) helper->user_data;
+       gchar *signature = NULL;
+                       
+       g_return_if_fail (user_data != NULL);
+       rf_helper = (ReplyForwardHelper *) user_data;
+
+       rf_helper->pending_ops--;
 
        from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
                                                   rf_helper->account_name);
+       if (modest_account_mgr_get_bool (modest_runtime_get_account_mgr(),
+                                        rf_helper->account_name,
+                                        MODEST_ACCOUNT_USE_SIGNATURE, FALSE)) {
+               signature = modest_account_mgr_get_string (modest_runtime_get_account_mgr (),
+                                                          rf_helper->account_name,
+                                                          MODEST_ACCOUNT_SIGNATURE, FALSE);
+       }
+
        /* Create reply mail */
        switch (rf_helper->action) {
        case ACTION_REPLY:
                new_msg = 
-                       modest_tny_msg_create_reply_msg (msg,  from, 
+                       modest_tny_msg_create_reply_msg ((TnyMsg *) msg,  from, signature,
                                                         rf_helper->reply_forward_type,
                                                         MODEST_TNY_MSG_REPLY_MODE_SENDER);
                break;
        case ACTION_REPLY_TO_ALL:
                new_msg = 
-                       modest_tny_msg_create_reply_msg (msg, from, rf_helper->reply_forward_type,
+                       modest_tny_msg_create_reply_msg ((TnyMsg *) msg, from, signature, rf_helper->reply_forward_type,
                                                         MODEST_TNY_MSG_REPLY_MODE_ALL);
                edit_type = MODEST_EDIT_TYPE_REPLY;
                break;
        case ACTION_FORWARD:
                new_msg = 
-                       modest_tny_msg_create_forward_msg (msg, from, rf_helper->reply_forward_type);
+                       modest_tny_msg_create_forward_msg ((TnyMsg *) msg, from, signature, rf_helper->reply_forward_type);
                edit_type = MODEST_EDIT_TYPE_FORWARD;
                break;
        default:
@@ -405,6 +513,8 @@ reply_forward_func (gpointer data, gpointer user_data)
                return;
        }
 
+       g_free (signature);
+
        if (!new_msg) {
                g_printerr ("modest: failed to create message\n");
                goto cleanup;
@@ -424,7 +534,7 @@ reply_forward_func (gpointer data, gpointer user_data)
                goto cleanup;
        }
        
-       tny_folder_add_msg (folder, msg, &err);
+       tny_folder_add_msg (folder, (TnyMsg *) msg, &err);
        if (err) {
                g_printerr ("modest: error adding msg to Drafts folder: %s",
                            err->message);
@@ -448,8 +558,10 @@ cleanup:
        if (account)
                g_object_unref (G_OBJECT (account));
        
-       g_free (rf_helper->account_name);
-       g_slice_free (ReplyForwardHelper, rf_helper);
+       if (rf_helper->pending_ops == 0) {
+               g_free (rf_helper->account_name);
+               g_slice_free (ReplyForwardHelper, rf_helper);
+       }
 }
 /*
  * Common code for the reply and forward actions
@@ -457,12 +569,10 @@ cleanup:
 static void
 reply_forward (ReplyForwardAction action, ModestWindow *win)
 {
-       TnyList *header_list;
+       ModestMailOperation *mail_op = NULL;
+       TnyList *header_list = NULL;
+       ReplyForwardHelper *rf_helper = NULL;
        guint reply_forward_type;
-       TnyHeader *header;
-       TnyFolder *folder;
-       GetMsgAsyncHelper *helper;
-       ReplyForwardHelper *rf_helper;
        
        g_return_if_fail (MODEST_IS_WINDOW(win));
 
@@ -482,18 +592,12 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
        rf_helper = g_slice_new0 (ReplyForwardHelper);
        rf_helper->reply_forward_type = reply_forward_type;
        rf_helper->action = action;
-
+       rf_helper->pending_ops = tny_list_get_length (header_list);
        rf_helper->account_name = g_strdup (modest_window_get_active_account (win));
        if (!rf_helper->account_name)
                rf_helper->account_name =
                        modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
 
-       helper = g_slice_new0 (GetMsgAsyncHelper);
-       helper->window = win;
-       helper->func = reply_forward_func;
-       helper->iter = tny_list_create_iterator (header_list);
-       helper->user_data = rf_helper;
-
        if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
                TnyMsg *msg;
                msg = modest_msg_view_window_get_message(MODEST_MSG_VIEW_WINDOW(win));
@@ -501,23 +605,18 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                        g_printerr ("modest: no message found\n");
                        return;
                } else
-                       reply_forward_func (msg, helper);
+                       reply_forward_func (G_OBJECT(win), msg, rf_helper);
        } else {
-               header = TNY_HEADER (tny_iterator_get_current (helper->iter));
-               folder = tny_header_get_folder (header);
-               if (folder) {
-                       /* The callback will call it per each header */
-                       tny_folder_get_msg_async (folder, header, get_msg_cb, NULL, helper);
-                       g_object_unref (G_OBJECT (folder));
-               } else 
-                       g_printerr ("modest: no folder for header\n");
-               
+                               
+               mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win));
+               modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
+               modest_mail_operation_process_msg (mail_op, header_list, reply_forward_func, rf_helper);
+
                /* Clean */
-               g_object_unref (G_OBJECT (header));
+               g_object_unref(mail_op);
        }
 }
 
-
 void
 modest_ui_actions_on_reply (GtkAction *action, ModestWindow *win)
 {
@@ -587,13 +686,18 @@ void
 modest_ui_actions_on_sort (GtkAction *action, 
                           ModestWindow *window)
 {
-       ModestWindowMgr *mgr;
-
        g_return_if_fail (MODEST_IS_WINDOW(window));
 
-       /* Show sorting dialog */
-       mgr = modest_runtime_get_window_mgr ();
-       
+       if (MODEST_IS_MAIN_WINDOW (window)) {
+               GtkWidget *header_view;
+               header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(window),
+                                                                  MODEST_WIDGET_TYPE_HEADER_VIEW);
+               if (!header_view)
+                       return;
+
+               /* Show sorting dialog */
+               modest_platform_run_sort_dialog (GTK_WINDOW (window), MODEST_SORT_HEADERS);     
+       }
 }
 
 
@@ -605,14 +709,17 @@ action_send (const gchar* account_name)
 
        g_return_val_if_fail (account_name, FALSE);
 
+       /* Get the transport account according to the open connection, 
+        * because the account might specify connection-specific SMTP servers.
+        */
        tny_account = 
-               modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(),
-                                                                    account_name,
-                                                                    TNY_ACCOUNT_TYPE_TRANSPORT);
+               modest_tny_account_store_get_transport_account_for_open_connection (modest_runtime_get_account_store(),
+                                                                    account_name);
        if (!tny_account) {
                g_printerr ("modest: cannot get tny transport account for %s\n", account_name);
                return FALSE;
        }
+       
        send_queue = modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(tny_account));
        if (!send_queue) {
                g_object_unref (G_OBJECT(tny_account));
@@ -620,7 +727,7 @@ action_send (const gchar* account_name)
                return FALSE;
        }
        
-       //modest_tny_send_queue_flush (send_queue);
+       modest_tny_send_queue_flush (send_queue);
 
        g_object_unref (G_OBJECT(send_queue));
        g_object_unref (G_OBJECT(tny_account));
@@ -630,7 +737,8 @@ action_send (const gchar* account_name)
 
 
 static gboolean
-action_receive (const gchar* account_name)
+action_receive (const gchar* account_name, 
+               ModestWindow *win)
 {
        TnyAccount *tny_account;
        ModestMailOperation *mail_op;
@@ -647,7 +755,8 @@ action_receive (const gchar* account_name)
        }
 
        /* Create the mail operation */
-       mail_op = modest_mail_operation_new ();
+       /* TODO: The spec wants us to first do any pending deletions, before receiving. */
+       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win));
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
        modest_mail_operation_update_account (mail_op, TNY_STORE_ACCOUNT(tny_account));
 
@@ -657,34 +766,127 @@ action_receive (const gchar* account_name)
        return TRUE;
 }
 
+/** Check that an appropriate connection is open.
+ */
+gboolean check_for_connection (const gchar *account_name)
+{
+       TnyDevice *device = modest_runtime_get_device ();
 
-
-void
-modest_ui_actions_on_send_receive (GtkAction *action,  ModestWindow *win)
+/*
+       g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
+       
+       TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
+*/
+       
+       if (tny_device_is_online (device))
+               return TRUE;
+       else {
+               modest_platform_connect_and_wait (NULL);
+               
+               /* TODO: Wait until a result. */
+               return TRUE;
+       }
+}
+       
+static void
+do_send_receive_current_or_default (ModestWindow *win)
 {
        gchar *account_name;
 
-       
-       g_message ("online? %s", 
+       g_message ("%s: online? %s", __FUNCTION__,  
                tny_device_is_online(modest_runtime_get_device()) ? "yes":"no");
-                                                               
+                               
+       /* As per the UI spec, only the active account should be affected, 
+        * else the default folder if there is no active account: */                            
        account_name =
                g_strdup(modest_window_get_active_account(MODEST_WINDOW(win)));
        if (!account_name)
                account_name  = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
        if (!account_name) {
-               g_printerr ("modest: cannot get account\n");
+               g_printerr ("modest: cannot get default account\n");
                return;
        }
+       
+       /* Do not continue if no suitable connection is open: */
+       if (check_for_connection (account_name)) {
+               /* As per the UI spec,
+                * for POP accounts, we should receive,
+                * for IMAP we should synchronize everything, including receiving,
+                * for SMTP we should send,
+                * first receiving, then sending:
+                */
+               if (!action_receive(account_name, win))
+                       g_printerr ("modest: failed to receive\n");
+               if (!action_send(account_name))
+                       g_printerr ("modest: failed to send\n");
+               
+               g_free (account_name);
+       }
+}
+
 
-       if (!action_send(account_name))
-               g_printerr ("modest: failed to send\n");
-       if (!action_receive(account_name))
-               g_printerr ("modest: failed to receive\n");
+static void
+do_send_receive_auto (ModestWindow *win)
+{
+       g_message ("%s: online? %s", __FUNCTION__,  
+               tny_device_is_online(modest_runtime_get_device()) ? "yes":"no");
+
+       /* TODO: Delete the item->data strings as well as the list? */
+       GSList *account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
+                               TRUE /* enabled accounts only */);
+       GSList *iter = account_names;
+       while (iter) {                  
+               const gchar * account_name = (const char*) iter->data;
+               
+               /* Do not continue if no suitable connection is open: */
+               if (account_name && check_for_connection (account_name)) {
+                       /* As per the UI spec,
+                        * for POP accounts, we should receive,
+                        * for IMAP we should synchronize everything, including receiving,
+                        * for SMTP we should send,
+                        * first receiving, then sending:
+                        */
+                       if (!action_receive(account_name, win))
+                               g_printerr ("modest: failed to receive for account %s\n", account_name);
+                       if (!action_send(account_name))
+                               g_printerr ("modest: failed to send for account %s\n", account_name);
+               }
+       }
+       
+       
+       g_slist_free (account_names);
+}
+
+void
+do_send_receive (ModestWindow *win)
+{
+       const gboolean auto_update = TRUE; /* TODO: Get gconf setting. */
+       if (auto_update)
+               do_send_receive_current_or_default (win);
+       else
+               do_send_receive_auto (win);
+}
+
+void
+modest_ui_actions_on_send_receive (GtkAction *action,  ModestWindow *win)
+{
+       /* Check that at least one account exists: */
+       GSList *account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(),
+                               TRUE /* enabled accounts only */);
+       gboolean accounts_exist = account_names != NULL;
+       g_slist_free (account_names);
+       
+       /* If not, allow the user to create an account before trying to send/receive. */
+       if (!accounts_exist) {
+               modest_ui_actions_on_accounts (NULL, win);
+       }
+               
+       do_send_receive (win);
 }
 
 
 
+
 void
 modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow *main_window)
 {
@@ -703,7 +905,7 @@ modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow *
        /* what is saved/restored is depending on the style; thus; we save with
         * old style, then update the style, and restore for this new style
         */
-       modest_widget_memory_save (conf, G_OBJECT(header_view), "header-view");
+       modest_widget_memory_save (conf, G_OBJECT(header_view), MODEST_CONF_HEADER_VIEW_KEY);
        
        if (modest_header_view_get_style
            (MODEST_HEADER_VIEW(header_view)) == MODEST_HEADER_VIEW_STYLE_DETAILS)
@@ -714,107 +916,20 @@ modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow *
                                              MODEST_HEADER_VIEW_STYLE_DETAILS);
 
        modest_widget_memory_restore (conf, G_OBJECT(header_view),
-                                     "header-view");
-}
-
-
-
-/*
- * Marks a message as read and passes it to the msg preview widget
- */
-static void
-read_msg_func (gpointer data, gpointer user_data)
-{
-       TnyMsg *msg;
-       TnyHeader *header;
-       GetMsgAsyncHelper *helper;
-       TnyHeaderFlags header_flags;
-       GtkWidget *msg_preview;
-       
-       msg = TNY_MSG (data);
-       helper = (GetMsgAsyncHelper *) user_data;
-
-       msg_preview = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (helper->window),
-                                                          MODEST_WIDGET_TYPE_MSG_PREVIEW);
-       if (!msg_preview)
-               return;
-       
-       header = TNY_HEADER (tny_iterator_get_current (helper->iter));
-       header_flags = tny_header_get_flags (header);
-       tny_header_set_flags (header, header_flags | TNY_HEADER_FLAG_SEEN);
-       g_object_unref (G_OBJECT (header));
-
-       /* Set message on msg view */
-       modest_msg_view_set_message (MODEST_MSG_VIEW(msg_preview), msg);
+                                     MODEST_CONF_HEADER_VIEW_KEY);
 }
 
-/*
- * This function is a generic handler for the tny_folder_get_msg_async
- * call. It expects as user_data a #GetMsgAsyncHelper. This helper
- * contains a user provided function that is called inside this
- * method. This will allow us to use this callback in many different
- * places. This callback performs the common actions for the
- * get_msg_async call, more specific actions will be done by the user
- * function
- */
-static void
-get_msg_cb (TnyFolder *folder, TnyMsg *msg, GError **err, gpointer user_data)
-{
-       GetMsgAsyncHelper *helper;
-
-       helper = (GetMsgAsyncHelper *) user_data;
-
-       if ((*err && ((*err)->code == TNY_FOLDER_ERROR_GET_MSG)) || !msg) {
-               modest_ui_actions_on_item_not_found (NULL,
-                                                    MODEST_ITEM_TYPE_MESSAGE,
-                                                    helper->window);
-               return;
-       }
-
-       /* Call user function */
-       helper->func (msg, user_data);
-
-       /* Process next element (if exists) */
-       tny_iterator_next (helper->iter);
-       if (tny_iterator_is_done (helper->iter)) {
-               TnyList *headers;
-               headers = tny_iterator_get_list (helper->iter);
-               /* Free resources */
-               g_object_unref (G_OBJECT (headers));
-               g_object_unref (G_OBJECT (helper->iter));
-               g_slice_free (GetMsgAsyncHelper, helper);
-       } else {
-               TnyHeader *header;
-               header = TNY_HEADER (tny_iterator_get_current (helper->iter)); 
-               tny_folder_get_msg_async (folder, header,                         
-                                         get_msg_cb, NULL, helper);
-               g_object_unref (G_OBJECT(header));
-       }
-}
 
 void 
 modest_ui_actions_on_header_selected (ModestHeaderView *header_view, 
                                      TnyHeader *header,
                                      ModestMainWindow *main_window)
 {
-       TnyFolder *folder;
-       GetMsgAsyncHelper *helper;
-       TnyList *list;
-
        g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
 
-       /* when there's no header, clear the msgview */
-       if (!header) {
-               GtkWidget *msg_preview;
-
-               /* Clear msg preview if exists */
-               msg_preview = modest_main_window_get_child_widget(main_window,
-                                                                 MODEST_WIDGET_TYPE_MSG_PREVIEW);
-       
-               if (msg_preview)
-                       modest_msg_view_set_message (MODEST_MSG_VIEW(msg_preview), NULL);
+       /* If no header has been selected then exit */
+       if (!header)
                return;
-       }
 
        /* Update Main window title */
        if (GTK_WIDGET_HAS_FOCUS (header_view)) {
@@ -824,81 +939,138 @@ modest_ui_actions_on_header_selected (ModestHeaderView *header_view,
                else
                        gtk_window_set_title (GTK_WINDOW (main_window), _("mail_va_no_subject"));
        }
-
-       /* Create list */
-       list = tny_simple_list_new ();
-       tny_list_prepend (list, G_OBJECT (header));
-
-       /* Fill helper data */
-       helper = g_slice_new0 (GetMsgAsyncHelper);
-       helper->window = MODEST_WINDOW (main_window);
-       helper->iter = tny_list_create_iterator (list);
-       helper->func = read_msg_func;
-
-       folder = tny_header_get_folder (TNY_HEADER(header));
-
-       tny_folder_get_msg_async (TNY_FOLDER(folder),
-                                 header, get_msg_cb,
-                                 NULL, helper);
-
-       /* Frees */
-       g_object_unref (G_OBJECT (folder));
 }
 
 
 
-void 
-modest_ui_actions_on_header_activated (ModestHeaderView *folder_view, TnyHeader *header,
+/* void */
+/* modest_ui_actions_on_header_activated (ModestHeaderView *header_view, TnyHeader *header, */
+/*                                    ModestMainWindow *main_window) */
+/* { */
+/*     ModestWindow *win = NULL; */
+/*     TnyFolder *folder = NULL; */
+/*     TnyMsg    *msg    = NULL; */
+/*     TnyFolderType folder_type = TNY_FOLDER_TYPE_UNKNOWN; */
+/*     ModestWindowMgr *mgr; */
+/*     GtkTreeModel *model; */
+/*     GtkTreeIter iter; */
+/*     GtkTreeSelection *sel = NULL; */
+/*     GList *sel_list = NULL; */
+       
+/*     g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window)); */
+       
+/*     if (!header) */
+/*             return; */
+
+/*     folder = tny_header_get_folder (header); */
+/*     if (!folder) { */
+/*             g_printerr ("modest: cannot get folder for header\n"); */
+/*             return; */
+/*     } */
+/*     if (modest_tny_folder_is_local_folder (folder)) */
+/*             folder_type = modest_tny_folder_get_local_folder_type (folder); */
+
+/*     /\* FIXME: make async?; check error  *\/ */
+/*     msg = tny_folder_get_msg (folder, header, NULL); */
+/*     if (!msg) { */
+/*             g_printerr ("modest: cannot get msg for header\n"); */
+/*             goto cleanup; */
+/*     } */
+
+/*     /\* Look if we already have a message view for that header *\/ */
+/*     mgr = modest_runtime_get_window_mgr (); */
+/*     win = modest_window_mgr_find_window_by_msguid (mgr, tny_header_get_uid (header)); */
+
+/*     /\* If not, create a new window *\/ */
+/*     if (!win) { */
+/*             gchar *account; */
+
+/*             account =  g_strdup(modest_window_get_active_account(MODEST_WINDOW(main_window))); */
+/*             if (!account) */
+/*                     account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr()); */
+
+/*             sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view)); */
+/*             sel_list = gtk_tree_selection_get_selected_rows (sel, &model); */
+/*             if (sel_list != NULL) { */
+/*                     gtk_tree_model_get_iter (model, &iter, (GtkTreePath *) sel_list->data); */
+                       
+/*                     switch (folder_type) { */
+/*                     case TNY_FOLDER_TYPE_DRAFTS: */
+/*                             win = modest_msg_edit_window_new (msg, account); */
+/*                             break; */
+/*                     default: */
+/*                             win = modest_msg_view_window_new_with_header_model (msg, account, model, iter); */
+/*                     } */
+
+/*                     g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL); */
+/*                     g_list_free (sel_list); */
+/*             } else { */
+/*                     win = modest_msg_view_window_new (msg, account); */
+/*             } */
+/*             modest_window_mgr_register_window (mgr, win); */
+
+/*             gtk_window_set_transient_for (GTK_WINDOW (win), */
+/*                                           GTK_WINDOW (main_window)); */
+/*     } */
+
+/*     gtk_widget_show_all (GTK_WIDGET(win)); */
+
+/*     g_object_unref (G_OBJECT (msg)); */
+       
+/* cleanup: */
+/*     g_object_unref (G_OBJECT (folder)); */
+/* } */
+
+void
+modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
+                                      TnyHeader *header,
                                       ModestMainWindow *main_window)
 {
+       ModestMailOperation *mail_op = NULL;
+       HeaderActivatedHelper *helper = NULL;
+       ModestWindowMgr *mgr = NULL;
        ModestWindow *win = NULL;
-       TnyFolder *folder = NULL;
-       TnyMsg    *msg    = NULL;
-       ModestWindowMgr *mgr;
+       GtkTreeModel *model = NULL;
+       GtkTreeIter iter;
+       GtkTreeSelection *sel = NULL;
+       GList *sel_list = NULL;
        
        g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
        
        if (!header)
                return;
 
-       folder = tny_header_get_folder (header);
-       if (!folder) {
-               g_printerr ("modest: cannot get folder for header\n");
-               return;
-       }
-
-       /* FIXME: make async?; check error  */
-       msg = tny_folder_get_msg (folder, header, NULL);
-       if (!msg) {
-               g_printerr ("modest: cannot get msg for header\n");
-               goto cleanup;
-       }
-
-       /* Look if we already have a message view for that header */    
+       /* Look if we already have a message view for that header */
        mgr = modest_runtime_get_window_mgr ();
        win = modest_window_mgr_find_window_by_msguid (mgr, tny_header_get_uid (header));
+       if (win) return;
+
+       /* Build helper */
+       helper = g_slice_new0 (HeaderActivatedHelper);
+       helper->folder = tny_header_get_folder (header);
+       helper->model = NULL;
+
+       /* Get headers tree model and selected iter to build message view */
+       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
+       sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
+       if (sel_list != NULL) {
+               gtk_tree_model_get_iter (model, &iter, (GtkTreePath *) sel_list->data);
+               
+               /* Fill helpers */
+               helper->model = model;
+               helper->iter = iter;
 
-       /* If not, create a new window */
-       if (!win) {
-               gchar *account;
-
-               account =  g_strdup(modest_window_get_active_account(MODEST_WINDOW(main_window)));
-               if (!account)
-                       account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
-
-               win = modest_msg_view_window_new (msg, account);
-               modest_window_mgr_register_window (mgr, win);
-
-               gtk_window_set_transient_for (GTK_WINDOW (win),
-                                             GTK_WINDOW (main_window));
+               g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
+               g_list_free (sel_list);
        }
 
-       gtk_widget_show_all (GTK_WIDGET(win));
+       /* New mail operation */
+       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(main_window));
+       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
+       modest_mail_operation_get_msg (mail_op, header, open_msg_func, helper);
 
-       g_object_unref (G_OBJECT (msg));
-       
-cleanup:
-       g_object_unref (G_OBJECT (folder));
+       /* Free */
+       g_object_unref (mail_op);
 }
 
 void 
@@ -927,9 +1099,9 @@ modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
                        modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
                                                       TNY_FOLDER (folder_store));
                        modest_widget_memory_restore (conf, G_OBJECT(header_view),
-                                                     "header-view");
+                                                     MODEST_CONF_HEADER_VIEW_KEY);
                } else {
-                       modest_widget_memory_save (conf, G_OBJECT (header_view), "header-view");
+                       modest_widget_memory_save (conf, G_OBJECT (header_view), MODEST_CONF_HEADER_VIEW_KEY);
                        modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view), NULL);
                }
        } else if (TNY_IS_ACCOUNT (folder_store)) {
@@ -979,7 +1151,7 @@ modest_ui_actions_on_item_not_found (ModestHeaderView *header_view,ModestItemTyp
 
                gtk_window_set_default_size (GTK_WINDOW(dialog), 300, 300);
                if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
-//                     tny_device_force_online (modest_runtime_get_device());
+//                     modest_platform_connect_and_wait ();;
                }
        }
        gtk_widget_destroy (dialog);
@@ -1026,6 +1198,67 @@ modest_ui_actions_on_msg_recpt_activated (ModestMsgView *msgview,
 }
 
 void
+modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edit_window)
+{
+       TnyTransportAccount *transport_account;
+       ModestMailOperation *mail_operation;
+       MsgData *data;
+       gchar *account_name, *from;
+       ModestAccountMgr *account_mgr;
+
+       g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
+       
+       data = modest_msg_edit_window_get_msg_data (edit_window);
+
+       account_mgr = modest_runtime_get_account_mgr();
+       account_name = g_strdup(modest_window_get_active_account (MODEST_WINDOW(edit_window)));
+       if (!account_name) 
+               account_name = modest_account_mgr_get_default_account (account_mgr);
+       if (!account_name) {
+               g_printerr ("modest: no account found\n");
+               modest_msg_edit_window_free_msg_data (edit_window, data);
+               return;
+       }
+       transport_account =
+               TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_tny_account_by_account
+                                     (modest_runtime_get_account_store(),
+                                      account_name,
+                                      TNY_ACCOUNT_TYPE_TRANSPORT));
+       if (!transport_account) {
+               g_printerr ("modest: no transport account found for '%s'\n", account_name);
+               g_free (account_name);
+               modest_msg_edit_window_free_msg_data (edit_window, data);
+               return;
+       }
+       from = modest_account_mgr_get_from_string (account_mgr, account_name);
+
+       /* Create the mail operation */         
+       mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO, G_OBJECT(edit_window));
+       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
+
+       modest_mail_operation_save_to_drafts (mail_operation,
+                                             transport_account,
+                                             from,
+                                             data->to, 
+                                             data->cc, 
+                                             data->bcc,
+                                             data->subject, 
+                                             data->plain_body, 
+                                             data->html_body,
+                                             data->attachments,
+                                             data->priority_flags);
+       /* Frees */
+       g_free (from);
+       g_free (account_name);
+       g_object_unref (G_OBJECT (transport_account));
+       g_object_unref (G_OBJECT (mail_operation));
+
+       modest_msg_edit_window_free_msg_data (edit_window, data);
+
+       /* Save settings and close the window */
+       gtk_widget_destroy (GTK_WIDGET (edit_window));
+}
+void
 modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
 {
        TnyTransportAccount *transport_account;
@@ -1035,6 +1268,9 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        ModestAccountMgr *account_mgr;
 
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
+
+       if (!modest_msg_edit_window_check_names (edit_window))
+               return;
        
        data = modest_msg_edit_window_get_msg_data (edit_window);
 
@@ -1051,10 +1287,9 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
                return;
        }
        transport_account =
-               TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_tny_account_by_account
+               TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
                                      (modest_runtime_get_account_store(),
-                                      account_name,
-                                      TNY_ACCOUNT_TYPE_TRANSPORT));
+                                      account_name));
        if (!transport_account) {
                g_printerr ("modest: no transport account found for '%s'\n", account_name);
                g_free (account_name);
@@ -1064,7 +1299,7 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        from = modest_account_mgr_get_from_string (account_mgr, account_name);
 
        /* Create the mail operation */         
-       mail_operation = modest_mail_operation_new ();
+       mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_SEND, G_OBJECT(edit_window));
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
 
        modest_mail_operation_send_new_mail (mail_operation,
@@ -1285,7 +1520,7 @@ modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_windo
                        if (result == GTK_RESPONSE_REJECT) {
                                finished = TRUE;
                        } else {
-                               ModestMailOperation *mail_op = modest_mail_operation_new ();
+                               ModestMailOperation *mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO, G_OBJECT(main_window));
                                TnyFolder *new_folder = NULL;
 
                                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
@@ -1339,7 +1574,7 @@ modest_ui_actions_on_rename_folder (GtkAction *action,
                if (folder_name != NULL && strlen (folder_name) > 0) {
                        ModestMailOperation *mail_op;
 
-                       mail_op = modest_mail_operation_new ();
+                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_INFO, G_OBJECT(main_window));
                        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                                         mail_op);
 
@@ -1360,6 +1595,7 @@ delete_folder (ModestMainWindow *main_window, gboolean move_to_trash)
        TnyFolderStore *folder;
        GtkWidget *folder_view;
        gint response;
+       gchar *message;
        
        g_return_if_fail (MODEST_IS_MAIN_WINDOW(main_window));
 
@@ -1371,12 +1607,14 @@ delete_folder (ModestMainWindow *main_window, gboolean move_to_trash)
        folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
 
        /* Ask the user */      
+       message =  g_strdup_printf (_("mcen_nc_delete_folder_text"), 
+                                   tny_folder_get_name (TNY_FOLDER (folder)));
        response = modest_platform_run_confirmation_dialog (GTK_WINDOW (main_window), 
-                                                           MODEST_CONFIRMATION_DELETE_FOLDER,
-                                                           folder);
+                                                           (const gchar *) message);
+       g_free (message);
 
        if (response == GTK_RESPONSE_OK) {
-               ModestMailOperation *mail_op = modest_mail_operation_new ();
+               ModestMailOperation *mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_DELETE, G_OBJECT(main_window));
 
                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                                 mail_op);
@@ -1647,17 +1885,19 @@ modest_ui_actions_on_change_fullscreen (GtkAction *action,
        gtk_window_present (GTK_WINDOW (window));
 }
 
-/*
- * Show the header details in a ModestDetailsDialog widget
+/* 
+ * Used by modest_ui_actions_on_details to call do_headers_action 
  */
 static void
-show_header_details (TnyHeader *header, 
-                    GtkWindow *window)
+headers_action_show_details (TnyHeader *header, 
+                            ModestWindow *window,
+                            gpointer user_data)
+
 {
        GtkWidget *dialog;
        
        /* Create dialog */
-       dialog = modest_details_dialog_new_with_header (window, header);
+       dialog = modest_details_dialog_new_with_header (GTK_WINDOW (window), header);
 
        /* Run dialog */
        gtk_widget_show_all (dialog);
@@ -1685,7 +1925,9 @@ show_folder_details (TnyFolder *folder,
        gtk_widget_destroy (dialog);
 }
 
-
+/*
+ * Show the header details in a ModestDetailsDialog widget
+ */
 void     
 modest_ui_actions_on_details (GtkAction *action, 
                              ModestWindow *win)
@@ -1708,7 +1950,7 @@ modest_ui_actions_on_details (GtkAction *action,
                        iter = tny_list_create_iterator (headers_list);
 
                        header = TNY_HEADER (tny_iterator_get_current (iter));
-                       show_header_details (header, GTK_WINDOW (win));
+                       headers_action_show_details (header, win, NULL);
                        g_object_unref (header);
 
                        g_object_unref (iter);
@@ -1733,23 +1975,8 @@ modest_ui_actions_on_details (GtkAction *action,
                } else {
                        header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
                                                                           MODEST_WIDGET_TYPE_HEADER_VIEW);
-                       if (!gtk_widget_is_focus (header_view))
-                               return;
-
-                       headers_list = get_selected_headers (win);
-                       if (!headers_list)
-                               return;
-
-                       iter = tny_list_create_iterator (headers_list);
-                       while (!tny_iterator_is_done (iter)) {
-
-                               header = TNY_HEADER (tny_iterator_get_current (iter));
-                               show_header_details (header, GTK_WINDOW (win));
-                               g_object_unref (header);
-
-                               tny_iterator_next (iter);
-                       }
-                       g_object_unref (iter);
+                       /* Show details of each header */
+                       do_headers_action (win, headers_action_show_details, header_view);
                }
        }
 }
@@ -1840,15 +2067,20 @@ modest_ui_actions_on_select_contacts (GtkAction *action, ModestMsgEditWindow *wi
        modest_msg_edit_window_select_contacts (window);
 }
 
-
-void 
-modest_ui_actions_on_move_to (GtkAction *action, 
-                             ModestWindow *win)
+void
+modest_ui_actions_on_check_names (GtkAction *action, ModestMsgEditWindow *window)
 {
-       GtkWidget *dialog, *scroll, *folder_view, *tree_view;
-       gint result;
+       g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
+       modest_msg_edit_window_check_names (window);
+}
 
-       g_return_if_fail (MODEST_IS_MAIN_WINDOW (win));
+
+static GtkWidget*
+create_move_to_dialog (ModestWindow *win,
+                      GtkWidget *folder_view,
+                      GtkWidget **tree_view)
+{
+       GtkWidget *dialog, *scroll;
 
        dialog = gtk_dialog_new_with_buttons (_("mcen_ti_moveto_folders_title"),
                                              GTK_WINDOW (win),
@@ -1866,52 +2098,290 @@ modest_ui_actions_on_move_to (GtkAction *action,
                                         GTK_POLICY_AUTOMATIC);
 
        /* Create folder view */
-       folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
-                                                          MODEST_WIDGET_TYPE_FOLDER_VIEW);
-       tree_view = modest_folder_view_new (NULL);
-       gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view),
+       *tree_view = modest_folder_view_new (NULL);
+       gtk_tree_view_set_model (GTK_TREE_VIEW (*tree_view),
                                 gtk_tree_view_get_model (GTK_TREE_VIEW (folder_view)));
-
-       gtk_container_add (GTK_CONTAINER (scroll), tree_view);
+       gtk_container_add (GTK_CONTAINER (scroll), *tree_view);
 
        /* Add scroll to dialog */
        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
                            scroll, FALSE, FALSE, 0);
 
        gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
-       
+
+       return dialog;
+}
+
+/*
+ * Returns TRUE if at least one of the headers of the list belongs to
+ * a message that has been fully retrieved.
+ */
+static gboolean
+has_retrieved_msgs (TnyList *list)
+{
+       TnyIterator *iter;
+       gboolean found = FALSE;
+
+       iter = tny_list_create_iterator (list);
+       while (tny_iterator_is_done (iter) && !found) {
+               TnyHeader *header;
+               TnyHeaderFlags flags;
+
+               header = TNY_HEADER (tny_iterator_get_current (iter));
+               flags = tny_header_get_flags (header);
+               if (!(flags & TNY_HEADER_FLAG_PARTIAL))
+                       found = TRUE;
+
+               if (!found)
+                       tny_iterator_next (iter);
+       }
+       g_object_unref (iter);
+
+       return found;
+}
+
+/*
+ * Shows a confirmation dialog to the user when we're moving messages
+ * from a remote server to the local storage. Returns the dialog
+ * response. If it's other kind of movement the it always returns
+ * GTK_RESPONSE_OK
+ */
+static gint
+msgs_move_to_confirmation (GtkWindow *win,
+                          TnyFolder *dest_folder,
+                          TnyList *headers)
+{
+       gint response = GTK_RESPONSE_OK;
+
+       /* If the destination is a local folder */
+       if (modest_tny_folder_is_local_folder (dest_folder)) {
+               TnyFolder *src_folder;
+               TnyIterator *iter;
+               TnyHeader *header;
+
+               /* Get source folder */
+               iter = tny_list_create_iterator (headers);
+               header = TNY_HEADER (tny_iterator_get_current (iter));
+               src_folder = tny_header_get_folder (header);
+               g_object_unref (header);
+               g_object_unref (iter);
+
+               /* If the source is a remote folder */
+               if (!modest_tny_folder_is_local_folder (src_folder)) {
+                       const gchar *message;
+                       
+                       if (tny_list_get_length (headers) == 1)
+                               if (has_retrieved_msgs (headers))
+                                       message = _("mcen_nc_move_retrieve");
+                               else
+                                       message = _("mcen_nc_move_header");
+                       else
+                               if (has_retrieved_msgs (headers))
+                                       message = _("mcen_nc_move_retrieves");
+                               else
+                                       message = _("mcen_nc_move_headers");
+                       
+                       response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
+                                                                           (const gchar *) message);
+               }
+       }
+       return response;
+}
+
+/*
+ * UI handler for the "Move to" action when invoked from the
+ * ModestMainWindow
+ */
+static void 
+modest_ui_actions_on_main_window_move_to (GtkAction *action, 
+                                         ModestMainWindow *win)
+{
+       GtkWidget *dialog, *folder_view, *tree_view = NULL;
+       gint result;
+       TnyFolderStore *folder_store;
+       ModestMailOperation *mail_op = NULL;
+
+       g_return_if_fail (MODEST_IS_MAIN_WINDOW (win));
+
+       /* Get the folder view */
+       folder_view = modest_main_window_get_child_widget (win,
+                                                          MODEST_WIDGET_TYPE_FOLDER_VIEW);
+
+       /* Create and run the dialog */
+       dialog = create_move_to_dialog (MODEST_WINDOW (win), folder_view, &tree_view);
        result = gtk_dialog_run (GTK_DIALOG(dialog));
-       if (result == GTK_RESPONSE_ACCEPT) {
-               TnyFolderStore *folder_store;
-               folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (tree_view));
-               if (TNY_IS_ACCOUNT (folder_store)) {
-               } else {
-                       ModestMailOperation *mail_op;
 
-                       /* Create mail operation */
-                       mail_op = modest_mail_operation_new ();
+       /* We do this to save an indentation level ;-) */
+       if (result != GTK_RESPONSE_ACCEPT)
+               goto end;
+
+       folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (tree_view));
+
+       if (TNY_IS_ACCOUNT (folder_store))
+               goto end;
+
+       /* Get folder or messages to transfer */
+       if (gtk_widget_is_focus (folder_view)) {
+               TnyFolderStore *src_folder;
+               src_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
+
+               if (TNY_IS_FOLDER (src_folder)) {
+                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win));
                        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
                                                         mail_op);
 
-                       /* Get folder or messages to transfer */
-                       if (gtk_widget_is_focus (folder_view)) {
-                               TnyFolderStore *src_folder;
-                               src_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
+                       modest_mail_operation_xfer_folder (mail_op, 
+                                                          TNY_FOLDER (src_folder),
+                                                          folder_store,
+                                                          TRUE);
+                       g_object_unref (G_OBJECT (mail_op));
+               }
 
-                               if (TNY_IS_FOLDER (src_folder)) 
-                                       modest_mail_operation_xfer_folder (mail_op, 
-                                                                          TNY_FOLDER (src_folder), 
-                                                                          folder_store,
-                                                                          TRUE);
+               /* Frees */
+               g_object_unref (G_OBJECT (src_folder));
+       } else {
+               GtkWidget *header_view;
+               header_view = modest_main_window_get_child_widget (win,
+                                                                  MODEST_WIDGET_TYPE_HEADER_VIEW);
+               if (gtk_widget_is_focus (header_view)) {
+                       TnyList *headers;
+                       gint response;
 
-                               /* Frees */
-                               g_object_unref (G_OBJECT (src_folder));
-                       } else {
+                       headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (header_view));
+
+                       /* Ask for user confirmation */
+                       response = msgs_move_to_confirmation (GTK_WINDOW (win), 
+                                                             TNY_FOLDER (folder_store), 
+                                                             headers);
+
+                       /* Transfer messages */
+                       if (response == GTK_RESPONSE_OK) {
+                               mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win));
+                               modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
+                                                                mail_op);
+
+                               modest_mail_operation_xfer_msgs (mail_op, 
+                                                                headers,
+                                                                TNY_FOLDER (folder_store),
+                                                                TRUE);
+                               g_object_unref (G_OBJECT (mail_op));
                        }
+               }
+       }
+       g_object_unref (folder_store);
+
+ end:
+       gtk_widget_destroy (dialog);
+}
+
+
+/*
+ * UI handler for the "Move to" action when invoked from the
+ * ModestMsgViewWindow
+ */
+static void 
+modest_ui_actions_on_msg_view_window_move_to (GtkAction *action, 
+                                             ModestMsgViewWindow *win)
+{
+       GtkWidget *dialog, *folder_view, *tree_view = NULL;
+       gint result;
+       ModestMainWindow *main_window;
+       TnyMsg *msg;
+       TnyHeader *header;
+       TnyList *headers;
+
+       /* Get the folder view */
+       main_window = MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ()));
+       folder_view = modest_main_window_get_child_widget (main_window,
+                                                          MODEST_WIDGET_TYPE_FOLDER_VIEW);
+
+       /* Create and run the dialog */
+       dialog = create_move_to_dialog (MODEST_WINDOW (win), folder_view, &tree_view);  
+       result = gtk_dialog_run (GTK_DIALOG(dialog));
+
+       if (result == GTK_RESPONSE_ACCEPT) {
+               TnyFolderStore *folder_store;
+               gint response;
+
+               folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (tree_view));
+
+               /* Create header list */
+               msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
+               header = tny_msg_get_header (msg);
+               headers = tny_simple_list_new ();
+               tny_list_prepend (headers, G_OBJECT (header));
+               g_object_unref (header);
+               g_object_unref (msg);
+
+               /* Ask user for confirmation. MSG-NOT404 */
+               response = msgs_move_to_confirmation (GTK_WINDOW (win), 
+                                                     TNY_FOLDER (folder_store), 
+                                                     headers);
+
+               /* Transfer current msg */
+               if (response == GTK_RESPONSE_OK) {
+                       ModestMailOperation *mail_op;
+
+                       /* Create mail op */
+                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT(win));
+                       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
+                                                        mail_op);
+                       
+                       /* Transfer messages */
+                       modest_mail_operation_xfer_msgs (mail_op, 
+                                                        headers,
+                                                        TNY_FOLDER (folder_store),
+                                                        TRUE);
                        g_object_unref (G_OBJECT (mail_op));
+               } else {
+                       g_object_unref (headers);
                }
                g_object_unref (folder_store);
        }
-
        gtk_widget_destroy (dialog);
 }
+
+void 
+modest_ui_actions_on_move_to (GtkAction *action, 
+                             ModestWindow *win)
+{
+       g_return_if_fail (MODEST_IS_MAIN_WINDOW (win) ||
+                         MODEST_IS_MSG_VIEW_WINDOW (win));
+
+       if (MODEST_IS_MAIN_WINDOW (win)) 
+               modest_ui_actions_on_main_window_move_to (action, 
+                                                         MODEST_MAIN_WINDOW (win));
+       else
+               modest_ui_actions_on_msg_view_window_move_to (action, 
+                                                             MODEST_MSG_VIEW_WINDOW (win));
+}
+
+/*
+ * Calls #HeadersFunc for each header already selected in the main
+ * window or the message currently being shown in the msg view window
+ */
+static void
+do_headers_action (ModestWindow *win, 
+                  HeadersFunc func,
+                  gpointer user_data)
+{
+       TnyList *headers_list;
+       TnyIterator *iter;
+
+       /* Get headers */
+       headers_list = get_selected_headers (win);
+       if (!headers_list)
+               return;
+
+       /* Call the function for each header */
+       iter = tny_list_create_iterator (headers_list);
+       while (!tny_iterator_is_done (iter)) {
+               TnyHeader *header;
+
+               header = TNY_HEADER (tny_iterator_get_current (iter));
+               func (header, win, user_data);
+               g_object_unref (header);
+               tny_iterator_next (iter);
+       }
+       g_object_unref (iter);
+}