[evolution] Bug 756883 - Add an option for line wrapping in a quoted text



commit f436ed10b8a8b2ed3b9c418e7706a7526848fc5c
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Oct 21 13:47:16 2015 +0200

    Bug 756883 - Add an option for line wrapping in a quoted text
    
    This patch adds a new "Wrap quoted text in replies" option under the
    "Edit->Preferences->Composer Preferences->Replies and Forwards" section in
    preferences. If set to FALSE the quoted text in replies will be parsed to
    'Preformated' blocks and the won't be wrapped. The option is set to TRUE by
    default, so the behavior won't change for current users.
    
    Also rename various variables/functions around code that parses the HTML to
    blocks to reflect that we are not parsing just to paragraphs.

 data/org.gnome.evolution.mail.gschema.xml.in |    5 +
 e-util/e-html-editor-view.c                  |  187 +++++++++++++++-----------
 mail/mail-config.ui                          |   17 +++
 modules/mail/em-composer-prefs.c             |    6 +
 4 files changed, 134 insertions(+), 81 deletions(-)
---
diff --git a/data/org.gnome.evolution.mail.gschema.xml.in b/data/org.gnome.evolution.mail.gschema.xml.in
index 85db8b2..004d08d 100644
--- a/data/org.gnome.evolution.mail.gschema.xml.in
+++ b/data/org.gnome.evolution.mail.gschema.xml.in
@@ -195,6 +195,11 @@
       <_summary>Whether to always show Sign and Encrypt buttons on the toolbar</_summary>
       <_description>If set to 'true', the Sign and Encrypt buttons for either PGP or S/MIME are always shown 
in the composer's toolbar. Otherwise they are shown only when being used.</_description>
     </key>
+    <key name="composer-wrap-quoted-text-in-replies" type="b">
+      <default>true</default>
+      <_summary>Wrap quoted text in replies</_summary>
+      <_description>If set to 'true' quoted text in replies will be wrapped.</_description>
+    </key>
     <key name="drag-and-drop-save-file-format" type="s">
       <default>'mbox'</default>
       <_summary>Save file format for drag-and-drop operation</_summary>
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 8854a2e..d3f3994 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -6800,40 +6800,41 @@ surround_links_with_anchor (const gchar *text)
 }
 
 static void
-append_new_paragraph (WebKitDOMElement *parent,
-                      WebKitDOMElement **paragraph)
+append_new_block (WebKitDOMElement *parent,
+                  WebKitDOMElement **block)
 {
        webkit_dom_node_append_child (
                WEBKIT_DOM_NODE (parent),
-               WEBKIT_DOM_NODE (*paragraph),
+               WEBKIT_DOM_NODE (*block),
                NULL);
 
-       *paragraph = NULL;
+       *block = NULL;
 }
 
 static WebKitDOMElement *
