[easytag/wip/application-window: 37/39] Move some GApplication handlers to EtApplication



commit ca0c4cfd3f9bee47d86c8d2072c9a7ff27e6af7e
Author: David King <amigadave amigadave com>
Date:   Tue Aug 5 16:38:06 2014 +0100

    Move some GApplication handlers to EtApplication

 src/application.c |  376 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/application.h |    4 +-
 src/easytag.c     |  333 -----------------------------------------------
 3 files changed, 372 insertions(+), 341 deletions(-)
---
diff --git a/src/application.c b/src/application.c
index 408cd5c..0ca3711 100644
--- a/src/application.c
+++ b/src/application.c
@@ -21,14 +21,25 @@
 #include "application.h"
 
 #include "about.h"
+#include "bar.h"
 #include "charset.h"
 #include "easytag.h"
+#include "log.h"
+#include "misc.h"
 
 #include <glib/gi18n.h>
 #include <stdlib.h>
 
+/* TODO: Use G_DEFINE_TYPE_WITH_PRIVATE. */
 G_DEFINE_TYPE (EtApplication, et_application, GTK_TYPE_APPLICATION)
 
+#define et_application_get_instance_private(app) (app->priv)
+
+struct _EtApplicationPrivate
+{
+    guint idle_handler;
+};
+
 static const GOptionEntry entries[] =
 {
     { "version", 'v', 0, G_OPTION_ARG_NONE, NULL,
@@ -80,7 +91,189 @@ static const GActionEntry actions[] =
 };
 
 /*
- * et_local_command_line:
+ * Load the default directory when the user interface is completely displayed
+ * to avoid bad visualization effect at startup.
+ */
+static gboolean
+on_idle_init (EtApplication *self)
+{
+    EtApplicationPrivate *priv;
+
+    priv = et_application_get_instance_private (self);
+
+    ET_Core_Free ();
+    ET_Core_Initialize ();
+
+    if (g_settings_get_boolean (MainSettings, "scan-startup"))
+    {
+        g_action_group_activate_action (G_ACTION_GROUP (MainWindow), "scanner",
+                                        NULL);
+    }
+
+    if (INIT_DIRECTORY)
+    {
+        et_application_window_select_dir (ET_APPLICATION_WINDOW (MainWindow),
+                                          INIT_DIRECTORY);
+    }
+    else
+    {
+        Statusbar_Message(_("Select a directory to browse"),FALSE);
+        g_action_group_activate_action (G_ACTION_GROUP (MainWindow),
+                                        "go-default", NULL);
+    }
+
+    /* Set sensitivity of buttons if the default directory is invalid. */
+    et_application_window_update_actions (ET_APPLICATION_WINDOW (MainWindow));
+
+    priv->idle_handler = 0;
+
+    return G_SOURCE_REMOVE;
+}
+
+/*
+ * common_init:
+ * @application: the application
+ *
+ * Create and show the main window. Common to all actions which may occur after
+ * startup, such as "activate" and "open".
+ */
+static void
+common_init (EtApplication *self)
+{
+    EtApplicationPrivate *priv;
+    gboolean settings_warning;
+    EtApplicationWindow *window;
+
+    priv = et_application_get_instance_private (self);
+
+    /* Create all config files. */
+    settings_warning = !Setting_Create_Files ();
+
+    /* Load Config */
+    Init_Config_Variables ();
+
+    /* Initialization */
+    ET_Core_Create ();
+    Main_Stop_Button_Pressed = FALSE;
+    Init_Mouse_Cursor ();
+
+    /* The main window */
+    window = et_application_window_new (GTK_APPLICATION (self));
+    MainWindow = GTK_WIDGET (window);
+
+    gtk_widget_show (MainWindow);
+
+    /* Starting messages */
+    Log_Print (LOG_OK, _("Starting EasyTAG version %s (PID: %d)…"),
+               PACKAGE_VERSION, getpid ());
+#ifdef G_OS_WIN32
+    if (g_getenv("EASYTAGLANG"))
+        Log_Print(LOG_OK,_("Variable EASYTAGLANG defined. Setting locale: '%s'"),g_getenv("EASYTAGLANG"));
+    else
+        Log_Print(LOG_OK,_("Setting locale: '%s'"),g_getenv("LANG"));
+#endif /* G_OS_WIN32 */
+
+    if (get_locale ())
+        Log_Print (LOG_OK,
+                   _("Currently using locale '%s' (and eventually '%s')"),
+                   get_locale (), get_encoding_from_locale (get_locale ()));
+
+    if (settings_warning)
+    {
+        Log_Print (LOG_WARNING, _("Unable to create setting directories"));
+    }
+
+    /* Load the default dir when the UI is created and displayed
+     * to the screen and open also the scanner window */
+    priv->idle_handler = g_idle_add ((GSourceFunc)on_idle_init, self);
+    g_source_set_name_by_id (priv->idle_handler, "Init idle function");
+}
+
+/*
+ * 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-show-hidden to TRUE.
+ */
+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))
+            {
+                g_settings_set_boolean (MainSettings, "browse-show-hidden",
+                                        TRUE);
+            }
+        }
+
+        g_object_unref (info);
+
+        if (file)
+        {
+            g_clear_object (&file);
+        }
+
+        file = parent;
+    }
+    while ((parent = g_file_get_parent (file)) != NULL);
+
+    g_clear_object (&file);
+}
+
+/*
+ * et_application_activate:
+ * @application: the application
+ *
+ * Handle the application being activated, which occurs after startup and if
+ * no files are opened.
+ */
+static void
+et_application_activate (GApplication *application)
+{
+    GList *windows;
+
+    windows = gtk_application_get_windows (GTK_APPLICATION (application));
+
+    if (windows != NULL)
+    {
+        gtk_window_present (windows->data);
+    }
+    else
+    {
+        common_init (ET_APPLICATION (application));
+    }
+}
+
+/*
+ * et_application_local_command_line:
  * @application: the application
  * @arguments: pointer to the argument string array
  * @exit_status: pointer to the returned exit status
@@ -91,8 +284,9 @@ static const GActionEntry actions[] =
  * handled in the local instance
  */
 static gboolean
