[gnome-latex] App: move command line options handling to the liblatexila



commit 12d9c8b58754835a0331a1b08206ebf40a445157
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Apr 28 19:35:02 2020 +0200

    App: move command line options handling to the liblatexila

 src/glatex_app.vala            | 50 -----------------------------------
 src/liblatexila/latexila-app.c | 60 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 50 deletions(-)
---
diff --git a/src/glatex_app.vala b/src/glatex_app.vala
index 321d968..f68671c 100644
--- a/src/glatex_app.vala
+++ b/src/glatex_app.vala
@@ -33,8 +33,6 @@ public class GlatexApp : Latexila.App
 
     public GlatexApp ()
     {
-        setup_main_option_entries ();
-
         startup.connect (startup_cb);
         open.connect (open_documents);
         shutdown.connect (shutdown_cb);
@@ -56,54 +54,6 @@ public class GlatexApp : Latexila.App
         return main_window as MainWindow;
     }
 
-    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 GNOME LaTeX"), 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 ("tepl-new-window", null);
-
-            if (new_document)
-                activate_action ("new-document", null);
-
-            return -1;
-        });
-    }
-
     private void startup_cb ()
     {
         hold ();
diff --git a/src/liblatexila/latexila-app.c b/src/liblatexila/latexila-app.c
index 4a0bbe5..ce0d110 100644
--- a/src/liblatexila/latexila-app.c
+++ b/src/liblatexila/latexila-app.c
@@ -112,6 +112,63 @@ add_action_entries (LatexilaApp *app)
                                                       app);
 }
 
+static void
+add_main_option_entries (LatexilaApp *app)
+{
+       const GOptionEntry options[] =
+       {
+               { "version", 'V', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
+                 N_("Show the application’s version") },
+
+               { "new-window", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
+                 N_("Create a new top-level window in an existing instance of GNOME LaTeX") },
+
+               { "new-document", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
+                 N_("Create a new document in an existing instance of GNOME LaTeX") },
+
+               { NULL }
+       };
+
+       g_application_add_main_option_entries (G_APPLICATION (app), options);
+}
+
+static gint
+latexila_app_handle_local_options (GApplication *app,
+                                  GVariantDict *options)
+{
+       GError *error = NULL;
+
+       if (g_variant_dict_contains (options, "version"))
+       {
+               g_print ("%s %s\n", g_get_application_name (), PACKAGE_VERSION);
+               return 0;
+       }
+
+       g_application_register (app, NULL, &error);
+       if (error != NULL)
+       {
+               g_warning ("Failed to register the application: %s", error->message);
+               g_clear_error (&error);
+       }
+
+       if (g_variant_dict_contains (options, "new-window"))
+       {
+               g_action_group_activate_action (G_ACTION_GROUP (app), "tepl-new-window", NULL);
+       }
+
+       if (g_variant_dict_contains (options, "new-document"))
+       {
+               g_action_group_activate_action (G_ACTION_GROUP (app), "new-document", NULL);
+       }
+
+       if (G_APPLICATION_CLASS (latexila_app_parent_class)->handle_local_options != NULL)
+       {
+               return G_APPLICATION_CLASS (latexila_app_parent_class)->handle_local_options (app, options);
+       }
+
+       return -1;
+}
+
 static void
 latexila_app_startup (GApplication *app)
 {
@@ -147,6 +204,7 @@ latexila_app_class_init (LatexilaAppClass *klass)
 
        object_class->constructed = latexila_app_constructed;
 
+       gapp_class->handle_local_options = latexila_app_handle_local_options;
        gapp_class->startup = latexila_app_startup;
 }
 
@@ -159,6 +217,8 @@ latexila_app_init (LatexilaApp *app)
        g_set_application_name (PACKAGE_NAME);
        gtk_window_set_default_icon_name ("gnome-latex");
 
+       add_main_option_entries (app);
+
        tepl_app = tepl_application_get_from_gtk_application (GTK_APPLICATION (app));
        tepl_application_handle_activate (tepl_app);
        tepl_application_handle_metadata (tepl_app);


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