* Reworked attachments mime type detection and body detection, to work
authorJose Dapena Paz <jdapena@igalia.com>
Wed, 19 Nov 2008 18:40:24 +0000 (18:40 +0000)
committerJose Dapena Paz <jdapena@igalia.com>
Wed, 19 Nov 2008 18:40:24 +0000 (18:40 +0000)
  better with "direct attachment" messages (the ones that include as
  its top mime part the attached file) (fixes NB#92926)

pmo-trunk-r6349

src/maemo/modest-msg-view-window.c
src/modest-tny-mime-part.c
src/modest-tny-mime-part.h
src/modest-tny-msg.c
src/modest-ui-dimming-rules.c
src/widgets/modest-attachment-view.c
src/widgets/modest-attachments-view.c

index 0455e3e..4f47097 100644 (file)
@@ -2532,7 +2532,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
        g_chmod(helper->filepath, 0444);
        
        /* Activate the file */
-       modest_platform_activate_file (helper->filepath, tny_mime_part_get_content_type (mime_part));
+       modest_platform_activate_file (helper->filepath, modest_tny_mime_part_get_content_type (mime_part));
 
  free:
        /* Frees */
@@ -2550,6 +2550,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
        gchar *attachment_uid = NULL;
        gint attachment_index = 0;
        TnyList *attachments;
+       TnyMimePart *window_msg;
 
        g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
        g_return_if_fail (TNY_IS_MIME_PART (mime_part) || (mime_part == NULL));
@@ -2591,7 +2592,11 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                return;
        }
 
-       if (!modest_tny_mime_part_is_msg (mime_part)) {
+       /* we also check for mime_part == priv->msg, as this means it's a direct attachment
+        * shown as attachment, so it should behave as a file */
+       window_msg = TNY_MIME_PART (tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view)));
+       if (!modest_tny_mime_part_is_msg (mime_part)||
+           mime_part == window_msg) {
                gchar *filepath = NULL;
                const gchar *att_filename = tny_mime_part_get_filename (mime_part);
                gboolean show_error_banner = FALSE;
@@ -2616,7 +2621,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                                const gchar *content_type;
                                /* the file may already exist but it isn't writable,
                                 * let's try to open it anyway */
-                               content_type = tny_mime_part_get_content_type (mime_part);
+                               content_type = modest_tny_mime_part_get_content_type (mime_part);
                                modest_platform_activate_file (filepath, content_type);
                        } else {
                                g_warning ("%s: modest_utils_create_temp_stream failed", __FUNCTION__);
@@ -2658,6 +2663,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                        gtk_widget_show_all (GTK_WIDGET (msg_win));
                }
        }
+       g_object_unref (window_msg);
        g_object_unref (mime_part);
 }
 
@@ -2859,6 +2865,7 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, TnyList *m
        gchar *folder = NULL;
        gchar *filename = NULL;
        gchar *save_multiple_str = NULL;
+       TnyMsg *window_msg;
 
        g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
@@ -2871,6 +2878,7 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, TnyList *m
                g_object_ref (mime_parts);
        }
 
+       window_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
        /* prepare dialog */
        if (tny_list_get_length (mime_parts) == 1) {
                TnyIterator *iter;
@@ -2893,6 +2901,7 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, TnyList *m
                save_multiple_str = g_strdup_printf (_FM("sfil_va_number_of_objects_attachments"), 
                                                     tny_list_get_length (mime_parts));
        }
+       g_object_unref (window_msg);
        
        save_dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), 
                                                      GTK_FILE_CHOOSER_ACTION_SAVE);
index 67cde71..e438f34 100644 (file)
@@ -207,3 +207,43 @@ modest_tny_mime_part_to_string (TnyMimePart *part, gint indent)
        g_string_free (indent_prefix, TRUE);
 }
 
