Work to fix bug NB#81989:
[modest] / src / modest-ui-actions.c
index d3cd601..d7d3d7a 100644 (file)
@@ -70,7 +70,7 @@
 #include "modest-text-utils.h"
 
 #ifdef MODEST_HAVE_EASYSETUP
-#include "easysetup/modest-easysetup-wizard.h"
+#include "easysetup/modest-easysetup-wizard-dialog.h"
 #endif /* MODEST_HAVE_EASYSETUP */
 
 #include <modest-widget-memory.h>
@@ -703,7 +703,8 @@ modest_ui_actions_compose_msg(ModestWindow *win,
                              const gchar *bcc_str,
                              const gchar *subject_str,
                              const gchar *body_str,
-                             GSList *attachments)
+                             GSList *attachments,
+                             gboolean set_as_modified)
 {
        gchar *account_name = NULL;
        TnyMsg *msg = NULL;
@@ -758,6 +759,8 @@ modest_ui_actions_compose_msg(ModestWindow *win,
                attachments = g_slist_next(attachments);
        }
        modest_window_mgr_register_window (modest_runtime_get_window_mgr(), msg_win);
+       modest_msg_edit_window_set_modified (MODEST_MSG_EDIT_WINDOW (msg_win), set_as_modified);
+
        gtk_widget_show_all (GTK_WIDGET (msg_win));
 
 cleanup:
@@ -779,7 +782,7 @@ modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win)
                if (!modest_ui_actions_run_account_setup_wizard (win))
                        return;
                
-       modest_ui_actions_compose_msg(win, NULL, NULL, NULL, NULL, NULL, NULL);
+       modest_ui_actions_compose_msg(win, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
 }
 
 
@@ -804,6 +807,35 @@ modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op,
        return TRUE;
 }
 
