[Xfce4-commits] <postler:master> Remove the module concept in favour of URIs

Christian Dywan noreply at xfce.org
Mon Aug 1 22:32:01 CEST 2011


Updating branch refs/heads/master
         to 0d9ec87bd7df142ffb64fdfc2fdf2fb6ef7dc9a0 (commit)
       from 9357cb517ad4eafda8ae8e69fce9a9049ebe8b96 (commit)

commit 0d9ec87bd7df142ffb64fdfc2fdf2fb6ef7dc9a0
Author: Christian Dywan <christian at twotoasts.de>
Date:   Fri Jul 29 21:36:16 2011 +0200

    Remove the module concept in favour of URIs
    
    No module is needed for source, an editor can be used.
    
    The remaining special-case is "service" which is no URI.

 postler/postler-app.vala      |   26 +++--------
 postler/postler-bureau.vala   |    2 +-
 postler/postler-client.vala   |    6 +--
 postler/postler-content.vala  |   99 ++++++++++++-----------------------------
 postler/postler-messages.vala |    4 +-
 postler/postler-reader.vala   |   25 +++++------
 postler/postler-service.vala  |    8 ++--
 7 files changed, 55 insertions(+), 115 deletions(-)

diff --git a/postler/postler-app.vala b/postler/postler-app.vala
index 8153080..8496704 100644
--- a/postler/postler-app.vala
+++ b/postler/postler-app.vala
@@ -149,12 +149,8 @@ public class Postler.App : Unique.App {
     }
 
     public static bool show_uri (Gdk.Screen screen, string uri) {
-        string real_uri = uri;
-        if (!("://" in uri))
-            return spawn_module ("compose", uri);
-
         try {
-            Gtk.show_uri (screen, real_uri, Gtk.get_current_event_time ());
+            Gtk.show_uri (screen, uri, Gtk.get_current_event_time ());
         }
         catch (GLib.Error error) {
             try {
@@ -167,7 +163,7 @@ public class Postler.App : Unique.App {
                     GLib.AppInfo info;
                     info = GLib.AppInfo.create_from_commandline (handler, "", 0);
                     var uris = new List<string>();
-                    uris.prepend (real_uri);
+                    uris.prepend (uri);
                     if (info.launch_uris (uris, new GLib.AppLaunchContext ()))
                         return true;
                 }
@@ -203,29 +199,21 @@ public class Postler.App : Unique.App {
         return true;
     }
 
-    public static bool spawn_module (string name,
-        string? arg1=null, string? arg2=null) {
-
-        string command = argv0 + " --module " + name;
-        if (arg1 != null)
-            command += " " + Shell.quote (arg1);
-        if (arg2 != null)
-            command += " " + Shell.quote (arg2);
+    public static bool spawn_uri (string uri) {
         try {
             /* Can't use GLib.AppInfo as it would mangle the arguments */
-            if (Process.spawn_command_line_async (command))
-                return true;
+            return Process.spawn_command_line_async (argv0 + " " + uri);
         }
         catch (GLib.Error error) {
             var error_dialog = new Gtk.MessageDialog (null, 0,
                 Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
-                _("Failed to open module %s."), name);
+                _("Failed to open \"%s\"."), uri);
             error_dialog.format_secondary_text (error.message);
             error_dialog.response.connect ((dialog, response)
                 => { dialog.destroy (); });
             error_dialog.show ();
         }
-        return true;
+        return false;
     }
 
     static Notify.Notification? notification = null;
@@ -246,7 +234,7 @@ public class Postler.App : Unique.App {
                 foreach (string cap in caps) {
                     if (cap == "actions") {
                         notification.add_action ("default", _("Open"), (n, a) => {
-                            Postler.App.spawn_module ("bureau");
+                            Postler.App.spawn_uri ("");
                         });
                     }
                     else if (cap == "sound")
diff --git a/postler/postler-bureau.vala b/postler/postler-bureau.vala
index 75049c3..c38372b 100644
--- a/postler/postler-bureau.vala
+++ b/postler/postler-bureau.vala
@@ -274,7 +274,7 @@ public class Postler.Bureau : Gtk.Window {
     }
 
     void action_view_source () {
-        Postler.App.spawn_module ("source", content.last_location);
+        content.view_source ();
     }
 
     static AccountSetup? setup = null;
diff --git a/postler/postler-client.vala b/postler/postler-client.vala
index 954a438..14cb784 100644
--- a/postler/postler-client.vala
+++ b/postler/postler-client.vala
@@ -53,10 +53,8 @@ namespace Postler {
                     sent (account, filename, error_message != "" ? error_message : null);
                 });
                 /* Ensure Postler is running, ignore errors */
-                Process.spawn_async (null,
-                    { Postler.App.argv0, "-m", "service" }, null,
-                    SpawnFlags.SEARCH_PATH,
-                    null, null);
+                Process.spawn_async (null, { Postler.App.argv0, "service" }, null,
+                                     SpawnFlags.SEARCH_PATH, null, null);
             } catch (GLib.Error error) { }
         }
 
diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index a790213..de2e03a 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -243,11 +243,33 @@ public class Postler.Content : WebKit.WebView {
         return linkified.str;
     }
 
+    public void view_source () {
+        var app_info = GLib.AppInfo.get_default_for_type ("text/plain", false);
+        var context = new Gdk.AppLaunchContext ();
+        context.set_screen (get_screen ());
+        context.set_timestamp (Gtk.get_current_event_time ());
+        try {
+            var files = new GLib.List<GLib.File> ();
+            files.append (GLib.File.new_for_uri (message.uri));
+            app_info.launch (files, context);
+        }
+        catch (GLib.Error error) {
+            var dialog = new Gtk.MessageDialog (null, 0,
+                Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
+                _("Failed to execute external command."));
+            dialog.format_secondary_text (error.message);
+            dialog.response.connect ((dialog, response)
+                => { dialog.destroy (); });
+            dialog.show ();
+        }
+    }
+
     void populate_menu (Gtk.Menu menu) {
         if (editable || can_copy_clipboard ())
             return;
 
-        menu.hide ();
+        foreach (var child in menu.get_children ())
+            child.hide ();
 
         var event = Gtk.get_current_event ();
         var result = get_hit_test_result ((Gdk.EventButton?)event);
@@ -270,23 +292,6 @@ public class Postler.Content : WebKit.WebView {
             return;
         }
 
-        menuitem = new Gtk.ImageMenuItem.from_stock (Gtk.STOCK_COPY, null);
-        menuitem.activate.connect ((menuitem) => {
-            copy_clipboard (); });
-        menuitem.show ();
-        menuitem.sensitive = can_copy_clipboard ();
-        menu.append (menuitem);
-        menuitem = new Gtk.MenuItem.with_mnemonic (_("Copy _Filename"));
-        menuitem.activate.connect ((menuitem) => {
-            var clipboard = get_clipboard (Gdk.SELECTION_CLIPBOARD);
-            clipboard.set_text (last_location, -1);
-        });
-        menuitem.show ();
-        menu.append (menuitem);
-
-        menuitem = new Gtk.SeparatorMenuItem ();
-        menuitem.show ();
-        menu.append (menuitem);
         menuitem = new Gtk.ImageMenuItem.from_stock (Gtk.STOCK_ZOOM_IN, null);
         menuitem.label = _("_Enlarge Text");
         menuitem.activate.connect ((menuitem) => {
@@ -302,15 +307,13 @@ public class Postler.Content : WebKit.WebView {
         menuitem.show ();
         menu.append (menuitem);
 
-        if (get_view_source_mode ())
-            return;
-
         menuitem = new Gtk.SeparatorMenuItem ();
         menuitem.show ();
         menu.append (menuitem);
         menuitem = new Gtk.MenuItem.with_mnemonic (_("View _Source"));
         menuitem.activate.connect ((menuitem) => {
-            Postler.App.spawn_module ("source", last_location); });
+            view_source ();
+        });
         menuitem.show ();
         menuitem.sensitive = last_location != null;
         menu.append (menuitem);
@@ -408,10 +411,6 @@ public class Postler.Content : WebKit.WebView {
         "([a-zA-Z0-9.\\-]+@[a-zA-Z0-9.\\-]+[a-zA-Z0-9.]+)"
     };
 
-    static string parse_encoded (string quoted, out string charset) {
-        return Postler.Messages.parse_encoded (quoted, out charset);
-    }
-
     string format_header (string header, string? data) {
         if (data != null)
             return "<br><b>%s</b> %s<br>".printf (header, data);
@@ -575,50 +574,6 @@ public class Postler.Content : WebKit.WebView {
         return g_content_type_is_a (mime_type, "text/plain");
     }
 
-    public async void display_source (Message message) {
-        last_location = message.get_path ();
-        subject = _("Source Code: %s").printf (last_location);
-
-        string line;
-        string content_type = null;
-        string mime_type = "text/plain";
-        string charset = null;
-        string[] parts;
-
-        var contents = File.new_for_path (last_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;
-        Postler.Message.parse_content_type (content_type, ref charset, ref boundary,
-                            ref mime_type, ref fname);
-        load_string ("""
-            <style text="text/css">
-            %s * { white-space: pre-wrap !important; }</style>
-            </style><body>%s</body>
-            """.
-            printf (themed_style_sheet (), body.str),
-            "text/html", charset, "about:blank");
-    }
-
     public string? list_or_all () {
         if (list_post == null)
             return message.recipients + "," + message.reply_to ?? message.sender;
@@ -685,7 +640,7 @@ public class Postler.Content : WebKit.WebView {
 
     public void compose_message (string? prefix=null, string? recipients=null) {
         string mailto = reply_uri (message, prefix, recipients);
-        Postler.App.spawn_module ("compose", mailto);
+        Postler.App.spawn_uri (mailto);
     }
 
     public async bool display (Message the_message, AccountInfo? account_info=null) {
@@ -1121,6 +1076,8 @@ public class Postler.Content : WebKit.WebView {
             if (!should_open)
                 return true;
         }
+        else if (uri.has_prefix ("mailto:"))
+            return Postler.App.spawn_uri (uri);
 
         return Postler.App.show_uri (get_screen (), uri);
     }
diff --git a/postler/postler-messages.vala b/postler/postler-messages.vala
index 7e23168..95eeda8 100644
--- a/postler/postler-messages.vala
+++ b/postler/postler-messages.vala
@@ -252,7 +252,7 @@ public class Postler.Messages : Gtk.TreeView {
         Gtk.TreeIter iter;
         if (get_selected_iter (out iter)) {
             toggle_message_flag (iter, MessageFlags.MARK_READ, (message, result) => {
-                Postler.App.spawn_module ("content", (message as Message).get_path ());
+                Postler.App.spawn_uri ("mid:" + (message as Message).id);
             });
         }
     }
@@ -678,7 +678,7 @@ public class Postler.Messages : Gtk.TreeView {
             if (get_selected_iter (out iter)) {
                 toggle_message_flag (iter, MessageFlags.MARK_READ,
                     (message, result) => {
-                    Postler.App.spawn_module ("content", (message as Message).get_path ());
+                    Postler.App.spawn_uri ("mid:" + (message as Message).id);
                 });
             }
         } else if (event.type == Gdk.EventType.BUTTON_PRESS
diff --git a/postler/postler-reader.vala b/postler/postler-reader.vala
index 5e4c56e..06fd865 100644
--- a/postler/postler-reader.vala
+++ b/postler/postler-reader.vala
@@ -10,12 +10,10 @@
 */
 
 public class Postler.Reader {
-    static string module = null;
     static bool verbose = false;
     static bool version = false;
     static string[] filenames = null;
     const OptionEntry[] options = {
-        { "module", 'm', 0, OptionArg.STRING, ref module, N_("Module"), null },
         { "verbose", 'v', 0, OptionArg.NONE, ref verbose, N_("Verbose"), null },
         { "version", 'V', 0, OptionArg.NONE, ref version, N_("Display program version"), null },
         { "", 0, 0, GLib.OptionArg.STRING_ARRAY, ref filenames, N_("Filenames"), null },
@@ -104,7 +102,8 @@ public class Postler.Reader {
         }
 
         /* mailto, ? or @ implies compose, otherwise file implies content */
-        if (module == null && filenames != null && filenames[0] != null) {
+        string? module = null;
+        if (filenames != null && filenames[0] != null) {
             if (filenames[0].has_prefix ("file://")
              || filenames[0].has_prefix ("mid:"))
                 module = "content";
@@ -112,13 +111,15 @@ public class Postler.Reader {
              || filenames[0].has_prefix ("?")
              || (filenames[0].chr (-1, '@') != null && filenames[0].chr (-1, '/') == null))
                 module = "compose";
+            else if (filenames[0] == "service")
+                module = "service";
+            else if (filenames[0].has_prefix ("search:"))
+                module = "bureau";
             else
                 GLib.error (_("Invalid argument passed: %s"), filenames[0]);
         }
-
-        if (module == null)
+        else
             module = "bureau";
-
         Environment.set_prgname ("postler-" + module);
 
         if (module == "service") {
@@ -136,9 +137,9 @@ public class Postler.Reader {
         Postler.App.register_stock_items ();
 
         if (module != "bureau") {
-            if (module == "content" || module == "source") {
+            if (module == "content") {
                 var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL);
-                window.set_icon_name (module == "source" ? "text-html" : "emblem-mail");
+                window.set_icon_name ("emblem-mail");
                 var screen = window.get_screen ();
                 Gdk.Rectangle monitor;
                 screen.get_monitor_geometry (0, out monitor);
@@ -174,10 +175,7 @@ public class Postler.Reader {
                     content.notify["subject"].connect ((object, pspec) => {
                         window.set_title (content.subject);
                     });
-                    if (module == "source")
-                        content.display_source (message);
-                    else
-                        content.display (message);
+                    content.display (message);
                 }
 
                 shelf.show_all ();
@@ -226,8 +224,7 @@ public class Postler.Reader {
                 return 0;
             }
             else
-                GLib.error ("Unknown module \"%s\". Valid modules are: %s",
-                    module, "bureau content source compose service");
+                assert_not_reached ();
         }
 
         var app = new Postler.App ();
diff --git a/postler/postler-service.vala b/postler/postler-service.vala
index e118a42..4950dba 100644
--- a/postler/postler-service.vala
+++ b/postler/postler-service.vala
@@ -136,7 +136,7 @@ namespace Postler {
         List<Indicate.Indicator> items;
 
         void update_inbox_indicator (Indicate.Indicator item) {
-            string path = "file://" + item.get_property ("url") + "/INBOX/%";
+            string path = "search:uri/" + item.get_property ("url") + "/INBOX";
             int64 new_messages = 0;
 
             notify["unread"].connect ((object, pspec) => {
@@ -167,7 +167,7 @@ namespace Postler {
             item.set_property ("url", info.path);
             item.user_display.connect ((item) => {
                 string url = item.get_property ("url");
-                Postler.App.spawn_module ("bureau", url + "/INBOX");
+                Postler.App.spawn_uri ("search:uri/" + url + "/INBOX");
             });
             items.append (item);
             update_inbox_indicator (item);
@@ -310,13 +310,13 @@ namespace Postler {
             indicator.set_desktop_file (
                 Config.DATADIR + "/applications/postler.desktop");
             indicator.server_display.connect (() => {
-                Postler.App.spawn_module ("bureau");
+                Postler.App.spawn_uri ("");
             });
 
             var item = new Indicate.Indicator.with_server (indicator);
             item.set_property ("name", _("Compose Message"));
             item.user_display.connect (() => {
-                Postler.App.spawn_module ("compose");
+                Postler.App.spawn_uri ("mailto:");
             });
             item.emit_show ();
             items.append (item);


More information about the Xfce4-commits mailing list