[Xfce4-commits] <postler:master> Introduce MessagePart rather than separate arrays

Christian Dywan noreply at xfce.org
Sat Nov 27 04:34:01 CET 2010


Updating branch refs/heads/master
         to 90052a060bcd500c430fb35d065315e3a948bee2 (commit)
       from e2b6f487ce4ea8b30235b9eac2ab1fa96785ea7e (commit)

commit 90052a060bcd500c430fb35d065315e3a948bee2
Author: Christian Dywan <christian at twotoasts.de>
Date:   Sat Nov 27 00:57:11 2010 +0100

    Introduce MessagePart rather than separate arrays

 postler/postler-content.vala |   92 +++++++++++++++++++++---------------------
 postler/postler-viewer.vala  |   23 +++++-----
 2 files changed, 57 insertions(+), 58 deletions(-)

diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index f17d512..e1d67a0 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -9,15 +9,23 @@
  See the file COPYING for the full license text.
 */
 
+public class Postler.MessagePart {
+    public StringBuilder? body;
+    public string? mime_type;
+    public string? filename;
+    public MessagePart (string mime_type) {
+        body = new StringBuilder ();
+        this.mime_type = mime_type;
+        filename = null;
+    }
+}
+
 struct Postler.EmoticonMapping {
     public string token;
     public string icon_name;
 }
 
 public class Postler.Content : WebKit.WebView {
-    GLib.StringBuilder[] body = {};
-    string[] mime_types = {};
-    int body_parts = -1;
     string content_encoding;
     string date;
     string carbon_copy;
@@ -28,7 +36,6 @@ public class Postler.Content : WebKit.WebView {
     public string default_charset { get; set; default = "ISO-8859-1"; }
 
     public string? last_location { get; set; }
-    public uint? current_part { get; private set; }
     public string? sender { get; set; }
     public string? recipient { get; set; }
     public string? subject { get; set; }
@@ -36,10 +43,8 @@ public class Postler.Content : WebKit.WebView {
     public string? reply_to_all { get; set; }
     public string list_post { get; set; }
 
-    public string[] get_parts () {
-        return mime_types;
-    }
-    public int n_parts { get { return body_parts + 1; } }
+    public unowned List<MessagePart> message_parts { public get; set; }
+    public MessagePart? current_part { get; private set; }
 
     public bool view_source { get; set; }
 
@@ -425,9 +430,7 @@ public class Postler.Content : WebKit.WebView {
     public bool display (string location) {
         last_location = location;
         subject = _("(No subject)");
-        body = {};
-        mime_types = {};
-        body_parts = -1;
+        message_parts = new List<MessagePart> ();
 
         var contents = File.new_for_path (location);
         try {
@@ -580,11 +583,11 @@ public class Postler.Content : WebKit.WebView {
                 return false;
             }
 
+            MessagePart? message_part = null;
             /* Message body starts here */
             if (multipart == 0) {
-                body += new GLib.StringBuilder ();
-                mime_types += mime_type;
-                body_parts++;
+                message_part = new MessagePart (mime_type);
+                message_parts.append (message_part);
             }
 
             bool plain_text = mime_type == "text/plain";
@@ -595,19 +598,17 @@ public class Postler.Content : WebKit.WebView {
                 if (multipart > 0) {
                     if (line.has_prefix ("--")) {
                         if (line == "--" + inner_boundary) {
-                            body += new GLib.StringBuilder ();
-                            mime_types += "text/plain";
+                            message_part = new MessagePart ("text/plain");
+                            message_parts.append (message_part);
                             plain_text = true;
-                            body_parts++;
                             multipart = 2;
                             continue;
                         } else if (line == "--" + boundary) {
                             /* TODO: Merge inner text parts */
                             inner_boundary = "";
-                            body += new GLib.StringBuilder ();
-                            mime_types += "text/plain";
+                            message_part = new MessagePart ("text/plain");
+                            message_parts.append (message_part);
                             plain_text = true;
-                            body_parts++;
                             multipart = 2;
                             continue;
                         } else if (line == "--" + boundary + "--") {
@@ -622,9 +623,9 @@ public class Postler.Content : WebKit.WebView {
                         }
 
                         /* Content-Type can span over multiple lines */
-                        if (mime_types[body_parts].has_suffix (";"))
+                        if (message_part.mime_type.has_suffix (";"))
                             parts = { "content-type",
-                                      mime_types[body_parts] + line };
+                                      message_part.mime_type + line };
                         else
                             parts = line.split (":", 2);
 
@@ -639,7 +640,7 @@ public class Postler.Content : WebKit.WebView {
                                 mtype += ";";
                             if (mtype != "text/plain")
                                 plain_text = false;
-                            mime_types[body_parts] = mtype;
+                            message_part.mime_type = mtype;
                         }
                         else if (field == "content-transfer-encoding") {
                             content_encoding = parts[1].strip ();
@@ -662,11 +663,11 @@ public class Postler.Content : WebKit.WebView {
                 if (plain_text) {
                     /* Looks like a signature */
                     if (in_signature && line[0] == '\0') {
-                        body[body_parts].append ("</span>");
+                        message_part.body.append ("</span>");
                         in_signature = false;
                     } else if (!in_signature && line[0] == '-' && line[1] == '-'
                             && line[2] == ' ' && line[3] == '\0') {
-                        body[body_parts].append ("<span class=\"signature\">");
+                        message_part.body.append ("<span class=\"signature\">");
                         in_signature = true;
                         line = "";
                     }
@@ -684,9 +685,9 @@ public class Postler.Content : WebKit.WebView {
                         } while (true);
 
                         if (quote_mark < in_quote)
-                            body[body_parts].append ("</blockquote>");
+                            message_part.body.append ("</blockquote>");
                         else if (quote_mark > in_quote)
-                            body[body_parts].append ("<blockquote>");
+                            message_part.body.append ("<blockquote>");
                         in_quote = quote_mark;
                     }
 
@@ -706,49 +707,48 @@ public class Postler.Content : WebKit.WebView {
                 if (content_encoding == "base64") {
                     if (plain_text)
                         line = line.replace ("\n", "<br>");
-                    body[body_parts].append (line);
+                    message_part.body.append (line);
                 } else {
-                    body[body_parts].append (line);
+                    message_part.body.append (line);
                     if (content_encoding == "quoted-printable" && line.has_suffix ("="))
-                        body[body_parts].truncate (body[body_parts].len - 1);
-                    body[body_parts].append (plain_text ? "<br>" : " ");
+                        message_part.body.truncate (message_part.body.len - 1);
+                    message_part.body.append (plain_text ? "<br>" : " ");
                 }
             }
 
-            int part;
-            for (part = 0; part < body_parts; part++) {
+            message_part = null;
+            foreach (var part in message_parts) {
                 /* Ignore empty parts inserted by faulty clients */
-                if (body[part].str.strip () == "")
+                if (part.body.str.strip () == "")
                     continue;
-                if (mime_types[part].has_prefix ("text/"))
+                if (part.mime_type.has_prefix ("text/")) {
+                    message_part = part;
                     break;
+                }
             }
-            if (mime_types[part].has_prefix ("text/")) {
-                display_part(part);
-                current_part = part;
-            }
+            if (message_part != null)
+                display_part (message_part);
             else
                 throw new GLib.FileError.FAILED (
                     _("No text in the message that can be displayed"));
         } catch (GLib.Error contents_error) {
             display_error (_("Failed to read message: %s").printf (contents_error.message));
         }
-        notify_property ("n-parts");
+        notify_property ("message-parts");
         return false;
     }
 
-    public void display_part (uint part)
-        requires (part < n_parts) {
+    public void display_part (MessagePart message_part) {
         string body_chunk;
-        string mime_type = mime_types[part];
+        string mime_type = message_part.mime_type;
         bool plain_text = false;
         if (mime_type == "text/plain") {
             mime_type = "text/html";
             plain_text = true;
-            body_chunk = "<span class=\"plain_text\">" + body[part].str + "</span>";
+            body_chunk = "<span class=\"plain_text\">" + message_part.body.str + "</span>";
         }
         else
-            body_chunk = body[part].str;
+            body_chunk = message_part.body.str;
 
         try {
             /* Linkify */
@@ -808,7 +808,7 @@ public class Postler.Content : WebKit.WebView {
             display_error (_("Failed to display message part \"%s\": %s").printf (
                 mime_type, contents_error.message));
         }
-        current_part = part;
+        current_part = message_part;
     }
 
     void display_error (string message) {
diff --git a/postler/postler-viewer.vala b/postler/postler-viewer.vala
index 3e90d62..2201824 100644
--- a/postler/postler-viewer.vala
+++ b/postler/postler-viewer.vala
@@ -45,8 +45,8 @@ public class Postler.Viewer : Gtk.VBox {
         scrolled = new Postler.ScrolledWindow (message_parts);
         content_box.pack_start (scrolled, false, false, 0);
         message_parts.parent.parent.set_no_show_all (true);
-        notify_n_parts (content, null);
-        content.notify["n-parts"].connect (notify_n_parts);
+        notify_message_parts (content, null);
+        content.notify["message-parts"].connect (notify_message_parts);
 
         findbar = new Gtk.Toolbar ();
         pack_start (findbar, false, false, 0);
@@ -95,12 +95,12 @@ public class Postler.Viewer : Gtk.VBox {
         return "application-x-executable";
     }
 
-    void notify_n_parts (GLib.Object object, GLib.ParamSpec? pspec) {
+    void notify_message_parts (GLib.Object object, GLib.ParamSpec? pspec) {
         infobar.hide ();
         allow_external_images = false;
 
         var scrollable = message_parts.parent.parent;
-        if (content.n_parts < 2) {
+        if (content.message_parts.length () < 2) {
             scrollable.hide ();
             return;
         }
@@ -108,31 +108,30 @@ public class Postler.Viewer : Gtk.VBox {
         var children = message_parts.get_children ();
         foreach (var child in children)
             child.destroy ();
-        var parts = content.get_parts ();
         uint index = 0;
-        foreach (var part in parts) {
+        foreach (var part in content.message_parts) {
             /* Don't display the toplevel multipart part */
-            if (index == 0 && part.has_prefix ("multipart/")) {
+            if (index == 0 && part.mime_type.has_prefix ("multipart/")) {
                 index++;
                 continue;
             }
 
-            string icon_name = icon_name_for_mime_type (part, this);
+            string icon_name = icon_name_for_mime_type (part.mime_type, this);
             var icon = new Gtk.Image.from_icon_name (icon_name,
                                                      Gtk.IconSize.BUTTON);
             var button = new Gtk.ToggleButton ();
             button.relief = Gtk.ReliefStyle.NONE;
             button.add (icon);
-            button.set_tooltip_text (part);
-            button.set_data ("index", index);
-            if (index == content.current_part) {
+            button.set_tooltip_text (part.filename ?? part.mime_type);
+            button.set_data ("part", part);
+            if (part == content.current_part) {
                 last_button = button;
                 button.active = true;
             }
             button.clicked.connect ((button) => {
                 last_button.active = false;
                 last_button = button as Gtk.ToggleButton;
-                uint current_part = button.get_data ("index");
+                MessagePart current_part = button.get_data ("part");
                 content.display_part (current_part);
             });
             message_parts.pack_start (button, false, false, 0);



More information about the Xfce4-commits mailing list