[gedit] GeditSettings: make it a singleton



commit b2ce48a05d8875ce88ad1485577dd287608473f1
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Mar 24 17:49:15 2020 +0100

    GeditSettings: make it a singleton
    
    Like in Devhelp (DhSettingsApp).
    
    This permits to remove one instance variable from GeditApp, which
    contains quite a lot of instance variables.
    
    _gedit_app_get_settings() will be removed in a later commit.
    
    In GeditSettings, do some code formatting changes:
    - dispose() before finalize().
    - class_init() before init().
    - better format the header.
    - other minor stuff.
    
    Future things to do:
    - add peek functions to GeditSettings to get the GSettings, instead of
    creating the GSettings objects a bit everywhere in the codebase.
    - gedit_settings -> _gedit_settings when possible, to not export the
    symbols in the library.

 gedit/gedit-app.c      | 12 +++-------
 gedit/gedit-settings.c | 65 +++++++++++++++++++++++++++++++++++---------------
 gedit/gedit-settings.h | 24 +++++++++++--------
 gedit/gedit.c          |  3 +++
 4 files changed, 66 insertions(+), 38 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 7e7f3da37..e7ccfc878 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -56,7 +56,6 @@ typedef struct
        GtkPageSetup      *page_setup;
        GtkPrintSettings  *print_settings;
 
-       GeditSettings     *settings;
        GSettings         *ui_settings;
        GSettings         *window_settings;
 
@@ -148,7 +147,6 @@ gedit_app_dispose (GObject *object)
 
        g_clear_object (&priv->ui_settings);
        g_clear_object (&priv->window_settings);
-       g_clear_object (&priv->settings);
 
        g_clear_object (&priv->page_setup);
        g_clear_object (&priv->print_settings);
@@ -672,8 +670,8 @@ gedit_app_startup (GApplication *application)
 
        setup_theme_extensions (GEDIT_APP (application));
 
-       /* Load settings */
-       priv->settings = gedit_settings_new ();
+       /* Load/init settings */
+       _gedit_settings_get_singleton ();
        priv->ui_settings = g_settings_new ("org.gnome.gedit.preferences.ui");
        priv->window_settings = g_settings_new ("org.gnome.gedit.state.window");
 
@@ -1589,13 +1587,9 @@ _gedit_app_set_default_print_settings (GeditApp         *app,
 GeditSettings *
 _gedit_app_get_settings (GeditApp *app)
 {
-       GeditAppPrivate *priv;
-
        g_return_val_if_fail (GEDIT_IS_APP (app), NULL);
 
-       priv = gedit_app_get_instance_private (app);
-
-       return priv->settings;
+       return _gedit_settings_get_singleton ();
 }
 
 GMenuModel *
diff --git a/gedit/gedit-settings.c b/gedit/gedit-settings.c
index aea234984..abbcde35d 100644
--- a/gedit/gedit-settings.c
+++ b/gedit/gedit-settings.c
@@ -1,9 +1,9 @@
 /*
- * gedit-settings.c
  * This file is part of gedit
  *
  * Copyright (C) 2002-2005 - Paolo Maggi
- *               2009 - Ignacio Casal Quinteiro
+ * Copyright (C) 2009 - Ignacio Casal Quinteiro
+ * Copyright (C) 2020 - Sébastien Wilmet <swilmet gnome org>
  *
  * gedit is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -46,33 +46,41 @@ struct _GeditSettings
        gchar *old_scheme;
 };
 
+/* GeditSettings is a singleton. */
+static GeditSettings *singleton = NULL;
+
 G_DEFINE_TYPE (GeditSettings, gedit_settings, G_TYPE_OBJECT)
 
 static void
-gedit_settings_finalize (GObject *object)
+gedit_settings_dispose (GObject *object)
 {
        GeditSettings *gs = GEDIT_SETTINGS (object);
 
-       g_free (gs->old_scheme);
+       g_clear_object (&gs->interface);
+       g_clear_object (&gs->editor);
+       g_clear_object (&gs->ui);
 
-       G_OBJECT_CLASS (gedit_settings_parent_class)->finalize (object);
+       G_OBJECT_CLASS (gedit_settings_parent_class)->dispose (object);
 }
 
 static void
-gedit_settings_dispose (GObject *object)
+gedit_settings_finalize (GObject *object)
 {
        GeditSettings *gs = GEDIT_SETTINGS (object);
 
-       g_clear_object (&gs->interface);
-       g_clear_object (&gs->editor);
-       g_clear_object (&gs->ui);
+       g_free (gs->old_scheme);
 
-       G_OBJECT_CLASS (gedit_settings_parent_class)->dispose (object);
+       if (singleton == gs)
+       {
+               singleton = NULL;
+       }
+
+       G_OBJECT_CLASS (gedit_settings_parent_class)->finalize (object);
 }
 
 static void
 set_font (GeditSettings *gs,
-         const gchar *font)
+         const gchar   *font)
 {
        GList *views, *l;
        guint ts;
@@ -285,6 +293,15 @@ on_syntax_highlighting_changed (GSettings     *settings,
        g_list_free (windows);
 }
 
+static void
+gedit_settings_class_init (GeditSettingsClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = gedit_settings_dispose;
+       object_class->finalize = gedit_settings_finalize;
+}
+
 static void
 gedit_settings_init (GeditSettings *gs)
 {
@@ -326,19 +343,29 @@ gedit_settings_init (GeditSettings *gs)
                          gs);
 }
 