-et_local_command_line (GApplication *application, gchar **arguments[],
-                       gint *exit_status)
+et_application_local_command_line (GApplication *application,
+                                   gchar **arguments[],
+                                   gint *exit_status)
 {
     GError *error = NULL;
     guint n_args;
@@ -173,6 +367,144 @@ et_local_command_line (GApplication *application, gchar **arguments[],
     return TRUE;
 }
 
+/*
+ * et_application_open:
+ * @application: the application
+ * @files: array of files to open
+ * @n_files: the number of files
+ * @hint: hint of method to open files, currently empty
+ *
+ * Handle the files passed to the primary instance.
+ *
+ * Returns: the exit status to be passed to the calling process
+ */
+static void
+et_application_open (GApplication *self,
+                     GFile **files,
+                     gint n_files,
+                     const gchar *hint)
+{
+    GList *windows;
+    gboolean activated;
+    GFile *arg;
+    GFile *parent;
+    GFileInfo *info;
+    GError *err = NULL;
+    GFileType type;
+    gchar *path;
+    gchar *path_utf8;
+
+    windows = gtk_application_get_windows (GTK_APPLICATION (self));
+    activated = windows ? TRUE : FALSE;
+
+    /* Only take the first file; ignore the rest. */
+    arg = files[0];
+
+    check_for_hidden_path_in_tree (arg);
+
+    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)
+    {
+        if (activated)
+        {
+            Log_Print (LOG_ERROR, _("Error while querying information for file: '%s' (%s)"),
+                       path_utf8, err->message);
+
+        }
+        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)
+            {
+                et_application_window_select_dir (windows->data, path);
+                g_free (path);
+            }
+            else
+            {
+                INIT_DIRECTORY = path;
+            }
+
+            g_free (path_utf8);
+            g_object_unref (info);
+            break;
+        case G_FILE_TYPE_REGULAR:
+            /* When given a file, load the parent directory. */
+            parent = g_file_get_parent (arg);
+
+            if (parent)
+            {
+                g_free (path_utf8);
+                g_free (path);
+
+                if (activated)
+                {
+                    gchar *parent_path;
+
+                    parent_path = g_file_get_path (arg);
+                    et_application_window_select_dir (windows->data,
+                                                      parent_path);
+
+                    g_free (parent_path);
+                }
+                else
+                {
+                    INIT_DIRECTORY = g_file_get_path (parent);
+                }
+
+                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;
+    }
+
+    if (!activated)
+    {
+        common_init (ET_APPLICATION (self));
+    }
+}
+
+/*
+ * et_application_shutdown:
+ * @application: the application
+ * @user_data: user data set when the signal handler was connected
+ *
+ * Handle the application being shutdown, which occurs after the main loop has
+ * exited and before returning from g_application_run().
+ */
+static void
+et_application_shutdown (GApplication *application)
+{
+    ET_Core_Destroy ();
+    Charset_Insert_Locales_Destroy ();
+
+    G_APPLICATION_CLASS (et_application_parent_class)->shutdown (application);
+}
+
 static void
 et_application_startup (GApplication *application)
 {
@@ -207,22 +539,52 @@ et_application_startup (GApplication *application)
 }
 
 static void
