[evolution/449-support-markdown-in-composer] EMailNodes: Change how selected Markdown format is stored



commit dd5b6937d6d1a4b4bc6502641900d7ebe87bba42
Author: Milan Crha <mcrha redhat com>
Date:   Wed Feb 9 17:52:44 2022 +0100

    EMailNodes: Change how selected Markdown format is stored

 src/mail/e-mail-notes.c | 104 ++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 44 deletions(-)
---
diff --git a/src/mail/e-mail-notes.c b/src/mail/e-mail-notes.c
index edca97ef6d..7a4a7589bf 100644
--- a/src/mail/e-mail-notes.c
+++ b/src/mail/e-mail-notes.c
@@ -30,6 +30,8 @@
 
 #include "e-mail-notes.h"
 
+#define E_MAIL_NOTES_FORMAT "X-Evolution-Format"
+
 #define E_TYPE_MAIL_NOTES_EDITOR \
        (e_mail_notes_editor_get_type ())
 #define E_MAIL_NOTES_EDITOR(obj) \
@@ -95,22 +97,15 @@ e_mail_notes_extract_text_content (CamelMimePart *part)
 static gboolean
 e_mail_notes_editor_extract_text_part (EHTMLEditor *editor,
                                       CamelContentType *content_type,
-                                      CamelMimePart *part)
+                                      CamelMimePart *part,
+                                      EContentEditorMode mode)
 {
-       EContentEditorMode mode = E_CONTENT_EDITOR_MODE_PLAIN_TEXT;
        guint32 insert_flag = E_CONTENT_EDITOR_INSERT_TEXT_PLAIN;
        gchar *text;
 
        if (camel_content_type_is (content_type, "text", "plain")) {
-               const gchar *format;
-
-               format = camel_content_type_param (content_type, "x-evolution-format");
-               if (format) {
-                       if (g_ascii_strcasecmp (format, "markdown-plain") == 0)
-                               mode = E_CONTENT_EDITOR_MODE_MARKDOWN_PLAIN_TEXT;
-                       else if (g_ascii_strcasecmp (format, "markdown-html") == 0)
-                               mode = E_CONTENT_EDITOR_MODE_MARKDOWN_HTML;
-               }
+               if (mode == E_CONTENT_EDITOR_MODE_UNKNOWN)
+                       mode = E_CONTENT_EDITOR_MODE_PLAIN_TEXT;
        } else if (camel_content_type_is (content_type, "text", "markdown")) {
                mode = E_CONTENT_EDITOR_MODE_MARKDOWN;
        } else if (camel_content_type_is (content_type, "text", "html")) {
@@ -140,8 +135,10 @@ e_mail_notes_editor_extract_text_part (EHTMLEditor *editor,
 
 static void
 e_mail_notes_extract_text_from_multipart_alternative (EHTMLEditor *editor,
-                                                     CamelMultipart *in_multipart)
+                                                     CamelMultipart *in_multipart,
+                                                     EContentEditorMode mode)
 {
+       CamelMimePart *fallback_part = NULL;
        guint ii, nparts;
 
        g_return_if_fail (E_IS_HTML_EDITOR (editor));
@@ -162,14 +159,36 @@ e_mail_notes_extract_text_from_multipart_alternative (EHTMLEditor *editor,
                if (!ct)
                        continue;
 
-               if (e_mail_notes_editor_extract_text_part (editor, ct, part))
+               if (mode == E_CONTENT_EDITOR_MODE_MARKDOWN ||
+                   mode == E_CONTENT_EDITOR_MODE_MARKDOWN_PLAIN_TEXT ||
+                   mode == E_CONTENT_EDITOR_MODE_MARKDOWN_HTML) {
+                       /* Fallback to the text/html part when reading markdown text,
+                          to avoid conversion from HTML to markdown, because the text/plain
+                          part contains the raw markdown */
+                       if (camel_content_type_is (ct, "text", "html")) {
+                               fallback_part = part;
+                               continue;
+                       }
+               }
+
+               if (e_mail_notes_editor_extract_text_part (editor, ct, part, mode)) {
+                       fallback_part = NULL;
                        break;
+               }
+       }
+
+       if (fallback_part) {
+               CamelContentType *ct;
+
+               ct = camel_mime_part_get_content_type (fallback_part);
+               e_mail_notes_editor_extract_text_part (editor, ct, fallback_part, mode);
        }
 }
 
 static void
 e_mail_notes_editor_extract_text_from_multipart_related (EMailNotesEditor *notes_editor,
-                                                        CamelMultipart *multipart)
+                                                        CamelMultipart *multipart,
+                                                        EContentEditorMode mode)
 {
        guint ii, nparts;
 
@@ -198,7 +217,7 @@ e_mail_notes_editor_extract_text_from_multipart_related (EMailNotesEditor *notes
 
                        if (CAMEL_IS_MULTIPART (content)) {
                                e_mail_notes_extract_text_from_multipart_alternative (notes_editor->editor,
-                                       CAMEL_MULTIPART (content));
+                                       CAMEL_MULTIPART (content), mode);
                        }
                }
        }