+typedef struct {
+       guint idle_handler;
+       gchar *message;
+       GtkWidget *banner;
+} OpenMsgBannerInfo;
+
+typedef struct {
+       GtkTreeModel *model;
+       TnyList *headers;
+       OpenMsgBannerInfo *banner_info;
+       GHashTable *row_refs_per_header;
+} OpenMsgHelper;
+
+gboolean
+open_msg_banner_idle (gpointer userdata)
+{
+       OpenMsgBannerInfo *banner_info = (OpenMsgBannerInfo *) userdata;
+
+       gdk_threads_enter ();
+       banner_info->idle_handler = 0;
+       banner_info->banner = modest_platform_animation_banner (NULL, NULL, banner_info->message);
+       g_object_ref (banner_info->banner);
+       
+       gdk_threads_leave ();
+
+       return FALSE;
+       
+}
+
 static void
 open_msg_cb (ModestMailOperation *mail_op, 
             TnyHeader *header,  
@@ -819,6 +851,7 @@ open_msg_cb (ModestMailOperation *mail_op,
        gchar *account = NULL;
        TnyFolder *folder;
        gboolean open_in_editor = FALSE;
+       OpenMsgHelper *helper = (OpenMsgHelper *) user_data;
        
        /* Do nothing if there was any problem with the mail
           operation. The error will be shown by the error_handler of
@@ -905,39 +938,17 @@ open_msg_cb (ModestMailOperation *mail_op,
                win = modest_msg_edit_window_new (msg, account, TRUE);
 
 
-               /* Show banner */
-               modest_platform_information_banner_with_timeout
-                       (NULL, NULL, _("mail_ib_opening_draft_message"), 1200);
 
        } else {
                gchar *uid = modest_tny_folder_get_header_unique_id (header);
                
                if (MODEST_IS_MAIN_WINDOW (parent_win)) {
-                       GtkWidget *header_view;
-                       GtkTreeSelection *sel;
-                       GList *sel_list = NULL;
-                       GtkTreeModel *model;
-                       
-                       header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(parent_win),
-                                                                          MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
-
-                       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
-                       sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
+                       GtkTreeRowReference *row_reference;
 
-                       if (sel_list != NULL) {
-                               GtkTreeRowReference *row_reference;
-
-                               row_reference = gtk_tree_row_reference_new (model, (GtkTreePath *) sel_list->data);
-                               g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
-                               g_list_free (sel_list);
+                       row_reference = (GtkTreeRowReference *) g_hash_table_lookup (helper->row_refs_per_header, header);
                                
-                               win = modest_msg_view_window_new_with_header_model (
-                                               msg, account, (const gchar*) uid,
-                                               model, row_reference);
-                               gtk_tree_row_reference_free (row_reference);
-                       } else {
-                               win = modest_msg_view_window_new_for_attachment (msg, account, (const gchar*) uid);
-                       }
+                       win = modest_msg_view_window_new_with_header_model (msg, account, (const gchar*) uid,
+                                                                           helper->model, row_reference);
                } else {
                        win = modest_msg_view_window_new_for_attachment (msg, account, (const gchar*) uid);
                }
@@ -965,8 +976,8 @@ cleanup:
 }
 
 void
-modest_ui_actions_get_msgs_full_error_handler (ModestMailOperation *mail_op,
-                                              gpointer user_data)
+modest_ui_actions_disk_operations_error_handler (ModestMailOperation *mail_op,
+                                                   gpointer user_data)
 {
        const GError *error;
        GObject *win = NULL;
@@ -1007,9 +1018,29 @@ get_account_from_header_list (TnyList *headers)
                TnyIterator *iter = tny_list_create_iterator (headers);
                TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
                TnyFolder *folder = tny_header_get_folder (header);
-               account = tny_folder_get_account (folder);
-               g_object_unref (folder);
-               g_object_unref (header);
+               
+               if (!folder) {
+                       g_object_unref (header);
+                       
+                       while (!tny_iterator_is_done (iter)) {
+                               header = TNY_HEADER (tny_iterator_get_current (iter));
+                               folder = tny_header_get_folder (header);
+                               if (folder) 
+                                       break;
+                               g_object_unref (header);
+                               header = NULL;
+                               tny_iterator_next (iter);
+                       }
+               }
+
+               if (folder) {
+                       account = tny_folder_get_account (folder);
+                       g_object_unref (folder);
+               }
+               
+               if (header)
+                       g_object_unref (header);
+               
                g_object_unref (iter);
        }
        return account;
@@ -1026,6 +1057,31 @@ foreach_unregister_headers (gpointer data,
 }
 
 static void
+open_msgs_helper_destroyer (gpointer user_data)
+{
+       OpenMsgHelper *helper = (OpenMsgHelper *) user_data;
+
+       if (helper->banner_info) {
+               g_free (helper->banner_info->message);
+               if (helper->banner_info->idle_handler > 0) {
+                       g_source_remove (helper->banner_info->idle_handler);
+                       helper->banner_info->idle_handler = 0;
+               }
+               if (helper->banner_info->banner != NULL) {
+                       gtk_widget_destroy (helper->banner_info->banner);
+                       g_object_unref (helper->banner_info->banner);
+                       helper->banner_info->banner = NULL;
+               }
+               g_slice_free (OpenMsgBannerInfo, helper->banner_info);
+               helper->banner_info = NULL;
+       }
+       g_object_unref (helper->model);
+       g_object_unref (helper->headers);
+       g_hash_table_destroy (helper->row_refs_per_header);
+       g_slice_free (OpenMsgHelper, helper);
+}
+
+static void
 open_msgs_performer(gboolean canceled, 
                    GError *err,
                    GtkWindow *parent_window,
@@ -1038,14 +1094,19 @@ open_msgs_performer(gboolean canceled,
        ModestTransportStoreProtocol proto;
        TnyList *not_opened_headers;
        TnyConnectionStatus status;
+       gboolean show_open_draft = FALSE;
+       OpenMsgHelper *helper = NULL;
 
-       not_opened_headers = TNY_LIST (user_data);
+       helper = (OpenMsgHelper *) user_data;
+       not_opened_headers = helper->headers;
 
        status = tny_account_get_connection_status (account);
        if (err || canceled) {
                /* Unregister the already registered headers */
                tny_list_foreach (not_opened_headers, foreach_unregister_headers, 
                                  modest_runtime_get_window_mgr ());
+               /* Free the helper */
+               open_msgs_helper_destroyer (helper);
                goto clean;
        }
 
@@ -1069,6 +1130,19 @@ open_msgs_performer(gboolean canceled,
                        g_object_unref (header);
                        g_object_unref (iter);
                } else {
+                       TnyHeader *header;
+                       TnyFolder *folder;
+                       TnyIterator *iter;
+                       TnyFolderType folder_type;
+
+                       iter = tny_list_create_iterator (not_opened_headers);
+                       header = TNY_HEADER (tny_iterator_get_current (iter));
+                       folder = tny_header_get_folder (header);
+                       folder_type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
+                       show_open_draft = (folder_type == TNY_FOLDER_TYPE_DRAFTS);
+                       g_object_unref (folder);
+                       g_object_unref (header);
+                       g_object_unref (iter);
                        error_msg = g_strdup (_("mail_ni_ui_folder_get_msg_folder_error"));
                }
        } else {
@@ -1078,22 +1152,29 @@ open_msgs_performer(gboolean canceled,
        /* Create the mail operation */
        mail_op = 
                modest_mail_operation_new_with_error_handling ((GObject *) parent_window,
-                                                              modest_ui_actions_get_msgs_full_error_handler,
+                                                              modest_ui_actions_disk_operations_error_handler,
                                                               error_msg, g_free);
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                         mail_op);
-               
+
+       if (show_open_draft) {
+               helper->banner_info = g_slice_new (OpenMsgBannerInfo);
+               helper->banner_info->message = g_strdup (_("mail_ib_opening_draft_message"));
+               helper->banner_info->banner = NULL;
+               helper->banner_info->idle_handler = g_timeout_add (500, open_msg_banner_idle, 
+                                                                  helper->banner_info);
+       }
+
        modest_mail_operation_get_msgs_full (mail_op,
                                             not_opened_headers,
                                             open_msg_cb,
-                                            NULL,
-                                            NULL);
+                                            helper,
+                                            open_msgs_helper_destroyer);
 
        /* Frees */
  clean:
        if (mail_op)
                g_object_unref (mail_op);
-       g_object_unref (not_opened_headers);
        g_object_unref (account);
 }
 
@@ -1111,13 +1192,19 @@ open_msgs_from_headers (TnyList *headers, ModestWindow *win)
        TnyHeaderFlags flags = 0;
        TnyAccount *account;
        gint uncached_msgs = 0;
+       GtkWidget *header_view;
+       GtkTreeModel *model;
+       GHashTable *refs_for_headers;
+       OpenMsgHelper *helper;
+       GtkTreeSelection *sel;
+       GList *sel_list = NULL, *sel_list_iter = NULL;
                
        g_return_if_fail (headers != NULL);
 
        /* Check that only one message is selected for opening */
        if (tny_list_get_length (headers) != 1) {
-               modest_platform_run_information_dialog ((win) ? GTK_WINDOW (win) : NULL,
-                                                       _("mcen_ib_select_one_message"));
+               modest_platform_information_banner ((win) ? GTK_WIDGET (win) : NULL,
+                                                   NULL, _("mcen_ib_select_one_message"));
                return;
        }
 
@@ -1126,12 +1213,27 @@ open_msgs_from_headers (TnyList *headers, ModestWindow *win)
 
        /* Get the account */
        account = get_account_from_header_list (headers);
-       
+
+       if (!account)
+               return;
+
+       /* Get the selections, we need to get the references to the
+          rows here because the treeview/model could dissapear (the
+          user might want to select another folder)*/
+       header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                          MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
+       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view));
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
+       sel_list = gtk_tree_selection_get_selected_rows (sel, &model);
+       refs_for_headers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, 
+                                                 (GDestroyNotify) gtk_tree_row_reference_free);
+
        /* Look if we already have a message view for each header. If
           true, then remove the header from the list of headers to
           open */
