[Xfce4-commits] <postler:master> Split off autonomous display_source method

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


Updating branch refs/heads/master
         to d45152a5a372c11f691d4edb137b3e5975eb37bb (commit)
       from 9041c17246c32110025d6a0ca6d6244a9b79e908 (commit)

commit d45152a5a372c11f691d4edb137b3e5975eb37bb
Author: Christian Dywan <christian at twotoasts.de>
Date:   Sat Nov 27 01:53:00 2010 +0100

    Split off autonomous display_source method

 postler/postler-content.vala |   73 ++++++++++++++++++++++++-----------------
 postler/postler-reader.vala  |   19 +++++------
 2 files changed, 52 insertions(+), 40 deletions(-)

diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index d27da24..7f6277b 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -46,8 +46,6 @@ public class Postler.Content : WebKit.WebView {
     public unowned List<MessagePart> message_parts { public get; set; }
     public MessagePart? current_part { get; private set; }
 
-    public bool view_source { get; set; }
-
     const string style_sheet = """
         .headers {
             display: block;
@@ -192,7 +190,7 @@ public class Postler.Content : WebKit.WebView {
         });
         menuitem.show ();
         menu.append (menuitem);
-        if (view_source)
+        if (last_location.has_prefix ("view-source:"))
             return;
 
         menuitem = new Gtk.SeparatorMenuItem ();
@@ -433,46 +431,61 @@ public class Postler.Content : WebKit.WebView {
         return g_content_type_is_a (mime_type, "text/plain");
     }
 
+    public void display_source (string location) {
+        last_location = "view-source:" + location;
+        subject = _("Source Code: %s").printf (location);
+
+        string line;
+        string content_type = null;
+        string mime_type = "text/plain";
+        string charset = null;
+        string[] parts;
+
+        var contents = File.new_for_path (location);
+        GLib.StringBuilder body = new GLib.StringBuilder ();
+
+        try {
+            var stream = new DataInputStream (contents.read (null));
+            while ((line = stream.read_line (null, null)) != null) {
+                parts = line.split (":", 2);
+                if (parts != null && parts[0] != null) {
+                    string field = ascii_strdown (parts[0]);
+                    if (field == "content-type")
+                        content_type = parts[1].strip ();
+                    else if (field == "subject")
+                        subject = _("Source Code: %s").printf (parse_encoded (parts[1], out charset));
+                body.append (line + "\n");
+                }
+            }
+        } catch (GLib.Error error) {
+            display_error (_("Failed to view source: %s").printf (error.message));
+            return;
+        }
+
+        string? boundary = null;
+        string? fname = null;
+        parse_content_type (content_type, ref charset, ref boundary,
+                            ref mime_type, ref fname);
+
+        /* FIXME view_source_mode requires WebKitGTK+ 1.1.something */
+        set_view_source_mode (true);
+        load_string (body.str, mime_type, charset, "about:blank");
+    }
+
     public bool display (string location) {
         last_location = location;
         subject = _("(No subject)");
-        message_parts = new List<MessagePart> ();
 
+        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 mime_type = "text/plain";
             string charset = null;
             string[] parts;
 
-            if (view_source) {
-                GLib.StringBuilder body = new GLib.StringBuilder ();
-                while ((line = stream.read_line (null, null)) != null) {
-                    parts = line.split (":", 2);
-                    if (parts != null && parts[0] != null) {
-                        string field = ascii_strdown (parts[0]);
-                        if (field == "content-type")
-                            content_type = parts[1].strip ();
-                        else if (field == "subject")
-                            subject = parse_encoded (parts[1], out charset);
-                    }
-                    body.append (line + "\n");
-                }
-
-                string? boundary = null;
-                string? fname = null;
-                parse_content_type (content_type, ref charset, ref boundary,
-                                    ref mime_type, ref fname);
-
-                /* FIXME view_source_mode requires WebKitGTK+ 1.1.something */
-                set_view_source_mode (true);
-                load_string (body.str, mime_type, charset, "about:blank");
-                return true;
-            }
-
             content_encoding = "";
             string from = _("Unknown");
             date = _("(No date)");
diff --git a/postler/postler-reader.vala b/postler/postler-reader.vala
index 7c76487..cab704e 100644
--- a/postler/postler-reader.vala
+++ b/postler/postler-reader.vala
@@ -65,23 +65,22 @@ public class Postler.Reader {
             /* TODO: Implement "reply", "reply-all", "forward" */
             if (module == "content" || module == "source") {
                 instance = new Postler.Content ();
-                icon_name = "emblem-mail" /* FIXME */;
+                icon_name = module == "source" ? "text-html" : "emblem-mail";
                 title = _("(No subject)");
                 if (filenames != null && filenames[0] != null) {
                     Postler.Content content = (Postler.Content)instance;
-                    if (module == "source")
-                        content.view_source = true;
+                    string filename;
                     try {
-                        content.display (Filename.from_uri (filenames[0], null));
-                    } catch (GLib.Error error)  {
-                        content.display (filenames[0]);
+                       filename = Filename.from_uri (filenames[0], null);
+                    } catch (GLib.Error error) {
+                        filename = filenames[0];
                     }
+                    if (module == "source")
+                        content.display_source (filename);
+                    else
+                        content.display (filename);
                     title = content.subject;
                 }
-                if (module == "source") {
-                    icon_name = "text-html";
-                    title = _("Source Code: %s").printf (title);
-                }
             }
             else if (module == "compose") {
                 instance = new Postler.Composer ();



More information about the Xfce4-commits mailing list