* modest-folder-view.c: change sorting for remote folders: make Inbox be the first...
[modest] / src / modest-ui-actions.c
index 4d1cd29..be1d2a0 100644 (file)
@@ -105,11 +105,9 @@ typedef struct _ReplyForwardHelper {
  */
 typedef void (*HeadersFunc) (TnyHeader *header, ModestWindow *win, gpointer user_data);
 
-static void
-do_headers_action (ModestWindow *win, 
-                  HeadersFunc func,
-                  gpointer user_data);
-
+static void     do_headers_action     (ModestWindow *win, 
+                                      HeadersFunc func,
+                                      gpointer user_data);
 
 static void     open_msg_cb            (ModestMailOperation *mail_op, 
                                        TnyHeader *header, 
@@ -123,6 +121,9 @@ static void     reply_forward_cb       (ModestMailOperation *mail_op,
 
 static void     reply_forward          (ReplyForwardAction action, ModestWindow *win);
 
+static void     folder_refreshed_cb    (ModestMailOperation *mail_op, 
+                                       TnyFolder *folder, 
+                                       gpointer user_data);
 
 static void     _on_send_receive_progress_changed (ModestMailOperation  *mail_op, 
                                                   ModestMailOperationState *state,
@@ -250,10 +251,14 @@ headers_action_delete (TnyHeader *header,
        /* 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));
+
+       /* refilter treemodel to hide marked-as-deleted rows */
+       if (MODEST_IS_HEADER_VIEW (user_data))
+               modest_header_view_refilter (MODEST_HEADER_VIEW (user_data));
 }
 
 void
-modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
+modest_ui_actions_on_delete_message (GtkAction *action, ModestWindow *win)
 {
        TnyList *header_list = NULL;
        TnyIterator *iter = NULL;
@@ -261,47 +266,50 @@ modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
        gchar *message = NULL;
        gchar *desc = NULL;
        gint response;
-       gboolean found;
        ModestWindowMgr *mgr;
-       GtkWidget *header_view;
+       GtkWidget *header_view = NULL;
 
        g_return_if_fail (MODEST_IS_WINDOW(win));
-
+       
        /* Check first if the header view has the focus */
        if (MODEST_IS_MAIN_WINDOW (win)) {
-               header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
-                                                                  MODEST_WIDGET_TYPE_HEADER_VIEW);
+               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;
        }
-
+       
        header_list = get_selected_headers (win);
        if (!header_list) return;
 
        /* Check if any of the headers is already opened, or in the process of being opened */
-       iter = tny_list_create_iterator (header_list);
-       found = FALSE;
-       mgr = modest_runtime_get_window_mgr ();
-       while (!tny_iterator_is_done (iter) && !found) {
-               header = TNY_HEADER (tny_iterator_get_current (iter));
-               found =  modest_window_mgr_find_registered_header (mgr, header, NULL);
-               g_object_unref (header);
-               tny_iterator_next (iter);
-       }
-       g_object_unref (iter);
-
-       if (found) {
-               gchar *num, *msg;
+       if (MODEST_IS_MAIN_WINDOW (win)) {
+               gboolean found;
+               iter = tny_list_create_iterator (header_list);
+               found = FALSE;
+               mgr = modest_runtime_get_window_mgr ();
+               while (!tny_iterator_is_done (iter) && !found) {
+                       header = TNY_HEADER (tny_iterator_get_current (iter));
+                       found =  modest_window_mgr_find_registered_header (mgr, header, NULL);
+                       g_object_unref (header);
+                       tny_iterator_next (iter);
+               }
+               g_object_unref (iter);
 
-               num = g_strdup_printf ("%d", tny_list_get_length (header_list));
-               msg = g_strdup_printf (_("mcen_nc_unable_to_delete_n_messages"), num);
+               if (found) {
+                       gchar *num, *msg;
 
-               modest_platform_run_information_dialog (GTK_WINDOW (win), (const gchar *) msg);
+                       num = g_strdup_printf ("%d", tny_list_get_length (header_list));
+                       msg = g_strdup_printf (_("mcen_nc_unable_to_delete_n_messages"), num);
 
-               g_free (msg);
-               g_free (num);
-               g_object_unref (header_list);
-               return;
+                       modest_platform_run_information_dialog (GTK_WINDOW (win), (const gchar *) msg);
+                       
+                       g_free (msg);
+                       g_free (num);
+                       g_object_unref (header_list);
+                       return;
+               }
        }
 
        /* Select message */
@@ -320,23 +328,37 @@ modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
                                                            message);
        
 
-       if (response == GTK_RESPONSE_OK) {
-               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 (response == GTK_RESPONSE_OK) {      
+               ModestMainWindow *main_window;
+               ModestWindowMgr *mgr;
+
+               /* Remove each header. If it's a view window header_view == NULL */
+               do_headers_action (win, headers_action_delete, header_view);
 
                if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
-                       gtk_widget_destroy (GTK_WIDGET(win));
-               } 
-       }
+                       /* Close msg view window or select next */
+                       if (modest_msg_view_window_last_message_selected (MODEST_MSG_VIEW_WINDOW (win)) &&
+                           modest_msg_view_window_first_message_selected (MODEST_MSG_VIEW_WINDOW (win))) {
+                               modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (win));
+                       } else {
+                               modest_msg_view_window_select_next_message (MODEST_MSG_VIEW_WINDOW (win));
+                       }
+               }
 
-       /* Update toolbar dimming state */
-       modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (win));
+               /* Refilter header view model, if main window still exists */
+               mgr = modest_runtime_get_window_mgr ();
+               main_window = MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (mgr));
+               if (main_window) {
+                       GtkWidget *widget;
+
+                       widget = modest_main_window_get_child_widget (main_window,
+                                                                     MODEST_WIDGET_TYPE_HEADER_VIEW);
+                       modest_header_view_refilter (MODEST_HEADER_VIEW (widget));
+
+                       /* Update toolbar dimming state */
+                       modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
+               }
+       }
 
        /* free */
        g_free(message);