@@ -206,7 +225,8 @@ e_mail_notes_editor_extract_text_from_multipart_related (EMailNotesEditor *notes
 
 static void
 e_mail_notes_editor_extract_text_from_part (EMailNotesEditor *notes_editor,
-                                           CamelMimePart *part)
+                                           CamelMimePart *part,
+                                           EContentEditorMode mode)
 {
        CamelContentType *ct;
        CamelDataWrapper *content;
@@ -223,13 +243,13 @@ e_mail_notes_editor_extract_text_from_part (EMailNotesEditor *notes_editor,
        if (camel_content_type_is (ct, "multipart", "related")) {
                g_return_if_fail (CAMEL_IS_MULTIPART (content));
 
-               e_mail_notes_editor_extract_text_from_multipart_related (notes_editor, CAMEL_MULTIPART 
(content));
+               e_mail_notes_editor_extract_text_from_multipart_related (notes_editor, CAMEL_MULTIPART 
(content), mode);
        } else if (camel_content_type_is (ct, "multipart", "alternative")) {
                if (CAMEL_IS_MULTIPART (content)) {
-                       e_mail_notes_extract_text_from_multipart_alternative (notes_editor->editor, 
CAMEL_MULTIPART (content));
+                       e_mail_notes_extract_text_from_multipart_alternative (notes_editor->editor, 
CAMEL_MULTIPART (content), mode);
                }
        } else {
-               e_mail_notes_editor_extract_text_part (notes_editor->editor, ct, part);
+               e_mail_notes_editor_extract_text_part (notes_editor->editor, ct, part, mode);
        }
 }
 
@@ -239,7 +259,8 @@ e_mail_notes_editor_extract_text_from_message (EMailNotesEditor *notes_editor,
 {
        CamelContentType *ct;
        CamelDataWrapper *content;
-       EContentEditor *cnt_editor;
+       EContentEditorMode mode = E_CONTENT_EDITOR_MODE_UNKNOWN;
+       const gchar *format_header;
 
        g_return_if_fail (E_IS_MAIL_NOTES_EDITOR (notes_editor));
        g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message));
@@ -250,7 +271,14 @@ e_mail_notes_editor_extract_text_from_message (EMailNotesEditor *notes_editor,
        g_return_if_fail (content != NULL);
        g_return_if_fail (ct != NULL);
 
-       cnt_editor = e_html_editor_get_content_editor (notes_editor->editor);
+       format_header = camel_medium_get_header (CAMEL_MEDIUM (message), E_MAIL_NOTES_FORMAT);
+
+       if (format_header) {
+               if (g_ascii_strcasecmp (format_header, "text/markdown-plain") == 0)
+                       mode = E_CONTENT_EDITOR_MODE_MARKDOWN_PLAIN_TEXT;
+               else if (g_ascii_strcasecmp (format_header, "text/markdown-html") == 0)
+                       mode = E_CONTENT_EDITOR_MODE_MARKDOWN_HTML;
+       }
 
        if (camel_content_type_is (ct, "multipart", "mixed")) {
                EAttachmentStore *attachment_store;
@@ -276,7 +304,7 @@ e_mail_notes_editor_extract_text_from_message (EMailNotesEditor *notes_editor,
                                continue;
 
                        if (ii == 0) {
-                               e_mail_notes_editor_extract_text_from_part (notes_editor, part);
+                               e_mail_notes_editor_extract_text_from_part (notes_editor, part, mode);
                        } else {
                                EAttachment *attachment;
 
@@ -291,27 +319,10 @@ e_mail_notes_editor_extract_text_from_message (EMailNotesEditor *notes_editor,
                        }
                }
        } else {
-               e_mail_notes_editor_extract_text_from_part (notes_editor, CAMEL_MIME_PART (message));
+               e_mail_notes_editor_extract_text_from_part (notes_editor, CAMEL_MIME_PART (message), mode);
        }
 
-       e_content_editor_set_changed (cnt_editor, FALSE);
-}
-
-static void
-e_mail_notes_maybe_set_markdown_content_type_param (CamelMimePart *part,
-                                                   EContentEditorMode mode)
-{
-       if (mode == E_CONTENT_EDITOR_MODE_MARKDOWN_PLAIN_TEXT ||
-           mode == E_CONTENT_EDITOR_MODE_MARKDOWN_HTML) {
-               CamelContentType *content_type;
-
-               content_type = camel_data_wrapper_get_mime_type_field (camel_medium_get_content (CAMEL_MEDIUM 
(part)));
-
-               if (mode == E_CONTENT_EDITOR_MODE_MARKDOWN_PLAIN_TEXT)
-                       camel_content_type_set_param (content_type, "x-evolution-format", "markdown-plain");
-               else
-                       camel_content_type_set_param (content_type, "x-evolution-format", "markdown-html");
-       }
+       e_content_editor_set_changed (e_html_editor_get_content_editor (notes_editor->editor), FALSE);
 }
 
 static CamelMimeMessage *
@@ -356,6 +367,14 @@ e_mail_notes_editor_encode_text_to_message (EMailNotesEditor *notes_editor,
 
        mode = e_html_editor_get_mode (notes_editor->editor);
 
+       if (mode == E_CONTENT_EDITOR_MODE_MARKDOWN_PLAIN_TEXT ||
+           mode == E_CONTENT_EDITOR_MODE_MARKDOWN_HTML) {
+               if (mode == E_CONTENT_EDITOR_MODE_MARKDOWN_PLAIN_TEXT)
+                       camel_medium_add_header (CAMEL_MEDIUM (message), E_MAIL_NOTES_FORMAT, 
"text/markdown-plain");
+               else
+                       camel_medium_add_header (CAMEL_MEDIUM (message), E_MAIL_NOTES_FORMAT, 
"text/markdown-html");
+       }
+
        if (mode == E_CONTENT_EDITOR_MODE_HTML ||
            mode == E_CONTENT_EDITOR_MODE_MARKDOWN_HTML) {
                CamelMultipart *multipart_alternative;
@@ -382,7 +401,6 @@ e_mail_notes_editor_encode_text_to_message (EMailNotesEditor *notes_editor,
                        camel_mime_part_set_content (part, text, strlen (text), mode == 
E_CONTENT_EDITOR_MODE_MARKDOWN ?
                                "text/markdown" : "text/plain");
                        camel_multipart_add_part (multipart_alternative, part);
-                       e_mail_notes_maybe_set_markdown_content_type_param (part, mode);
 
                        g_object_unref (part);
                        g_free (tmp);
@@ -497,7 +515,6 @@ e_mail_notes_editor_encode_text_to_message (EMailNotesEditor *notes_editor,
                                camel_mime_part_set_content (part, text, strlen (text), mode == 
E_CONTENT_EDITOR_MODE_MARKDOWN ?
                                        "text/markdown" : "text/plain");
                                camel_multipart_add_part (multipart, part);
-                               e_mail_notes_maybe_set_markdown_content_type_param (part, mode);
                                g_object_unref (part);
 
                                e_attachment_store_add_to_multipart (attachment_store, multipart, "UTF-8");
@@ -508,7 +525,6 @@ e_mail_notes_editor_encode_text_to_message (EMailNotesEditor *notes_editor,
                        } else {
                                camel_mime_part_set_content (CAMEL_MIME_PART (message), text, strlen (text), 
mode == E_CONTENT_EDITOR_MODE_MARKDOWN ?
                                        "text/markdown" : "text/plain");
-                               e_mail_notes_maybe_set_markdown_content_type_param (CAMEL_MIME_PART 
(message), mode);
                        }
 
                        has_text = TRUE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]