[easytag] Use GApplication::open to open paths



commit 2ecd8b2ffc4d4287a5acfd8ea5e8fc42dc986134
Author: David King <amigadave amigadave com>
Date:   Wed May 22 00:06:57 2013 +0100

    Use GApplication::open to open paths
    
    Add a handler for the "open" signal, and use it to load paths. Refactor
    et_local_command_line() to always handle the commandline arguments in
    full (and therefore return TRUE). Remove the "command-line" handler.

 src/application.c |   39 ++++++--
 src/easytag.c     |  251 +++++++++++++++++++++++++++++++++++------------------
 2 files changed, 195 insertions(+), 95 deletions(-)
---
diff --git a/src/application.c b/src/application.c
index 99276df..8586b17 100644
--- a/src/application.c
+++ b/src/application.c
@@ -76,8 +76,8 @@ display_usage (void)
  *
  * Parse the local instance command-line arguments.
  *
- * Returns: %FALSE to indicate that the command-line arguments were not
- *          completely handled in the local instance, or does not return
+ * Returns: %TRUE to indicate that the command-line arguments were completely
+ * handled in the local instance
  */
 static gboolean
 et_local_command_line (GApplication *application, gchar **arguments[],
@@ -85,7 +85,6 @@ et_local_command_line (GApplication *application, gchar **arguments[],
 {
     GError *error = NULL;
     guint n_args;
-    gint i;
     gchar **argv;
 
     /* Try to register. */
@@ -101,16 +100,17 @@ et_local_command_line (GApplication *application, gchar **arguments[],
     n_args = g_strv_length (argv);
     *exit_status = 0;
 
+    g_debug ("Received %d commandline arguments", n_args);
+
     if (n_args <= 1)
     {
-        gtk_init (NULL, NULL);
         g_application_activate (application);
         return TRUE;
     }
-    i = 1;
-
-    while (argv[i])
+    else
     {
+        const gsize i = 1;
+
         /* Exit the local instance for --help and --version. */
         if ((strcmp (argv[i], "--version") == 0)
             || (strcmp (argv[i], "-v") == 0))
@@ -129,11 +129,30 @@ et_local_command_line (GApplication *application, gchar **arguments[],
         {
             /* Assume a filename otherwise, and allow the primary instance to
              * handle it. */
-            i++;
+            GFile **files;
+            gsize n_files;
+            gsize j;
+
+            n_files = n_args - 1;
+            files = g_new (GFile *, n_files);
+
+            for (j = 0; j < n_files; j++)
+            {
+                files[j] = g_file_new_for_commandline_arg (argv[j + 1]);
+            }
+
+            g_application_open (application, files, n_files, "");
+
+            for (j = 0; j < n_files; j++)
+            {
+                g_object_unref (files[j]);
+            }
+
+            g_free (files);
         }
     }
 
-    return FALSE;
+    return TRUE;
 }
 
 static void
@@ -178,7 +197,7 @@ et_application_new ()
 
     return g_object_new (et_application_get_type (), "application-id",
                          "org.gnome.EasyTAG", "flags",
-                         G_APPLICATION_FLAGS_NONE, NULL);
+                         G_APPLICATION_HANDLES_OPEN, NULL);
 }
 
 /*
diff --git a/src/easytag.c b/src/easytag.c
index 3df84f5..9945605 100644
--- a/src/easytag.c
+++ b/src/easytag.c
@@ -165,123 +165,200 @@ setup_sigchld (void)
 #endif /* !G_OS_WIN32 */
 
 /*
- * command_line:
+ * check_for_hidden_path_in_tree:
+ * @arg: the path to check
+ *
+ * Recursively check for a hidden path in the directory tree given by @arg. If
+ * a hidden path is found, set BROWSE_HIDDEN to 1.
+ */
+static void
+check_for_hidden_path_in_tree (GFile *arg)
+{
+    GFile *file = NULL;
+    GFile *parent;
+    GFileInfo *info;
+    GError *err = NULL;
+
+    /* Not really the parent until an iteration through the loop below. */
+    parent = g_file_dup (arg);
+
+    do
+    {
+        info = g_file_query_info (parent,
+                                  G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN,
+                                  G_FILE_QUERY_INFO_NONE, NULL, &err);
+
+        if (info == NULL)
+        {
+            g_message ("Error querying file information (%s)", err->message);
+            g_clear_error (&err);
+
+            if (file)
+            {
+                g_clear_object (&file);
+            }
+            g_object_unref (parent);
+            break;
+        }
+        else
+        {
+            if (g_file_info_get_is_hidden (info))
+            {
+                /* If the user saves the configuration for this session,
+                 * this value will be saved. */
+                BROWSE_HIDDEN_DIR = 1;
+            }
+        }
+
+        g_object_unref (info);
+
+        if (file)
+        {
+            g_clear_object (&file);
+        }
+
+        file = parent;
+    }
+    while ((parent = g_file_get_parent (file)) != NULL);
+
+    if (file)
+    {
+        g_clear_object (&file);
+    }
+}
+
+/*
+ * on_application_open:
  * @application: the application
- * @command_line: the command line to process
+ * @files: array of files to open
+ * @n_files: the number of files
+ * @hint: hint of method to open files, currently empty
  * @user_data: user data set when the signal handler was connected
  *
- * Handle the command-line arguments passed to the primary instance. The local
- * instance arguments are handled in EtApplication.
+ * Handle the files passed to the primary instance. The local instance
+ * arguments are handled in EtApplication.
  *
  * Returns: the exit status to be passed to the calling process
  */
-static gint
-command_line (GApplication *application,
-              GApplicationCommandLine *command_line, gpointer user_data)
+static void
+on_application_open (GApplication *application, GFile **files, gint n_files,
+                     gchar *hint, gpointer user_data)
 {
-    gchar **argv;
-    gint argc;
+    GtkWindow *main_window;
+    gboolean activated;
+    GFile *arg;
+    GFile *parent;
+    GFileInfo *info;
+    GError *err = NULL;
+    GFileType type;
+    gchar *path;
+    gchar *path_utf8;
 
-    argv = g_application_command_line_get_arguments (command_line, &argc);
+    main_window = et_application_get_window (ET_APPLICATION (application));
+    activated = main_window ? TRUE : FALSE;
+
+    /* Only take the first file; ignore the rest. */
+    arg = files[0];
+
+    check_for_hidden_path_in_tree (arg);
 
-    /* Check given arguments */
-    if (argc > 1)
+    path = g_file_get_path (arg);
+    path_utf8 = filename_to_display (path);
+    info = g_file_query_info (arg, G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                              G_FILE_QUERY_INFO_NONE, NULL, &err);
+
+    if (info == NULL)
     {
-        GFile *arg;
-        GFile *file = NULL;
-        GFile *parent;
-        GFileInfo *info;
-        GError *err = NULL;
-        GFileType type;
-
-        arg = g_file_new_for_commandline_arg (argv[1]);
-        /* Not really the parent of arg. */
-        parent = g_file_dup (arg);
-
-        do
+        if (activated)
         {
-            info = g_file_query_info (parent,
-                                      G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN,
-                                      G_FILE_QUERY_INFO_NONE, NULL, &err);
+            Log_Print (LOG_ERROR, _("Error while querying information for file: '%s' (%s)"),
+                       path_utf8, err->message);
 
-            if (info == NULL)
+        }
+        else
+        {
+            g_warning ("Error while querying information for file: '%s' (%s)",
+                       path_utf8, err->message);
+        }
+
+        g_free (path);
+        g_free (path_utf8);
+        g_error_free (err);
+        return;
+    }
+
+    type = g_file_info_get_file_type (info);
+
+    switch (type)
+    {
+        case G_FILE_TYPE_DIRECTORY:
+            if (activated)
             {
-                g_warning ("Error querying file information (%s)",
-                           err->message);
-                g_error_free (err);
-                if (file)
-                {
-                    g_clear_object (&file);
-                }
-                g_object_unref (parent);
-                break;
+                Browser_Tree_Select_Dir (path);
+                g_free (path);
             }
             else
             {
-                if (g_file_info_get_is_hidden (info))
-                {
-                    /* If the user saves the configuration for this session,
-                     * this value will be saved. */
-                    /* FIXME: Avoid this being overridden in activate(). */
-                    BROWSE_HIDDEN_DIR = 1;
-                }
+                INIT_DIRECTORY = path;
             }
 
+            g_free (path_utf8);
             g_object_unref (info);
-            if (file)
+            break;
+        case G_FILE_TYPE_REGULAR:
+            /* When given a file, load the parent directory. */
+            parent = g_file_get_parent (arg);
+            g_object_unref (arg);
+
+            if (parent)
             {
-                g_clear_object (&file);
-            }
-            file = parent;
-        }
-        while ((parent = g_file_get_parent (file)) != NULL);
+                g_free (path_utf8);
+                g_free (path);
 
-        info = g_file_query_info (arg, G_FILE_ATTRIBUTE_STANDARD_TYPE,
-                                  G_FILE_QUERY_INFO_NONE, NULL, &err);
-        type = g_file_info_get_file_type (info);
+                if (activated)
+                {
+                    gchar *parent_path;
 
-        switch (type)
-        {
-            case G_FILE_TYPE_DIRECTORY:
-                INIT_DIRECTORY = g_file_get_path (arg);
-                g_object_unref (arg);
-                break;
-            case G_FILE_TYPE_REGULAR:
-                /* When passing a file, only load the directory. */
-                parent = g_file_get_parent (arg);
-                g_object_unref (arg);
+                    parent_path = g_file_get_path (arg);
+                    Browser_Tree_Select_Dir (parent_path);
 
-                if (parent)
+                    g_free (parent_path);
+                    g_free (path_utf8);
+                }
+                else
                 {
                     INIT_DIRECTORY = g_file_get_path (parent);
-                    g_object_unref (parent);
-                    break;
                 }
-                /* Fall through on error. */
-            default:
-                g_application_command_line_printerr (command_line,
-                                                     _("Unknown parameter or path '%s'\n"),
-                                                     argv[1]);
-                return 1;
+
+                g_object_unref (parent);
+                g_object_unref (info);
                 break;
-        }
+            }
+            /* Fall through on error. */
+        default:
+            Log_Print (LOG_WARNING, _("Cannot open path '%s'"), path_utf8);
+            g_free (path);
+            g_free (path_utf8);
+            return;
+            break;
     }
 
-    /* Initialize GTK. */
-    if (et_application_get_window (ET_APPLICATION (application)) == NULL)
+    if (!activated)
     {
-        gtk_init (&argc, &argv);
+        g_application_activate (application);
     }
-
-    g_strfreev (argv);
-
-    g_application_activate (application);
-
-    return 0;
 }
 