+       sel_list_iter = sel_list;
        not_opened_headers = tny_simple_list_new ();
-       while (!tny_iterator_is_done (iter)) {
+       while (!tny_iterator_is_done (iter) && sel_list_iter) {
 
                ModestWindow *window = NULL;
                TnyHeader *header = NULL;
@@ -1147,27 +1249,39 @@ open_msgs_from_headers (TnyList *headers, ModestWindow *win)
                /* Do not open again the message and present the
                   window to the user */
                if (found) {
-                       if (window)
+                       if (window) {
                                gtk_window_present (GTK_WINDOW (window));
-                       else
+                       } else {
                                /* the header has been registered already, we don't do
                                 * anything but wait for the window to come up*/
                                g_debug ("header %p already registered, waiting for window", header);
+                       }
                } else {
+                       GtkTreeRowReference *row_reference;
+
                        tny_list_append (not_opened_headers, G_OBJECT (header));
+                       /* Create a new row reference and add it to the hash table */
+                       row_reference = gtk_tree_row_reference_new (model, (GtkTreePath *) sel_list_iter->data);
+                       g_hash_table_insert (refs_for_headers, header, row_reference);
                }
 
                if (header)
                        g_object_unref (header);
 
+               /* Go to next */
                tny_iterator_next (iter);
+               sel_list_iter = g_list_next (sel_list_iter);
        }
        g_object_unref (iter);
        iter = NULL;
+       g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
+       g_list_free (sel_list);
 
        /* Open each message */
-       if (tny_list_get_length (not_opened_headers) == 0)
+       if (tny_list_get_length (not_opened_headers) == 0) {
+               g_hash_table_destroy (refs_for_headers);
                goto cleanup;
+       }
        
        /* 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) 
@@ -1188,8 +1302,10 @@ open_msgs_from_headers (TnyList *headers, ModestWindow *win)
                                                                                             uncached_msgs));
 
                                /* End if the user does not want to continue */
-                               if (response == GTK_RESPONSE_CANCEL)
+                               if (response == GTK_RESPONSE_CANCEL) {
+                                       g_hash_table_destroy (refs_for_headers);
                                        goto cleanup;
+                               }
                        }
                }
        }
