[Xfce4-commits] <postler:master> Refactor display of individual parts as display_part

Christian Dywan noreply at xfce.org
Sun Jun 6 08:20:02 CEST 2010


Updating branch refs/heads/master
         to e5263989fcce2caa72454e53d683fbc1ff763d60 (commit)
       from 740b3d0f5cb1774ac8291ff779b3b383383e8e1c (commit)

commit e5263989fcce2caa72454e53d683fbc1ff763d60
Author: Christian Dywan <christian at twotoasts.de>
Date:   Sun Jun 6 07:39:13 2010 +0200

    Refactor display of individual parts as display_part

 postler/postler-content.vala |  127 ++++++++++++++++++++++++++----------------
 1 files changed, 78 insertions(+), 49 deletions(-)

diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index 2887554..8dc5139 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -20,6 +20,14 @@ public class Postler.Content : WebKit.WebView {
     GLib.StringBuilder[] body = {};
     string[] mime_types = {};
     int body_parts = -1;
+    string content_encoding;
+    string date;
+    string recipient;
+    string carbon_copy;
+    string reply;
+    string sender;
+    string organization;
+    string x_mailer;
 
     public string default_charset { get; set; default = "ISO-8859-1"; }
 
@@ -36,6 +44,14 @@ public class Postler.Content : WebKit.WebView {
     public bool view_source { get; set; }
 
     const string style_sheet = """
+        /* Quotations */
+        blockquote {
+            margin: 1em;
+            padding: 0.5em;
+            white-space: pre;
+            color: GrayText;
+            border-left: 0.1em solid GrayText;
+        }
         /* Addresses not underlined, but underlined when hovering */
         a[href] {
             text-decoration: underline !important;
@@ -246,17 +262,17 @@ public class Postler.Content : WebKit.WebView {
                 return true;
             }
 
-            string content_encoding = "";
+            content_encoding = "";
             string from = _("Unknown");
-            string date = _("(No date)");
+            date = _("(No date)");
             var now = GLib.Date ();
             now.set_time_val (GLib.TimeVal ());
-            string recipient = "";
-            string carbon_copy = "";
+            recipient = "";
+            carbon_copy = "";
             /* TODO: blind_copy, some clients keep it */
-            string reply = "";
-            string organization = "";
-            string x_mailer = "";
+            reply = "";
+            organization = "";
+            x_mailer = "";
             /* Skip the header */
             string previous_line = "";
             while ((line = stream.read_line (null, null)) != null) {
@@ -318,7 +334,7 @@ public class Postler.Content : WebKit.WebView {
             /* FIXME: Use raw subject for argument? */
             /* TODO: Show addressbook icons beside addresses */
             string arguments = "?from=" + recipient + "&subject=Re: " + subject;
-            string sender = linkify_address (from, arguments);
+            sender = linkify_address (from, arguments);
             if (recipient != "")
                 recipient = linkify_address (recipient, arguments);
             if (carbon_copy != "")
@@ -350,13 +366,6 @@ public class Postler.Content : WebKit.WebView {
                 }
             }
 
-            bool plain_text = false;
-            /* We always want HTML, to render the headers nicely */
-            if (mime_type == "text/plain") {
-                mime_type = "text/html";
-                plain_text = true;
-            }
-
             uint multipart = mime_type.has_prefix ("multipart/") ? 1 : 0;
             if (multipart > 0 && boundary != null)
                 boundary = boundary.replace ("\"", " ").strip ();
@@ -378,6 +387,9 @@ public class Postler.Content : WebKit.WebView {
                 mime_types += mime_type;
                 body_parts++;
             }
+
+            bool plain_text = mime_type == "text/plain";
+            bool in_quote = false;
             while ((line = stream.read_line (null, null)) != null) {
                 if (multipart > 0) {
                     if (line.has_prefix ("--")) {
@@ -421,38 +433,58 @@ public class Postler.Content : WebKit.WebView {
                         line = GLib.convert (line, -1, "UTF-8", charset, null);
                 }
                 catch (GLib.ConvertError error) { }
-                /* TODO: Encoding, attachments */
-                /* TODO: Can we parse and localize quoting, such as this?
-                   > Em Quinta-feira 20 Maio 2010, =E0s 17:20:09, Pablo escreveu:
-                   > On 20/05/10 15:53, Will Thompson wrote: */
-                /* Render signature gray, beginning with "--=20" */
-                /* Looks like quoting */
-                if (line.has_prefix (">"))
-                    body[body_parts].append ("<span style=\"color: GrayText\">");
-                string appendage;
-                if (plain_text && content_encoding != "base64")
-                    appendage = line + "\n";
-                else
-                    appendage = line;
-                body[body_parts].append (appendage);
-                if (line.has_prefix (">"))
-                    body[body_parts].append ("</span>");
-                }
-
-            string body_chunk;
-            if (multipart == 2) {
-                body_chunk = body[0].str;
-                mime_type = mime_types[0];
-                if (mime_type == "text/plain") {
-                    mime_type = "text/html";
-                    plain_text = true;
+                if (plain_text) {
+                    /* TODO: Render signature gray, beginning with "--=20" */
+                    /* Looks like quoting */
+                    if (in_quote && !line.has_prefix (">")) {
+                        body[body_parts].append ("</blockquote>");
+                        in_quote = false;
+                    }
+                    else if (!in_quote && line.has_prefix (">")) {
+                        body[body_parts].append ("<blockquote>");
+                        in_quote = true;
+                    }
+                    if (in_quote && line[0] == '>' && line[1] == ' ')
+                        line = line.substring (2);
+                    else if (in_quote && line[0] == '>' && line[1] == '\0')
+                        line = line.substring (1);
                 }
+                body[body_parts].append (line);
+                if (plain_text && content_encoding != "base64")
+                    body[body_parts].append_c ('\n');
             }
-            else if (multipart > 0)
-                body_chunk = content_type;
-            else
-                body_chunk = body[0].str;
 
+            display_part (0);
+        } catch (GLib.Error contents_error) {
+            load_string ("""
+                <title>%s</title>
+                <body>
+                <h1>%s</h1>
+                <p>%s</p>
+                </body>
+                """.
+                printf (_("Error"), _("Error"),
+                        contents_error.message),
+                "text/html", "UTF-8", "about:blank");
+            GLib.critical (_("Failed to read message \"%s\": %s"),
+                contents.get_path (), contents_error.message);
+        }
+        notify_property ("n-parts");
+        return false;
+    }
+
+    public void display_part (uint part)
+        requires (part < n_parts) {
+        string body_chunk = body[part].str;
+        string mime_type = mime_types[part];
+        bool plain_text = false;
+        if (mime_type == "text/plain") {
+            mime_type = "text/html";
+            plain_text = true;
+            body_chunk = "<pre>" + body_chunk + "</pre>";
+        }
+
+        try {
             /* Linkify */
             if (plain_text) {
                 foreach (var link_format in link_formats) {
@@ -460,7 +492,6 @@ public class Postler.Content : WebKit.WebView {
                     body_chunk = regex.replace (body_chunk, -1, 0,
                         "<a href=\"\\1\">\\1</a>");
                 }
-                body_chunk = body_chunk.replace ("\n", "<br>");
             }
 
             /* Emoticons */
@@ -510,11 +541,9 @@ public class Postler.Content : WebKit.WebView {
                 printf (_("Error"), _("Error"),
                         contents_error.message),
                 "text/html", "UTF-8", "about:blank");
-            GLib.critical (_("Failed to read message \"%s\": %s"),
-                contents.get_path (), contents_error.message);
+            GLib.critical (_("Failed to display message part \"%s\": %s"),
+                mime_type, contents_error.message);
         }
-        notify_property ("n-parts");
-        return false;
     }
 
     /* TODO: resource request, block external images */



More information about the Xfce4-commits mailing list