@@ -345,6 +367,38 @@ modest_ui_actions_on_delete (GtkAction *action, ModestWindow *win)
 }
 
 
+
+
+/* delete either message or folder, based on what has focus */
+void
+modest_ui_actions_on_delete_message_or_folder (GtkAction *action, ModestWindow *win)
+{
+       g_return_if_fail (MODEST_IS_WINDOW(win));
+       
+       /* Check first if the header view has the focus */
+       if (MODEST_IS_MAIN_WINDOW (win)) {
+               GtkWidget *w;
+
+               w = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                        MODEST_WIDGET_TYPE_HEADER_VIEW);
+               if (gtk_widget_is_focus (w)) {
+                       modest_ui_actions_on_delete_message (action, win);
+                       return;
+               }
+               
+               w = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                        MODEST_WIDGET_TYPE_FOLDER_VIEW);
+               if (gtk_widget_is_focus (w)) {
+                       modest_ui_actions_on_delete_folder (action, MODEST_MAIN_WINDOW(win));
+                       return;
+               }
+       }
+       g_warning ("BUG: delete toolbar button pressed with neither headerview nor folderview "
+                  "having focus");
+}
+
+
+
 void
 modest_ui_actions_on_quit (GtkAction *action, ModestWindow *win)
 {
@@ -589,6 +643,27 @@ cleanup:
                g_object_unref (G_OBJECT(folder));
 }
 
+gboolean 
+modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op,
+                                      TnyHeader *header,
+                                      TnyMsg *msg)
+{
+       ModestMailOperationStatus status;
+
+       /* If there is no message or the operation was not successful */
+       status = modest_mail_operation_get_status (mail_op);
+       if (!msg || status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
+
+               /* Remove the header from the preregistered uids */
+               modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (),  
+                                                    header);
+
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
 static void
 open_msg_cb (ModestMailOperation *mail_op, 
             TnyHeader *header, 
@@ -602,9 +677,14 @@ open_msg_cb (ModestMailOperation *mail_op,
        gchar *account = NULL;
        TnyFolder *folder;
        
-       /* TODO: Show an error? (review the specs) */
-       if (!msg)
+       /* Do nothing if there was any problem with the mail
+          operation. The error will be shown by the error_handler of
+          the mail operation */
+       if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg)) {
+               printf ("DEBUG: %s: modest_ui_actions_msg_retrieval_check() failed.\n", 
+                       __FUNCTION__);
                return;
+       }
 
        parent_win = (ModestWindow *) modest_mail_operation_get_source (mail_op);
        folder = tny_header_get_folder (header);
@@ -689,24 +769,27 @@ cleanup:
        g_object_unref (folder);
 }
 
