Properly handle download of parts for forwarding subpart messages
authorJosé Dapena Paz <jdapena@igalia.com>
Tue, 22 Dec 2009 17:25:31 +0000 (18:25 +0100)
committerJosé Dapena Paz <jdapena@igalia.com>
Tue, 22 Dec 2009 17:25:58 +0000 (18:25 +0100)
(fixes NB#151640).

src/hildon2/modest-msg-view-window.c
src/modest-mail-operation.c
src/modest-ui-actions.c
src/widgets/modest-msg-view-window.h

index 25843a5..ab39d00 100644 (file)
@@ -124,6 +124,7 @@ struct _ModestMsgViewWindowPrivate {
 
        gchar *msg_uid;
        TnyMimePart *other_body;
+       TnyMsg * top_msg;
 
        GSList *sighandlers;
 };
@@ -632,6 +633,11 @@ modest_msg_view_window_finalize (GObject *obj)
                priv->other_body = NULL;
        }
 
+       if (priv->top_msg != NULL) {
+               g_object_unref (priv->top_msg);
+               priv->top_msg = NULL;
+       }
+
        if (priv->header_model != NULL) {
                g_object_unref (priv->header_model);
                priv->header_model = NULL;
@@ -882,6 +888,7 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember the message list's TreeModel so we can detect changes
         * and change the list selection when necessary: */
@@ -960,6 +967,7 @@ modest_msg_view_window_new_from_uid (const gchar *modest_account_name,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        is_merge = g_str_has_prefix (msg_uid, "merge:");
 
@@ -1040,6 +1048,7 @@ modest_msg_view_window_new_from_header_view (ModestHeaderView *header_view,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember the message list's TreeModel so we can detect changes 
         * and change the list selection when necessary: */
@@ -1133,6 +1142,7 @@ modest_msg_view_window_new_for_search_result (TnyMsg *msg,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember that this is a search result, 
         * so we can disable some UI appropriately: */
@@ -1167,6 +1177,7 @@ modest_msg_view_window_is_other_body (ModestMsgViewWindow *self)
 ModestWindow *
 modest_msg_view_window_new_with_other_body (TnyMsg *msg, 
                                            TnyMimePart *other_body,
+                                           TnyMsg *top_msg,
                                            const gchar *modest_account_name,
                                            const gchar *mailbox,
                                            const gchar *msg_uid)
@@ -1188,6 +1199,11 @@ modest_msg_view_window_new_with_other_body (TnyMsg *msg,
        } else {
                tny_msg_view_set_msg (TNY_MSG_VIEW (priv->msg_view), msg);
        }
+       if (top_msg) {
+               priv->top_msg = g_object_ref (top_msg);
+       } else {
+               priv->top_msg = NULL;
+       }
        update_window_title (MODEST_MSG_VIEW_WINDOW (obj));
        update_branding (MODEST_MSG_VIEW_WINDOW (obj));
 
@@ -1202,12 +1218,13 @@ modest_msg_view_window_new_with_other_body (TnyMsg *msg,
 }
 
 ModestWindow *
-modest_msg_view_window_new_for_attachment (TnyMsg *msg, 
+modest_msg_view_window_new_for_attachment (TnyMsg *msg,
+                                          TnyMsg *top_msg,
                                           const gchar *modest_account_name,
                                           const gchar *mailbox,
                                           const gchar *msg_uid)
 {
-       return modest_msg_view_window_new_with_other_body (msg, NULL, modest_account_name, mailbox, msg_uid);
+       return modest_msg_view_window_new_with_other_body (msg, NULL, top_msg, modest_account_name, mailbox, msg_uid);
 }
 
 static void
@@ -1514,6 +1531,21 @@ modest_msg_view_window_get_message (ModestMsgViewWindow *self)
        return tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
 }
 
+TnyMsg*
+modest_msg_view_window_get_top_message (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+       
+       g_return_val_if_fail (self, NULL);
+       
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
+
+       if (priv->top_msg)
+               return g_object_ref (priv->top_msg);
+       else
+               return NULL;
+}
+
 const gchar*
 modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
 {
@@ -2672,6 +2704,9 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 {
        DecodeAsyncHelper *helper = (DecodeAsyncHelper *) user_data;
        const gchar *content_type;
+       ModestMsgViewWindowPrivate *priv;
+
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (helper->self);
 
        if (cancelled || err) {
                if (err) {
@@ -2709,6 +2744,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 
                fd = g_open (helper->file_path, O_RDONLY, 0644);
                if (fd != -1) {
+                       TnyMsg *top_msg;
                        file_stream = tny_fs_stream_new (fd);
 
                        mgr = modest_runtime_get_window_mgr ();
@@ -2721,7 +2757,15 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 
                        msg = tny_camel_msg_new ();
                        tny_camel_msg_parse (msg, file_stream);
-                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (msg), account, mailbox, helper->attachment_uid);
+
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
+
+                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (msg), top_msg, 
+                                                                            account, mailbox, helper->attachment_uid);
+                       if (top_msg) g_object_unref (top_msg);
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (helper->self)));
                        if (modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (helper->self)))
@@ -2924,6 +2968,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                if (found) {
                        g_debug ("window for this body is already being created");
                } else {
+                       TnyMsg *top_msg;
 
                        /* it's not found, so create a new window for it */
                        modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */
@@ -2931,9 +2976,16 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                        const gchar *mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (window));
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
+
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
                        
-                       msg_win = modest_msg_view_window_new_with_other_body (TNY_MSG (current_msg), TNY_MIME_PART (mime_part),
+                       msg_win = modest_msg_view_window_new_with_other_body (TNY_MSG (current_msg), TNY_MIME_PART (mime_part), top_msg,
                                                                              account, mailbox, attachment_uid);
+
+                       if (top_msg) g_object_unref (top_msg);
                        
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (window)));
@@ -2959,14 +3011,20 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                         * thus, we don't do anything */
                        g_debug ("window for is already being created");
                } else {
+                       TnyMsg *top_msg;
                        /* it's not found, so create a new window for it */
                        modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */
                        gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window)));
                        const gchar *mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (window));
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
-                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, 
-                                                                            mailbox, attachment_uid);
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
+                       msg_win = modest_msg_view_window_new_for_attachment (
+                               TNY_MSG (mime_part), top_msg, account, 
+                               mailbox, attachment_uid);
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (window)));
                        if (modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (window)))