-create_and_append_new_paragraph (EHTMLEditorSelection *selection,
-                                 WebKitDOMDocument *document,
-                                 WebKitDOMElement *parent,
-                                 WebKitDOMNode *block,
-                                 const gchar *content)
+create_and_append_new_block (EHTMLEditorSelection *selection,
+                             WebKitDOMDocument *document,
+                             WebKitDOMElement *parent,
+                             WebKitDOMElement *block_template,
+                             const gchar *content)
 {
-       WebKitDOMElement *paragraph;
+       WebKitDOMElement *block;
 
-       if (!block || WEBKIT_DOM_IS_HTML_DIV_ELEMENT (block))
-               paragraph = e_html_editor_selection_get_paragraph_element (
+       if (WEBKIT_DOM_IS_HTML_DIV_ELEMENT (block_template))
+               block = e_html_editor_selection_get_paragraph_element (
                        selection, document, -1, 0);
        else
-               paragraph = WEBKIT_DOM_ELEMENT (webkit_dom_node_clone_node (block, FALSE));
+               block = WEBKIT_DOM_ELEMENT (webkit_dom_node_clone_node (
+                       WEBKIT_DOM_NODE (block_template), FALSE));
 
        webkit_dom_html_element_set_inner_html (
-               WEBKIT_DOM_HTML_ELEMENT (paragraph),
+               WEBKIT_DOM_HTML_ELEMENT (block),
                content,
                NULL);
 
-       append_new_paragraph (parent, &paragraph);
+       append_new_block (parent, &block);
 
-       return paragraph;
+       return block;
 }
 
 static void
@@ -6886,9 +6887,9 @@ get_decoded_line_length (WebKitDOMDocument *document,
 }
 
 static gboolean
-check_if_end_paragraph (const gchar *input,
-                        glong length,
-                        gboolean preserve_next_line)
+check_if_end_block (const gchar *input,
+                    glong length,
+                    gboolean preserve_next_line)
 {
        const gchar *next_space;
 
@@ -6920,30 +6921,52 @@ check_if_end_paragraph (const gchar *input,
 }
 
 /* This parses the HTML code (that contains just text, &nbsp; and BR elements)
- * into paragraphs.
+ * into blocks.
  * HTML code in that format we can get by taking innerText from some element,
  * setting it to another one and finally getting innerHTML from it */
 static void
-parse_html_into_paragraphs (EHTMLEditorView *view,
-                            WebKitDOMDocument *document,
-                            WebKitDOMElement *blockquote,
-                            WebKitDOMNode *block,
-                            const gchar *html)
+parse_html_into_blocks (EHTMLEditorView *view,
+                        WebKitDOMDocument *document,
+                        WebKitDOMElement *parent,
+                        WebKitDOMElement *passed_block_template,
+                        const gchar *html)
 {
        EHTMLEditorSelection *selection;
        gboolean ignore_next_br = FALSE;
        gboolean first_element = TRUE;
        gboolean citation_was_first_element = FALSE;
-       const gchar *prev_br, *next_br;
-       GRegex *regex_nbsp = NULL, *regex_link = NULL, *regex_email = NULL;
-       WebKitDOMElement *paragraph = NULL;
        gboolean preserve_next_line = FALSE;
        gboolean has_citation = FALSE;
+       const gchar *prev_br, *next_br;
+       GRegex *regex_nbsp = NULL, *regex_link = NULL, *regex_email = NULL;
+       WebKitDOMElement *block = NULL, *block_template = passed_block_template;
 
        selection = e_html_editor_view_get_selection (view);
 
        webkit_dom_html_element_set_inner_html (
-               WEBKIT_DOM_HTML_ELEMENT (blockquote), "", NULL);
+               WEBKIT_DOM_HTML_ELEMENT (parent), "", NULL);
+
+       if (!block_template) {
+               if (WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (parent)) {
+                       gboolean use_paragraphs;
+                       GSettings *settings;
+
+                       settings = e_util_ref_settings ("org.gnome.evolution.mail");
+
+                       use_paragraphs = g_settings_get_boolean (
+                               settings, "composer-wrap-quoted-text-in-replies");
+
+                       if (use_paragraphs)
+                               block_template = e_html_editor_selection_get_paragraph_element (
+                                       selection, document, -1, 0);
+                       else
+                               block_template = webkit_dom_document_create_element (document, "pre", NULL);
+
+                       g_object_unref (settings);
+               } else
+                       block_template = e_html_editor_selection_get_paragraph_element (
+                               selection, document, -1, 0);
+       }
 
        prev_br = html;
        next_br = strstr (prev_br, "<br>");
@@ -6974,8 +6997,8 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                        has_citation = TRUE;
                        if (strstr (citation, "END##")) {
                                ignore_next_br = TRUE;
-                               if (paragraph)
-                                       append_new_paragraph (blockquote, &paragraph);
+                               if (block)
+                                       append_new_block (parent, &block);
                        }
 
                        citation_end = strstr (citation + 2, "##");
@@ -6985,13 +7008,13 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                        if (first_element)
                                citation_was_first_element = TRUE;
 
-                       if (paragraph)
-                               append_new_paragraph (blockquote, &paragraph);
+                       if (block)
+                               append_new_block (parent, &block);
 
                        citation_mark = g_utf8_substring (
                                citation, 0, g_utf8_pointer_to_offset (citation, rest));
 
-                       append_citation_mark (document, blockquote, citation_mark);
+                       append_citation_mark (document, parent, citation_mark);
 
                        g_free (citation_mark);
                } else
@@ -7049,25 +7072,26 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                        }
 
                        if (g_strcmp0 (rest_to_insert, UNICODE_ZERO_WIDTH_SPACE) == 0) {
-                               if (paragraph)
-                                       append_new_paragraph (blockquote, &paragraph);
+                               if (block)
+                                       append_new_block (parent, &block);
 
-                               paragraph = create_and_append_new_paragraph (
-                                       selection, document, blockquote, block, "<br>");
+                               block = create_and_append_new_block (
+                                       selection, document, parent, block_template, "<br>");
                        } else if (preserve_block) {
                                gchar *html;
                                gchar *content_to_append;
 
-                               if (!paragraph) {
-                                       if (!block || WEBKIT_DOM_IS_HTML_DIV_ELEMENT (block))
-                                               paragraph = e_html_editor_selection_get_paragraph_element (
+                               if (!block) {
+                                       if (WEBKIT_DOM_IS_HTML_DIV_ELEMENT (block_template))
+                                               block = e_html_editor_selection_get_paragraph_element (
                                                        selection, document, -1, 0);
                                        else
-                                               paragraph = WEBKIT_DOM_ELEMENT (webkit_dom_node_clone_node 
(block, FALSE));
+                                               block = WEBKIT_DOM_ELEMENT (webkit_dom_node_clone_node (
+                                                       WEBKIT_DOM_NODE (block_template), FALSE));
                                }
 
                                html = webkit_dom_html_element_get_inner_html (
-                                       WEBKIT_DOM_HTML_ELEMENT (paragraph));
+                                       WEBKIT_DOM_HTML_ELEMENT (block));
 
                                content_to_append = g_strconcat (
                                        html && *html ? " " : "",
@@ -7075,7 +7099,7 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                                        NULL),
 
                                webkit_dom_html_element_insert_adjacent_html (
-                                       WEBKIT_DOM_HTML_ELEMENT (paragraph),
+                                       WEBKIT_DOM_HTML_ELEMENT (block),
                                        "beforeend",
                                        content_to_append,
                                        NULL);
@@ -7083,14 +7107,14 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                                g_free (html);
                                g_free (content_to_append);
                        } else {
-                               if (paragraph)
-                                       append_new_paragraph (blockquote, &paragraph);
+                               if (block)
+                                       append_new_block (parent, &block);
 
-                               paragraph = create_and_append_new_paragraph (
-                                       selection, document, blockquote, block, rest_to_insert);
+                               block = create_and_append_new_block (
+                                       selection, document, parent, block_template, rest_to_insert);
                        }
 
-                       if (rest_to_insert && *rest_to_insert && preserve_block && paragraph) {
+                       if (rest_to_insert && *rest_to_insert && preserve_block && block) {
                                glong length = 0;
 
                                /* If the line contains some encoded chracters (i.e. &gt;)
@@ -7103,13 +7127,13 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                                /* End the block if there is line with less that 62 characters. */
                                /* The shorter line can also mean that there is a long word on next
                                 * line (and the line was wrapped). So look at it and decide what to do. */
-                               if (length < 62 && check_if_end_paragraph (next_br, length, 
local_preserve_next_line)) {
-                                       append_new_paragraph (blockquote, &paragraph);
+                               if (length < 62 && check_if_end_block (next_br, length, 
local_preserve_next_line)) {
+                                       append_new_block (parent, &block);
                                        preserve_next_line = FALSE;
                                }
 
                                if (length > 72) {
-                                       append_new_paragraph (blockquote, &paragraph);
+                                       append_new_block (parent, &block);
                                        preserve_next_line = FALSE;
                                }
                        }
@@ -7119,28 +7143,28 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                        g_free (rest_to_insert);
                } else if (with_br) {
                        if (!citation && (!local_ignore_next_br || citation_was_first_element)) {
-                               if (paragraph)
-                                       append_new_paragraph (blockquote, &paragraph);
+                               if (block)
+                                       append_new_block (parent, &block);
 
-                               paragraph = create_and_append_new_paragraph (
-                                       selection, document, blockquote, block, "<br>");
+                               block = create_and_append_new_block (
+                                       selection, document, parent, block_template, "<br>");
 
                                citation_was_first_element = FALSE;
                        } else if (first_element && !citation_was_first_element) {
-                               paragraph = create_and_append_new_paragraph (
+                               block = create_and_append_new_block (
                                        selection,
                                        document,
-                                       blockquote,
-                                       block,
+                                       parent,
+                                       block_template,
                                        "<br class=\"-x-evo-first-br\">");
                        } else
                                preserve_next_line = FALSE;
                } else if (first_element && !citation_was_first_element) {
-                       paragraph = create_and_append_new_paragraph (
+                       block = create_and_append_new_block (
                                selection,
                                document,
-                               blockquote,
-                               block,
+                               parent,
+                               block_template,
                                "<br class=\"-x-evo-first-br\">");
                } else
                        preserve_next_line = FALSE;
@@ -7151,8 +7175,8 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                g_free (to_insert);
        }
 
-       if (paragraph)
-               append_new_paragraph (blockquote, &paragraph);
+       if (block)
+               append_new_block (parent, &block);
 
        if (g_utf8_strlen (prev_br, -1) > 0) {
                gchar *rest_to_insert;
@@ -7165,7 +7189,7 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                        WebKitDOMNode *child;
 
                        child = webkit_dom_node_get_last_child (
-                               WEBKIT_DOM_NODE (blockquote));
+                               WEBKIT_DOM_NODE (parent));
                        if (child) {
                                child = webkit_dom_node_get_first_child (child);
                                if (child && WEBKIT_DOM_IS_HTMLBR_ELEMENT (child)) {
@@ -7176,17 +7200,17 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                                                        WEBKIT_DOM_ELEMENT (child),
                                                        "-x-evo-last-br");
                                } else if (!view->priv->is_editting_message)
-                                       create_and_append_new_paragraph (
-                                               selection, document, blockquote, block, "<br>");
+                                       create_and_append_new_block (
+                                               selection, document, parent, block_template, "<br>");
                        } else
-                               create_and_append_new_paragraph (
-                                       selection, document, blockquote, block, "<br>");
+                               create_and_append_new_block (
+                                       selection, document, parent, block_template, "<br>");
                        g_free (truncated);
                        goto end;
                }
 
                if (g_ascii_strncasecmp (truncated, "##CITATION_END##", 16) == 0) {
-                       append_citation_mark (document, blockquote, truncated);
+                       append_citation_mark (document, parent, truncated);
                        g_free (truncated);
                        goto end;
                }
@@ -7227,11 +7251,11 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
                }
 
                if (g_strcmp0 (rest_to_insert, UNICODE_ZERO_WIDTH_SPACE) == 0)