-/*
- * This function is the error handler of the
- * modest_mail_operation_get_msgs_full operation
- */
-static void
+void
 modest_ui_actions_get_msgs_full_error_handler (ModestMailOperation *mail_op,
                                               gpointer user_data)
 {
        const GError *error;
+       GObject *win = modest_mail_operation_get_source (mail_op);
 
        error = modest_mail_operation_get_error (mail_op);
+       printf ("DEBUG: %s: Error: code=%d, text=%s\n", __FUNCTION__, error->code, error->message);
        if (error->code == MODEST_MAIL_OPERATION_ERROR_MESSAGE_SIZE_LIMIT) {
-               GObject *win = modest_mail_operation_get_source (mail_op);
 
                modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
                                                        error->message);
-               g_object_unref (win);
+       } else {
+               modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
+                                                       _("mail_ni_ui_folder_get_msg_folder_error"));
        }
+
+       if (win)
+               g_object_unref (win);
 }
 
 /*
@@ -755,9 +838,6 @@ _modest_ui_actions_open (TnyList *headers, ModestWindow *win)
                                g_warning ("debug: header %p already registered, waiting for window",
                                           header);
                } else {
-                       /* register the header before actually creating the window */
-                       modest_window_mgr_register_header (mgr, header);
-                               
                        if (!(flags & TNY_HEADER_FLAG_CACHED))
                                tny_list_append (not_opened_headers, G_OBJECT (header));
                        /* Check if msg has already been retreived */
@@ -767,6 +847,47 @@ _modest_ui_actions_open (TnyList *headers, ModestWindow *win)
                g_object_unref (header);
                tny_iterator_next (iter);
        }
+       g_object_unref (iter);
+       iter = NULL;
+       
+       /* If some messages would have to be downloaded, ask the user to 
+        * make a connection. It's generally easier to do this here (in the mainloop) 
+        * than later in a thread:
+        */
+       if (tny_list_get_length (not_opened_cached_headers) > 0) {
+               gboolean connected = modest_platform_connect_and_wait (GTK_WINDOW (win));
+               
+               /* Don't go further if a connection would be necessary but none is available: */
+               if (!connected) {
+                       g_object_unref (not_opened_headers);
+                       g_object_unref (not_opened_cached_headers);
+                       return;
+               }
+       }
+       
+       /* Register the headers before actually creating the windows: */
+       TnyIterator *iter_not_opened = tny_list_create_iterator (not_opened_headers);
+       while (!tny_iterator_is_done (iter_not_opened)) {
+               TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter_not_opened));
+               modest_window_mgr_register_header (mgr, header);
+               g_object_unref (header);
+               
+               tny_iterator_next (iter_not_opened);
+       }
+       g_object_unref (iter_not_opened);
+       iter_not_opened = NULL;
+       
+       TnyIterator *iter_cached = tny_list_create_iterator (not_opened_cached_headers);
+       while (!tny_iterator_is_done (iter_cached)) {
+               TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter_cached));
+               modest_window_mgr_register_header (mgr, header);
+               g_object_unref (header);
+               
+               tny_iterator_next (iter_cached);
+       }
+       g_object_unref (iter_cached);
+       iter_cached = NULL;
+       
        
        /* Open each uncached message */
        if (tny_list_get_length (not_opened_headers) > 0) {
@@ -792,7 +913,7 @@ _modest_ui_actions_open (TnyList *headers, ModestWindow *win)
 
        /* Open each cached message */
        if (tny_list_get_length (not_opened_cached_headers) > 0) {
-               mail_op2 = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_OPEN, 
+               mail_op2 = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
                                                                         G_OBJECT (win), 
                                                                         modest_ui_actions_get_msgs_full_error_handler, 
                                                                         NULL);
@@ -866,6 +987,10 @@ reply_forward_cb (ModestMailOperation *mail_op,
        TnyAccount *account = NULL;
        ModestWindowMgr *mgr = NULL;
        gchar *signature = NULL;
+
+       /* If there was any error */
+       if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg))
+               return;
                        
        g_return_if_fail (user_data != NULL);
        rf_helper = (ReplyForwardHelper *) user_data;
@@ -943,6 +1068,7 @@ cleanup:
                g_object_unref (G_OBJECT (account));
 /*     g_object_unref (msg); */
        g_object_unref (header);
+       free_reply_forward_helper (rf_helper);
 }
 
 /*
@@ -989,6 +1115,10 @@ download_uncached_messages (TnyList *header_list, GtkWindow *win)
                                                                         uncached_messages));
                if (response == GTK_RESPONSE_CANCEL)
                        retval = FALSE;
+               else {
+                       /* If a download will be necessary, make sure that we have a connection: */
+                       retval = modest_platform_connect_and_wait(win); 
+               }
        }
        return retval;
 }