+/*
+ * on_application_activate:
+ * @application: the application
+ * @user_data: user data set when the signal handler was connected
+ *
+ * Handle the application being activated, which occurs on startup or when
+ * opening a second instance.
+ */
 static void
-activate (GApplication *application, gpointer user_data)
+on_application_activate (GApplication *application, gpointer user_data)
 {
     GtkWindow *main_window;
     GtkWidget *MainVBox;
@@ -294,6 +371,9 @@ activate (GApplication *application, gpointer user_data)
         return;
     }
 
+    /* FIXME: Find a way to pass the arguments in. */
+    gtk_init (NULL, NULL);
+
     Charset_Insert_Locales_Init();
 
     /* Starting messages */
@@ -456,9 +536,10 @@ int main (int argc, char *argv[])
     INIT_DIRECTORY = NULL;
 
     application = et_application_new ();
-    g_signal_connect (application, "command-line", G_CALLBACK (command_line),
+    g_signal_connect (application, "open", G_CALLBACK (on_application_open),
                       NULL);
-    g_signal_connect (application, "activate", G_CALLBACK (activate), NULL);
+    g_signal_connect (application, "activate",
+                      G_CALLBACK (on_application_activate), NULL);
     status = g_application_run (G_APPLICATION (application), argc, argv);
     g_object_unref (application);
 


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