-                       create_and_append_new_paragraph (
-                               selection, document, blockquote, block, "<br>");
+                       create_and_append_new_block (
+                               selection, document, parent, block_template, "<br>");
                else
-                       create_and_append_new_paragraph (
-                               selection, document, blockquote, block, rest_to_insert);
+                       create_and_append_new_block (
+                               selection, document, parent, block_template, rest_to_insert);
 
                g_free (rest_to_insert);
        }
@@ -7243,13 +7267,13 @@ parse_html_into_paragraphs (EHTMLEditorView *view,
 
                /* Replace text markers with actual HTML blockquotes */
                inner_html = webkit_dom_html_element_get_inner_html (
-                       WEBKIT_DOM_HTML_ELEMENT (blockquote));
+                       WEBKIT_DOM_HTML_ELEMENT (parent));
                start = e_str_replace_string (
                        inner_html, "##CITATION_START##","<blockquote type=\"cite\">");
                end = e_str_replace_string (
                        start->str, "##CITATION_END##", "</blockquote>");
                webkit_dom_html_element_set_inner_html (
-                       WEBKIT_DOM_HTML_ELEMENT (blockquote), end->str, NULL);
+                       WEBKIT_DOM_HTML_ELEMENT (parent), end->str, NULL);
 
                g_free (inner_html);
                g_string_free (start, TRUE);