+et_application_dispose (GObject *object)
+{
+    EtApplication *self;
+    EtApplicationPrivate *priv;
+
+    self = ET_APPLICATION (object);
+    priv = et_application_get_instance_private (self);
+
+    if (priv->idle_handler)
+    {
+        g_source_remove (priv->idle_handler);
+        priv->idle_handler = 0;
+    }
+}
+
+static void
 et_application_finalize (GObject *object)
 {
     G_OBJECT_CLASS (et_application_parent_class)->finalize (object);
 }
 
 static void
-et_application_init (EtApplication *application)
+et_application_init (EtApplication *self)
 {
+    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ET_TYPE_APPLICATION,
+                                              EtApplicationPrivate);
 }
 
 static void
 et_application_class_init (EtApplicationClass *klass)
 {
-    G_OBJECT_CLASS (klass)->finalize = et_application_finalize;
-    G_APPLICATION_CLASS (klass)->local_command_line = et_local_command_line;
-    G_APPLICATION_CLASS (klass)->startup = et_application_startup;
+    GObjectClass *gobject_class;
+    GApplicationClass *gapplication_class;
+
+    gobject_class = G_OBJECT_CLASS (klass);
+    gapplication_class = G_APPLICATION_CLASS (klass);
+
+    gobject_class->dispose = et_application_dispose;
+    gobject_class->finalize = et_application_finalize;
+    gapplication_class->activate = et_application_activate;
+    gapplication_class->local_command_line = et_application_local_command_line;
+    gapplication_class->open = et_application_open;
+    gapplication_class->shutdown = et_application_shutdown;
+    gapplication_class->startup = et_application_startup;
+
+    g_type_class_add_private (klass, sizeof (EtApplicationPrivate));
 }
 
 /*
diff --git a/src/application.h b/src/application.h
index 806fe4e..5195907 100644
--- a/src/application.h
+++ b/src/application.h
@@ -1,5 +1,5 @@
 /* EasyTAG - tag editor for audio files
- * Copyright (C) 2013  David King <amigadave amigadave com>
+ * Copyright (C) 2013,2014  David King <amigadave amigadave com>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -31,11 +31,13 @@ G_BEGIN_DECLS
 
 typedef struct _EtApplication EtApplication;
 typedef struct _EtApplicationClass EtApplicationClass;
+typedef struct _EtApplicationPrivate EtApplicationPrivate;
 
 struct _EtApplication
 {
     /*< private >*/
     GtkApplication parent_instance;
+    EtApplicationPrivate *priv;
 };
 
 struct _EtApplicationClass
diff --git a/src/easytag.c b/src/easytag.c
index 465abb6..6dafbf7 100644
--- a/src/easytag.c
+++ b/src/easytag.c
@@ -61,8 +61,6 @@
 /****************
  * Declarations *
  ****************/
-static guint idle_handler_id;
-
 static GtkWidget *QuitRecursionWindow = NULL;
 
 /* Used to force to hide the msgbox when saving tag */
@@ -89,7 +87,6 @@ static gint Save_Selected_Files_With_Answer (gboolean force_saving_files);
 static gint Save_List_Of_Files (GList *etfilelist,
                                 gboolean force_saving_files);
 
