* Fixes NB#85964, do not crash when opening Drafts in memory full condition
[modest] / src / modest-ui-actions.c
index 0a1e0ea..f460812 100644 (file)
@@ -702,7 +702,7 @@ modest_ui_actions_compose_msg(ModestWindow *win,
        /* we check for low-mem; in that case, show a warning, and don't allow
         * composing a message with attachments
         */
-       if (attachments && modest_platform_check_memory_low (win))
+       if (attachments && modest_platform_check_memory_low (win, TRUE))
                goto cleanup;
 
        account_name = modest_account_mgr_get_default_account(mgr);
@@ -801,6 +801,18 @@ modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op,
        /* 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) {
+               const GError *error;
+
+               /* If it's a memory low issue, then show a banner */
+               error = modest_mail_operation_get_error (mail_op);
+               if (error && error->domain == MODEST_MAIL_OPERATION_ERROR &&
+                   error->code == MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY) {
+                       GObject *source = modest_mail_operation_get_source (mail_op);
+                       modest_platform_run_information_dialog (GTK_IS_WINDOW (source) ? GTK_WINDOW (source) : NULL,
+                                                               dgettext("ke-recv","memr_ib_operation_disabled"),
+                                                               TRUE);
+                       g_object_unref (source);
+               }
 
                /* Remove the header from the preregistered uids */
                modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (),  
@@ -890,13 +902,15 @@ open_msg_cb (ModestMailOperation *mail_op,
                        account = g_strdup(modest_tny_account_get_parent_modest_account_name_for_server_account(
                                                   TNY_ACCOUNT(traccount)));
                        send_queue = modest_runtime_get_send_queue(traccount, TRUE);
-                       msg_id = modest_tny_send_queue_get_msg_id (header);
-                       status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
-                       /* Only open messages in outbox with the editor if they are in Failed state */
-                       if (status == MODEST_TNY_SEND_QUEUE_FAILED) {
-                               open_in_editor = TRUE;
+                       if (TNY_IS_SEND_QUEUE (send_queue)) {
+                               msg_id = modest_tny_send_queue_get_msg_id (header);
+                               status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
+                               /* Only open messages in outbox with the editor if they are in Failed state */
+                               if (status == MODEST_TNY_SEND_QUEUE_FAILED) {
+                                       open_in_editor = TRUE;
+                               }
+                               g_free(msg_id);
                        }
-                       g_free(msg_id);
                        g_object_unref(traccount);
                } else {
                        g_warning("Cannot get transport account for message in outbox!!");
@@ -930,28 +944,19 @@ open_msg_cb (ModestMailOperation *mail_op,
                if (from_header) {
                        GSList *accounts = modest_account_mgr_account_names (mgr, TRUE);
                        GSList *node = NULL;
-                       gchar *from_header_email;
-
-                       from_header_email = modest_text_utils_get_email_address ((const gchar *) from_header);
 
                        for (node = accounts; node != NULL; node = g_slist_next (node)) {
-                               gchar *from, *from_email;
-                               
-                               from = modest_account_mgr_get_from_string (mgr, node->data);
-                               if (from) {
-                                       from_email = modest_text_utils_get_email_address ((const gchar *) from);
-                                       if (strcmp (from_header_email, from_email) == 0) {
-                                               g_free (account);
-                                               account = g_strdup (node->data);
-                                               g_free (from_email);
-                                               g_free (from);
-                                               break;
-                                       }
-                                       g_free (from_email);
+                               gchar *from = modest_account_mgr_get_from_string (mgr, node->data);
+                               
+                               if (from && (strcmp (from_header, from) == 0)) {
+                                       g_free (account);
+                                       account = g_strdup (node->data);
                                        g_free (from);
-                               }
-                       }
-                       g_free (from_header_email);
+                                       break;
+                               }
+                               g_free (from);
+                       }
+
                        g_free (from_header);
                        g_slist_foreach (accounts, (GFunc) g_free, NULL);
                        g_slist_free (accounts);
@@ -1402,7 +1407,7 @@ modest_ui_actions_on_open (GtkAction *action, ModestWindow *win)
        /* we check for low-mem; in that case, show a warning, and don't allow
         * opening
         */
-       if (modest_platform_check_memory_low (MODEST_WINDOW(win)))
+       if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE))
                return;
 
        /* Get headers */
@@ -1643,7 +1648,7 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                        
        /* we check for low-mem; in that case, show a warning, and don't allow
         * reply/forward (because it could potentially require a lot of memory */
-       if (modest_platform_check_memory_low (MODEST_WINDOW(win)))
+       if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE))
                return;
 
 