@@ -1207,14 +1323,23 @@ open_msgs_from_headers (TnyList *headers, ModestWindow *win)
        g_object_unref (iter_not_opened);
        iter_not_opened = NULL;
 
+       /* Create the helper. We need to get a reference to the model
+          here because it could change while the message is readed
+          (the user could switch between folders) */
+       helper = g_slice_new (OpenMsgHelper);
+       helper->model = g_object_ref (model);
+       helper->headers = g_object_ref (not_opened_headers);
+       helper->row_refs_per_header = refs_for_headers;
+       helper->banner_info = NULL;
+
        /* Connect to the account and perform */
        if (uncached_msgs > 0) {
                modest_platform_connect_and_perform ((GtkWindow *) win, TRUE, g_object_ref (account), 
-                                                    open_msgs_performer, g_object_ref (not_opened_headers));
+                                                    open_msgs_performer, helper);
        } else {
                /* Call directly the performer, do not need to connect */
-               open_msgs_performer (FALSE, NULL, (GtkWindow *) win, g_object_ref (account),
-                                    g_object_ref (not_opened_headers));
+               open_msgs_performer (FALSE, NULL, (GtkWindow *) win, 
+                                    g_object_ref (account), helper);
        }
 cleanup:
        /* Clean */
@@ -1449,8 +1574,10 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                 * whether he/she wants to download them. */
                if (num_of_unc_msgs) {
                        TnyAccount *account = get_account_from_header_list (header_list);
-                       continue_download = connect_to_get_msg (win, num_of_unc_msgs, account);
-                       g_object_unref (account);
+                       if (account) {
+                               continue_download = connect_to_get_msg (win, num_of_unc_msgs, account);
+                               g_object_unref (account);
+                       }
                }
        }
 