-static gboolean Init_Load_Default_Dir (void);
 static void EasyTAG_Exit (void);
 static gboolean et_rename_file (const char *old_filepath,
                                 const char *new_filepath, GError **error);
@@ -101,293 +98,6 @@ static void Destroy_Quit_Recursion_Function_Window (void);
 static void et_on_quit_recursion_response (GtkDialog *dialog, gint response_id,
                                            gpointer user_data);
 
-/*
- * common_init:
- * @application: the application
- *
- * Create and show the main window. Common to all actions which may occur after
- * startup, such as "activate" and "open".
- */
-static void
-common_init (GApplication *application)
-{
-    gboolean settings_warning;
-    EtApplicationWindow *window;
-
-    /* Create all config files. */
-    settings_warning = !Setting_Create_Files ();
-
-    /* Load Config */
-    Init_Config_Variables();
-    /* Display_Config(); // <- for debugging */
-
-    /* Initialization */
-    ET_Core_Create();
-    Main_Stop_Button_Pressed = FALSE;
-    Init_Mouse_Cursor();
-
-    /* The main window */
-    window = et_application_window_new (GTK_APPLICATION (application));
-    MainWindow = GTK_WIDGET (window);
-
-    gtk_widget_show (MainWindow);
-
-    /* Starting messages */
-    Log_Print(LOG_OK,_("Starting EasyTAG version %s (PID: %d)…"),PACKAGE_VERSION,getpid());
-#ifdef ENABLE_MP3
-    Log_Print(LOG_OK,_("Using libid3tag version %s"), ID3_VERSION);
-#endif
-#if defined ENABLE_MP3 && defined ENABLE_ID3LIB
-    Log_Print (LOG_OK, _("Using id3lib version %d.%d.%d"), ID3LIB_MAJOR_VERSION,
-               ID3LIB_MINOR_VERSION, ID3LIB_PATCH_VERSION);
-#endif
-
-#ifdef G_OS_WIN32
-    if (g_getenv("EASYTAGLANG"))
-        Log_Print(LOG_OK,_("Variable EASYTAGLANG defined. Setting locale: '%s'"),g_getenv("EASYTAGLANG"));
-    else
-        Log_Print(LOG_OK,_("Setting locale: '%s'"),g_getenv("LANG"));
-#endif /* G_OS_WIN32 */
-
-    if (get_locale())
-        Log_Print (LOG_OK,
-                   _("Currently using locale '%s' (and eventually '%s')"),
-                   get_locale (), get_encoding_from_locale (get_locale ()));
-
-    if (settings_warning)
-    {
-        Log_Print (LOG_WARNING, _("Unable to create setting directories"));
-    }
-
-    /* Minimised window icon */
-    gtk_widget_realize(MainWindow);
-
-    /* Load the default dir when the UI is created and displayed
-     * to the screen and open also the scanner window */
-    idle_handler_id = g_idle_add ((GSourceFunc)Init_Load_Default_Dir, NULL);
-    g_source_set_name_by_id (idle_handler_id, "Init idle function");
-}
-
-/*
- * 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-show-hidden to TRUE.
- */
-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))
-            {
-                g_settings_set_boolean (MainSettings, "browse-show-hidden",
-                                        TRUE);
-            }
-        }
-
-        g_object_unref (info);
-
-        if (file)
-        {
-            g_clear_object (&file);
-        }
-
-        file = parent;
-    }
-    while ((parent = g_file_get_parent (file)) != NULL);
-
-    g_clear_object (&file);
-}
-
-/*
- * on_application_open:
- * @application: the application
- * @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 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 void
-on_application_open (GApplication *application, GFile **files, gint n_files,
-                     gchar *hint, gpointer user_data)
-{
-    GList *windows;
-    gboolean activated;
-    GFile *arg;
-    GFile *parent;
-    GFileInfo *info;
-    GError *err = NULL;
-    GFileType type;
-    gchar *path;
-    gchar *path_utf8;
-
-    windows = gtk_application_get_windows (GTK_APPLICATION (application));
-    activated = windows ? TRUE : FALSE;
-
-    /* Only take the first file; ignore the rest. */
-    arg = files[0];
-
-    check_for_hidden_path_in_tree (arg);
-
-    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)
-    {
-        if (activated)
-        {
-            Log_Print (LOG_ERROR, _("Error while querying information for file: '%s' (%s)"),
-                       path_utf8, err->message);
-
-        }
-        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)
-            {
-                et_application_window_select_dir (windows->data, path);
-                g_free (path);
-            }
-            else
-            {
-                INIT_DIRECTORY = path;
-            }
-
-            g_free (path_utf8);
-            g_object_unref (info);
-            break;
-        case G_FILE_TYPE_REGULAR:
-            /* When given a file, load the parent directory. */
-            parent = g_file_get_parent (arg);
-
-            if (parent)
-            {
-                g_free (path_utf8);
-                g_free (path);
-
-                if (activated)
-                {
-                    gchar *parent_path;
-
-                    parent_path = g_file_get_path (arg);
-                    et_application_window_select_dir (windows->data,
-                                                      parent_path);
-
-                    g_free (parent_path);
-                }
-                else
-                {
-                    INIT_DIRECTORY = g_file_get_path (parent);
-                }
-
-                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;
-    }
-
-    if (!activated)
-    {
-        common_init (application);
-    }
-}
-
-/*
- * on_application_activate:
- * @application: the application
- * @user_data: user data set when the signal handler was connected
- *
- * Handle the application being activated, which occurs after startup and if
- * no files are opened.
- */
-static void
-on_application_activate (GApplication *application, gpointer user_data)
-{
-    GList *windows;
-
-    windows = gtk_application_get_windows (GTK_APPLICATION (application));
-
-    if (windows != NULL)
-    {
-        gtk_window_present (windows->data);
-    }
-    else
-    {
-        common_init (application);
-    }
-}
-
-/*
- * on_application_shutdown:
- * @application: the application
- * @user_data: user data set when the signal handler was connected
- *
- * Handle the application being shutdown, which occurs after the main loop has
- * exited and before returning from g_application_run().
- */
-static void
-on_application_shutdown (GApplication *application, gpointer user_data)
-{
-    ET_Core_Destroy ();
-    Charset_Insert_Locales_Destroy ();
-}
-
 /********
  * Main *
  ********/
