[evolution] Composer - User changed signature is lost when opening a draft or template



commit 8c72e4369cd827d10beeea1ef8eae23c71d0c2dd
Author: Tomas Popela <tpopela redhat com>
Date:   Tue Jul 21 17:56:45 2015 +0200

    Composer - User changed signature is lost when opening a draft or template
    
    When opening a draft or a template with a signature we were setting the current
    signature in "Signature chooser" to the one that was inside the message. The
    problem was that the code replaced the body's signature with the one that is
    saved on local storage. When the signature in the body was changed by the user
    the changes were thrown away. After this change the signatures (the one in the
    body and the one from local storage) are compared and if they are not the same
    the signature in the body is left untouched and current signature in "Signature
    chooser" is set to None (to stick with GtkHTML composer).

 composer/e-composer-private.c |   92 ++++++++++++++++++++++++++++++++---------
 composer/e-composer-private.h |    2 +
 2 files changed, 74 insertions(+), 20 deletions(-)
---
diff --git a/composer/e-composer-private.c b/composer/e-composer-private.c
index 515de09..a55e99c 100644
--- a/composer/e-composer-private.c
+++ b/composer/e-composer-private.c
@@ -139,6 +139,8 @@ e_composer_private_constructed (EMsgComposer *composer)
        priv->saved_editable = FALSE;
        priv->drop_occured = FALSE;
        priv->dnd_is_uri = FALSE;
+       priv->check_if_signature_is_changed = FALSE;
+       priv->ignore_next_signature_change = FALSE;
 
        priv->focused_entry = NULL;
 
