Download remote message attachments and open them from modest
authorJose Dapena Paz <jdapena@igalia.com>
Fri, 18 Sep 2009 14:16:46 +0000 (16:16 +0200)
committerJose Dapena Paz <jdapena@igalia.com>
Fri, 18 Sep 2009 15:22:19 +0000 (17:22 +0200)
(fix 1/3 NB#134905).

src/hildon2/modest-msg-view-window.c

index 41f61fc..d7719ed 100644 (file)
@@ -67,6 +67,7 @@
 #include <modest-debug.h>
 #include <modest-header-window.h>
 #include <modest-account-protocol.h>
+#include <tny-camel-msg.h>
 
 #define MYDOCS_ENV "MYDOCSDIR"
 #define DOCS_FOLDER ".documents"
@@ -2564,6 +2565,7 @@ modest_msg_view_window_get_attachments (ModestMsgViewWindow *win)
 typedef struct {
        ModestMsgViewWindow *self;
        gchar *file_path;
+       gchar *attachment_uid;
 } DecodeAsyncHelper;
 
 static void
@@ -2574,6 +2576,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
                                   gpointer user_data)
 {
        DecodeAsyncHelper *helper = (DecodeAsyncHelper *) user_data;
+       const gchar *content_type;
 
        /* It could happen that the window was closed */
        if (GTK_WIDGET_VISIBLE (helper->self))
@@ -2588,16 +2591,56 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
                goto free;
        }
 
-       /* make the file read-only */
-       g_chmod(helper->file_path, 0444);
+       content_type = tny_mime_part_get_content_type (mime_part);
+       if ((g_str_has_prefix (content_type, "message/rfc822") ||
+            g_str_has_prefix (content_type, "multipart/") ||
+            g_str_has_prefix (content_type, "text/"))) {
+               ModestWindowMgr *mgr;
+               ModestWindow *msg_win = NULL;
+               TnyMsg * msg;
+               gchar *account;
+               const gchar *mailbox;
+               TnyStream *file_stream;
+               gint fd;
+
+               fd = g_open (helper->file_path, O_RDONLY, 0644);
+               if (fd != -1) {
+                       file_stream = tny_fs_stream_new (fd);
+
+                       mgr = modest_runtime_get_window_mgr ();
+
+                       account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (helper->self)));
+                       mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (helper->self));
 
-       /* Activate the file */
-       modest_platform_activate_file (helper->file_path, tny_mime_part_get_content_type (mime_part));
+                       if (!account)
+                               account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
+
+                       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);
+                       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)))
+                               gtk_widget_show_all (GTK_WIDGET (msg_win));
+                       else
+                               gtk_widget_destroy (GTK_WIDGET (msg_win));
+                       g_object_unref (file_stream);
+               }
+
+       } else {
+
+               /* make the file read-only */
+               g_chmod(helper->file_path, 0444);
+
+               /* Activate the file */
+               modest_platform_activate_file (helper->file_path, tny_mime_part_get_content_type (mime_part));
+       }
 
  free:
        /* Frees */
        g_object_unref (helper->self);
        g_free (helper->file_path);
+       g_free (helper->attachment_uid);
        g_slice_free (DecodeAsyncHelper, helper);
 }
 
@@ -2671,21 +2714,25 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                        helper = g_slice_new0 (DecodeAsyncHelper);
                        helper->self = g_object_ref (window);
                        helper->file_path = g_strdup (filepath);
+                       helper->attachment_uid = g_strdup (attachment_uid);
 
                        decode_in_provider = FALSE;
                        mgr = modest_runtime_get_account_mgr ();
                        account = modest_window_get_active_account (MODEST_WINDOW (window));
                        if (modest_account_mgr_account_is_multimailbox (mgr, account, &protocol)) {
                                if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
+                                       gchar *uri;
+                                       uri = g_strconcat ("file://", filepath, NULL);
                                        decode_in_provider = 
                                                modest_account_protocol_decode_part_to_stream_async (
                                                        MODEST_ACCOUNT_PROTOCOL (protocol),
                                                        mime_part,
-                                                       NULL,
+                                                       filepath,
                                                        TNY_STREAM (temp_stream),
                                                        on_decode_to_stream_async_handler,
                                                        NULL,
                                                        helper);
+                                       g_free (uri);
                                }
                        }