-static void
-gedit_settings_class_init (GeditSettingsClass *klass)
+GeditSettings *
+_gedit_settings_get_singleton (void)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       if (singleton == NULL)
+       {
+               singleton = g_object_new (GEDIT_TYPE_SETTINGS, NULL);
+       }
 
-       object_class->finalize = gedit_settings_finalize;
-       object_class->dispose = gedit_settings_dispose;
+       return singleton;
 }
 
-GeditSettings *
-gedit_settings_new ()
+void
+gedit_settings_unref_singleton (void)
 {
-       return g_object_new (GEDIT_TYPE_SETTINGS, NULL);
+       if (singleton != NULL)
+       {
+               g_object_unref (singleton);
+       }
+
+       /* singleton is not set to NULL here, it is set to NULL in
+        * gedit_settings_finalize() (i.e. when we are sure that the ref count
+        * reaches 0).
+        */
 }
 
 gchar *
diff --git a/gedit/gedit-settings.h b/gedit/gedit-settings.h
index 8c93c1d72..06f65a242 100644
--- a/gedit/gedit-settings.h
+++ b/gedit/gedit-settings.h
@@ -1,9 +1,9 @@
 /*
- * gedit-settings.h
  * This file is part of gedit
  *
+ * Copyright (C) 2002 - Paolo Maggi
  * Copyright (C) 2009 - Ignacio Casal Quinteiro
- *               2002 - Paolo Maggi
+ * Copyright (C) 2020 - Sébastien Wilmet <swilmet gnome org>
  *
  * gedit is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,19 +32,23 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GeditSettings, gedit_settings, GEDIT, SETTINGS, GObject)
 
-GeditSettings          *gedit_settings_new                             (void);
+G_GNUC_INTERNAL
+GeditSettings *        _gedit_settings_get_singleton                   (void);
 
-gchar                  *gedit_settings_get_system_font                 (GeditSettings *gs);
+void           gedit_settings_unref_singleton                  (void);
 
-GSList                 *gedit_settings_get_candidate_encodings         (gboolean      *default_candidates);
+gchar *                gedit_settings_get_system_font                  (GeditSettings *gs);
+
+GSList *       gedit_settings_get_candidate_encodings          (gboolean *default_candidates);
 
 /* Utility functions */
-GSList                 *gedit_settings_get_list                        (GSettings     *settings,
-                                                                        const gchar   *key);
 
-void                    gedit_settings_set_list                        (GSettings     *settings,
-                                                                        const gchar   *key,
-                                                                        const GSList  *list);
+GSList *       gedit_settings_get_list                         (GSettings   *settings,
+                                                                const gchar *key);
+
+void           gedit_settings_set_list                         (GSettings    *settings,
+                                                                const gchar  *key,
+                                                                const GSList *list);
 
 /* key constants */
 #define GEDIT_SETTINGS_USE_DEFAULT_FONT                        "use-default-font"
diff --git a/gedit/gedit.c b/gedit/gedit.c
index c875362dc..48d65c196 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -34,6 +34,7 @@
 
 #include "gedit-dirs.h"
 #include "gedit-debug.h"
+#include "gedit-settings.h"
 
 #ifdef G_OS_WIN32
 #include <gmodule.h>
@@ -145,6 +146,8 @@ main (int argc, char *argv[])
 
        status = g_application_run (G_APPLICATION (app), argc, argv);
 
+       gedit_settings_unref_singleton ();
+
        /* Break reference cycles caused by the PeasExtensionSet
         * for GeditAppActivatable which holds a ref on the GeditApp
         */


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