@@ -2245,7 +2250,7 @@ modest_ui_actions_on_header_activated (ModestHeaderView *header_view,
        /* we check for low-mem; in that case, show a warning, and don't allow
         * activating headers
         */
-       if (modest_platform_check_memory_low (MODEST_WINDOW(main_window)))
+       if (modest_platform_check_memory_low (MODEST_WINDOW(main_window), TRUE))
                return;
 
        modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (main_window));
@@ -2300,10 +2305,22 @@ folder_refreshed_cb (ModestMailOperation *mail_op,
 {
        ModestMainWindow *win = NULL;
        GtkWidget *header_view;
+       const GError *error;
 
        g_return_if_fail (TNY_IS_FOLDER (folder));
 
        win = MODEST_MAIN_WINDOW (user_data);
+
+       /* Check if the operation failed due to memory low conditions */
+       error = modest_mail_operation_get_error (mail_op);
+       if (error && error->domain == MODEST_MAIL_OPERATION_ERROR && 
+           error->code == MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY) {
+               modest_platform_run_information_dialog (GTK_WINDOW (win),
+                                                       dgettext("ke-recv","memr_ib_operation_disabled"),
+                                                       TRUE);
+               return;
+       }
+
        header_view = 
                modest_main_window_get_child_widget(win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
 
@@ -2482,7 +2499,7 @@ modest_ui_actions_on_msg_attachment_clicked (ModestMsgView *msgview, TnyMimePart
        /* we check for low-mem; in that case, show a warning, and don't allow
         * viewing attachments
         */
-       if (modest_platform_check_memory_low (MODEST_WINDOW(win)))
+       if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE))
                return;
 
        modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (win), mime_part);
@@ -2566,7 +2583,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
         */
        if (expected_size > MODEST_MAX_LOW_MEMORY_MESSAGE_SIZE) {
 
-               if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window))) {
+               if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window), TRUE)) {
                        modest_msg_edit_window_free_msg_data (edit_window, data);
                        return FALSE;
                }
@@ -2736,7 +2753,7 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
         * this should still allow for sending anything critical... 
         */
        if (expected_size > MODEST_MAX_LOW_MEMORY_MESSAGE_SIZE) {
-               if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window))) {
+               if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window), TRUE)) {
                        modest_msg_edit_window_free_msg_data (edit_window, data);
                        return FALSE;
                }
@@ -2958,7 +2975,7 @@ modest_ui_actions_on_insert_image (GtkAction *action,
        g_return_if_fail (GTK_IS_ACTION (action));
 
 
-       if (modest_platform_check_memory_low (MODEST_WINDOW(window)))
+       if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
                return;
 
        if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT)
@@ -2974,7 +2991,7 @@ modest_ui_actions_on_attach_file (GtkAction *action,
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
        g_return_if_fail (GTK_IS_ACTION (action));
 
-       if (modest_platform_check_memory_low (MODEST_WINDOW(window)))
+       if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
                return;
        
        modest_msg_edit_window_offer_attach_file (window);
@@ -4672,6 +4689,21 @@ modest_ui_actions_msgs_move_to_confirmation (ModestWindow *win,
 }
 
 static void
+move_to_helper_destroyer (gpointer user_data)
+{
+       MoveToHelper *helper = (MoveToHelper *) user_data;
+
+       /* Close the "Pasting" information banner */
+       if (helper->banner) {
+               gtk_widget_destroy (GTK_WIDGET (helper->banner));
+               g_object_unref (helper->banner);
+       }
+       if (helper->reference != NULL)
+               gtk_tree_row_reference_free (helper->reference);
+       g_free (helper);
+}
+
+static void
 move_to_cb (ModestMailOperation *mail_op, 
            gpointer user_data)
 {
@@ -4709,15 +4741,8 @@ move_to_cb (ModestMailOperation *mail_op,
                }
                g_object_unref (object);
         }
-
-       /* Close the "Pasting" information banner */
-       if (helper->banner) {
-               gtk_widget_destroy (GTK_WIDGET(helper->banner));
-               g_object_unref (helper->banner);
-       }
-       if (helper->reference != NULL)
-               gtk_tree_row_reference_free (helper->reference);
-       g_free (helper);
+       /* Destroy the helper */
+       move_to_helper_destroyer (helper);
 }
 
 static void
