[Xfce4-commits] <postler:master> Refactor message reading into a Message struct

Christian Dywan noreply at xfce.org
Mon Nov 15 20:20:01 CET 2010


Updating branch refs/heads/master
         to 46c78e550e41d3f6242576049bd512f3a85133cb (commit)
       from 30aeb97f933e783919ce9a7161d8e650f21265af (commit)

commit 46c78e550e41d3f6242576049bd512f3a85133cb
Author: Christian Dywan <christian at twotoasts.de>
Date:   Mon Nov 15 20:12:45 2010 +0100

    Refactor message reading into a Message struct
    
    The read_message() method checks if the message shouldn't be
    visible and returns null in that case, otherwise it fills
    a struct with meta data.

 postler/postler-messages.vala |  249 +++++++++++++++++++++++------------------
 1 files changed, 139 insertions(+), 110 deletions(-)

diff --git a/postler/postler-messages.vala b/postler/postler-messages.vala
index a1a04ea..61abe33 100644
--- a/postler/postler-messages.vala
+++ b/postler/postler-messages.vala
@@ -21,6 +21,7 @@ public class Postler.Messages : Gtk.TreeView {
     public string? selected_location { get; set; }
     public bool newest_at_the_bottom { get; set; default = false; }
 
+    string to_or_from;
     string last_location;
     string last_filter = "";
 
@@ -466,6 +467,127 @@ public class Postler.Messages : Gtk.TreeView {
         populate (last_location, filter.down (), header);
     }
 
+    private struct Message {
+        string location;
+        string subject;
+        public string get_subject () {
+            return subject != null ? subject : _("No subject");
+        }
+        string status;
+        int font_weight;
+        bool flagged;
+        string from;
+        public string get_from () {
+            return from != null ? from : _("Unknown");
+        }
+        time_t timestamp;
+        int64 size;
+        string attachment;
+    }
+
+    private Message? read_message (File contents, bool folder_new,
+        string[] headers, string[] filters) {
+
+        string filename = contents.get_basename ();
+        if (filename[0] == '.')
+            return null;
+
+        string status = STOCK_MAIL_UNREAD;
+        int font_weight = Pango.Weight.BOLD;
+        string flags = null;
+        if (!folder_new) {
+            status = parse_flags (filename, out flags, out font_weight);
+            if (hide_read && font_weight != Pango.Weight.BOLD && flags == null)
+                return null;
+            if (flags != null && flags[0] == 'T')
+                return null;
+        }
+
+        var message = new Message ();
+        message.status = status;
+        message.font_weight = font_weight;
+        message.flagged = flags != null;
+        message.location = contents.get_path ();
+        message.subject = null;
+        message.from = null;
+        message.timestamp = 0;
+
+        string content_type = show_attachments ? null : "";
+        try {
+            var stream = new DataInputStream (contents.read (null));
+            string line;
+            while ((line = stream.read_line (null, null)) != null) {
+                if (line == "")
+                    break;
+                string[] parts = line.split (":", 2);
+                if (parts == null || parts[0] == null)
+                    continue;
+                string field = ascii_strdown (parts[0]);
+                if (filters[0] != null && parts[1] != null) {
+                    string lowercased = ascii_strdown (parts[1]);
+                    if (headers[0] == field
+                     && !(filters[0] in lowercased))
+                        return null;
+                    else if (filters[1] != null && headers[1] == field
+                          && !(filters[1] in lowercased))
+                        return null;
+                }
+                if (field == "subject") {
+                    message.subject = parts[1].strip ();
+                    if (message.from != null && message.timestamp != 0
+                     && content_type != null && filters[0] == null)
+                        break;
+                }
+                else if (field == to_or_from) {
+                    message.from = parts[1];
+                    if (message.subject != null && message.timestamp != 0
+                     && content_type != null && filters[0] == null)
+                        break;
+                    }
+                else if (field == "date") {
+                    var parsed = new Soup.Date.from_string (parts[1]);
+                    message.timestamp = parsed != null ? (time_t)parsed.to_time_t () : 0;
+                    if (message.subject != null && message.from != null
+                     && content_type != null && filters[0] == null)
+                        break;
+                }
+                else if (field == "content-type") {
+                    content_type = parts[1].strip ();
+                    if (message.subject != null && message.from != null
+                     && message.timestamp != 0 && filters[0] == null)
+                        break;
+                }
+            }
+
+            unowned string? fulltext = null;
+            if (headers[0] == "fulltext")
+                fulltext = filters[0];
+            else if (headers[1] == "fulltext")
+                fulltext = filters[1];
+            if (fulltext != null) {
+                bool skip = true;
+                while ((line = stream.read_line (null, null)) != null) {
+                    if (line.down ().contains (fulltext)) {
+                        skip = false;
+                        break;
+                    }
+                }
+                if (skip)
+                    return null;
+            }
+        } catch (GLib.Error contents_error) {
+            GLib.critical (_("Failed to read message \"%s\": %s"),
+                contents.get_path (), contents_error.message);
+        }
+
+        message.attachment = null;
+        if (show_attachments
+         && content_type != null
+         && content_type.has_prefix ("multipart/mixed"))
+            message.attachment = STOCK_MAIL_ATTACHMENT;
+        return message;
+    }
+
     public bool populate (string? location, string filter="", string header="subject") {
         clear ();
         if (location == null)
@@ -481,7 +603,6 @@ public class Postler.Messages : Gtk.TreeView {
         now.set_time_val (GLib.TimeVal ());
 
         string basename = Path.get_basename (location);
-        string to_or_from;
         if (basename == "Sent" || basename == "Queue" || basename == "Drafts")
             to_or_from = "to";
         else
@@ -519,117 +640,25 @@ public class Postler.Messages : Gtk.TreeView {
                 folder = folder_dir.get_basename ();
                 bool folder_new = folder_dir.get_path ().has_suffix ("new");
                 while ((info = folder_enumerator.next_file (null)) != null) {
-                    Gtk.TreeIter account_iter;
                     unowned string name = info.get_name ();
-                    if (name[0] == '.')
-                        continue;
-
-                    string status = STOCK_MAIL_UNREAD;
-                    int font_weight = Pango.Weight.BOLD;
-                    string flagged = null;
-                    if (!folder_new) {
-                        status = parse_flags (name, out flagged, out font_weight);
-                        if (hide_read && font_weight != Pango.Weight.BOLD && flagged == null)
-                            continue;
-                        if (flagged != null && flagged[0] == 'T')
-                            continue;
-                    }
 
-                    string subject = null;
-                    string from = null;
-                    time_t timestamp = 0;
-                    string content_type = show_attachments ? null : "";
-                    var contents = folder_dir.resolve_relative_path (name);
-                    try {
-                        var stream = new DataInputStream (contents.read (null));
-                        bool skip = false;
-                        string line;
-                        while ((line = stream.read_line (null, null)) != null) {
-                            if (line == "")
-                                break;
-                            string[] parts = line.split (":", 2);
-                            if (parts == null || parts[0] == null)
-                                continue;
-                            string field = ascii_strdown (parts[0]);
-                            if (filters[0] != null && parts[1] != null) {
-                                string lowercased = ascii_strdown (parts[1]);
-                                if (headers[0] == field
-                                 && !(filters[0] in lowercased)) {
-                                    skip = true;
-                                    break;
-                                }
-                                else if (filters[1] != null && headers[1] == field
-                                 && !(filters[1] in lowercased)) {
-                                    skip = true;
-                                    break;
-                                }
-                            }
-                            if (field == "subject") {
-                                subject = parts[1].strip ();
-                                if (from != null && timestamp != 0
-                                 && content_type != null && filters[0] == null)
-                                    break;
-                            }
-                            else if (field == to_or_from) {
-                                from = parts[1];
-                                if (subject != null && timestamp != 0
-                                 && content_type != null && filters[0] == null)
-                                    break;
-                            }
-                            else if (field == "date") {
-                                var parsed = new Soup.Date.from_string (parts[1]);
-                                timestamp = parsed != null ? (time_t)parsed.to_time_t () : 0;
-                                if (subject != null && from != null
-                                 && content_type != null && filters[0] == null)
-                                    break;
-                            }
-                            else if (field == "content-type") {
-                                content_type = parts[1].strip ();
-                                if (subject != null && from != null
-                                 && timestamp != 0 && filters[0] == null)
-                                    break;
-                            }
-                        }
-                        if (skip)
-                            continue;
-
-                        unowned string? fulltext = null;
-                        if (headers[0] == "fulltext")
-                            fulltext = filters[0];
-                        else if (headers[1] == "fulltext")
-                            fulltext = filters[1];
-                        if (fulltext != null) {
-                            skip = true;
-                            while ((line = stream.read_line (null, null)) != null) {
-                                if (line.down ().contains (fulltext)) {
-                                    skip = false;
-                                    break;
-                                }
-                            }
-                        }
-                        if (skip)
-                            continue;
-                    } catch (GLib.Error contents_error) {
-                        GLib.critical (_("Failed to read message \"%s\": %s"),
-                            contents.get_path (), contents_error.message);
-                    }
-
-                    unowned string attachment = null;
-                    if (show_attachments
-                     && content_type != null
-                     && content_type.has_prefix ("multipart/mixed"))
-                        attachment = STOCK_MAIL_ATTACHMENT;
-
-                    store.insert_with_values (out account_iter, null, -1,
-                        Columns.FLAGGED, flagged != null ? true : false,
-                        Columns.STATUS, status,
-                        Columns.ATTACHMENT, attachment,
-                        Columns.SUBJECT, subject != null ? subject : _("No subject"),
-                        Columns.WEIGHT, font_weight,
-                        Columns.FROM, from != null ? from : _("Unknown"),
-                        Columns.TIMESTAMP, timestamp,
-                        Columns.SIZE, info.get_size (),
-                        Columns.LOCATION, contents.get_path ());
+                    var message_path = folder_dir.resolve_relative_path (name);
+                    var message = read_message (message_path, folder_new,
+                        headers, filters);
+                    if (message == null)
+                        continue;
+                    message.size = info.get_size ();
+
+                    store.insert_with_values (null, null, 0,
+                        Columns.FLAGGED, message.flagged,
+                        Columns.STATUS, message.status,
+                        Columns.ATTACHMENT, message.attachment,
+                        Columns.SUBJECT, message.get_subject (),
+                        Columns.WEIGHT, message.font_weight,
+                        Columns.FROM, message.get_from (),
+                        Columns.TIMESTAMP, message.timestamp,
+                        Columns.SIZE, message.size,
+                        Columns.LOCATION, message.location);
                 }
             }
 



More information about the Xfce4-commits mailing list