[geary: 5/8] engine/client: move email quoting functions from engine to client



commit d6e71e7ca3ab296d480e32da2e66237208183a65
Author: Konstantin Kharlamov <Hi-Angel yandex ru>
Date:   Sun Mar 10 02:52:33 2019 +0300

    engine/client: move email quoting functions from engine to client
    
    Signed-off-by: Konstantin Kharlamov <Hi-Angel yandex ru>

 src/client/composer/composer-widget.vala |   6 +-
 src/client/util/util-email.vala          | 127 ++++++++++++++++++++++++++++++
 src/engine/rfc822/rfc822-utils.vala      | 129 -------------------------------
 3 files changed, 130 insertions(+), 132 deletions(-)
---
diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala
index cb6d9506..89298a4c 100644
--- a/src/client/composer/composer-widget.vala
+++ b/src/client/composer/composer-widget.vala
@@ -776,7 +776,7 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
             case ComposeType.REPLY_ALL:
                 this.subject = reply_subject;
                 this.references = Geary.RFC822.Utils.reply_references(referred);
-                referred_quote = Geary.RFC822.Utils.quote_email_for_reply(referred, quote,
+                referred_quote = Util.Email.quote_email_for_reply(referred, quote,
                     Geary.RFC822.TextFormat.HTML);
                 if (!Geary.String.is_empty(quote)) {
                     this.top_posting = false;
@@ -787,7 +787,7 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
 
             case ComposeType.FORWARD:
                 this.subject = forward_subject;
-                referred_quote = Geary.RFC822.Utils.quote_email_for_forward(referred, quote,
+                referred_quote = Util.Email.quote_email_for_forward(referred, quote,
                     Geary.RFC822.TextFormat.HTML);
             break;
         }
@@ -1011,7 +1011,7 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
             this.last_quote = quote;
             // Always use reply styling, since forward styling doesn't work for inline quotes
             this.editor.insert_html(
-                Geary.RFC822.Utils.quote_email_for_reply(referred, quote, Geary.RFC822.TextFormat.HTML)
+                Util.Email.quote_email_for_reply(referred, quote, Geary.RFC822.TextFormat.HTML)
             );
 
             if (!referred_ids.contains(referred.id)) {
diff --git a/src/client/util/util-email.vala b/src/client/util/util-email.vala
index 55bda5a1..755c7522 100644
--- a/src/client/util/util-email.vala
+++ b/src/client/util/util-email.vala
@@ -133,4 +133,131 @@ namespace Util.Email {
         ).printf(first_recipient, mailboxes.size - 1);
     }
 
+    /**
+     * Returns a quoted text string needed for a reply.
+     *
+     * If there's no message body in the supplied email or quote text, this
+     * function will return the empty string.
+     *
+     * If html_format is true, the message will be quoted in HTML format.
+     * Otherwise it will be in plain text.
+     */
+    public string quote_email_for_reply(Geary.Email email, string? quote, Geary.RFC822.TextFormat format) {
+        if (email.body == null && quote == null)
+            return "";
+
+        string quoted = "";
+
+        /// Format for the datetime that a message being replied to was received
+        /// See http://developer.gnome.org/glib/2.32/glib-GDateTime.html#g-date-time-format
+        string DATE_FORMAT = _("%a, %b %-e, %Y at %-l:%M %p");
+
+        if (email.date != null && email.from != null) {
+            /// The quoted header for a message being replied to.
+            /// %1$s will be substituted for the date, and %2$s will be substituted for
+            /// the original sender.
+            string QUOTED_LABEL = _("On %1$s, %2$s wrote:");
+            quoted += QUOTED_LABEL.printf(email.date.value.format(DATE_FORMAT),
+                                          Geary.RFC822.Utils.email_addresses_for_reply(email.from, format));
+
+        } else if (email.from != null) {
+            /// The quoted header for a message being replied to (in case the date is not known).
+            /// %s will be replaced by the original sender.
+            string QUOTED_LABEL = _("%s wrote:");
+            quoted += QUOTED_LABEL.printf(Geary.RFC822.Utils.email_addresses_for_reply(email.from, format));
+
+        } else if (email.date != null) {
+            /// The quoted header for a message being replied to (in case the sender is not known).
+            /// %s will be replaced by the original date
+            string QUOTED_LABEL = _("On %s:");
+            quoted += QUOTED_LABEL.printf(email.date.value.format(DATE_FORMAT));
+        }
+
+        quoted += "<br />";
+        try {
+            quoted += quote_body(email, quote, true, format);
+        } catch (Error err) {
+            debug("Failed to quote body for replying: %s".printf(err.message));
+        }
+
+        return quoted;
+    }
+
+    /**
+     * Returns a quoted text string needed for a forward.
+     *
+     * If there's no message body in the supplied email or quote text, this
+     * function will return the empty string.
+     *
+     * If html_format is true, the message will be quoted in HTML format.
+     * Otherwise it will be in plain text.
+     */
+    public string quote_email_for_forward(Geary.Email email, string? quote, Geary.RFC822.TextFormat format) {
+        if (email.body == null && quote == null)
+            return "";
+
+        const string HEADER_FORMAT = "%s %s\n";
+
+        string quoted = _("---------- Forwarded message ----------");
+        quoted += "\n";
+        string from_line = Geary.RFC822.Utils.email_addresses_for_reply(email.from, format);
+        if (!Geary.String.is_empty_or_whitespace(from_line)) {
+            // Translators: Human-readable version of the RFC 822 From header
+            quoted += HEADER_FORMAT.printf(_("From:"), from_line);
+        }
+        // Translators: Human-readable version of the RFC 822 Subject header
+        quoted += HEADER_FORMAT.printf(_("Subject:"), email.subject != null ? email.subject.to_string() : 
"");
+        // Translators: Human-readable version of the RFC 822 Date header
+        quoted += HEADER_FORMAT.printf(_("Date:"), email.date != null ? email.date.to_string() : "");
+        string to_line = Geary.RFC822.Utils.email_addresses_for_reply(email.to, format);
+        if (!Geary.String.is_empty_or_whitespace(to_line)) {
+            // Translators: Human-readable version of the RFC 822 To header
+            quoted += HEADER_FORMAT.printf(_("To:"), to_line);
+        }
+        string cc_line = Geary.RFC822.Utils.email_addresses_for_reply(email.cc, format);
+        if (!Geary.String.is_empty_or_whitespace(cc_line)) {
+            // Translators: Human-readable version of the RFC 822 CC header
+            quoted += HEADER_FORMAT.printf(_("Cc:"), cc_line);
+        }
+        quoted += "\n";  // A blank line between headers and body
+        quoted = quoted.replace("\n", "<br />");
+        try {
+            quoted += quote_body(email, quote, false, format);
+        } catch (Error err) {
+            debug("Failed to quote body for forwarding: %s".printf(err.message));
+        }
+        return quoted;
+    }
+
+    private string quote_body(Geary.Email email,
+                              string? html_quote,
+                              bool use_quotes,
+                              Geary.RFC822.TextFormat format)
+        throws Error {
+        Geary.RFC822.Message? message = email.get_message();
+        string? body_text = null;
+        if (Geary.String.is_empty(html_quote)) {
+            switch (format) {
+                case Geary.RFC822.TextFormat.HTML:
+                    body_text = message.has_html_body()
+                        ? message.get_html_body(null)
+                        : message.get_plain_body(true, null);
+                    break;
+
+                case Geary.RFC822.TextFormat.PLAIN:
+                    body_text = message.has_plain_body()
+                        ? message.get_plain_body(true, null)
+                        : message.get_html_body(null);
+                    break;
+            }
+        } else {
+            body_text = html_quote;
+        }
+
+        // Wrap the whole thing in a blockquote.
+        if (use_quotes && !Geary.String.is_empty(body_text))
+            body_text = "<blockquote type=\"cite\">%s</blockquote>".printf(body_text);
+
+        return body_text;
+    }
 }
diff --git a/src/engine/rfc822/rfc822-utils.vala b/src/engine/rfc822/rfc822-utils.vala
index fc5cdb6b..a8fbee0e 100644
--- a/src/engine/rfc822/rfc822-utils.vala
+++ b/src/engine/rfc822/rfc822-utils.vala
@@ -216,134 +216,6 @@ public string email_addresses_for_reply(Geary.RFC822.MailboxAddresses? addresses
 }
 
 
-/**
- * Returns a quoted text string needed for a reply.
- *
- * If there's no message body in the supplied email or quote text, this
- * function will return the empty string.
- *
- * If html_format is true, the message will be quoted in HTML format.
- * Otherwise it will be in plain text.
- */
-public string quote_email_for_reply(Geary.Email email, string? quote, TextFormat format) {
-    if (email.body == null && quote == null)
-        return "";
-
-    string quoted = "";
-
-    /// Format for the datetime that a message being replied to was received
-    /// See http://developer.gnome.org/glib/2.32/glib-GDateTime.html#g-date-time-format
-    string DATE_FORMAT = _("%a, %b %-e, %Y at %-l:%M %p");
-
-    if (email.date != null && email.from != null) {
-        /// The quoted header for a message being replied to.
-        /// %1$s will be substituted for the date, and %2$s will be substituted for
-        /// the original sender.
-        string QUOTED_LABEL = _("On %1$s, %2$s wrote:");
-        quoted += QUOTED_LABEL.printf(email.date.value.format(DATE_FORMAT),
-                                      email_addresses_for_reply(email.from, format));
-
-    } else if (email.from != null) {
-        /// The quoted header for a message being replied to (in case the date is not known).
-        /// %s will be replaced by the original sender.
-        string QUOTED_LABEL = _("%s wrote:");
-        quoted += QUOTED_LABEL.printf(email_addresses_for_reply(email.from, format));
-
-    } else if (email.date != null) {
-        /// The quoted header for a message being replied to (in case the sender is not known).
-        /// %s will be replaced by the original date
-        string QUOTED_LABEL = _("On %s:");
-        quoted += QUOTED_LABEL.printf(email.date.value.format(DATE_FORMAT));
-    }
-
-    quoted += "<br />";
-    try {
-        quoted += quote_body(email, quote, true, format);
-    } catch (Error err) {
-        debug("Failed to quote body for replying: %s".printf(err.message));
-    }
-
-    return quoted;
-}
-
-/**
- * Returns a quoted text string needed for a forward.
- *
- * If there's no message body in the supplied email or quote text, this
- * function will return the empty string.
- *
- * If html_format is true, the message will be quoted in HTML format.
- * Otherwise it will be in plain text.
- */
-public string quote_email_for_forward(Geary.Email email, string? quote, TextFormat format) {
-    if (email.body == null && quote == null)
-        return "";
-
-    const string HEADER_FORMAT = "%s %s\n";
-
-    string quoted = _("---------- Forwarded message ----------");
-    quoted += "\n";
-    string from_line = email_addresses_for_reply(email.from, format);
-    if (!String.is_empty_or_whitespace(from_line)) {
-        // Translators: Human-readable version of the RFC 822 From header
-        quoted += HEADER_FORMAT.printf(_("From:"), from_line);
-    }
-    // Translators: Human-readable version of the RFC 822 Subject header
-    quoted += HEADER_FORMAT.printf(_("Subject:"), email.subject != null ? email.subject.to_string() : "");
-    // Translators: Human-readable version of the RFC 822 Date header
-    quoted += HEADER_FORMAT.printf(_("Date:"), email.date != null ? email.date.to_string() : "");
-    string to_line = email_addresses_for_reply(email.to, format);
-    if (!String.is_empty_or_whitespace(to_line)) {
-        // Translators: Human-readable version of the RFC 822 To header
-        quoted += HEADER_FORMAT.printf(_("To:"), to_line);
-    }
-    string cc_line = email_addresses_for_reply(email.cc, format);
-    if (!String.is_empty_or_whitespace(cc_line)) {
-        // Translators: Human-readable version of the RFC 822 CC header
-        quoted += HEADER_FORMAT.printf(_("Cc:"), cc_line);
-    }
-    quoted += "\n";  // A blank line between headers and body
-    quoted = quoted.replace("\n", "<br />");
-    try {
-        quoted += quote_body(email, quote, false, format);
-    } catch (Error err) {
-        debug("Failed to quote body for forwarding: %s".printf(err.message));
-    }
-    return quoted;
-}
-
-private string quote_body(Geary.Email email,
-                          string? html_quote,
-                          bool use_quotes,
-                          TextFormat format)
-    throws Error {
-    Message? message = email.get_message();
-    string? body_text = null;
-    if (String.is_empty(html_quote)) {
-        switch (format) {
-        case TextFormat.HTML:
-            body_text = message.has_html_body()
-            ? message.get_html_body(null)
-            : message.get_plain_body(true, null);
-            break;
-
-        case TextFormat.PLAIN:
-            body_text = message.has_plain_body()
-            ? message.get_plain_body(true, null)
-            : message.get_html_body(null);
-            break;
-        }
-    } else {
-        body_text = html_quote;
-    }
-
-    // Wrap the whole thing in a blockquote.
-    if (use_quotes && !String.is_empty(body_text))
-        body_text = "<blockquote type=\"cite\">%s</blockquote>".printf(body_text);
-
-    return body_text;
-}
-
 public bool comp_char_arr_slice(char[] array, uint start, string comp) {
     for (int i = 0; i < comp.length; i++) {
         if (array[start + i] != comp[i])
@@ -446,4 +318,3 @@ public GMime.ContentEncoding get_best_encoding(GMime.Stream in_stream) {
 }
 
 }
-


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