[evolution/gnome-3-32] I#392 - Template user variables not replaced with Alternative Reply



commit b752678afc4cc3154b962ce62f6387176a5dc944
Author: Milan Crha <mcrha redhat com>
Date:   Mon Apr 8 11:05:59 2019 +0200

    I#392 - Template user variables not replaced with Alternative Reply
    
    Closes https://gitlab.gnome.org/GNOME/evolution/issues/392

 src/mail/e-mail-templates.c  |  88 +++++++++++++++++++---
 src/mail/em-composer-utils.c | 174 -------------------------------------------
 2 files changed, 77 insertions(+), 185 deletions(-)
---
diff --git a/src/mail/e-mail-templates.c b/src/mail/e-mail-templates.c
index f005e87fad..ad3777962e 100644
--- a/src/mail/e-mail-templates.c
+++ b/src/mail/e-mail-templates.c
@@ -31,22 +31,17 @@
 
 #include "e-mail-templates.h"
 
-/* Replaces $ORIG[variable] in given template by given replacement from the original message */
 static void
-replace_template_variable (GString *text,
-                           const gchar *variable,
-                           const gchar *replacement)
+replace_in_string (GString *text,
+                  const gchar *find,
+                  const gchar *replacement)
 {
        const gchar *p, *next;
        GString *str;
        gint find_len;
-       gchar *find;
 
        g_return_if_fail (text != NULL);
-       g_return_if_fail (variable != NULL);
-       g_return_if_fail (*variable);
-
-       find = g_strconcat ("$ORIG[", variable, "]", NULL);
+       g_return_if_fail (find != NULL);
 
        find_len = strlen (find);
        str = g_string_new ("");
@@ -58,14 +53,79 @@ replace_template_variable (GString *text,
                        g_string_append (str, replacement);
                p = next + find_len;
        }
-       g_string_append (str, p);
 
-       g_string_assign (text, str->str);
+       /* Avoid unnecessary allocation when the 'text' doesn't contain the variable */
+       if (p != text->str) {
+               g_string_append (str, p);
+               g_string_assign (text, str->str);
+       }
 
        g_string_free (str, TRUE);
+}
+
+/* Replaces $ORIG[variable] in given template by given replacement from the original message */
+static void
+replace_template_variable (GString *text,
+                           const gchar *variable,
+                           const gchar *replacement)
+{
+       gchar *find;
+
+       g_return_if_fail (text != NULL);
+       g_return_if_fail (variable != NULL);
+       g_return_if_fail (*variable);
+
+       find = g_strconcat ("$ORIG[", variable, "]", NULL);
+
+       replace_in_string (text, find, replacement);
+
        g_free (find);
 }
 
+static void
+replace_user_variables (GString *text,
+                       CamelMimeMessage *source_message)
+{
+       CamelInternetAddress *to;
+       const gchar *name, *addr;
+       GSettings *settings;
+       gchar **strv;
+       gint ii;
+
+       g_return_if_fail (text);
+       g_return_if_fail (CAMEL_IS_MIME_MESSAGE (source_message));
+
+       settings = e_util_ref_settings ("org.gnome.evolution.plugin.templates");
+       strv = g_settings_get_strv (settings, "template-placeholders");
+       g_object_unref (settings);
+
+       for (ii = 0; strv && strv[ii]; ii++) {
+               gchar *equal_sign, *find, *var_name = strv[ii];
+               const gchar *var_value;
+
+               equal_sign = strchr (var_name, '=');
+               if (!equal_sign)
+                       continue;
+
+               *equal_sign = '\0';
+               var_value = equal_sign + 1;
+
+               find = g_strconcat ("$", var_name, NULL);
+               replace_in_string (text, find, var_value);
+               g_free (find);
+
+               *equal_sign = '=';
+       }
+
+       g_strfreev (strv);
+
+       to = camel_mime_message_get_recipients (source_message, CAMEL_RECIPIENT_TYPE_TO);
+       if (to && camel_internet_address_get (to, 0, &name, &addr)) {
+               replace_in_string (text, "$sender_name", name);
+               replace_in_string (text, "$sender_email", addr);
+       }
+}
+
 static void
 replace_email_addresses (GString *template,
                          CamelInternetAddress *internet_address,
@@ -335,6 +395,8 @@ fill_template (CamelMimeMessage *message,
                g_free (reply_credits);
        }
 
+       replace_user_variables (template_body, message);
+
        return_part = camel_mime_part_new ();
 
        if (template_html)
@@ -478,10 +540,14 @@ e_mail_templates_apply_sync (CamelMimeMessage *source_message,
                                            g_ascii_strcasecmp (m_header_name, "subject") != 0)
                                                replace_template_variable (subject, m_header_name, 
m_header_value);
                                }