@@ -1059,20 +1189,35 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                                g_object_unref (header);
                        g_printerr ("modest: no message found\n");
                        return;
-               } else
+               } else {
                        reply_forward_cb (NULL, header, msg, rf_helper);
+               }
        } else {
+               TnyHeader *header;
+               TnyIterator *iter;
+
                /* Retrieve messages */
                mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
                                                                         G_OBJECT(win),
                                                                         modest_ui_actions_get_msgs_full_error_handler, 
                                                                         NULL);
                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
-               modest_mail_operation_get_msgs_full (mail_op, 
-                                                    header_list, 
-                                                    reply_forward_cb, 
-                                                    rf_helper, 
-                                                    free_reply_forward_helper);
+
+               /* Only reply/forward to one message */
+               iter = tny_list_create_iterator (header_list);
+               header = TNY_HEADER (tny_iterator_get_current (iter));
+               g_object_unref (iter);
+
+               modest_mail_operation_get_msg (mail_op,
+                                              header,
+                                              reply_forward_cb,
+                                              rf_helper);
+
+/*             modest_mail_operation_get_msgs_full (mail_op,  */
+/*                                                  header_list,  */
+/*                                                  reply_forward_cb,  */
+/*                                                  rf_helper,  */
+/*                                                  free_reply_forward_helper); */
 
                /* Clean */
                g_object_unref(mail_op);
@@ -1205,8 +1350,7 @@ modest_ui_actions_do_send_receive (const gchar *account_name, ModestWindow *win)
        /* Set send/receive operation in progress */    
        modest_main_window_notify_send_receive_initied (MODEST_MAIN_WINDOW(win));
 
-/*     mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(win)); */
-       mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_OPEN,
+       mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE,
                                                                 G_OBJECT (win),
                                                                 modest_ui_actions_send_receive_error_handler,
                                                                 NULL);
@@ -1230,6 +1374,76 @@ modest_ui_actions_do_send_receive (const gchar *account_name, ModestWindow *win)
        g_free (acc_name);
 }
 
+
+static void
+modest_ui_actions_do_cancel_send (const gchar *account_name,  
+                                 ModestWindow *win)
+{
+       TnyTransportAccount *transport_account;
+       TnySendQueue *send_queue = NULL;
+       GError *error = NULL;
+
+       /* Get transport account */
+       transport_account =
+               TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_server_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);
+               goto frees;
+       }
+
+       /* Get send queue*/
+       send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
+       if (!TNY_IS_SEND_QUEUE(send_queue)) {
+               g_set_error (&error, MODEST_MAIL_OPERATION_ERROR,
+                            MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+                            "modest: could not find send queue for account\n");
+       } else {
+               /* Keeep messages in outbox folder */
+               tny_send_queue_cancel (send_queue, FALSE, &error);
+       }       
+
+ frees:
+       if (transport_account != NULL) 
+               g_object_unref (G_OBJECT (transport_account));
+}
+
+static void
+modest_ui_actions_cancel_send_all (ModestWindow *win) 
+{
+       GSList *account_names, *iter;
+
+       account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
+                                                         TRUE);
+
+       iter = account_names;
+       while (iter) {                  
+               modest_ui_actions_do_cancel_send ((const char*) iter->data, win);
+               iter = g_slist_next (iter);
+       }
+
+       modest_account_mgr_free_account_names (account_names);
+       account_names = NULL;
+}
+
+void
+modest_ui_actions_cancel_send (GtkAction *action,  ModestWindow *win)
+
+{
+       /* Check if accounts exist */
+       gboolean accounts_exist = 
+               modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE);
+       
+       /* If not, allow the user to create an account before trying to send/receive. */
+       if (!accounts_exist)
+               modest_ui_actions_on_accounts (NULL, win);
+       
+       /* Cancel all sending operaitons */     
+       modest_ui_actions_cancel_send_all (win);
+}
+
 /*
  * Refreshes all accounts. This function will be used by automatic
  * updates
@@ -1265,6 +1479,38 @@ modest_ui_actions_on_send_receive (GtkAction *action,  ModestWindow *win)
        /* If not, allow the user to create an account before trying to send/receive. */
        if (!accounts_exist)
                modest_ui_actions_on_accounts (NULL, win);