@@ -5004,7 +5029,7 @@ xfer_messages_error_handler (ModestMailOperation *mail_op,
                             gpointer user_data)
 {
        ModestWindow *main_window = NULL;
-       
+
        /* Disable next automatic folder selection */
        main_window = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
                                                         FALSE); /* don't create */
@@ -5016,6 +5041,7 @@ xfer_messages_error_handler (ModestMailOperation *mail_op,
                if (win)
                        g_object_unref (win);
        }
+       move_to_helper_destroyer (user_data);
 }
 
 /**
@@ -5096,7 +5122,7 @@ xfer_messages_performer  (gboolean canceled,
        ModestMailOperation *mail_op = 
                modest_mail_operation_new_with_error_handling (G_OBJECT(win),
                                                               xfer_messages_error_handler,
-                                                              NULL, NULL);
+                                                              helper, NULL);
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
                                         mail_op);
 
@@ -5447,7 +5473,7 @@ modest_ui_actions_save_attachments (GtkAction *action,
 {
        if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
 
-               if (modest_platform_check_memory_low (MODEST_WINDOW(window)))
+               if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE))
                        return;
 
                modest_msg_view_window_save_attachments (MODEST_MSG_VIEW_WINDOW (window), NULL);
@@ -5493,15 +5519,53 @@ modest_ui_actions_on_help (GtkAction *action,
 {
        const gchar *help_id;
 
-       g_return_if_fail (action);
        g_return_if_fail (win && GTK_IS_WINDOW(win));
        
        help_id = modest_window_mgr_get_help_id (modest_runtime_get_window_mgr(), win);
-       
-       if (help_id)
-               modest_platform_show_help (GTK_WINDOW (win), help_id);
+
+        if (help_id)
+                modest_platform_show_help (GTK_WINDOW (win), help_id);
+}
+
+void 
+modest_ui_actions_on_csm_help (GtkAction *action, 
+                              GtkWindow *win)
+{
+       const gchar* help_id = NULL;
+       GtkWidget *folder_view;
+       TnyFolderStore *folder_store;
+
+       g_return_if_fail (win && MODEST_IS_MAIN_WINDOW (win));
+
+       /* Get selected folder */
+       folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
+                                                          MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
+       folder_store = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
+
+       /* Switch help_id */
+       if (folder_store && TNY_IS_FOLDER (folder_store))
+               help_id = modest_tny_folder_get_help_id (TNY_FOLDER (folder_store));
+
+       if (folder_store)
+               g_object_unref (folder_store);
+
+        if (help_id)
+                modest_platform_show_help (GTK_WINDOW (win), help_id);
        else
-               g_warning ("%s: no help for window %p", __FUNCTION__, win);
+               modest_ui_actions_on_help (action, win);
+}
+
+static void     
+retrieve_contents_cb (ModestMailOperation *mail_op, 
+                     TnyHeader *header, 
+                     gboolean canceled,
+                     TnyMsg *msg,
+                     GError *err,
+                     gpointer user_data)
+{
+       /* We only need this callback to show an error in case of
+          memory low condition */
+       modest_ui_actions_msg_retrieval_check (mail_op, header, msg);
 }
 
 static void
@@ -5528,7 +5592,7 @@ retrieve_msg_contents_performer (gboolean canceled,
                                                                 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);
+       modest_mail_operation_get_msgs_full (mail_op, headers, retrieve_contents_cb, NULL, NULL);
 
        /* Frees */
        g_object_unref (mail_op);
@@ -5688,7 +5752,7 @@ modest_ui_actions_on_search_messages (GtkAction *action, ModestWindow *window)
        /* we check for low-mem; in that case, show a warning, and don't allow
         * searching
         */
-       if (modest_platform_check_memory_low (window))
+       if (modest_platform_check_memory_low (window, TRUE))
                return;
        
        modest_platform_show_search_messages (GTK_WINDOW (window));
@@ -5703,7 +5767,7 @@ modest_ui_actions_on_open_addressbook (GtkAction *action, ModestWindow *win)
        /* we check for low-mem; in that case, show a warning, and don't allow
         * for the addressbook
         */
-       if (modest_platform_check_memory_low (win))
+       if (modest_platform_check_memory_low (win, TRUE))
                return;