@@ -7343,7 +7367,7 @@ e_html_editor_view_insert_quoted_text (EHTMLEditorView *view,
                        NULL);
        }
 
-       parse_html_into_paragraphs (view, document, blockquote, NULL, inner_html);
+       parse_html_into_blocks (view, document, blockquote, NULL, inner_html);
 
        if (!e_html_editor_view_get_html_mode (view)) {
                WebKitDOMNode *node;
@@ -7458,8 +7482,9 @@ quote_plain_text_elements_after_wrapping_in_document (WebKitDOMDocument *documen
        gint length, ii;
        WebKitDOMNodeList *list;
 
+       /* Also quote the PRE elements as well. */
        list = webkit_dom_document_query_selector_all (
-               document, "blockquote[type=cite] > div.-x-evo-paragraph", NULL);
+               document, "blockquote[type=cite] > div.-x-evo-paragraph, blockquote[type=cite] > pre", NULL);
 
        length = webkit_dom_node_list_get_length (list);
        for (ii = 0; ii < length; ii++) {
@@ -7797,7 +7822,7 @@ html_editor_convert_view_content (EHTMLEditorView *view,
                empty = FALSE;
 
        if (!empty)
-               parse_html_into_paragraphs (view, document, content_wrapper, NULL, inner_html);
+               parse_html_into_blocks (view, document, content_wrapper, NULL, inner_html);
        else
                webkit_dom_node_append_child (
                        WEBKIT_DOM_NODE (content_wrapper),
@@ -7960,7 +7985,7 @@ html_editor_view_insert_converted_html_into_selection (EHTMLEditorView *view,
 
        inner_html = webkit_dom_html_element_get_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (element));
-       parse_html_into_paragraphs (view, document, element, current_block, inner_html);
+       parse_html_into_blocks (view, document, element, WEBKIT_DOM_ELEMENT (current_block), inner_html);
        g_free (inner_html);
 
        has_selection = !e_html_editor_selection_is_collapsed (selection);
@@ -9701,7 +9726,7 @@ convert_element_from_html_to_plain_text (EHTMLEditorView *view,
        inner_html = webkit_dom_html_element_get_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (blockquote));
 
-       parse_html_into_paragraphs (
+       parse_html_into_blocks (
                view, document,
                main_blockquote ? blockquote : WEBKIT_DOM_ELEMENT (element),
                NULL,
diff --git a/mail/mail-config.ui b/mail/mail-config.ui
index c99c3b7..49f7732 100644
--- a/mail/mail-config.ui
+++ b/mail/mail-config.ui
@@ -605,6 +605,23 @@
                       </packing>
                     </child>
                     <child>
+                      <object class="GtkCheckButton" id="chkWrapQuotedTextInReplies">
+                        <property name="label" translatable="yes">_Wrap quoted text in replies</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="receives_default">True</property>
+                        <property name="use_underline">True</property>
+                        <property name="xalign">0.5</property>
+                        <property name="draw_indicator">True</property>
+                      </object>
+                      <packing>
+                        <property name="right_attach">2</property>
+                        <property name="top_attach">7</property>
+                        <property name="bottom_attach">8</property>
+                        <property name="y_options"/>
+                      </packing>
+                    </child>
+                    <child>
                       <object class="GtkAlignment" id="reply-style-alignment">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
diff --git a/modules/mail/em-composer-prefs.c b/modules/mail/em-composer-prefs.c
index 2d23221..11d9172 100644
--- a/modules/mail/em-composer-prefs.c
+++ b/modules/mail/em-composer-prefs.c
@@ -1237,6 +1237,12 @@ em_composer_prefs_construct (EMComposerPrefs *prefs,
                widget, "active",
                G_SETTINGS_BIND_DEFAULT);
 
+       widget = e_builder_get_widget (prefs->builder, "chkWrapQuotedTextInReplies");
+       g_settings_bind (
+               settings, "composer-wrap-quoted-text-in-replies",
+               widget, "active",
+               G_SETTINGS_BIND_DEFAULT);
+
        widget = e_builder_get_widget (prefs->builder, "chkTopSignature");
        g_settings_bind (
                settings, "composer-top-signature",


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