+
+       /* Refresh currently selected folder. Note that if we only
+          want to retrive the headers, then the refresh only will
+          invoke a poke_status over all folders, i.e., only the
+          total/unread count will be updated */
+       if (MODEST_IS_MAIN_WINDOW (win)) {
+               GtkWidget *header_view, *folder_view;
+               TnyFolderStore *folder_store;
+
+               /* Get folder and header view */
+               folder_view = 
+                       modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win), 
+                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
+
+               folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
+
+               if (TNY_IS_FOLDER (folder_store)) {
+                       header_view = 
+                               modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
+               
+                       /* We do not need to set the contents style
+                          because it hasn't changed. We also do not
+                          need to save the widget status. Just force
+                          a refresh */
+                       modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
+                                                      TNY_FOLDER (folder_store),
+                                                      folder_refreshed_cb,
+                                                      MODEST_MAIN_WINDOW (win));
+               }
+               g_object_unref (folder_store);
+       }
        
        /* Refresh the active account */
        modest_ui_actions_do_send_receive (NULL, win);
@@ -1375,7 +1621,7 @@ set_active_account_from_tny_account (TnyAccount *account,
 
 
 static void
-folder_refreshed_cb (const GObject *obj, 
+folder_refreshed_cb (ModestMailOperation *mail_op, 
                     TnyFolder *folder, 
                     gpointer user_data)
 {
@@ -1564,6 +1810,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        gchar *account_name, *from;
        ModestAccountMgr *account_mgr;
        gchar *info_text = NULL;
+       TnyMsg *new_draft = NULL;
 
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
        
@@ -1600,18 +1847,18 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_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,
-                                             data->draft_msg,
-                                             from,
-                                             data->to, 
-                                             data->cc, 
-                                             data->bcc,
-                                             data->subject, 
-                                             data->plain_body, 
-                                             data->html_body,
-                                             data->attachments,
-                                             data->priority_flags);
+       new_draft = modest_mail_operation_save_to_drafts (mail_operation,
+                                                         transport_account,
+                                                         data->draft_msg,
+                                                         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);
@@ -1620,6 +1867,10 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
 
        modest_msg_edit_window_free_msg_data (edit_window, data);
 
+       modest_msg_edit_window_set_draft (edit_window, new_draft);
+       if (new_draft != NULL)
+               g_object_unref (new_draft);
+
        info_text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
        modest_platform_information_banner (NULL, NULL, info_text);
        g_free (info_text);
@@ -1634,6 +1885,10 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        if (!modest_msg_edit_window_check_names (edit_window))
                return;
        
+       /* Offer the connection dialog, if necessary: */        
+       if (!modest_platform_connect_and_wait (GTK_WINDOW (edit_window)))
+               return;
+       
        /* FIXME: Code added just for testing. The final version will
           use the send queue provided by tinymail and some
           classifier */
@@ -1723,7 +1978,7 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        modest_msg_edit_window_set_sent (edit_window, TRUE);
 
        /* Save settings and close the window: */
-       gtk_widget_destroy (GTK_WIDGET (edit_window));
+       modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (edit_window));
 }
 
 void 
@@ -1871,6 +2126,25 @@ modest_ui_actions_on_remove_attachments (GtkAction *action,
        modest_msg_edit_window_remove_attachments (window, NULL);
 }
 
+static void
+modest_ui_actions_new_folder_error_handler (ModestMailOperation *mail_op,
+                                            gpointer user_data)
+{
+       ModestMainWindow *window = MODEST_MAIN_WINDOW (user_data);
+
+       /* TODO: Note that folder creation might go wrong due to other
+        * failures such as when the parent folder is non-writable. We can
+        * query a GError* with modest_mail_operation_get_error(), but the
+        * the error code (from tinymail) does not give us a clue about what
+        * has gone wrong. We might use the error->message but it might come
+        * from camel and not be suitable to show to the user directly. */
+       modest_platform_information_banner (GTK_WIDGET (window), NULL,
+                                           _CS("ckdg_ib_folder_already_exists"));
+
+/*     modest_platform_information_banner (GTK_WIDGET (window), NULL,
+                                           modest_mail_operation_get_error (mail_op)->message);*/
+}
+
 void 
 modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_window)
 {
@@ -1898,14 +2172,20 @@ modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_windo
                                                                        suggested_name,
                                                                        &folder_name);
 