index a23633e..da3d2da 100644 (file)
@@ -68,6 +68,7 @@
 #endif
 #include "modest-account-protocol.h"
 #include <camel/camel-stream-null.h>
+#include <widgets/modest-msg-view-window.h>
 
 #define KB 1024
 
@@ -2723,7 +2724,18 @@ modest_mail_operation_get_msg_and_parts (ModestMailOperation *self,
 
        /* Get account and set it into mail_operation */
        folder = tny_header_get_folder (header);
-       priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
+       if (folder == NULL && MODEST_IS_MSG_VIEW_WINDOW (priv->source)) {
+               const gchar *acc_name;
+               acc_name = modest_window_get_active_account (MODEST_WINDOW (priv->source));
+               priv->account = modest_tny_account_store_get_server_account
+                       (modest_runtime_get_account_store (),
+                        acc_name,
+                        TNY_ACCOUNT_TYPE_STORE);
+               folder = modest_tny_folder_store_find_folder_from_uri (TNY_FOLDER_STORE (priv->account), 
+                                                                      modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (priv->source)));
+       } else {
+               priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
+       }
        
        /* Check for cached messages */
        if (progress_feedback) {
index caa0c3e..1b9a649 100644 (file)
@@ -115,6 +115,8 @@ typedef struct _ReplyForwardHelper {
        gchar *mailbox;
        GtkWidget *parent_window;
        TnyHeader *header;
+       TnyHeader *top_header;
+       TnyMsg    *msg_part;
        TnyList *parts;
 } ReplyForwardHelper;
 
@@ -1258,7 +1260,7 @@ open_msg_cb (ModestMailOperation *mail_op,
                        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, mailbox, (const gchar*) uid);
+                       win = modest_msg_view_window_new_for_attachment (msg, NULL, account, mailbox, (const gchar*) uid);
                }
                g_free (uid);
        }
