[totem] main: Re-implement command-line handling



commit c0a46d58623be6ae182bca8b694364c3e2de1ef5
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Oct 20 18:37:47 2014 +0200

    main: Re-implement command-line handling
    
    Instead of implementing command-line parsing ourselves, use
    the "handle-local-options" signal along with
    g_application_add_main_option_entries() to parse command-line options.
    
    Command-line options that work with a remote primary instance will be
    passed over D-Bus with the "remote-command" action.

 src/totem-menu.c   |    3 ++
 src/totem-object.c |   62 ++++--------------------------------------------
 src/totem.c        |   66 ++++++++++-----------------------------------------
 3 files changed, 21 insertions(+), 110 deletions(-)
---
diff --git a/src/totem-menu.c b/src/totem-menu.c
index 76a4a15..89a2142 100644
--- a/src/totem-menu.c
+++ b/src/totem-menu.c
@@ -277,6 +277,9 @@ remote_command_cb (GSimpleAction *action,
        const char *url;
 
        totem = TOTEM_OBJECT (user_data);
+
+       g_application_activate (G_APPLICATION (totem));
+
        g_variant_get (parameter, "(i&s)", &cmd, &url);
 
        if (url && *url == '\0')
diff --git a/src/totem-object.c b/src/totem-object.c
index 74b8f44..cb423da 100644
--- a/src/totem-object.c
+++ b/src/totem-object.c
@@ -145,6 +145,8 @@ totem_object_app_open (GApplication  *application,
        GSList *slist = NULL;
        int i;
 
+       g_application_activate (application);
+
        totem_object_set_main_page (TOTEM_OBJECT (application), "player");
 
        for (i = 0 ; i < n_files; i++)
@@ -156,62 +158,6 @@ totem_object_app_open (GApplication  *application,
 }
 
 static gboolean
-totem_object_local_command_line (GApplication              *application,
-                                gchar                   ***arguments,
-                                int                       *exit_status)
-{
-       GOptionContext *context;
-       GError *error = NULL;
-       char **argv;
-       int argc;
-
-       /* Dupe so that the remote arguments are listed, but
-        * not removed from the list */
-       argv = g_strdupv (*arguments);
-       argc = g_strv_length (argv);
-
-       context = totem_options_get_context ();
-       if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) {
-               g_print (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
-                               error->message, argv[0]);
-               g_error_free (error);
-               *exit_status = 1;
-               goto bail;
-       }
-
-       /* Replace relative paths with absolute URIs */
-       if (optionstate.filenames != NULL) {
-               guint n_files;
-               int i, n_args;
-
-               n_args = g_strv_length (*arguments);
-               n_files = g_strv_length (optionstate.filenames);
-
-               i = n_args - n_files;
-               for ( ; i < n_args; i++) {
-                       char *new_path;
-
-                       new_path = totem_create_full_path ((*arguments)[i]);
-                       if (new_path == NULL)
-                               continue;
-
-                       g_free ((*arguments)[i]);
-                       (*arguments)[i] = new_path;
-               }
-       }
-
-       g_strfreev (optionstate.filenames);
-       optionstate.filenames = NULL;
-
-       *exit_status = 0;
-bail:
-       g_option_context_free (context);
-       g_strfreev (argv);
-
-       return FALSE;
-}
-
-static gboolean
 accumulator_first_non_null_wins (GSignalInvocationHint *ihint,
                                 GValue *return_accu,
                                 const GValue *handler_return,
@@ -239,7 +185,6 @@ totem_object_class_init (TotemObjectClass *klass)
        object_class->get_property = totem_object_get_property;
        object_class->finalize = totem_object_finalize;
 
-       app_class->local_command_line = totem_object_local_command_line;
        app_class->open = totem_object_app_open;
 
        /**
@@ -439,6 +384,9 @@ totem_object_init (TotemObject *totem)
 
        totem->settings = g_settings_new (TOTEM_GSETTINGS_SCHEMA);
 
+       g_application_add_main_option_entries (G_APPLICATION (totem), all_options);
+       g_application_add_option_group (G_APPLICATION (totem), bacon_video_widget_get_option_group ());
+
        totem_app_actions_setup (totem);
 }
 
diff --git a/src/totem.c b/src/totem.c
index ce3e164..e156ebc 100644
--- a/src/totem.c
+++ b/src/totem.c
@@ -49,8 +49,6 @@
 #include "totem-preferences.h"
 #include "totem-session.h"
 
-static gboolean startup_called = FALSE;
-
 static void
 app_activate (GApplication *app,
              Totem        *totem)
@@ -149,57 +147,19 @@ app_activate (GApplication *app,
        gtk_window_set_application (GTK_WINDOW (totem->win), GTK_APPLICATION (totem));
 }
 
-static void
-app_startup (GApplication *application,
-            Totem        *totem)
-{
-       /* We don't do anything here, as we need to know the options
-        * when we set everything up.
-        * Note that this will break D-Bus activation of the application */
-       startup_called = TRUE;
-}
-
 static int
-app_command_line (GApplication             *app,
-                 GApplicationCommandLine  *command_line,
-                 Totem                    *totem)
+handle_local_options (GApplication *application,
+                     GVariantDict *options,
+                     TotemObject  *totem)
 {
-       GOptionContext *context;
-       int argc;
-       char **argv;
-
-       argv = g_application_command_line_get_arguments (command_line, &argc);
-
-       /* Reset the options, if they were used before */
-       memset (&optionstate, 0, sizeof (optionstate));
+       GError *error = NULL;
 
-       /* Options parsing */
-       context = totem_options_get_context ();
-       g_option_context_set_help_enabled (context, FALSE);
-       if (g_option_context_parse (context, &argc, &argv, NULL) == FALSE) {
-               g_option_context_free (context);
-               return 1;
+       if (!g_application_register (application, NULL, &error)) {
+               g_warning ("Failed to register application: %s", error->message);
+               g_error_free (error);
+               return 1;
        }
-       g_option_context_free (context);
-
-       totem_options_process_early (totem, &optionstate);
-
-       /* Don't create another window if we're remote.
-        * We can't use g_application_get_is_remote() because it's not registered yet */
-       if (startup_called != FALSE) {
-               app_init (totem, argv);
-
-               gdk_notify_startup_complete ();
-
-               /* Don't add files again through totem_options_process_for_server() */
-               g_clear_pointer (&optionstate.filenames, g_strfreev);
-               startup_called = FALSE;
-       }
-
-       /* Now do something with it */
        totem_options_process_for_server (totem, &optionstate);
-
-       g_strfreev (argv);
        return 0;
 }
 
@@ -231,13 +191,13 @@ main (int argc, char **argv)
        /* Build the main Totem object */
        totem = g_object_new (TOTEM_TYPE_OBJECT,
                              "application-id", "org.gnome.Totem",
-                             "flags", G_APPLICATION_HANDLES_COMMAND_LINE | G_APPLICATION_HANDLES_OPEN,
+                             "flags", G_APPLICATION_HANDLES_OPEN,
                              NULL);
 
-       g_signal_connect (G_OBJECT (totem), "startup",
-                         G_CALLBACK (app_startup), totem);
-       g_signal_connect (G_OBJECT (totem), "command-line",
-                         G_CALLBACK (app_command_line), totem);
+       g_signal_connect (G_OBJECT (totem), "handle-local-options",
+                         G_CALLBACK (handle_local_options), totem);
+       g_signal_connect (G_OBJECT (totem), "activate",
+                         G_CALLBACK (app_activate), totem);
 
        g_application_run (G_APPLICATION (totem), argc, argv);
 


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