@@ -1507,7 +1634,7 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                        if (do_retrieve) {
                                mail_op = 
                                        modest_mail_operation_new_with_error_handling (G_OBJECT(win),
-                                                                                      modest_ui_actions_get_msgs_full_error_handler, 
+                                                                                      modest_ui_actions_disk_operations_error_handler, 
                                                                                       NULL, NULL);
                                modest_mail_operation_queue_add (
                                        modest_runtime_get_mail_operation_queue (), mail_op);
@@ -1673,6 +1800,8 @@ typedef struct {
        TnyAccount *account;
        ModestWindow *win;
        gchar *account_name;
+       gboolean poke_status;
+       gboolean interactive;
 } SendReceiveInfo;
 
 static void
@@ -1707,7 +1836,7 @@ do_send_receive_performer (gboolean canceled,
 
        /* Send & receive. */
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
-       modest_mail_operation_update_account (mail_op, info->account_name, (info->win) ? FALSE : TRUE,
+       modest_mail_operation_update_account (mail_op, info->account_name, info->poke_status, info->interactive,
                                              (info->win) ? retrieve_all_messages_cb : NULL, 
                                              new_messages_arrived, info->win);
        g_object_unref (G_OBJECT (mail_op));
@@ -1732,6 +1861,8 @@ do_send_receive_performer (gboolean canceled,
 void
 modest_ui_actions_do_send_receive (const gchar *account_name, 
                                   gboolean force_connection,
+                                  gboolean poke_status,
+                                  gboolean interactive,
                                   ModestWindow *win)
 {
        gchar *acc_name = NULL;
@@ -1759,6 +1890,8 @@ modest_ui_actions_do_send_receive (const gchar *account_name,
        info = g_slice_new (SendReceiveInfo);
        info->account_name = acc_name;
        info->win = (win) ? g_object_ref (win) : NULL;
+       info->poke_status = poke_status;
+       info->interactive = interactive;
        info->account = modest_tny_account_store_get_server_account (acc_store, acc_name,
                                                                     TNY_ACCOUNT_TYPE_STORE);
 
@@ -1847,7 +1980,9 @@ modest_ui_actions_cancel_send (GtkAction *action,  ModestWindow *win)
  */
 void
 modest_ui_actions_do_send_receive_all (ModestWindow *win, 
-                                      gboolean force_connection)
+                                      gboolean force_connection,
+                                      gboolean poke_status,
+                                      gboolean interactive)
 {
        GSList *account_names, *iter;
 
@@ -1856,7 +1991,9 @@ modest_ui_actions_do_send_receive_all (ModestWindow *win,
 
        iter = account_names;
        while (iter) {                  
-               modest_ui_actions_do_send_receive ((const char*) iter->data, force_connection, win);
+               modest_ui_actions_do_send_receive ((const char*) iter->data, 
+                                                  force_connection, 
+                                                  poke_status, interactive, win);
                iter = g_slist_next (iter);
        }
 
@@ -1897,8 +2034,9 @@ modest_ui_actions_on_send_receive (GtkAction *action, ModestWindow *win)
                        g_object_unref (folder_store);
        }       
        
-       /* Refresh the active account. Force the connection if needed */
-       modest_ui_actions_do_send_receive (NULL, TRUE, win);
+       /* Refresh the active account. Force the connection if needed
+          and poke the status of all folders */
+       modest_ui_actions_do_send_receive (NULL, TRUE, TRUE, TRUE, win);
 }
 
 
@@ -1966,6 +2104,7 @@ modest_ui_actions_on_header_selected (ModestHeaderView *header_view,
            gtk_widget_grab_focus (GTK_WIDGET(header_view));
 
        /* Update toolbar dimming state */
+       modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (main_window));
        modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
 }
 
@@ -2238,15 +2377,10 @@ on_save_to_drafts_cb (ModestMailOperation *mail_op,
 
        edit_window = MODEST_MSG_EDIT_WINDOW (user_data);
 
-       /* It might not be a good idea to do nothing if there was an error,
-        * so let's at least show a generic error banner. */
-       /* TODO error while saving attachment, show "Saving draft failed" banner */
-       if (modest_mail_operation_get_error (mail_op) != NULL) {
-               g_warning ("%s failed: %s\n", __FUNCTION__, (modest_mail_operation_get_error (mail_op))->message);
-               modest_platform_information_banner (NULL, NULL, _("mail_ib_file_operation_failed"));
-       } else {
+       /* Set draft is there was no error */
+       if (!modest_mail_operation_get_error (mail_op))
                modest_msg_edit_window_set_draft (edit_window, saved_draft);
-       }
+
        g_object_unref(edit_window);
 }
 
@@ -2263,6 +2397,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        guint64 available_disk, expected_size;
        gint parts_count;
        guint64 parts_size;
+       ModestMainWindow *win;
 
        g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window), FALSE);
        
@@ -2313,7 +2448,8 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        from = modest_account_mgr_get_from_string (account_mgr, account_name);
 
        /* Create the mail operation */         
-       mail_operation = modest_mail_operation_new (NULL);
+       mail_operation = modest_mail_operation_new_with_error_handling (NULL, modest_ui_actions_disk_operations_error_handler,
+                                                                       NULL, NULL);
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
 
        modest_mail_operation_save_to_drafts (mail_operation,
@@ -2332,9 +2468,19 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
                                              on_save_to_drafts_cb,
                                              g_object_ref(edit_window));
 
-/*     info_text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts")); */
-       modest_platform_information_banner (NULL, NULL, _CS("sfil_ib_saving"));
-       modest_msg_edit_window_reset_modified (edit_window);
+       /* Use the main window as the parent of the banner, if the
+          main window does not exist it won't be shown, if the parent
+          window exists then it's properly shown. We don't use the
+          editor window because it could be closed (save to drafts
+          could happen after closing the window */
+       win = (ModestMainWindow *)
+               modest_window_mgr_get_main_window( modest_runtime_get_window_mgr(), FALSE);
+       if (win) {
+               gchar *text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
+               modest_platform_information_banner (GTK_WIDGET (win), NULL, text);
+               g_free (text);
+       }
+       modest_msg_edit_window_set_modified (edit_window, FALSE);
 
        /* Frees */
        g_free (from);
@@ -2359,8 +2505,6 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
         * we find a better solution it should be removed
         * See NB#65125 (commend #18) for details.
         */
-       ModestMainWindow *win = MODEST_MAIN_WINDOW(modest_window_mgr_get_main_window(
-               modest_runtime_get_window_mgr(), FALSE));
        if (!had_error && win != NULL) {
                ModestFolderView *view = MODEST_FOLDER_VIEW(modest_main_window_get_child_widget(
                        win, MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW));
@@ -2399,9 +2543,6 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        if (!modest_msg_edit_window_check_names (edit_window, TRUE))
                return TRUE;
        
-       /* FIXME: Code added just for testing. The final version will
-          use the send queue provided by tinymail and some
-          classifier */
        MsgData *data = modest_msg_edit_window_get_msg_data (edit_window);
 
        /* Check size */
@@ -2655,11 +2796,20 @@ do_create_folder_cb (ModestMailOperation *mail_op,
        GtkWindow *source_win = (GtkWindow *) modest_mail_operation_get_source (mail_op);
 
        if (modest_mail_operation_get_error (mail_op)) {
-               /* Show an error */
-               modest_platform_information_banner (GTK_WIDGET (source_win), NULL,
-                                                   _("mail_in_ui_folder_create_error"));
 
-               /* Try again */
+               /* Show an error. If there was some problem writing to
+                  disk, show it, otherwise show the generic folder
+                  create error. We do it here and not in an error
+                  handler because the call to do_create_folder will
+                  stop the main loop in a gtk_dialog_run and then,
+                  the message won't be shown until that dialog is
+                  closed */
+               modest_ui_actions_disk_operations_error_handler (mail_op,
+                                                                _("mail_in_ui_folder_create_error"));
+
+               /* Try again. Do *NOT* show any error because the mail
+                  operations system will do it for us because we
+                  created the mail_op with new_with_error_handler */
                do_create_folder (source_win, parent_folder, (const gchar *) suggested_name);
        } else {
                /* the 'source_win' is either the ModestMainWindow, or the 'Move to folder'-dialog
@@ -2700,8 +2850,7 @@ do_create_folder (GtkWindow *parent_window,
        if (result == GTK_RESPONSE_ACCEPT) {
                ModestMailOperation *mail_op;
                
-               mail_op  = modest_mail_operation_new (G_OBJECT(parent_window));
-                       
+               mail_op  = modest_mail_operation_new (NULL);                    
                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
                                                 mail_op);
                modest_mail_operation_create_folder (mail_op,
@@ -2770,7 +2919,6 @@ static void
 modest_ui_actions_rename_folder_error_handler (ModestMailOperation *mail_op,
                                               gpointer user_data)
 {
-       ModestMainWindow *window = MODEST_MAIN_WINDOW (user_data);
        const GError *error = NULL;
        const gchar *message = NULL;
        
@@ -2784,12 +2932,12 @@ modest_ui_actions_rename_folder_error_handler (ModestMailOperation *mail_op,
                message = _CS("ckdg_ib_folder_already_exists");
                break;
        default:
-               g_warning ("%s: BUG: unexpected error:[%d]: %s", __FUNCTION__,
-                          error->code, error->message);
-               return;
+               message = _("emev_ib_ui_imap_unable_to_rename");
        }
 
-       modest_platform_information_banner (GTK_WIDGET (window), NULL, message);
+       /* We don't set a parent for the dialog because the dialog
+          will be destroyed so the banner won't appear */
+       modest_platform_information_banner (NULL, NULL, message);
 }
 
 typedef struct {
@@ -2802,9 +2950,15 @@ on_rename_folder_cb (ModestMailOperation *mail_op,
                     TnyFolder *new_folder,
                     gpointer user_data)
 {
-       /* Select now */
-       modest_folder_view_select_folder (MODEST_FOLDER_VIEW (user_data),
-                                         new_folder, FALSE);
+       ModestFolderView *folder_view;
+
+       folder_view = MODEST_FOLDER_VIEW (user_data);
+       /* Note that if the rename fails new_folder will be NULL */
+       if (new_folder) {
+               modest_folder_view_select_folder (folder_view, new_folder, FALSE);
+       } else {
+               modest_folder_view_select_first_inbox_or_local (folder_view);
+       }
 }
 
 static void
@@ -3038,6 +3192,7 @@ modest_ui_actions_on_password_requested (TnyAccountStore *account_store,
                                         ModestMainWindow *main_window)
 {
        g_return_if_fail(server_account_name);
+       gboolean completed = FALSE;
        
        /* Initalize output parameters: */
        if (cancel)
@@ -3155,49 +3310,58 @@ modest_ui_actions_on_password_requested (TnyAccountStore *account_store,
 */
 
        gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
+
+       while (!completed) {
        
-       if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
-               if (username) {
-                       *username = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_username)));
-                       
-                       modest_account_mgr_set_server_account_username (
-                                modest_runtime_get_account_mgr(), server_account_name, 
-                                *username);
-                                
-                       const gboolean username_was_changed = 
-                               (strcmp (*username, initial_username) != 0);
-                       if (username_was_changed) {
-                               g_warning ("%s: tinymail does not yet support changing the "
-                                       "username in the get_password() callback.\n", __FUNCTION__);
+               if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+                       if (username) {
+                               *username = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_username)));
+                               
+                               /* Note that an empty field becomes the "" string */
+                               if (*username && strlen (*username) > 0) {
+                                       modest_account_mgr_set_server_account_username (modest_runtime_get_account_mgr(), 
+                                                                                       server_account_name, 
+                                                                                       *username);
+                                       completed = TRUE;
+                               
+                                       const gboolean username_was_changed = 
+                                               (strcmp (*username, initial_username) != 0);
+                                       if (username_was_changed) {
+                                               g_warning ("%s: tinymail does not yet support changing the "
+                                                          "username in the get_password() callback.\n", __FUNCTION__);
+                                       }
+                               } else {
+                                       /* Show error */
+                                       modest_platform_information_banner (GTK_WIDGET (dialog), NULL, 
+                                                                           _("mcen_ib_username_pw_incorrect"));
+                                       completed = FALSE;
+                               }
                        }
-               }
                        
-               if (password) {
-                       *password = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_password)));
+                       if (password) {
+                               *password = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry_password)));
                        