@@ -1718,6 +1720,8 @@ create_reply_forward_helper (ReplyForwardAction action,
                             ModestWindow *win,
                             guint reply_forward_type,
                             TnyHeader *header,
+                            TnyMsg *msg_part,
+                            TnyHeader *top_header,
                             TnyList *parts)
 {
        ReplyForwardHelper *rf_helper = NULL;
@@ -1729,6 +1733,8 @@ create_reply_forward_helper (ReplyForwardAction action,
        rf_helper->action = action;
        rf_helper->parent_window = (MODEST_IS_WINDOW (win)) ? GTK_WIDGET (win) : NULL;
        rf_helper->header = (header) ? g_object_ref (header) : NULL;
+       rf_helper->top_header = (top_header) ? g_object_ref (top_header) : NULL;
+       rf_helper->msg_part = (msg_part) ? g_object_ref (msg_part) : NULL;
        rf_helper->account_name = (active_acc) ?
                g_strdup (active_acc) :
                modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
@@ -1758,6 +1764,10 @@ free_reply_forward_helper (gpointer data)
        g_free (helper->mailbox);
        if (helper->header)
                g_object_unref (helper->header);
+       if (helper->top_header)
+               g_object_unref (helper->top_header);
+       if (helper->msg_part)
+               g_object_unref (helper->msg_part);
        if (helper->parts)
                g_object_unref (helper->parts);
        if (helper->parent_window)
@@ -1806,18 +1816,18 @@ reply_forward_cb (ModestMailOperation *mail_op,
                   information. The summary can lack some data */
                TnyHeader *msg_header;
        case ACTION_REPLY:
-               msg_header = tny_msg_get_header (msg);
+               msg_header = tny_msg_get_header (rf_helper->msg_part?rf_helper->msg_part:msg);
                new_msg =
-                       modest_tny_msg_create_reply_msg (msg, msg_header, from,
+                       modest_tny_msg_create_reply_msg (rf_helper->msg_part?rf_helper->msg_part:msg, msg_header, from,
                                                         (use_signature) ? signature : NULL,
                                                         rf_helper->reply_forward_type,
                                                         MODEST_TNY_MSG_REPLY_MODE_SENDER);
                g_object_unref (msg_header);
                break;
        case ACTION_REPLY_TO_ALL:
-               msg_header = tny_msg_get_header (msg);
+               msg_header = tny_msg_get_header (rf_helper->msg_part?rf_helper->msg_part:msg);
                new_msg =
-                       modest_tny_msg_create_reply_msg (msg, msg_header, from,
+                       modest_tny_msg_create_reply_msg (rf_helper->msg_part?rf_helper->msg_part:msg, msg_header, from,
                                                         (use_signature) ? signature : NULL,
                                                         rf_helper->reply_forward_type,
                                                         MODEST_TNY_MSG_REPLY_MODE_ALL);
@@ -1826,7 +1836,8 @@ reply_forward_cb (ModestMailOperation *mail_op,
                break;
        case ACTION_FORWARD:
                new_msg =
-                       modest_tny_msg_create_forward_msg (msg, from, (use_signature) ? signature : NULL,
+                       modest_tny_msg_create_forward_msg (rf_helper->msg_part?rf_helper->msg_part:msg, from, 
+                                                          (use_signature) ? signature : NULL,
                                                           rf_helper->reply_forward_type);
                edit_type = MODEST_EDIT_TYPE_FORWARD;
                break;
@@ -1958,7 +1969,7 @@ reply_forward_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_msg_and_parts (mail_op, rf_helper->header, rf_helper->parts, TRUE, reply_forward_cb, rf_helper);
+       modest_mail_operation_get_msg_and_parts (mail_op, rf_helper->top_header, rf_helper->parts, TRUE, reply_forward_cb, rf_helper);
 
        /* Frees */
        g_object_unref(mail_op);
@@ -2064,21 +2075,25 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
 
        if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
                TnyMsg *msg = NULL;
+               TnyMsg *top_msg = NULL;
                TnyHeader *header = NULL;
                /* Get header and message. Do not free them here, the
                   reply_forward_cb must do it */
                msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW(win));
+               top_msg = modest_msg_view_window_get_top_message (MODEST_MSG_VIEW_WINDOW(win));
                header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
 
                if (msg && header && (action != ACTION_FORWARD || all_parts_retrieved (TNY_MIME_PART (msg)))) {
                        /* Create helper */
                        rf_helper = create_reply_forward_helper (action, win,
-                                                                reply_forward_type, header, NULL);
+                                                                reply_forward_type, header, NULL, NULL, NULL);
                        reply_forward_cb (NULL, header, FALSE, msg, NULL, rf_helper);
                } else {
                        gboolean do_download = TRUE;
 
                        if (msg && header && action == ACTION_FORWARD) {
+                               if (top_msg == NULL)
+                                       top_msg = g_object_ref (msg);
                                /* Not all parts retrieved. Then we have to retrieve them all before
                                 * creating the forward message */
                                if (!tny_device_is_online (modest_runtime_get_device ())) {
@@ -2099,21 +2114,24 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                                        TnyList *pending_parts;
                                        TnyFolder *folder;
                                        TnyAccount *account;
+                                       TnyHeader *top_header;
 
                                        /* Create helper */
-                                       pending_parts = forward_pending_parts (msg);
+                                       top_header = tny_msg_get_header (top_msg);
+                                       pending_parts = forward_pending_parts (top_msg);
                                        rf_helper = create_reply_forward_helper (action, win,
-                                                                                reply_forward_type, header, pending_parts);
+                                                                                reply_forward_type, header, msg, top_header, pending_parts);
                                        g_object_unref (pending_parts);
 
-                                       folder = tny_header_get_folder (header);
+                                       folder = tny_header_get_folder (top_header);
                                        account = tny_folder_get_account (folder);
                                        modest_platform_connect_and_perform (GTK_WINDOW (win),
                                                                             TRUE, account,
                                                                             reply_forward_performer,
                                                                             rf_helper);
-                                       g_object_unref (folder);
+                                       if (folder) g_object_unref (folder);
                                        g_object_unref (account);
+                                       if (top_header) g_object_unref (top_header);
                                }
 
                        } else {
@@ -2123,6 +2141,8 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
 
                if (msg)
                        g_object_unref (msg);
+               if (top_msg)
+                       g_object_unref (top_msg);
                if (header)
                        g_object_unref (header);
        } else {
@@ -2186,7 +2206,7 @@ reply_forward (ReplyForwardAction action, ModestWindow *win)
                        if (download) {
                                /* Create helper */
                                rf_helper = create_reply_forward_helper (action, win,
-                                                                        reply_forward_type, header, NULL);
+                                                                        reply_forward_type, header, NULL, NULL, NULL);
                                if (uncached_msgs > 0) {
                                        modest_platform_connect_and_perform (GTK_WINDOW (win),
                                                                             TRUE, account,
index bad28f4..0a47d32 100644 (file)
@@ -101,6 +101,7 @@ GType        modest_msg_view_window_get_type    (void) G_GNUC_CONST;
  * Returns: a new #ModestMsgViewWindow, or NULL in case of error
  */
 ModestWindow*   modest_msg_view_window_new_for_attachment         (TnyMsg *msg, 
+                                                                  TnyMsg *top_msg,
                                                                   const gchar *modest_account_name,
                                                                   const gchar *mailbox,
                                                                   const gchar *msg_uid);
@@ -120,6 +121,7 @@ ModestWindow*   modest_msg_view_window_new_for_attachment         (TnyMsg *msg,
  */
 ModestWindow*   modest_msg_view_window_new_with_other_body         (TnyMsg *msg,
                                                                   TnyMimePart *other_body,
+                                                                  TnyMsg *top_msg,
                                                                   const gchar *modest_account_name,
                                                                   const gchar *mailbox,
                                                                   const gchar *msg_uid);
@@ -228,6 +230,17 @@ modest_msg_view_window_get_header (ModestMsgViewWindow *self);
 TnyMsg*         modest_msg_view_window_get_message     (ModestMsgViewWindow *window);
 
 /**
+ * modest_msg_view_window_get_top_message:
+ * @window: an #ModestMsgViewWindow instance
+ * 
+ * get a new reference to the parent message of the tree of views. The caller
+ * must free this new reference
+ * 
+ * Returns: a new #TnyMsg instance, or NULL in case of error
+ */
+TnyMsg*         modest_msg_view_window_get_top_message     (ModestMsgViewWindow *window);
+
+/**
  * modest_msg_view_window_get_message_uid:
  * @msg: an #ModestMsgViewWindow instance
  *