Added msg view window reload feature. Now we can force a message reload
[modest] / src / modest-ui-actions.c
index e342e7f..23967ab 100644 (file)
@@ -109,6 +109,7 @@ typedef struct _ReplyForwardHelper {
        guint reply_forward_type;
        ReplyForwardAction action;
        gchar *account_name;
+       gchar *mailbox;
        GtkWidget *parent_window;
        TnyHeader *header;
 } ReplyForwardHelper;
@@ -729,7 +730,9 @@ void
 modest_ui_actions_on_new_account (GtkAction *action,
                                  ModestWindow *window)
 {
-       modest_ui_actions_run_account_setup_wizard (window);
+       if (!modest_ui_actions_run_account_setup_wizard (window)) {
+               g_debug ("%s: wizard was already running", __FUNCTION__);
+       }
 }
 
 void
@@ -785,10 +788,12 @@ modest_ui_actions_compose_msg(ModestWindow *win,
                              gboolean set_as_modified)
 {
        gchar *account_name = NULL;
+       const gchar *mailbox;
        TnyMsg *msg = NULL;
        TnyAccount *account = NULL;
        TnyFolder *folder = NULL;
        gchar *from_str = NULL, *signature = NULL, *body = NULL;
+       gchar *recipient = NULL;
        gboolean use_signature = FALSE;
        ModestWindow *msg_win = NULL;
        ModestAccountMgr *mgr = modest_runtime_get_account_mgr();
@@ -800,7 +805,8 @@ modest_ui_actions_compose_msg(ModestWindow *win,
                goto cleanup;
 
 #ifdef MODEST_TOOLKIT_HILDON2
-       account_name = g_strdup (modest_window_get_active_account(win));
+       if (win)
+               account_name = g_strdup (modest_window_get_active_account(win));
 #endif
        if (!account_name) {
                account_name = modest_account_mgr_get_default_account(mgr);
@@ -809,6 +815,11 @@ modest_ui_actions_compose_msg(ModestWindow *win,
                g_printerr ("modest: no account found\n");
                goto cleanup;
        }
+
+       if (win)
+               mailbox = modest_window_get_active_mailbox (win);
+       else
+               mailbox = NULL;
        account = modest_tny_account_store_get_server_account (store, account_name, TNY_ACCOUNT_TYPE_STORE);
        if (!account) {
                g_printerr ("modest: failed to get tnyaccount for '%s'\n", account_name);
@@ -819,20 +830,22 @@ modest_ui_actions_compose_msg(ModestWindow *win,
                g_printerr ("modest: failed to find Drafts folder\n");
                goto cleanup;
        }
-       from_str = modest_account_mgr_get_from_string (mgr, account_name);
+       from_str = modest_account_mgr_get_from_string (mgr, account_name, mailbox);
        if (!from_str) {
                g_printerr ("modest: failed get from string for '%s'\n", account_name);
                goto cleanup;
        }
 
-       signature = modest_account_mgr_get_signature (mgr, account_name, &use_signature);
+       recipient = modest_text_utils_get_email_address (from_str);
+       signature = modest_account_mgr_get_signature_from_recipient (mgr, recipient, &use_signature);
+       g_free (recipient);
        if (body_str != NULL) {
                body = use_signature ? g_strconcat(body_str, "\n--\n", signature, NULL) : g_strdup(body_str);
        } else {
                body = use_signature ? g_strconcat("\n--\n", signature, NULL) : g_strdup("");
        }
 
-       msg = modest_tny_msg_new (to_str, from_str, cc_str, bcc_str, subject_str, body, NULL, NULL, NULL);
+       msg = modest_tny_msg_new (to_str, from_str, cc_str, bcc_str, subject_str, NULL, NULL, body, NULL, NULL, NULL);
        if (!msg) {
                g_printerr ("modest: failed to create new msg\n");
                goto cleanup;
@@ -842,7 +855,7 @@ modest_ui_actions_compose_msg(ModestWindow *win,
        /* This is destroyed by TODO. */
        total_size = 0;
        allowed_size = MODEST_MAX_ATTACHMENT_SIZE;
-       msg_win = modest_msg_edit_window_new (msg, account_name, FALSE);
+       msg_win = modest_msg_edit_window_new (msg, account_name, mailbox, FALSE);
 
        if (!modest_window_mgr_register_window (modest_runtime_get_window_mgr(), msg_win, win)) {
                gtk_widget_destroy (GTK_WIDGET (msg_win));
@@ -1108,6 +1121,7 @@ open_msg_cb (ModestMailOperation *mail_op,
        if (open_in_editor) {
                ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
                gchar *from_header = NULL, *acc_name;
+               gchar *mailbox = NULL;
 
                from_header = tny_header_dup_from (header);
 
@@ -1121,22 +1135,28 @@ open_msg_cb (ModestMailOperation *mail_op,
                        }
                }
 
-               acc_name = modest_utils_get_account_name_from_recipient (from_header);
+               acc_name = modest_utils_get_account_name_from_recipient (from_header, &mailbox);
                g_free (from_header);
                if (acc_name) {
                        g_free (account);
                        account = acc_name;
                }
 
-               win = modest_msg_edit_window_new (msg, account, TRUE);
+               win = modest_msg_edit_window_new (msg, account, mailbox, TRUE);
+               if (mailbox)
+                       g_free (mailbox);
        } else {
                gchar *uid = modest_tny_folder_get_header_unique_id (header);
+               const gchar *mailbox = NULL;
+
+               if (parent_win && MODEST_IS_WINDOW (parent_win))
+                       mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (parent_win));
 
                if (helper->rowref && helper->model) {
-                       win = modest_msg_view_window_new_with_header_model (msg, account, (const gchar*) uid,
+                       win = modest_msg_view_window_new_with_header_model (msg, account, mailbox, (const gchar*) uid,
                                                                            helper->model, helper->rowref);
                } else {
-                       win = modest_msg_view_window_new_for_attachment (msg, account, (const gchar*) uid);
+                       win = modest_msg_view_window_new_for_attachment (msg, account, mailbox, (const gchar*) uid);
                }
                g_free (uid);
        }
@@ -1218,10 +1238,11 @@ check_memory_full_error (GtkWidget *parent_window, GError *err)
        if (err == NULL)
                return FALSE;
 
-       if (is_memory_full_error (err, NULL))
-               modest_platform_information_banner (parent_window,
-                                                   NULL, _KR("cerm_device_memory_full"));
-       else if (err->code == TNY_SYSTEM_ERROR_MEMORY)
+       if (is_memory_full_error (err, NULL)) {
+               gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+               modest_platform_information_banner (parent_window, NULL, msg);
+               g_free (msg);
+       } else if (err->code == TNY_SYSTEM_ERROR_MEMORY)
                /* If the account was created in memory full
                   conditions then tinymail won't be able to
                   connect so it'll return this error code */
@@ -1249,8 +1270,9 @@ modest_ui_actions_disk_operations_error_handler (ModestMailOperation *mail_op,
           don't show any message */
        if (status != MODEST_MAIL_OPERATION_STATUS_CANCELED) {
                if (is_memory_full_error ((GError *) error, mail_op)) {
-                       modest_platform_information_banner ((GtkWidget *) win,
-                                                           NULL, _KR("cerm_device_memory_full"));
+                       gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+                       modest_platform_information_banner ((GtkWidget *) win, NULL, msg);
+                       g_free (msg);
                } else if (error->code == TNY_SYSTEM_ERROR_MEMORY) {
                        modest_platform_information_banner ((GtkWidget *) win,
                                                            NULL, _("emev_ui_imap_inbox_select_error"));
@@ -1432,8 +1454,10 @@ open_msg_performer(gboolean canceled,
                header_view = get_header_view_from_window (MODEST_WINDOW (parent_window));
                uid = modest_tny_folder_get_header_unique_id (helper->header);
                if (header_view) {
-                       window = modest_msg_view_window_new_from_header_view
-                               (MODEST_HEADER_VIEW (header_view), account_name, uid, helper->rowref);
+                       const gchar *mailbox = NULL;
+                       mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (parent_window));
+                       window = modest_msg_view_window_new_from_header_view 
+                               (MODEST_HEADER_VIEW (header_view), account_name, mailbox, uid, helper->rowref);
                        if (window != NULL) {
                                if (!modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
                                                                        window, NULL)) {
@@ -1642,6 +1666,7 @@ create_reply_forward_helper (ReplyForwardAction action,
 {
        ReplyForwardHelper *rf_helper = NULL;
        const gchar *active_acc = modest_window_get_active_account (win);
+       const gchar *active_mailbox = modest_window_get_active_mailbox (win);
 
        rf_helper = g_slice_new0 (ReplyForwardHelper);
        rf_helper->reply_forward_type = reply_forward_type;
@@ -1651,6 +1676,7 @@ create_reply_forward_helper (ReplyForwardAction action,
        rf_helper->account_name = (active_acc) ?
                g_strdup (active_acc) :
                modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
+       rf_helper->mailbox = g_strdup (active_mailbox);
 
        /* Note that window could be destroyed just AFTER calling
           register_window so we must ensure that this pointer does
@@ -1669,6 +1695,7 @@ free_reply_forward_helper (gpointer data)
 
        helper = (ReplyForwardHelper *) data;
        g_free (helper->account_name);
+       g_free (helper->mailbox);
        if (helper->header)
                g_object_unref (helper->header);
        if (helper->parent_window)
@@ -1694,6 +1721,7 @@ reply_forward_cb (ModestMailOperation *mail_op,
        ModestWindowMgr *mgr = NULL;
        gchar *signature = NULL;
        gboolean use_signature;
+       gchar *recipient;
 
        /* If there was any error. The mail operation could be NULL,
           this means that we already have the message downloaded and
@@ -1703,10 +1731,12 @@ reply_forward_cb (ModestMailOperation *mail_op,
                goto cleanup;
 
        from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
-                                                  rf_helper->account_name);
-       signature = modest_account_mgr_get_signature (modest_runtime_get_account_mgr(),
-                                                     rf_helper->account_name,
-                                                     &use_signature);
+                                                  rf_helper->account_name, rf_helper->mailbox);
+       recipient = modest_text_utils_get_email_address (from);
+       signature = modest_account_mgr_get_signature_from_recipient (modest_runtime_get_account_mgr(), 
+                                                                    recipient, 
+                                                                    &use_signature);
+       g_free (recipient);
 
        /* Create reply mail */
        switch (rf_helper->action) {
@@ -1755,7 +1785,7 @@ reply_forward_cb (ModestMailOperation *mail_op,
        }
 
        /* Create and register the windows */
-       msg_win = modest_msg_edit_window_new (new_msg, rf_helper->account_name, FALSE);
+       msg_win = modest_msg_edit_window_new (new_msg, rf_helper->account_name, rf_helper->mailbox, FALSE);
        mgr = modest_runtime_get_window_mgr ();
        modest_window_mgr_register_window (mgr, msg_win, (ModestWindow *) rf_helper->parent_window);
 
@@ -2522,7 +2552,7 @@ set_active_account_from_tny_account (TnyAccount *account,
        /* We need the TnyAccount provided by the
           account store because that is the one that
           knows the name of the Modest account */
-       TnyAccount *modest_server_account = modest_server_account =
+       TnyAccount *modest_server_account =
                modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store (),
                                                             MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
                                                             server_acc_name);
@@ -2550,7 +2580,7 @@ folder_refreshed_cb (ModestMailOperation *mail_op,
                     gpointer user_data)
 {
        ModestMainWindow *win = NULL;
-       GtkWidget *folder_view;
+       GtkWidget *folder_view, *header_view;
        const GError *error;
 
        g_return_if_fail (TNY_IS_FOLDER (folder));
@@ -2569,6 +2599,8 @@ folder_refreshed_cb (ModestMailOperation *mail_op,
 
        folder_view =
                modest_main_window_get_child_widget(win, MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
+       header_view =
+               modest_main_window_get_child_widget(win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
 
        if (folder_view) {
                TnyFolderStore *current_folder;
@@ -2583,10 +2615,10 @@ folder_refreshed_cb (ModestMailOperation *mail_op,
        }
 
        /* Check if folder is empty and set headers view contents style */
-       if (tny_folder_get_all_count (folder) == 0)
+       if ((tny_folder_get_all_count (folder) == 0) ||
+           modest_header_view_is_empty (MODEST_HEADER_VIEW (header_view)))
                modest_main_window_set_contents_style (win,
                                                       MODEST_MAIN_WINDOW_CONTENTS_STYLE_EMPTY);
-
 }
 
 void
@@ -2819,9 +2851,10 @@ enough_space_for_message (ModestMsgEditWindow *edit_window,
        /* Double check: memory full condition or message too big */
        if (available_disk < MIN_FREE_SPACE ||
            expected_size > available_disk) {
+               gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+               modest_platform_information_banner (NULL, NULL, msg);
+               g_free (msg);
 
-               modest_platform_information_banner (NULL, NULL,
-                                                   _KR("cerm_device_memory_full"));
                return FALSE;
        }
 
@@ -2858,7 +2891,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        TnyTransportAccount *transport_account;
        ModestMailOperation *mail_operation;
        MsgData *data;
-       gchar *account_name, *from;
+       gchar *account_name;
        ModestAccountMgr *account_mgr;
        gboolean had_error = FALSE;
        ModestMainWindow *win = NULL;
@@ -2900,7 +2933,6 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
                modest_msg_edit_window_free_msg_data (edit_window, data);
                return FALSE;
        }
-       from = modest_account_mgr_get_from_string (account_mgr, account_name);
 
        /* Create the mail operation */
        mail_operation = modest_mail_operation_new_with_error_handling (NULL, modest_ui_actions_disk_operations_error_handler,
@@ -2910,9 +2942,9 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        modest_mail_operation_save_to_drafts (mail_operation,
                                              transport_account,
                                              data->draft_msg,
-                                             from,
-                                             data->to,
-                                             data->cc,
+                                             data->from,
+                                             data->to, 
+                                             data->cc, 
                                              data->bcc,
                                              data->subject,
                                              data->plain_body,
@@ -2920,6 +2952,8 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
                                              data->attachments,
                                              data->images,
                                              data->priority_flags,
+                                             data->references,
+                                             data->in_reply_to,
                                              on_save_to_drafts_cb,
                                              g_object_ref(edit_window));
 
@@ -2947,7 +2981,6 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        modest_msg_edit_window_set_modified (edit_window, FALSE);
 
        /* Frees */
-       g_free (from);
        g_free (account_name);
        g_object_unref (G_OBJECT (transport_account));
        g_object_unref (G_OBJECT (mail_operation));
@@ -3001,7 +3034,6 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        MsgData *data;
        ModestAccountMgr *account_mgr;
        gchar *account_name;
-       gchar *from;
        ModestMailOperation *mail_operation;
 
        g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window), TRUE);
@@ -3050,14 +3082,13 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
 
 
        /* Create the mail operation */
-       from = modest_account_mgr_get_from_string (account_mgr, account_name);
        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_send_new_mail (mail_operation,
                                             transport_account,
                                             data->draft_msg,
-                                            from,
+                                            data->from,
                                             data->to,
                                             data->cc,
                                             data->bcc,
@@ -3066,6 +3097,8 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
                                             data->html_body,
                                             data->attachments,
                                             data->images,
+                                            data->references,
+                                            data->in_reply_to,
                                             data->priority_flags);
 
        if (modest_mail_operation_get_status (mail_operation) == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS)
@@ -3082,7 +3115,6 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        }
 
        /* Free data: */
-       g_free (from);
        g_free (account_name);
        g_object_unref (G_OBJECT (transport_account));
        g_object_unref (G_OBJECT (mail_operation));
@@ -3430,15 +3462,19 @@ modest_ui_actions_rename_folder_error_handler (ModestMailOperation *mail_op,
                                               gpointer user_data)
 {
        const GError *error = NULL;
-       const gchar *message = NULL;
+       gchar *message = NULL;
+       gboolean mem_full;
 
        /* Get error message */
        error = modest_mail_operation_get_error (mail_op);
        if (!error)
                g_return_if_reached ();
 
-       if (error->domain == MODEST_MAIL_OPERATION_ERROR &&
-           error->code == MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS) {
+       mem_full = is_memory_full_error ((GError *) error, mail_op);
+       if (mem_full) {
+               message = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+       } else if (error->domain == MODEST_MAIL_OPERATION_ERROR &&
+                  error->code == MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS) {
                message = _CS("ckdg_ib_folder_already_exists");
        } else if (error->domain == TNY_ERROR_DOMAIN &&
                   error->code == TNY_SERVICE_ERROR_STATE) {
@@ -3452,6 +3488,9 @@ modest_ui_actions_rename_folder_error_handler (ModestMailOperation *mail_op,
        /* 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);
+
+       if (mem_full)
+               g_free (message);
 }
 
 typedef struct {
@@ -4561,7 +4600,18 @@ headers_action_show_details (TnyHeader *header,
                             gpointer user_data)
 
 {
-       modest_platform_run_header_details_dialog (GTK_WINDOW (window), header);
+       gboolean async_retrieval;
+       TnyMsg *msg = NULL;
+
+       if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
+               async_retrieval = TRUE;
+               msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (window));
+       } else {
+               async_retrieval = FALSE;
+       }
+       modest_platform_run_header_details_dialog (GTK_WINDOW (window), header, async_retrieval, msg);
+       if (msg)
+               g_object_unref (msg);
 }
 
 /*
@@ -5022,7 +5072,9 @@ create_move_to_dialog (GtkWindow *win,
        g_object_set_data (G_OBJECT(dialog), MODEST_MOVE_TO_DIALOG_FOLDER_VIEW, tree_view);
 
        /* Hide special folders */
+#ifndef MODEST_TOOLKIT_HILDON2
        modest_folder_view_show_non_move_folders (MODEST_FOLDER_VIEW (tree_view), FALSE);
+#endif
        if (list_to_move)
                modest_folder_view_set_list_to_move (MODEST_FOLDER_VIEW (tree_view), list_to_move);
 #ifndef MODEST_TOOLKIT_HILDON2
@@ -5452,13 +5504,15 @@ xfer_messages_error_handler (ModestMailOperation *mail_op,
        win = modest_mail_operation_get_source (mail_op);
        error = modest_mail_operation_get_error (mail_op);
 
-       if (error && is_memory_full_error ((GError *) error, mail_op))
-               modest_platform_information_banner ((GtkWidget *) win,
-                                                   NULL, _KR("cerm_device_memory_full"));
-       else
+       if (error && is_memory_full_error ((GError *) error, mail_op)) {
+               gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+               modest_platform_information_banner ((GtkWidget *) win, NULL, msg);
+               g_free (msg);
+       } else {
                modest_platform_run_information_dialog ((GtkWindow *) win,
                                                        _("mail_in_ui_folder_move_target_error"),
                                                        FALSE);
+       }
        if (win)
                g_object_unref (win);
 }
@@ -6069,7 +6123,9 @@ retrieve_contents_cb (ModestMailOperation *mail_op,
 {
        /* 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);
+       if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg)) {
+               g_debug ("%s: message failed to retrieve. Memory low?", __FUNCTION__);
+       }
 }
 
 static void
@@ -6550,7 +6606,11 @@ modest_ui_actions_get_msg_already_deleted_error_msg (ModestWindow *win)
                }
                iter = tny_list_create_iterator (headers);
                header = TNY_HEADER (tny_iterator_get_current (iter));
-               folder = TNY_FOLDER_STORE (tny_header_get_folder (header));
+               if (header) {
+                       folder = TNY_FOLDER_STORE (tny_header_get_folder (header));
+               } else {
+                       g_warning ("List should contain headers");
+               }
                g_object_unref (iter);
                g_object_unref (headers);
 #endif
@@ -6633,3 +6693,31 @@ modest_ui_actions_on_delete_account (GtkWindow *parent_window,
        }
        return removed;
 }
+
+void 
+modest_ui_actions_on_fetch_images (GtkAction *action,
+                                  ModestWindow *window)
+{
+       g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
+
+       modest_msg_view_window_fetch_images (MODEST_MSG_VIEW_WINDOW (window));
+
+}
+
+void
+modest_ui_actions_on_reload_message (const gchar *msg_id)
+{
+       ModestWindow *window = NULL;
+
+       g_return_if_fail (msg_id && msg_id[0] != '\0');
+       if (!modest_window_mgr_find_registered_message_uid (modest_runtime_get_window_mgr (),
+                                                           msg_id,
+                                                           &window))
+               return;
+
+
+       if (window == NULL || !MODEST_IS_MSG_VIEW_WINDOW (window))
+               return;
+
+       modest_msg_view_window_reload (MODEST_MSG_VIEW_WINDOW (window));
+}