-                       /* We do not save the password in the configuration, 
-                        * because this function is only called for passwords that should 
-                        * not be remembered:
-                       modest_server_account_set_password (
+                               /* We do not save the password in the configuration, 
+                                * because this function is only called for passwords that should 
+                                * not be remembered:
+                                modest_server_account_set_password (
                                 modest_runtime_get_account_mgr(), server_account_name, 
                                 *password);
-                       */
+                                */
+                       }                       
+                       if (cancel)
+                               *cancel   = FALSE;                      
+               } else {
+                       /* Set parent to NULL or the banner will disappear with its parent dialog */
+                       modest_platform_information_banner(NULL, NULL, _("mail_ib_login_cancelled"));
+                       completed = TRUE;
+                       if (username)
+                               *username = NULL;                       
+                       if (password)
+                               *password = NULL;                       
+                       if (cancel)
+                               *cancel   = TRUE;
                }
-               
-               if (cancel)
-                       *cancel   = FALSE;
-                       
-       } else {
-               modest_platform_information_banner(GTK_WIDGET (main_window), NULL, _("mail_ib_login_cancelled"));
-               
-               if (username)
-                       *username = NULL;
-                       
-               if (password)
-                       *password = NULL;
-                       
-               if (cancel)
-                       *cancel   = TRUE;
        }
 
 /* This is not in the Maemo UI spec:
@@ -3244,8 +3408,10 @@ modest_ui_actions_on_cut (GtkAction *action,
 
                if (num_of_unc_msgs) {
                        TnyAccount *account = get_account_from_header_list (header_list);
-                       continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
-                       g_object_unref (account);
+                       if (account) {
+                               continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
+                               g_object_unref (account);
+                       }
                }
 
                if (num_of_unc_msgs == 0 || continue_download) {
@@ -3305,8 +3471,10 @@ modest_ui_actions_on_copy (GtkAction *action,
 
                if (num_of_unc_msgs) {
                        TnyAccount *account = get_account_from_header_list (header_list);
-                       continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
-                       g_object_unref (account);
+                       if (account) {
+                               continue_download = connect_to_get_msg (window, num_of_unc_msgs, account);
+                               g_object_unref (account);
+                       }
                }
 
                if (num_of_unc_msgs == 0 || continue_download) {
@@ -3963,6 +4131,7 @@ on_move_to_dialog_folder_selection_changed (ModestFolderView* self,
                        new_sensitive = (proto != MODEST_PROTOCOL_STORE_POP);
                }
                g_object_unref (local_account);
+               g_object_unref (mmc_account);
        }
 
        /* Check the target folder rules */
