[gnome-boxes] Proper support for command line args in secondary process



commit 0d6c295628d952df08bf4200991ba175e861bd62
Author: Alexander Larsson <alexl redhat com>
Date:   Fri Jan 25 15:35:54 2013 +0100

    Proper support for command line args in secondary process
    
    We now handle most of the command line parsing (except a few
    specialized things) in the primary instance, by implementing the
    command_line signal of GApplication. This means we now support
    arguments like --open-uuid in the non-primary instance, as needed
    for e.g. gnome-shell search results.
    
    This additionally moves the UI initialization and window to
    the activate state so that it only happens in the primary instance,
    and it doesn't happen if we exit early in the command_line handler.
    
    Unfortunately we can't use the automatic --help support in GOptions
    anymore as that prints to/exists the primary instance rather than
    the calling one, so we have to now manually do that.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=692306

 src/app.vala  |  116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/main.vala |   67 +++-----------------------------
 2 files changed, 120 insertions(+), 63 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index fa869cb..1834fb0 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -13,6 +13,7 @@ private enum Boxes.AppPage {
 private class Boxes.Application: Gtk.Application {
     public Application () {
         application_id = "org.gnome.Boxes";
+        flags |= ApplicationFlags.HANDLES_COMMAND_LINE;
     }
 
     public override void startup () {
@@ -24,6 +25,10 @@ private class Boxes.Application: Gtk.Application {
         base.activate ();
         App.app.activate ();
     }
+
+    public override int command_line (GLib.ApplicationCommandLine cmdline) {
+        return App.app.command_line (cmdline);
+    }
 }
 
 private class Boxes.App: Boxes.UI {
@@ -137,6 +142,10 @@ private class Boxes.App: Boxes.UI {
     }
 
     public void startup () {
+        string [] args = {};
+        unowned string [] args2 = args;
+        GtkClutter.init (ref args2);
+
         var menu = new GLib.Menu ();
         menu.append (_("New"), "app.new");
 
@@ -150,7 +159,6 @@ private class Boxes.App: Boxes.UI {
 
         collection = new Collection ();
         duration = settings.get_int ("animation-duration");
-        setup_ui ();
 
         collection.item_added.connect ((item) => {
             view.add_item (item);
@@ -179,11 +187,95 @@ private class Boxes.App: Boxes.UI {
     }
 
     public void activate () {
+        setup_ui ();
         window.present ();
     }
 
-    public int run () {
-        return application.run ();
+    static bool opt_fullscreen;
+    static bool opt_help;
+    static string opt_open_uuid;
+    static string[] opt_uris;
+    static const OptionEntry[] options = {
+        { "version", 0, 0, OptionArg.NONE, null, N_("Display version number"), null },
+        { "help", 'h', OptionFlags.HIDDEN, OptionArg.NONE, ref opt_help, null, null },
+        { "full-screen", 'f', 0, OptionArg.NONE, ref opt_fullscreen, N_("Open in full screen"), null },
+        { "checks", 0, 0, OptionArg.NONE, null, N_("Check virtualization capabilities"), null },
+        { "open-uuid", 0, 0, OptionArg.STRING, ref opt_open_uuid, N_("Open box with UUID"), null },
+        // A 'broker' is a virtual-machine manager (could be local or remote). Currently libvirt is the only one supported.
+        { "", 0, 0, OptionArg.STRING_ARRAY, ref opt_uris, N_("URI to display, broker or installer media"), null },
+        { null }
+    };
+
+    public int command_line (GLib.ApplicationCommandLine cmdline) {
+        opt_fullscreen = false;
+        opt_help = false;
+        opt_open_uuid = null;
+        opt_uris = null;
+
+        var parameter_string = _("- A simple application to access remote or virtual machines");
+        var opt_context = new OptionContext (parameter_string);
+        opt_context.add_main_entries (options, null);
+        opt_context.set_help_enabled (false);
+
+        try {
+            string[] args1 = cmdline.get_arguments();
+            unowned string[] args2 = args1;
+            opt_context.parse (ref args2);
+        } catch (OptionError error) {
+            cmdline.printerr ("%s\n", error.message);
+            cmdline.printerr (opt_context.get_help (true, null));
+            return 1;
+        }
+
+        if (opt_help) {
+            cmdline.printerr (opt_context.get_help (true, null));
+            return 1;
+        }
+
+        if (opt_uris.length > 1 ||
+            (opt_open_uuid != null && opt_uris != null)) {
+            cmdline.printerr (_("Too many command line arguments specified.\n"));
+            cmdline.printerr (opt_context.get_help (true, null));
+            return 1;
+        }
+
+        application.activate ();
+
+        var app = this;
+        if (opt_open_uuid != null) {
+            var uuid = opt_open_uuid;
+            call_when_ready (() => {
+                    app.open_uuid (uuid);
+            });
+        } else if (opt_uris != null) {
+            var arg = opt_uris[0];
+            var file = File.new_for_commandline_arg (arg);
+
+            if (file.query_exists () || Uri.parse_scheme (arg) != null) {
+                call_when_ready (() => {
+                    wizard.open_with_uri (file.get_uri ());
+                });
+            } else {
+                call_when_ready (() => {
+                    open (arg);
+                });
+            }
+        } else {
+            call_when_ready ((first_time) => {
+                if (first_time)
+                    app.ui_state = Boxes.UIState.WIZARD;
+            });
+        }
+
+
+        if (opt_fullscreen)
+            app.fullscreen = true;
+
+        return 0;
+    }
+
+    public int run (string [] args) {
+        return application.run (args);
     }
 
     // To be called to tell App to release the resources it's handling.
@@ -388,7 +480,25 @@ private class Boxes.App: Boxes.UI {
         settings.set_value ("window-position", new int[] { x, y });
     }
 
+    private bool ui_is_setup;
     private void setup_ui () {
+        if (ui_is_setup)
+            return;
+        ui_is_setup = true;
+        Gtk.Window.set_default_icon_name ("gnome-boxes");
+        Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = true;
+
+        var provider = new Gtk.CssProvider ();
+        try {
+            var sheet = Boxes.get_style ("gtk-style.css");
+            provider.load_from_path (sheet);
+            Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (),
+                                                      provider,
+                                                      600);
+        } catch (GLib.Error error) {
+            warning (error.message);
+        }
+
         window = new Gtk.ApplicationWindow (application);
         window.show_menubar = false;
         window.hide_titlebar_when_maximized = true;
diff --git a/src/main.vala b/src/main.vala
index 8dd52a6..e38e33c 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -3,46 +3,24 @@ using Config;
 using Posix;
 
 private static bool version;
-private static bool fullscreen;
 private static bool checks;
-private static string open_uuid;
-private static string[] uris;
 
-private const OptionEntry[] options = {
+private const OptionEntry[] local_options = {
     { "version", 0, 0, OptionArg.NONE, ref version, N_("Display version number"), null },
-    { "full-screen", 'f', 0, OptionArg.NONE, ref fullscreen, N_("Open in full screen"), null },
     { "checks", 0, 0, OptionArg.NONE, ref checks, N_("Check virtualization capabilities"), null },
-    { "open-uuid", 0, 0, OptionArg.STRING, ref open_uuid, N_("Open box with UUID"), null },
-    // A 'broker' is a virtual-machine manager (could be local or remote). Currently libvirt is the only one supported.
-    { "", 0, 0, OptionArg.STRING_ARRAY, ref uris, N_("URI to display, broker or installer media"), null },
     { null }
 };
 
-private static void parse_args (ref unowned string[] args) {
-    var parameter_string = _("- A simple application to access remote or virtual machines");
+private static void parse_local_args (ref unowned string[] args) {
+    var parameter_string = _("");
     var opt_context = new OptionContext (parameter_string);
-    opt_context.set_help_enabled (true);
     opt_context.set_ignore_unknown_options (true);
-    opt_context.add_main_entries (options, null);
-    opt_context.add_group (Gtk.get_option_group (true));
-    opt_context.add_group (Cogl.get_option_group ());
-    opt_context.add_group (Clutter.get_option_group_without_init ());
-    opt_context.add_group (GtkClutter.get_option_group ());
-    // FIXME: add spice
+    opt_context.add_main_entries (local_options, null);
+    opt_context.set_help_enabled (false);
 
     try {
         opt_context.parse (ref args);
-    } catch (OptionError.BAD_VALUE err) {
-        GLib.stdout.printf (opt_context.get_help (true, null));
-        exit (1);
     } catch (OptionError error) {
-        warning (error.message);
-    }
-
-    if (uris.length > 1 ||
-        (open_uuid != null && uris != null)) {
-        GLib.stderr.printf (_("Too many command line arguments specified.\n"));
-        exit (1);
     }
 
     if (version) {
@@ -94,7 +72,7 @@ public int main (string[] args) {
     Intl.textdomain (GETTEXT_PACKAGE);
     GLib.Environment.set_application_name (_("Boxes"));
 
-    parse_args (ref args);
+    parse_local_args (ref args);
 
     try {
         GVir.init_object_check (ref args);
@@ -102,40 +80,9 @@ public int main (string[] args) {
         error (err.message);
     }
 
-    Gtk.Window.set_default_icon_name ("gnome-boxes");
-    Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = true;
-    var provider = new Gtk.CssProvider ();
-    try {
-        var sheet = Boxes.get_style ("gtk-style.css");
-        provider.load_from_path (sheet);
-        Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (),
-                                                  provider,
-                                                  600);
-    } catch (GLib.Error error) {
-        warning (error.message);
-    }
-
     var app = new Boxes.App ();
 
-    app.ready.connect ((first_time) => {
-        if (open_uuid != null) {
-            app.open_uuid (open_uuid);
-        } else if (uris != null) {
-            var arg = uris[0];
-            var file = File.new_for_commandline_arg (arg);
-
-            if (file.query_exists () || Uri.parse_scheme (arg) != null)
-                app.wizard.open_with_uri (file.get_uri ());
-            else
-                app.open (arg);
-        } else if (first_time) {
-            app.ui_state = Boxes.UIState.WIZARD;
-        }
-
-        app.fullscreen = fullscreen;
-    });
-
-    var exit_status = app.run ();
+    var exit_status = app.run (args);
 
     app.shutdown ();
     app.suspend_machines ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]