[latexila] app: simplify handling of command line options



commit 9fa363547c0652de60eee48d8937c977097e5e90
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Dec 18 19:17:32 2015 +0100

    app: simplify handling of command line options

 src/latexila_app.vala |   77 +++++++++++++++++++++++++----------
 src/main.vala         |  105 +-----------------------------------------------
 2 files changed, 58 insertions(+), 124 deletions(-)
---
diff --git a/src/latexila_app.vala b/src/latexila_app.vala
index af08863..7f134be 100644
--- a/src/latexila_app.vala
+++ b/src/latexila_app.vala
@@ -38,8 +38,11 @@ public class LatexilaApp : Gtk.Application
     public LatexilaApp ()
     {
         Object (application_id: "org.gnome.latexila");
+        set_flags (ApplicationFlags.HANDLES_OPEN);
         Environment.set_application_name (Config.PACKAGE_NAME);
 
+        setup_main_option_entries ();
+
         startup.connect (startup_cb);
 
         activate.connect (() =>
@@ -49,6 +52,8 @@ public class LatexilaApp : Gtk.Application
             release ();
         });
 
+        open.connect (open_documents);
+
         shutdown.connect (shutdown_cb);
     }
 
@@ -57,11 +62,58 @@ public class LatexilaApp : Gtk.Application
         return GLib.Application.get_default () as LatexilaApp;
     }
 
+    private void setup_main_option_entries ()
+    {
+        bool show_version = false;
+        bool new_document = false;
+        bool new_window = false;
+
+        OptionEntry[] options = new OptionEntry[4];
+
+        options[0] = { "version", 'V', 0, OptionArg.NONE, ref show_version,
+            N_("Show the application's version"), null };
+
+        options[1] = { "new-document", 'n', 0, OptionArg.NONE, ref new_document,
+            N_("Create new document"), null };
+
+        options[2] = { "new-window", 0, 0, OptionArg.NONE, ref new_window,
+            N_("Create a new top-level window in an existing instance of LaTeXila"), null };
+
+        options[3] = { null };
+
+        add_main_option_entries (options);
+
+        handle_local_options.connect (() =>
+        {
+            if (show_version)
+            {
+                stdout.printf ("%s %s\n", Config.PACKAGE_NAME, Config.PACKAGE_VERSION);
+                return 0;
+            }
+
+            try
+            {
+                register ();
+            }
+            catch (Error e)
+            {
+                error ("Failed to register the application: %s", e.message);
+            }
+
+            if (new_window)
+                activate_action ("new-window", null);
+
+            if (new_document)
+                activate_action ("new-document", null);
+
+            return -1;
+        });
+    }
+
     private void startup_cb ()
     {
         hold ();
         add_action_entries (_app_actions, this);
-        add_open_files_action ();
         set_application_icons ();
         Latexila.utils_register_icons ();
         StockIcons.register_stock_icons ();
@@ -98,27 +150,6 @@ public class LatexilaApp : Gtk.Application
         release ();
     }
 
-    private void add_open_files_action ()
-    {
-        VariantType strings_array = new VariantType ("as");
-        SimpleAction open_files_action = new SimpleAction ("open-files", strings_array);
-        add_action (open_files_action);
-
-        open_files_action.activate.connect ((param) =>
-        {
-            string[] uris = param.dup_strv ();
-            File[] files = {};
-
-            foreach (string uri in uris)
-            {
-                if (0 < uri.length)
-                    files += File.new_for_uri (uri);
-            }
-
-            open_documents (files);
-        });
-    }
-
     private void new_document_cb ()
     {
         MainWindow window = active_window as MainWindow;
@@ -364,6 +395,8 @@ public class LatexilaApp : Gtk.Application
             window.open_document (file, jump_to);
             jump_to = false;
         }
+
+        active_window.present ();
     }
 
     private string get_accel_filename ()
diff --git a/src/main.vala b/src/main.vala
index 821a106..d4fe340 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -1,7 +1,7 @@
 /*
  * This file is part of LaTeXila.
  *
- * Copyright © 2010-2012 Sébastien Wilmet
+ * Copyright © 2010-2015 Sébastien Wilmet
  *
  * LaTeXila is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,13 +21,6 @@
 
 using Gtk;
 
-private struct CmdLineData
-{
-    bool new_document;
-    bool new_window;
-    Variant? files_to_open;
-}
-
 private void init_i18n ()
 {
     Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALE_DIR);
@@ -35,103 +28,11 @@ private void init_i18n ()
     Intl.textdomain (Config.GETTEXT_PACKAGE);
 }
 
-// Put this here because inside a function is not possible...
-[CCode (array_length = false, array_null_terminated = true)]
-string[] files_list;
-
-private CmdLineData parse_cmd_line_options (string[] args)
-{
-    bool show_version = false;
-    CmdLineData data = CmdLineData ();
-
-    /* Definition of the options */
-    OptionEntry[] options = new OptionEntry[5];
-
-    options[0] = { "version", 'V', 0, OptionArg.NONE, ref show_version,
-        N_("Show the application's version"), null };
-
-    options[1] = { "new-document", 'n', 0, OptionArg.NONE, ref data.new_document,
-        N_("Create new document"), null };
-
-    options[2] = { "new-window", 0, 0, OptionArg.NONE, ref data.new_window,
-        N_("Create a new top-level window in an existing instance of LaTeXila"), null };
-
-    options[3] = { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files_list,
-        null, "[FILE...]" };
-
-    options[4] = { null };
-
-    /* Parse the command line and extract data */
-    OptionContext context =
-        new OptionContext (_("- Integrated LaTeX Environment for GNOME"));
-    context.add_main_entries (options, Config.GETTEXT_PACKAGE);
-    context.add_group (Gtk.get_option_group (false));
-
-    try
-    {
-        context.parse (ref args);
-    }
-    catch (OptionError e)
-    {
-        warning ("%s", e.message);
-        stderr.printf (
-            _("Run '%s --help' to see a full list of available command line options.\n"),
-            args[0]);
-
-        Process.exit (1);
-    }
-
-    if (show_version)
-    {
-        stdout.printf ("%s %s\n", Config.PACKAGE_NAME, Config.PACKAGE_VERSION);
-        Process.exit (0);
-    }
-
-    if (files_list.length == 0)
-        data.files_to_open = null;
-    else
-    {
-        string[] uris = {};
-        foreach (string filename in files_list)
-        {
-            // Call File.new_for_commandline_arg() here (and not in the LatexilaApp class)
-            // because relative path resolution needs the right current working directory,
-            // which can be different for the primary instance.
-            File file = File.new_for_commandline_arg (filename);
-            uris += file.get_uri ();
-        }
-
-        data.files_to_open = new Variant.strv (uris);
-    }
-
-    return data;
-}
-
-int main (string[] args)
+int main (string[] argv)
 {
     init_i18n ();
 
-    CmdLineData data = parse_cmd_line_options (args);
-
     LatexilaApp app = new LatexilaApp ();
 
-    try
-    {
-        app.register ();
-    }
-    catch (Error e)
-    {
-        error ("Failed to register the application: %s", e.message);
-    }
-
-    if (data.new_window)
-        app.activate_action ("new-window", null);
-
-    if (data.files_to_open != null)
-        app.activate_action ("open-files", data.files_to_open);
-
-    if (data.new_document)
-        app.activate_action ("new-document", null);
-
-    return app.run ();
+    return app.run (argv);
 }


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