@@ -4516,7 +4685,7 @@ modest_ui_actions_on_main_window_remove_attachments (GtkAction *action,
                ModestMailOperation *mail_op = NULL;
                modest_window_mgr_register_header (modest_runtime_get_window_mgr (), header, NULL);
                mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT (win),
-                                                                        modest_ui_actions_get_msgs_full_error_handler,
+                                                                        modest_ui_actions_disk_operations_error_handler,
                                                                         NULL, NULL);
                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
                modest_mail_operation_get_msg (mail_op, header, open_msg_for_purge_cb, win);
@@ -5101,7 +5270,7 @@ retrieve_msg_contents_performer (gboolean canceled,
 
        /* Create mail operation */
        mail_op = modest_mail_operation_new_with_error_handling ((GObject *) parent_window,
-                                                                modest_ui_actions_get_msgs_full_error_handler, 
+                                                                modest_ui_actions_disk_operations_error_handler, 
                                                                 NULL, NULL);
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
        modest_mail_operation_get_msgs_full (mail_op, headers, NULL, NULL, NULL);
@@ -5346,21 +5515,20 @@ modest_ui_actions_on_send_queue_error_happened (TnySendQueue *self,
        TnyTransportAccount *server_account;
        gchar *message = NULL;
 
-       /* Don't show anything if the user cancelled something */
-       if (err->code == TNY_SYSTEM_ERROR_CANCEL)
+       /* Don't show anything if the user cancelled something or the send receive request is not
+        * interactive */
+       if (err->code == TNY_SYSTEM_ERROR_CANCEL ||
+           !modest_tny_send_queue_get_requested_send_receive (MODEST_TNY_SEND_QUEUE (self)))
                return;
 
+
        /* Get the server name: */
        server_account = 
                TNY_TRANSPORT_ACCOUNT (tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE (self)));
-       if (server_account) {
-               server_name = tny_account_get_hostname (TNY_ACCOUNT (server_account));
-                       
-               g_object_unref (server_account);
-               server_account = NULL;
-       }
-       
-       g_return_if_fail (server_name);
+       if (server_account)
+               server_name = tny_account_get_hostname (TNY_ACCOUNT (server_account));          
+       else
+               g_return_if_reached ();
 
        /* Show the appropriate message text for the GError: */
        switch (err->code) {
@@ -5373,6 +5541,9 @@ modest_ui_actions_on_send_queue_error_happened (TnySendQueue *self,
        case TNY_SERVICE_ERROR_SEND:
                message = g_strdup (_("emev_ib_ui_smtp_send_error"));
                break;
+       case TNY_SERVICE_ERROR_UNAVAILABLE:
+               message = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
+               break;
        default:
                g_warning ("%s: unexpected ERROR %d",
                           __FUNCTION__, err->code);
@@ -5386,6 +5557,7 @@ modest_ui_actions_on_send_queue_error_happened (TnySendQueue *self,
 
        modest_platform_run_information_dialog (NULL, message);
        g_free (message);
+       g_object_unref (server_account);
 }
 
 void