+gchar *
+modest_tny_mime_part_get_headers_content_type (TnyMimePart *part)
+{
+       gchar *header_content_type;
+       gchar *header_content_type_lower;
+       gchar *suffix;
+
+       g_return_val_if_fail (TNY_IS_MIME_PART (part), NULL);
+
+       header_content_type = modest_tny_mime_part_get_header_value (part, "Content-Type");
+       header_content_type = g_strstrip (header_content_type);
+
+       /* remove the ; suffix */
+       suffix = index (header_content_type, ';');
+       suffix[0] = '\0';
+
+       header_content_type_lower = (header_content_type ) ?
+               g_ascii_strdown (header_content_type, -1) : NULL;
+       return header_content_type_lower;
+}
+
+gchar *
+modest_tny_mime_part_get_content_type (TnyMimePart *part)
+{
+       const gchar *content_type;
+       gchar *retval = NULL;
+
+       g_return_val_if_fail (TNY_IS_MIME_PART (part), NULL);
+       content_type = tny_mime_part_get_content_type (part);
+
+       if (g_str_has_prefix (content_type, "message/rfc822")) {
+               retval = modest_tny_mime_part_get_headers_content_type (part);
+       } 
+
+       if (retval == NULL) {
+               retval = g_ascii_strdown (content_type, -1);
+       }
+
+       return retval;
+}
index 91a54f2..08825da 100644 (file)
@@ -77,4 +77,22 @@ gboolean modest_tny_mime_part_is_msg (TnyMimePart *part);
  */
 void modest_tny_mime_part_to_string (TnyMimePart *part, gint indent);
 
+/**
+ * modest_tny_mime_part_get_content_type:
+ * @part: a #TnyMimePart
+ *
+ * obtains the content type in headers
+ */
+gchar *modest_tny_mime_part_get_headers_content_type (TnyMimePart *part);
+
+/**
+ * modest_tny_mime_part_get_content_type:
+ * @part: a #TnyMimePart
+ *
+ * obtains the content type of a mime part, taking into account that,
+ * for messages with type message/rfc822 it has to get it from
+ * the headers
+ */
+gchar *modest_tny_mime_part_get_content_type (TnyMimePart *part);
+
 #endif /*__MODEST_TNY_MIME_PART_H__*/
index db282e1..2790914 100644 (file)
@@ -465,19 +465,14 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
        /* no parts? assume it's single-part message */
        if (tny_iterator_is_done(iter)) {
                gchar *content_type;
-               gchar *content_type_lower;
                gboolean is_text_part;
                g_object_unref (G_OBJECT(iter));
-               content_type = g_strdup (tny_mime_part_get_content_type (msg));
+               content_type = modest_tny_mime_part_get_content_type (msg);
                if (content_type == NULL)
                        return NULL;
-               content_type = g_strstrip (content_type);
-               content_type_lower = g_ascii_strdown (content_type, -1);
-               g_free (content_type);
                is_text_part = 
-                       g_str_has_prefix (content_type_lower, "text/") ||
-                       g_str_has_prefix (content_type_lower, "message/rfc822");
-               g_free (content_type_lower);
+                       g_str_has_prefix (content_type, "text/");
+               g_free (content_type);
                /* if this part cannot be a supported body return NULL */
                if (!is_text_part) {
                        return NULL;
index ffb571c..af08d11 100644 (file)
@@ -2330,9 +2330,15 @@ _invalid_attach_selected (ModestWindow *win,
                        while (!tny_iterator_is_done (iter) && !result) {
                                TnyMimePart *mime_part = TNY_MIME_PART (tny_iterator_get_current (iter));
                                TnyList *nested_list = tny_simple_list_new ();
+
                                if (!for_remove && modest_tny_mime_part_is_msg (mime_part)) {
-                                       selected_messages = TRUE;
-                                       result = TRUE;
+                                       TnyMsg *window_msg;
+                                       window_msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
+                                       if ((TnyMimePart *) window_msg != mime_part) {
+                                               selected_messages = TRUE;
+                                               result = TRUE;
+                                       }
+                                       g_object_unref (window_msg);
                                }
                                tny_mime_part_get_parts (mime_part, nested_list);
                                if (!for_remove && tny_list_get_length (nested_list) > 0) {
index bcdadf2..f2a3eaa 100644 (file)
@@ -37,6 +37,7 @@
 #include <modest-attachment-view.h>
 #include <modest-platform.h>
 #include <modest-text-utils.h>
+#include <modest-tny-mime-part.h>
 #include <tny-msg.h>
 #include <modest-mail-operation.h>
 #include <modest-mail-operation-queue.h>
@@ -256,12 +257,24 @@ modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mim
                                filename = tny_header_dup_subject (header);
                        if (filename == NULL || filename[0] == '\0')
                                filename = g_strdup (_("mail_va_no_subject"));
-                       if (priv->is_purged)
+                       if (priv->is_purged) {
                                file_icon_name = modest_platform_get_file_icon_name (NULL, NULL, NULL);
-                       else
-                               file_icon_name = 
-                                       modest_platform_get_file_icon_name (
-                                               NULL, tny_mime_part_get_content_type (mime_part), NULL);
+                       } else {
+                               gchar *header_content_type;
+                               header_content_type = modest_tny_mime_part_get_content_type (mime_part);
+                               if ((g_str_has_prefix (header_content_type, "message/rfc822") ||
+                                    g_str_has_prefix (header_content_type, "multipart/") ||
+                                    g_str_has_prefix (header_content_type, "text/"))) {
+                                       file_icon_name = 
+                                               modest_platform_get_file_icon_name (
+                                                       NULL, tny_mime_part_get_content_type (mime_part), NULL);
+                               } else {
+                                       file_icon_name = 
+                                               modest_platform_get_file_icon_name (
+                                                       NULL, header_content_type, NULL);
+                               }
+                               g_free (header_content_type);
+                       }
                        g_object_unref (header);
                }
        } else {
@@ -270,7 +283,7 @@ modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mim
                        file_icon_name = modest_platform_get_file_icon_name (NULL, NULL, NULL);
                } else {
                        file_icon_name = modest_platform_get_file_icon_name (
-                               filename, tny_mime_part_get_content_type (mime_part), NULL);
+                               filename, modest_tny_mime_part_get_content_type (mime_part), NULL);
                        show_size = TRUE;
                }
        }
index 90eb51e..6a17f99 100644 (file)
@@ -100,13 +100,14 @@ modest_attachments_view_new (TnyMsg *msg)
        return GTK_WIDGET (self);
 }
 