@@ -409,12 +119,6 @@ int main (int argc, char *argv[])
 #endif /* !GLIB_CHECK_VERSION (2, 35, 1) */
 
     application = et_application_new ();
-    g_signal_connect (application, "open", G_CALLBACK (on_application_open),
-                      NULL);
-    g_signal_connect (application, "activate",
-                      G_CALLBACK (on_application_activate), NULL);
-    g_signal_connect (application, "shutdown",
-                      G_CALLBACK (on_application_shutdown), NULL);
     status = g_application_run (G_APPLICATION (application), argc, argv);
     g_object_unref (application);
 
@@ -1527,43 +1231,6 @@ void Action_Main_Stop_Button_Pressed (void)
 }
 
 /*
- * Load the default directory when the user interface is completely displayed
- * to avoid bad visualization effect at startup.
- */
-static gboolean
-Init_Load_Default_Dir (void)
-{
-    //ETCore->ETFileList = NULL;
-    ET_Core_Free();
-    ET_Core_Initialize();
-
-    if (g_settings_get_boolean (MainSettings, "scan-startup"))
-    {
-        g_action_group_activate_action (G_ACTION_GROUP (MainWindow), "scanner",
-                                        NULL);
-    }
-
-    if (INIT_DIRECTORY)
-    {
-        et_application_window_select_dir (ET_APPLICATION_WINDOW (MainWindow),
-                                          INIT_DIRECTORY);
-    }
-    else
-    {
-        Statusbar_Message(_("Select a directory to browse"),FALSE);
-        g_action_group_activate_action (G_ACTION_GROUP (MainWindow),
-                                        "go-default", NULL);
-    }
-
-    /* Set sensitivity of buttons if the default directory is invalid. */
-    et_application_window_update_actions (ET_APPLICATION_WINDOW (MainWindow));
-
-    return G_SOURCE_REMOVE;
-}
-
-
-
-/*
  * Exit the program
  */
 static void


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