+                       g_free (suggested_name);
+                       suggested_name = NULL;
+
                        if (result == GTK_RESPONSE_REJECT) {
                                finished = TRUE;
                        } else {
                                ModestMailOperation *mail_op;
                                TnyFolder *new_folder = NULL;
 
-                               mail_op  = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_INFO, 
-                                                                     G_OBJECT(main_window));
+                               mail_op  = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_INFO,
+                                                                                         G_OBJECT(main_window),
+                                                                                         modest_ui_actions_new_folder_error_handler,
+                                                                                         main_window);
+
                                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
                                                                 mail_op);
                                new_folder = modest_mail_operation_create_folder (mail_op,
@@ -1917,7 +2197,8 @@ modest_ui_actions_on_new_folder (GtkAction *action, ModestMainWindow *main_windo
                                }
                                g_object_unref (mail_op);
                        }
-                       g_free (folder_name);
+
+                       suggested_name = folder_name;
                        folder_name = NULL;
                }
 
@@ -1968,6 +2249,13 @@ modest_ui_actions_on_rename_folder (GtkAction *action,
                return;
 
        folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
+
+       /* Offer the connection dialog if necessary: */
+       if (!modest_platform_connect_and_wait_if_network_folderstore (NULL, folder)) {
+               g_object_unref (G_OBJECT (folder));
+               return;
+       }
+
        
        if (folder && TNY_IS_FOLDER (folder)) {
                gchar *folder_name;
@@ -2036,9 +2324,16 @@ delete_folder (ModestMainWindow *main_window, gboolean move_to_trash)
        if (!TNY_IS_FOLDER (folder)) {
                modest_platform_run_information_dialog (GTK_WINDOW (main_window),
                                                        _("mail_in_ui_folder_delete_error"));
+               g_object_unref (G_OBJECT (folder));
                return ;
        }
 
+       /* Offer the connection dialog if necessary: */
+       if (!modest_platform_connect_and_wait_if_network_folderstore (NULL, folder)) {
+               g_object_unref (G_OBJECT (folder));
+               return;
+       }
+
        /* Ask the user */      
        message =  g_strdup_printf (_("mcen_nc_delete_folder_text"), 
                                    tny_folder_get_name (TNY_FOLDER (folder)));
@@ -2274,6 +2569,7 @@ modest_ui_actions_on_cut (GtkAction *action,
                clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
                buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
                gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
+               gtk_clipboard_set_can_store (clipboard, NULL, 0);
        } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
                modest_header_view_cut_selection (MODEST_HEADER_VIEW (focused_widget));
        } else if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
@@ -2299,7 +2595,7 @@ modest_ui_actions_on_copy (GtkAction *action,
                GtkTextBuffer *buffer;
                buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
                gtk_text_buffer_copy_clipboard (buffer, clipboard);
-               modest_header_view_copy_selection (MODEST_HEADER_VIEW (focused_widget));
+               gtk_clipboard_set_can_store (clipboard, NULL, 0);
        } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
                TnyList *header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (focused_widget));
                TnyIterator *iter = tny_list_create_iterator (header_list);
@@ -2339,7 +2635,7 @@ modest_ui_actions_on_undo (GtkAction *action,
 
        if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
                modest_msg_edit_window_undo (MODEST_MSG_EDIT_WINDOW (window));
-       } if (MODEST_IS_MAIN_WINDOW (window)) {
+       } else if (MODEST_IS_MAIN_WINDOW (window)) {
                /* Clear clipboard source */
                clipboard = modest_runtime_get_email_clipboard ();
                modest_email_clipboard_clear (clipboard);               
@@ -2350,10 +2646,34 @@ modest_ui_actions_on_undo (GtkAction *action,
 }
 
 void
+modest_ui_actions_on_redo (GtkAction *action,
+                          ModestWindow *window)
+{
+       if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
+               modest_msg_edit_window_redo (MODEST_MSG_EDIT_WINDOW (window));
+       }
+       else {
+               g_return_if_reached ();
+       }
+}
+
+
+static void
+paste_msgs_cb (const GObject *object, gpointer user_data)
+{
+       g_return_if_fail (MODEST_IS_MAIN_WINDOW (object));
+       g_return_if_fail (GTK_IS_WIDGET (user_data));
+       
+       /* destroy information note */
+       gtk_widget_destroy (GTK_WIDGET(user_data));
+}
+
+void
 modest_ui_actions_on_paste (GtkAction *action,
                            ModestWindow *window)
 {
-       GtkWidget *focused_widget;
+       GtkWidget *focused_widget = NULL;
+       GtkWidget *inf_note = NULL;
        ModestMailOperation *mail_op = NULL;
 
        focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
@@ -2389,20 +2709,30 @@ modest_ui_actions_on_paste (GtkAction *action,
                /* Get destination folder */
                folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (focused_widget));
 
+               /* Launch notification */
+               inf_note = modest_platform_animation_banner (GTK_WIDGET (window), NULL, 
+                                                            _CS("ckct_nw_pasting"));
+               if (inf_note != NULL)  {
+                       gtk_window_set_modal (GTK_WINDOW(inf_note), FALSE);
+                       gtk_widget_show (GTK_WIDGET(inf_note));
+               }
+
                /* transfer messages  */
                if (data != NULL) {
                        modest_mail_operation_xfer_msgs (mail_op, 
                                                         data,
                                                         TNY_FOLDER (folder_store),
                                                         delete,
-                                                        NULL,
-                                                        NULL);
+                                                        paste_msgs_cb,
+                                                        inf_note);
                        
                } else if (src_folder != NULL) {                        
                        modest_mail_operation_xfer_folder (mail_op, 
                                                           src_folder,
                                                           folder_store,
-                                                          delete);
+                                                          delete,
+                                                          paste_msgs_cb,
+                                                          inf_note);
                }
 
                /* Free */