+
 void
 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
 {
        ModestAttachmentsViewPrivate *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
        TnyList *parts;
        TnyIterator *iter;
-       const gchar *msg_content_type = NULL;
+       gchar *msg_content_type = NULL;
        
        if (msg == priv->msg) return;
 
@@ -128,20 +129,18 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn
 
        /* If the top mime part is a multipart/related, we don't show the attachments, as they're
         * embedded images in body */
-       msg_content_type = tny_mime_part_get_content_type (TNY_MIME_PART (priv->msg));
+       msg_content_type = modest_tny_mime_part_get_content_type (TNY_MIME_PART (priv->msg));
        if ((msg_content_type != NULL) && !strcasecmp (msg_content_type, "multipart/related")) {
                gchar *header_content_type;
-               gchar *header_content_type_lower;
                gboolean application_multipart = FALSE;
-               header_content_type = modest_tny_mime_part_get_header_value (TNY_MIME_PART (priv->msg), "Content-Type");
-               header_content_type = g_strstrip (header_content_type);
-               header_content_type_lower = (header_content_type ) ?
-                       g_ascii_strdown (header_content_type, -1) : NULL;
+
+               g_free (msg_content_type);
+
+               header_content_type = modest_tny_mime_part_get_headers_content_type (TNY_MIME_PART (priv->msg));
                
-               if ((header_content_type_lower != NULL) && 
-                   !strstr (header_content_type_lower, "application/")) {
+               if ((header_content_type != NULL) && 
+                   !strstr (header_content_type, "application/")) {
                        application_multipart = TRUE;
-                       g_free (header_content_type_lower);
                }
                g_free (header_content_type);
 
@@ -150,14 +149,14 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn
                        return;
                }
        } else {
-               gchar *lower;
                gboolean direct_attach;
 
-               lower = g_ascii_strdown (msg_content_type, -1);
-               direct_attach = (!g_str_has_prefix (lower, "message/rfc822") && 
-                                !g_str_has_prefix (lower, "multipart") && 
-                                !g_str_has_prefix (lower, "text/"));
-               g_free (lower);
+               direct_attach = (!g_str_has_prefix (msg_content_type, "message/rfc822") && 
+                                !g_str_has_prefix (msg_content_type, "multipart") && 
+                                !g_str_has_prefix (msg_content_type, "text/"));
+
+               g_free (msg_content_type);
+
                if (direct_attach) {
                        modest_attachments_view_add_attachment (attachments_view, TNY_MIME_PART (msg), TRUE, 0);
                        gtk_widget_queue_draw (GTK_WIDGET (attachments_view));