[Xfce4-commits] <postler:master> Move logic from parse_message to Message.from_file

Christian Dywan noreply at xfce.org
Fri May 27 20:32:07 CEST 2011


Updating branch refs/heads/master
         to 8ed3c62f8b01d5fe75b4afc443ea3a43894a347e (commit)
       from 0ebe7a4d493ee7b863138ae4360a0f0d34b15ba8 (commit)

commit 8ed3c62f8b01d5fe75b4afc443ea3a43894a347e
Author: Christian Dywan <christian at twotoasts.de>
Date:   Wed May 25 21:38:05 2011 +0200

    Move logic from parse_message to Message.from_file

 postler/postler-content.vala |  197 +++++++++--------------------------------
 postler/postler-message.vala |   46 +++++++++-
 2 files changed, 85 insertions(+), 158 deletions(-)

diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index 0797765..83ec1f2 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -77,27 +77,23 @@ struct Postler.EmoticonMapping {
 
 public class Postler.Content : WebKit.WebView {
     AccountInfo? selected_account;
+    Message message;
     string charset;
     string content_encoding;
-    string date;
     string carbon_copy;
     string blind_copy;
     string reply;
-    string organization;
-    string x_mailer;
 
-    public string default_charset { get; set; default = "ISO-8859-1"; }
     string? save_folder = null;
 
     public string? last_location { get; set; }
     public string? message_id { get; set; }
     public string? sender { get; set; }
-    public string? recipient { get; set; }
     public string? subject { get; set; }
     public string? reply_to { get; set; }
     public string? reply_to_all { get; set; }
-    public string list_post { get; set; }
-    public string list_unsubscribe { get; set; }
+    public string? list_post { get; set; }
+    public string? list_unsubscribe { get; set; }
 
     public unowned List<MessagePart> message_parts { public get; set; }
     public MessagePart? current_part { get; private set; }
@@ -438,8 +434,8 @@ public class Postler.Content : WebKit.WebView {
         return Postler.Messages.parse_encoded (quoted, out charset);
     }
 
-    string format_header (string header, string data) {
-        if (data != "")
+    string format_header (string header, string? data) {
+        if (data != null)
             return "<b>%s</b> %s<br>".printf (header, data);
         return "";
     }
@@ -681,6 +677,7 @@ public class Postler.Content : WebKit.WebView {
 
     public string? choose_from () {
         /* See if recipient is among the accounts, otherwise pick a fallback */
+        string? recipient = message.get_field ("to");
         if (recipient != null) {
             string from = Postler.Messages.parse_address (recipient)[1];
             var accounts = new Accounts ();
@@ -694,146 +691,39 @@ public class Postler.Content : WebKit.WebView {
         return selected_account.address.split (",")[0];
     }
 
-    void parse_message (string location) throws GLib.FileError {
+    void parse_message (string location) throws GLib.Error {
         last_location = location;
-        charset = null;
-        subject = _("(No subject)");
+        message = new Message.from_file (File.new_for_path (location));
+        message_id = message.id;
+        subject = message.subject;
+        charset = message.get_charset ();
+        string arguments = "?subject=Re: " + subject;
+        string? chosen_from = choose_from ();
+        if (chosen_from != null)
+            arguments += "?from=" + html_escape (chosen_from);
+        sender = linkify_address (message.sender ?? _("Unknown"), arguments);
+        carbon_copy = message.get_field ("cc");
+        if (carbon_copy != null)
+            carbon_copy = linkify_address (carbon_copy, arguments);
+        blind_copy = message.get_field ("bcc");
+        if (blind_copy != null)
+            blind_copy = linkify_address (blind_copy, arguments);
+        reply = message.reply_to;
+        if (reply != null)
+            reply = linkify_address (reply, arguments);
+        reply_to_all = message.recipients;
+        list_post = message.get_field ("list-post");
+        if (list_post != null)
+            list_post = Postler.Messages.parse_address (list_post)[1];
+        list_unsubscribe = message.get_field ("list-unsubscribe");
+        if (list_unsubscribe != null)
+            list_unsubscribe = Postler.Messages.parse_address (list_unsubscribe)[1];
+        if (list_unsubscribe != null)
+            list_unsubscribe = linkify_address (list_unsubscribe, null);
 
         message_parts = new List<MessagePart> ();
-        var contents = File.new_for_path (location);
-        try {
-            var stream = new DataInputStream (contents.read (null));
-            string line;
-            string content_type = null;
-            string[] parts;
-
-            message_id = null;
-            content_encoding = "";
-            string from = _("Unknown");
-            date = _("(No date)");
-            recipient = "";
-            carbon_copy = "";
-            blind_copy = "";
-            reply = "";
-            list_post = "";
-            list_unsubscribe = "";
-            organization = "";
-            x_mailer = "";
-            string previous_line = "";
-            while ((line = stream.read_line (null, null)) != null) {
-                if (line == "")
-                    break;
-                if (line[0] == '\t' || line[0] == ' ')
-                    line = previous_line + " " + line.chug ();
-                previous_line = line;
-
-                parts = line.split (":", 2);
-                if (parts == null || parts[0] == null)
-                    continue;
-
-                string field = ascii_strdown (parts[0]);
-                if (field == "content-type")
-                    content_type = parts[1].strip ();
-                else if (field == "content-transfer-encoding")
-                    content_encoding = parts[1].strip ();
-                else if (field == "message-id")
-                    message_id = parts[1].strip ();
-                else if (field == "subject")
-                    subject = parse_encoded (parts[1], out charset);
-                else if (field == "from") {
-                    string from_charset = null;
-                    from = parse_encoded (parts[1], out from_charset);
-                }
-                else if (field == "date") {
-                    date = format_date (date_from_string (parts[1]));
-                }
-                else if (field == "to")
-                    recipient = parts[1].strip ();
-                else if (field == "cc") {
-                    string cc_charset = null;
-                    carbon_copy = parse_encoded (parts[1], out cc_charset);
-                }
-                else if (field == "bcc") {
-                    string bcc_charset = null;
-                    blind_copy = parse_encoded (parts[1], out bcc_charset);
-                }
-                else if (field == "reply-to") {
-                    string reply_charset = null;
-                    reply = parse_encoded (parts[1], out reply_charset);
-                }
-                else if (field == "list-post") {
-                    string list_post_charset = null;
-                    list_post = parse_encoded (parts[1], out list_post_charset);
-                    list_post = Postler.Messages.parse_address (list_post)[1];
-                }
-                else if (field == "list-unsubscribe") {
-                    string list_unsubscribe_charset = null;
-                    list_unsubscribe = parse_encoded (parts[1], out list_unsubscribe_charset);
-                    list_unsubscribe = Postler.Messages.parse_address (list_unsubscribe)[1];
-                }
-                else if (field == "organization")
-                    organization = parts[1];
-                else if (field == "x-mailer" || field == "user-agent")
-                    x_mailer = format_x_mailer (parts[1]);
-            }
-
-            if (charset == null)
-                charset = default_charset;
-
-            /* Some mailing list systems override Reply-To so that it doesn't
-               point to the author but the list.
-               Also Reply-To may equal From, which is at best confusing. */
-            if (reply != "") {
-                string canonical = Postler.Messages.parse_address (reply)[1];
-                if (canonical == Postler.Messages.parse_address (recipient)[1])
-                    reply = "";
-                else if (list_post != ""
-                  && canonical == list_post)
-                    reply = "";
-                else if (canonical == Postler.Messages.parse_address (from)[1])
-                    reply = "";
-            }
-
-            reply_to = reply != "" ? reply : from;
-            if ("," in recipient && carbon_copy != "")
-                reply_to_all = reply_to + "," + recipient + "," + carbon_copy;
-            else if ("," in recipient)
-                reply_to_all = reply_to + "," + recipient;
-            else if (carbon_copy != "")
-                reply_to_all = reply_to + "," + carbon_copy;
-            else
-                reply_to_all = null;
-
-            /* Linkify From, To and Reply-To */
-            /* FIXME: Use raw subject for argument? */
-            /* TODO: Show addressbook icons beside addresses */
-            string arguments = "?subject=Re: " + subject;
-            string? chosen_from = choose_from ();
-            if (chosen_from != null)
-                arguments += "?from=" + html_escape (chosen_from);
-            sender = linkify_address (from, arguments);
-            if (recipient != "") {
-                /* Show recipient only if isn't unique */
-                if ("," in recipient || "," in selected_address ()
-                                     || !recipient.contains (selected_address ()))
-                    recipient = linkify_address (recipient, arguments);
-                else
-                    recipient = "";
-            }
-            if (carbon_copy != "")
-                carbon_copy = linkify_address (carbon_copy, arguments);
-            if (blind_copy != "")
-                blind_copy = linkify_address (blind_copy, arguments);
-            if (reply != "")
-                reply = linkify_address (reply, arguments);
-            if (list_unsubscribe != "")
-                list_unsubscribe = linkify_address (list_unsubscribe, null);
-
-            parse_body (stream, content_type);
-        } catch (GLib.Error contents_error) {
-            throw new GLib.FileError.FAILED (_("Failed to read message: %s").
-                                             printf (contents_error.message));
-        }
+        content_encoding = message.get_field ("content-transfer-encoding");
+        parse_body (message.get_stream (), message.get_field ("content-type"));
         notify_property ("message-parts");
     }
 
@@ -1174,20 +1064,21 @@ public class Postler.Content : WebKit.WebView {
                 <p class="body" style="%s">%s</p>
                 """.
                 printf (themed_style_sheet (),
-                        date,
+                        format_date (message.date),
                         _("From:"), sender,
-                        format_header (_("To:"), recipient),
+                        format_header (_("To:"), message.get_field ("to")),
                         format_header (_("Copy:"), carbon_copy),
                         format_header (_("Blind Copy:"), blind_copy),
-                        reply != "" || organization != "" || x_mailer != ""
-                                    || list_unsubscribe != ""
-                                    || html_or_text != ""
+                        reply != null || message.organization != null
+                                      || message.application != null
+                                      || list_unsubscribe != null
+                                      || html_or_text != ""
                             ? "%s »".printf (_("More")) : "",
                         _("Subject:"), subject,
                         /* TODO: Sender:? */
                         format_header (_("Reply To:"), reply),
-                        format_header (_("Organization:"), organization),
-                        format_header (_("Application:"), x_mailer),
+                        format_header (_("Organization:"), message.organization),
+                        format_header (_("Application:"), message.application),
                         format_header (_("Unsubscribe:"), list_unsubscribe),
                         html_or_text,
                         message_part.plain_text ? "font-family: Monospace;" : "",
diff --git a/postler/postler-message.vala b/postler/postler-message.vala
index 9c27a90..9d0985c 100644
--- a/postler/postler-message.vala
+++ b/postler/postler-message.vala
@@ -14,15 +14,23 @@ namespace Postler {
         public string uri { public get; set; } /* TODO: uri without status flags */
         public string get_path () { return GLib.File.new_for_uri (uri).get_path (); }
         public string? id { public get; set; }
-        string? charset;
+        string? charset = null;
+        public string get_charset () { return charset ?? "ISO-8859-1"; }
         public GLib.DateTime? date { public get; set; }
         public int64 get_timestamp () { return date != null ? date.to_unix () : 0; }
         public string? subject { public get; set; }
         public string? sender { public get; set; }
         public string? recipients { public get; set; }
+        public string? reply_to { public get; set; }
         public bool unread { public get; set; } /* TODO: should be writable */
         public bool flagged { public get; set; } /* TODO: should be writable */
         public bool priority { public get; set; }
+        public string organization { public get; set; }
+        public string application { public get; set; }
+        GLib.HashTable<string,string> fields = new GLib.HashTable<string,string> (str_hash, str_equal);
+        public string get_field (string field) { return fields.lookup (field); }
+        GLib.DataInputStream? stream = null;
+        public GLib.DataInputStream get_stream () { return stream; }
 
         bool init (GLib.Cancellable? cancellable = null) throws GLib.Error {
             return true;
@@ -88,7 +96,7 @@ namespace Postler {
             uri = file.get_uri ();
             read_flags (file.get_path ());
 
-            var stream = new DataInputStream (file.read (cancellable));
+            stream = new DataInputStream (file.read (cancellable));
             string line;
             string previous_line = "";
             while ((line = stream.read_line (null, cancellable)) != null) {
@@ -113,16 +121,44 @@ namespace Postler {
                 }
                 else if (field == "date") {
                     date = Postler.Content.date_from_string (parts[1]);}
-                else if (field == "to" || field == "cc" || field == "bcc")
+                else if (field == "to" || field == "cc" || field == "bcc") {
                     recipients = (recipients ?? "") + parts[1].strip () + ",";
+                    string field_charset = null;
+                    fields.insert (field, parse_encoded (parts[1], out field_charset));
+                }
                 else if (field == "x-priority" || field == "importance")
                     priority = parts[1][0] == '1' || parts[1][0] == '2'
                             || parts[1] == "High";
+                else if (field == "organization")
+                    organization = parts[1];
+                else if (field == "x-mailer" || field == "user-agent")
+                    application = Postler.Content.format_x_mailer (parts[1]);
+                else if (field == "reply-to") {
+                    string field_charset = null;
+                    reply_to = parse_encoded (parts[1], out field_charset);
+                }
+                else if (field == "list-post" || field == "list-unsubscribe") {
+                    string field_charset = null;
+                    fields.insert (field, parse_encoded (parts[1], out field_charset));
+                }
+                else if (field == "content-type" || field == "content-transfer-encoding")
+                    fields.insert (field, parts[1]);
             }
 
-            if (charset == null)
-                charset = "ISO-8859-1";
             recipients = parse_encoded (recipients, out charset);
+
+            /* Some mailing list systems override Reply-To so that it doesn't
+               point to the author but the list.
+               Also Reply-To may equal From, which is at best confusing. */
+            if (reply_to != null) {
+                string canonical = Postler.Messages.parse_address (reply_to)[1];
+                if (canonical == Postler.Messages.parse_address (get_field ("to"))[1])
+                    reply_to = null;
+                else if (get_field ("list-post") != null && canonical == get_field ("list-post"))
+                    reply_to = null;
+                else if (canonical == Postler.Messages.parse_address (sender)[1])
+                    reply_to = null;
+            }
         }
     }
 }



More information about the Xfce4-commits mailing list