@@ -2415,6 +2745,7 @@ modest_ui_actions_on_paste (GtkAction *action,
        }
 }
 
+
 void
 modest_ui_actions_on_select_all (GtkAction *action,
                                 ModestWindow *window)
@@ -2891,6 +3222,7 @@ msgs_move_to_confirmation (GtkWindow *win,
 }
 
 
+
 static void
 transfer_msgs_from_viewer_cb (const GObject *object, gpointer user_data)
 {
@@ -2945,12 +3277,16 @@ modest_ui_actions_send_receive_error_handler (ModestMailOperation *mail_op,
        if (error->message != NULL)             
                g_printerr ("modest: %s\n", error->message);
        else
-               g_printerr ("modest: unkonw error on sedn&receive operation");
+               g_printerr ("modest: unkonw error on send&receive operation");
 
        /* Show error message */
-       modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
-                                               _CS("sfil_ib_unable_to_send")); 
-/*     g_object_unref (win); */
+/*     if (modest_mail_operation_get_id (mail_op) == MODEST_MAIL_OPERATION_TYPE_RECEIVE) */
+/*             modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL, */
+/*                                                     _CS("sfil_ib_unable_to_receive")); */
+/*     else  */
+/*             modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL, */
+/*                                                     _CS("sfil_ib_unable_to_send")); */
+       g_object_unref (win);
 }
 
 static void
@@ -2964,7 +3300,9 @@ open_msg_for_purge_cb (ModestMailOperation *mail_op,
        gint pending_purges = 0;
        gboolean some_purged = FALSE;
        ModestWindow *win = MODEST_WINDOW (user_data);
-       if (!msg)
+
+       /* If there was any error */
+       if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg))
                return;
 
        /* Once the message has been retrieved for purging, we check if
@@ -3064,7 +3402,7 @@ modest_ui_actions_on_main_window_remove_attachments (GtkAction *action,
        } else {
                ModestMailOperation *mail_op = NULL;
                modest_window_mgr_register_header (modest_runtime_get_window_mgr (), header);
-               mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_OPEN,
+               mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE,
                                                                         G_OBJECT (win),
                                                                         modest_ui_actions_get_msgs_full_error_handler,
                                                                         NULL);
@@ -3089,7 +3427,7 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action,
 {
        GtkWidget *dialog = NULL, *folder_view = NULL, *tree_view = NULL;
        GtkWidget *header_view = NULL;
-       gint result;
+       gint result = 0;
        TnyFolderStore *folder_store = NULL;
        ModestMailOperation *mail_op = NULL;
 
@@ -3098,6 +3436,17 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action,
        /* Get the folder view */
        folder_view = modest_main_window_get_child_widget (win,
                                                           MODEST_WIDGET_TYPE_FOLDER_VIEW);
+                                                          
+       TnyFolderStore *src_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
+       
+       /* Offer the connection dialog if necessary, if the source folder is in a networked account: */
+       if (!modest_platform_connect_and_wait_if_network_folderstore (GTK_WINDOW (win), src_folder)) {
+               if (src_folder) {
+                       g_object_unref (src_folder);
+               }
+               
+               return;
+       }
 
        /* Get header view */
        header_view = modest_main_window_get_child_widget (win,
@@ -3119,37 +3468,40 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action,
            !MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (folder_store))
                goto end;
 