@@ -1069,6 +1071,7 @@ composer_load_signature_cb (EMailSignatureComboBox *combo_box,
        EHTMLEditor *editor;
        EHTMLEditorView *view;
        WebKitDOMDocument *document;
+       WebKitDOMElement *element = NULL;
        WebKitDOMNodeList *signatures;
        gulong list_length, ii;
        GSettings *settings;
@@ -1083,6 +1086,11 @@ composer_load_signature_cb (EMailSignatureComboBox *combo_box,
                goto exit;
        }
 
+       if (composer->priv->ignore_next_signature_change) {
+               composer->priv->ignore_next_signature_change = FALSE;
+               goto exit;
+       }
+
        editor = e_msg_composer_get_editor (composer);
        view = e_html_editor_get_view (editor);
        is_message_from_edit_as_new =
@@ -1107,14 +1115,14 @@ composer_load_signature_cb (EMailSignatureComboBox *combo_box,
 
        /* If inserting HTML signature in plain text composer we have to convert it. */
        if (is_html && !html_mode) {
-               WebKitDOMElement *element;
+               WebKitDOMElement *tmp_element;
                gchar *inner_text;
 
-               element = webkit_dom_document_create_element (document, "div", NULL);
+               tmp_element = webkit_dom_document_create_element (document, "div", NULL);
                webkit_dom_html_element_set_inner_html (
-                       WEBKIT_DOM_HTML_ELEMENT (element), contents, NULL);
+                       WEBKIT_DOM_HTML_ELEMENT (tmp_element), contents, NULL);
                inner_text = webkit_dom_html_element_get_inner_text (
-                       WEBKIT_DOM_HTML_ELEMENT (element));
+                       WEBKIT_DOM_HTML_ELEMENT (tmp_element));
 
                g_free (contents);
                contents = inner_text ? g_strstrip (inner_text) : g_strdup ("");
@@ -1192,21 +1200,63 @@ insert:
 
                wrapper = webkit_dom_node_list_item (signatures, ii);
                signature = webkit_dom_node_get_first_child (wrapper);
-               id = webkit_dom_element_get_id (WEBKIT_DOM_ELEMENT (signature));
 
-               /* When we are editing a message with signature we need to set active
-                * signature id in signature combo box otherwise no signature will be
-                * added but we have to do it just once when the composer opens */
-               if (is_message_from_edit_as_new && composer->priv->set_signature_from_message) {
-                       gchar *name;
+               /* When we are editing a message with signature, we need to unset the
+                * active signature id as if the signature in the message was edited
+                * by the user we would discard these changes. */
+               if (composer->priv->set_signature_from_message &&
+                   (is_message_from_edit_as_new || e_html_editor_view_is_message_from_draft (view))) {
+                       if (composer->priv->check_if_signature_is_changed) {
+                               if (html_buffer && *html_buffer->str) {
+                                       gchar *body_signature_text, *signature_text;
+
+                                       element = webkit_dom_document_create_element (document, "div", NULL);
+                                       webkit_dom_html_element_set_inner_html (
+                                               WEBKIT_DOM_HTML_ELEMENT (element), html_buffer->str, NULL);
+
+                                       body_signature_text = webkit_dom_html_element_get_inner_text (
+                                               WEBKIT_DOM_HTML_ELEMENT (signature));
+                                       signature_text = webkit_dom_html_element_get_inner_text (
+                                               WEBKIT_DOM_HTML_ELEMENT (element));
+
+                                       /* Signature in the body is different than the one with the
+                                        * same id, so set the active signature to None and leave
+                                        * the signature that is in the body. */
+                                       if (g_strcmp0 (body_signature_text, signature_text) != 0) {
+                                               gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
+                                               composer->priv->ignore_next_signature_change = TRUE;
+                                       }
+
+                                       g_free (body_signature_text);
+                                       g_free (signature_text);
+                               } else {
+                                       gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
+                                       composer->priv->ignore_next_signature_change = TRUE;
+                               }
+
+                               composer->priv->check_if_signature_is_changed = FALSE;
+                               composer->priv->set_signature_from_message = FALSE;
+                       } else {
+                               gchar *name;
 
-                       composer->priv->set_signature_from_message = FALSE;
+                               /* Load the signature and check if is it the same
+                                * as the signature in body or the user previously
+                                * changed it. */
+                               name = webkit_dom_element_get_attribute (WEBKIT_DOM_ELEMENT (signature), 
"name");
+                               gtk_combo_box_set_active_id (GTK_COMBO_BOX (combo_box), name);
+                               g_free (name);
 
-                       name = webkit_dom_element_get_attribute (WEBKIT_DOM_ELEMENT (signature), "name");
-                       gtk_combo_box_set_active_id (GTK_COMBO_BOX (combo_box), name);
-                       g_free (name);
+                               composer->priv->check_if_signature_is_changed = TRUE;
+                       }
+                       g_object_unref (wrapper);
+                       g_object_unref (signatures);
+
+                       g_object_unref (composer);
+
+                       return;
                }
 
+               id = webkit_dom_element_get_id (WEBKIT_DOM_ELEMENT (signature));
                if (id && (strlen (id) == 1) && (*id == '1')) {
                        /* If the top signature was set we have to remove the NL
                         * that was inserted after it */
@@ -1233,15 +1283,17 @@ insert:
 
        if (html_buffer != NULL) {
                if (*html_buffer->str) {
-                       WebKitDOMElement *element;
                        WebKitDOMHTMLElement *body;
 
                        body = webkit_dom_document_get_body (document);
-                       element = webkit_dom_document_create_element (document, "DIV", NULL);
-                       webkit_dom_element_set_class_name (element, "-x-evo-signature-wrapper");
+                       if (!element) {
+                               element = webkit_dom_document_create_element (document, "DIV", NULL);
 
-                       webkit_dom_html_element_set_inner_html (
-                               WEBKIT_DOM_HTML_ELEMENT (element), html_buffer->str, NULL);
+                               webkit_dom_html_element_set_inner_html (
+                                       WEBKIT_DOM_HTML_ELEMENT (element), html_buffer->str, NULL);
+                       }
+
+                       webkit_dom_element_set_class_name (element, "-x-evo-signature-wrapper");
 
                        if (top_signature) {
                                WebKitDOMNode *child =
@@ -1278,7 +1330,7 @@ insert:
 
        composer_move_caret (composer);
 
-exit:
+ exit:
        /* Make sure the flag will be unset and won't influence user's choice */
        composer->priv->set_signature_from_message = FALSE;
 
diff --git a/composer/e-composer-private.h b/composer/e-composer-private.h
index 7f798b4..3b42140 100644
--- a/composer/e-composer-private.h
+++ b/composer/e-composer-private.h
@@ -102,6 +102,8 @@ struct _EMsgComposerPrivate {
        gboolean set_signature_from_message;
        gboolean drop_occured;
        gboolean dnd_is_uri;
+       gboolean check_if_signature_is_changed;
+       gboolean ignore_next_signature_change;
 
        gint focused_entry_selection_start;
        gint focused_entry_selection_end;


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