+
                                /* Now replace $ORIG[subject] variable, handling possible base64 encryption */
                                replace_template_variable (
                                        subject, "subject",
                                        camel_mime_message_get_subject (source_message));
+
+                               replace_user_variables (subject, source_message);
+
                                new_header_value = g_string_free (subject, FALSE);
                        }
 
diff --git a/src/mail/em-composer-utils.c b/src/mail/em-composer-utils.c
index c7e4641611..7480940b8e 100644
--- a/src/mail/em-composer-utils.c
+++ b/src/mail/em-composer-utils.c
@@ -1877,155 +1877,6 @@ em_utils_compose_new_message_with_mailto_and_selection (EShell *shell,
        e_msg_composer_new (shell, msg_composer_created_with_mailto_cb, ccd);
 }
 
-static gboolean
-replace_variables (GSList *clues,
-                   CamelMimeMessage *message,
-                   gchar **pstr)
-{
-       gint i;
-       gboolean string_changed = FALSE, count1 = FALSE;
-       gchar *str;
-
-       g_return_val_if_fail (pstr != NULL, FALSE);
-       g_return_val_if_fail (*pstr != NULL, FALSE);
-       g_return_val_if_fail (message != NULL, FALSE);
-
-       str = *pstr;
-
-       for (i = 0; i < strlen (str); i++) {
-               const gchar *cur = str + i;
-               if (!g_ascii_strncasecmp (cur, "$", 1)) {
-                       const gchar *end = cur + 1;
-                       gchar *out;
-                       gchar **temp_str;
-                       GSList *list;
-
-                       while (*end && (g_unichar_isalnum (*end) || *end == '_'))
-                               end++;
-
-                       out = g_strndup ((const gchar *) cur, end - cur);
-
-                       temp_str = g_strsplit (str, out, 2);
-
-                       for (list = clues; list; list = g_slist_next (list)) {
-                               gchar **temp = g_strsplit (list->data, "=", 2);
-                               if (!g_ascii_strcasecmp (temp[0], out + 1)) {
-                                       g_free (str);
-                                       str = g_strconcat (temp_str[0], temp[1], temp_str[1], NULL);
-                                       count1 = TRUE;
-                                       string_changed = TRUE;
-                               } else
-                                       count1 = FALSE;
-                               g_strfreev (temp);
-                       }
-
-                       if (!count1) {
-                               if (getenv (out + 1)) {
-                                       g_free (str);
-                                       str = g_strconcat (
-                                               temp_str[0],
-                                               getenv (out + 1),
-                                               temp_str[1], NULL);
-                                       count1 = TRUE;
-                                       string_changed = TRUE;
-                               } else
-                                       count1 = FALSE;
-                       }
-
-                       if (!count1) {
-                               CamelInternetAddress *to;
-                               const gchar *name, *addr;
-
-                               to = camel_mime_message_get_recipients (
-                                       message, CAMEL_RECIPIENT_TYPE_TO);
-                               if (!camel_internet_address_get (to, 0, &name, &addr))
-                                       continue;
-
-                               if (name && g_ascii_strcasecmp ("sender_name", out + 1) == 0) {
-                                       g_free (str);
-                                       str = g_strconcat (temp_str[0], name, temp_str[1], NULL);
-                                       count1 = TRUE;
-                                       string_changed = TRUE;
-                               } else if (addr && g_ascii_strcasecmp ("sender_email", out + 1) == 0) {
-                                       g_free (str);
-                                       str = g_strconcat (temp_str[0], addr, temp_str[1], NULL);
-                                       count1 = TRUE;
-                                       string_changed = TRUE;
-                               }
-                       }
-
-                       g_strfreev (temp_str);
-                       g_free (out);
-               }
-       }
-
-       *pstr = str;
-
-       return string_changed;
-}
-
-static void
-traverse_parts (GSList *clues,
-                CamelMimeMessage *message,
-                CamelDataWrapper *content)
-{
-       g_return_if_fail (message != NULL);
-
-       if (!content)
-               return;
-
-       if (CAMEL_IS_MULTIPART (content)) {
-               guint i, n;
-               CamelMultipart *multipart = CAMEL_MULTIPART (content);
-               CamelMimePart *part;
-
-               n = camel_multipart_get_number (multipart);
-               for (i = 0; i < n; i++) {
-                       part = camel_multipart_get_part (multipart, i);
-                       if (!part)
-                               continue;
-
-                       traverse_parts (clues, message, CAMEL_DATA_WRAPPER (part));
-               }
-       } else if (CAMEL_IS_MIME_PART (content)) {
-               CamelMimePart *part = CAMEL_MIME_PART (content);
-               CamelContentType *type;
-               CamelStream *stream;
-               GByteArray *byte_array;
-               gchar *str;
-
-               content = camel_medium_get_content (CAMEL_MEDIUM (part));
-               if (!content)
-                       return;
-
-               if (CAMEL_IS_MULTIPART (content)) {
-                       traverse_parts (clues, message, CAMEL_DATA_WRAPPER (content));
-                       return;
-               }
-
-               type = camel_mime_part_get_content_type (part);
-               if (!camel_content_type_is (type, "text", "*"))
-                       return;
-
-               byte_array = g_byte_array_new ();
-               stream = camel_stream_mem_new_with_byte_array (byte_array);
-               camel_data_wrapper_decode_to_stream_sync (
-                       content, stream, NULL, NULL);
-
-               str = g_strndup ((gchar *) byte_array->data, byte_array->len);
-               g_object_unref (stream);
-
-               if (replace_variables (clues, message, &str)) {
-                       stream = camel_stream_mem_new_with_buffer (str, strlen (str));
-                       camel_data_wrapper_construct_from_stream_sync (
-                               content, stream, NULL, NULL);
-                       g_object_unref (stream);
-               }
-
-               g_free (str);
-       }
-}
-
 static ESource *
 emcu_ref_identity_source_from_composer (EMsgComposer *composer)
 {
@@ -2352,31 +2203,6 @@ em_utils_edit_message (EMsgComposer *composer,
                folder_is_outbox = FALSE;
                folder_is_templates = FALSE;
        }
-
-       /* Template specific code follows. */
-       if (folder_is_templates) {
-               CamelDataWrapper *content;
-               GSettings *settings;
-               gchar **strv;
-               gint i;
-               GSList *clue_list = NULL;
-
-               settings = e_util_ref_settings ("org.gnome.evolution.plugin.templates");
-
-               /* Get the list from GSettings */
-               strv = g_settings_get_strv (settings, "template-placeholders");
-               for (i = 0; strv[i] != NULL; i++)
-                       clue_list = g_slist_append (clue_list, g_strdup (strv[i]));
-               g_object_unref (settings);
-               g_strfreev (strv);
-
-               content = camel_medium_get_content (CAMEL_MEDIUM (message));
-               traverse_parts (clue_list, message, content);
-
-               g_slist_foreach (clue_list, (GFunc) g_free, NULL);
-               g_slist_free (clue_list);
-       }
-
        if (folder) {
                if ((!folder_is_sent && !folder_is_drafts && !folder_is_outbox && !folder_is_templates) ||
                    (!folder_is_outbox && !folder_is_templates && !emcu_message_references_existing_account 
(message, composer))) {


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