+       /* Offer the connection dialog if necessary: */
+       if (!modest_platform_connect_and_wait_if_network_folderstore (GTK_WINDOW (win), 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));
-
+               
                /* Clean folder on header view before moving it */
                modest_header_view_clear (MODEST_HEADER_VIEW (header_view)); 
 
                if (TNY_IS_FOLDER (src_folder)) {
                        mail_op = 
                                modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
-                                                                              G_OBJECT(win),
-                                                                              modest_ui_actions_move_folder_error_handler,
-                                                                              NULL);
+                                                                      G_OBJECT(win),
+                                                                      modest_ui_actions_move_folder_error_handler,
+                                                                      NULL);
                        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
 
                        modest_mail_operation_xfer_folder (mail_op, 
                                                           TNY_FOLDER (src_folder),
                                                           folder_store,
-                                                          TRUE);
+                                                          TRUE, NULL, NULL);
                        /* Unref mail operation */
                        g_object_unref (G_OBJECT (mail_op));
+               } else {
+                       g_warning ("%s: src_folder is not a TnyFolder.\n", __FUNCTION__);       
                }
-
-               /* Frees */
-               g_object_unref (G_OBJECT (src_folder));
        } else {
                if (gtk_widget_is_focus (header_view)) {
-                       TnyList *headers;
-                       gint response;
+                       TnyList *headers = NULL;
+                       gint response = 0;
 
+                       /* TODO: Check for connection if the headers are on a network account. */
                        headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW (header_view));
 
                        /* Ask for user confirmation */
@@ -3179,9 +3531,14 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action,
                        g_object_unref (headers);
                }
        }
+       
  end:
-       if (folder_store != NULL)
+    if (src_folder)
+       g_object_unref (src_folder);
+                       
+       if (folder_store)
                g_object_unref (folder_store);
+
        gtk_widget_destroy (dialog);
 }
 
@@ -3195,10 +3552,10 @@ 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;
-       TnyHeader *header;
-       TnyList *headers;
+       gint result = 0;
+       ModestMainWindow *main_window = NULL;
+       TnyHeader *header = NULL;
+       TnyList *headers = NULL;
 
        /* Get the folder view */
        main_window = MODEST_MAIN_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ()));
@@ -3211,47 +3568,61 @@ modest_ui_actions_on_msg_view_window_move_to (GtkAction *action,
        /* Create and run the dialog */
        dialog = create_move_to_dialog (MODEST_WINDOW (win), folder_view, &tree_view);  
        result = gtk_dialog_run (GTK_DIALOG(dialog));
-       g_object_ref (tree_view);
 
        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 */
                header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));              
+               g_return_if_fail (header != NULL);
+
+               /* Offer the connection dialog if necessary: */
+               /* TODO: What's the extra g_object_ref() for? Isn't this leaking a ref? */
+               folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (g_object_ref (tree_view)));
+               TnyFolder *header_folder = tny_header_get_folder(header);
+               if (modest_platform_connect_and_wait_if_network_folderstore (NULL, folder_store) &&
+                   modest_platform_connect_and_wait_if_network_folderstore (NULL, TNY_FOLDER_STORE (header_folder))) {
                        
-               headers = tny_simple_list_new ();
-               tny_list_prepend (headers, G_OBJECT (header));
-               g_object_unref (header);
+                       headers = tny_simple_list_new ();
+                       tny_list_prepend (headers, G_OBJECT (header));
+                       g_object_unref (header);
 
-               /* Ask user for confirmation. MSG-NOT404 */
-               response = msgs_move_to_confirmation (GTK_WINDOW (win), 
+                       /* 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;
+                       /* Transfer current msg */
+                       if (response == GTK_RESPONSE_OK) {
+                               ModestMailOperation *mail_op;
 
-                       /* Create mail op */
-                       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_RECEIVE, G_OBJECT(win));
-                       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
+                               /* Create mail op */
+                               mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_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, 
+                               /* Transfer messages */
+                               modest_mail_operation_xfer_msgs (mail_op, 
                                                         headers,
                                                         TNY_FOLDER (folder_store),
                                                         TRUE,
                                                         transfer_msgs_from_viewer_cb,
                                                         NULL);
-                       g_object_unref (G_OBJECT (mail_op));
+                               g_object_unref (G_OBJECT (mail_op));
+                       }
                }
-               g_object_unref (headers);
-               g_object_unref (folder_store);
+               
+               if (header_folder)
+                       g_object_unref (header_folder);
+
+               if (headers)
+                       g_object_unref (headers);
+                       
+               if (folder_store)
+                       g_object_unref (folder_store);
        }
+       
        gtk_widget_destroy (dialog);
 }