[Xfce4-commits] [apps/mousepad] 02/45: Add GSettings subclass singleton and related hooks
noreply at xfce.org
noreply at xfce.org
Fri Jul 11 13:03:07 CEST 2014
This is an automated email from the git hooks/post-receive script.
mbrush pushed a commit to branch master
in repository apps/mousepad.
commit 78da59cb7278ef752073791d1eefe33551a41413
Author: Matthew Brush <mbrush at codebrainz.ca>
Date: Sun Jul 6 01:19:45 2014 -0700
Add GSettings subclass singleton and related hooks
---
mousepad/Makefile.am | 11 +-
mousepad/mousepad-application.c | 6 +
mousepad/mousepad-private.h | 6 +
mousepad/mousepad-settings.c | 157 ++++++++++++++++++++
mousepad/mousepad-settings.h | 24 +++
...s.gschema.xml => org.xfce.Mousepad.gschema.xml} | 0
6 files changed, 200 insertions(+), 4 deletions(-)
diff --git a/mousepad/Makefile.am b/mousepad/Makefile.am
index 6230f97..28c536e 100644
--- a/mousepad/Makefile.am
+++ b/mousepad/Makefile.am
@@ -42,6 +42,8 @@ mousepad_SOURCES = \
mousepad-replace-dialog.h \
mousepad-search-bar.c \
mousepad-search-bar.h \
+ mousepad-settings.c \
+ mousepad-settings.h \
mousepad-statusbar.c \
mousepad-statusbar.h \
mousepad-view.c \
@@ -57,7 +59,8 @@ mousepad_CFLAGS = \
$(GTK_CFLAGS) \
$(GTHREAD_CFLAGS) \
$(GTKSOURCEVIEW_CFLAGS) \
- $(PLATFORM_CFLAGS)
+ $(PLATFORM_CFLAGS) \
+ -DMOUSEPAD_GSETTINGS_SCHEMA_DIR=\""$(datadir)/glib-2.0/schemas"\"
mousepad_LDFLAGS = \
-no-undefined \
@@ -113,10 +116,10 @@ endif
EXTRA_DIST = \
mousepad-dbus-infos.xml \
mousepad-marshal.list \
- mousepad-settings.gschema.xml \
- mousepad-window-ui.xml
+ mousepad-window-ui.xml \
+ org.xfce.Mousepad.gschema.xml
-gsettings_SCHEMAS = mousepad-settings.gschema.xml
+gsettings_SCHEMAS = org.xfce.Mousepad.gschema.xml
@GSETTINGS_RULES@
# vi:set ts=8 sw=8 noet ai nocindent syntax=automake:
diff --git a/mousepad/mousepad-application.c b/mousepad/mousepad-application.c
index cb7c778..4ccfa74 100644
--- a/mousepad/mousepad-application.c
+++ b/mousepad/mousepad-application.c
@@ -85,6 +85,9 @@ mousepad_application_init (MousepadApplication *application)
/* cleanup */
g_free (filename);
}
+
+ /* Initialize the MousepadSettings singleton if not already initialized */
+ MOUSEPAD_GSETTINGS_ONCE_INIT ();
}
@@ -122,6 +125,9 @@ mousepad_application_finalize (GObject *object)
/* cleanup the list of windows */
g_slist_free (application->windows);
+ /* Flush GSettings to disk */
+ MOUSEPAD_GSETTINGS_SYNC ();
+
(*G_OBJECT_CLASS (mousepad_application_parent_class)->finalize) (object);
}
diff --git a/mousepad/mousepad-private.h b/mousepad/mousepad-private.h
index 49ec851..28bcf84 100644
--- a/mousepad/mousepad-private.h
+++ b/mousepad/mousepad-private.h
@@ -21,6 +21,7 @@
#include <glib-object.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
+#include <gio/gio.h>
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcestylescheme.h>
#include <gtksourceview/gtksourcestyleschememanager.h>
@@ -121,6 +122,11 @@ enum
#define mousepad_widget_set_tooltip_text(widget,text) (mousepad_util_set_tooltip (widget, text))
#endif
+/* settings */
+#define MOUSEPAD_GSETTINGS (G_SETTINGS (mousepad_settings_get_default ()))
+#define MOUSEPAD_GSETTINGS_ONCE_INIT() do { (void) MOUSEPAD_GSETTINGS; } while (0)
+#define MOUSEPAD_GSETTINGS_SYNC() do { g_settings_sync (); } while (0)
+
G_END_DECLS
#endif /* !__MOUSEPAD_PRIVATE_H__ */
diff --git a/mousepad/mousepad-settings.c b/mousepad/mousepad-settings.c
new file mode 100644
index 0000000..6bbbed3
--- /dev/null
+++ b/mousepad/mousepad-settings.c
@@ -0,0 +1,157 @@
+#include <mousepad/mousepad-private.h>
+#include <mousepad/mousepad-settings.h>
+#include <stdlib.h>
+
+/* Needed to use keyfile GSettings backend */
+# define G_SETTINGS_ENABLE_BACKEND
+#include <gio/gsettingsbackend.h>
+
+
+
+struct MousepadSettings_
+{
+ GSettings parent;
+};
+
+
+
+struct MousepadSettingsClass_
+{
+ GSettingsClass parent_class;
+};
+
+
+
+static void mousepad_settings_finalize (GObject *object);
+
+
+
+G_DEFINE_TYPE (MousepadSettings, mousepad_settings, G_TYPE_SETTINGS)
+
+
+
+/* Global GSettings subclass instance, accessed by mousepad_settings_get_default() */
+static MousepadSettings *default_settings = NULL;
+
+
+
+static void
+mousepad_settings_class_init (MousepadSettingsClass *klass)
+{
+ GObjectClass *g_object_class;
+
+ g_object_class = G_OBJECT_CLASS (klass);
+
+ g_object_class->finalize = mousepad_settings_finalize;
+}
+
+
+
+static void
+mousepad_settings_finalize (GObject *object)
+{/*
+ MousepadSettings *self;
+
+ g_return_if_fail (MOUSEPAD_IS_SETTINGS (object));
+
+ self = MOUSEPAD_SETTINGS (object);
+*/
+ G_OBJECT_CLASS (mousepad_settings_parent_class)->finalize (object);
+}
+
+
+
+static void
+mousepad_settings_init (MousepadSettings *self)
+{
+}
+
+
+
+/* Called at exit to cleanup the MousepadSettings singleton */
+static void
+mousepad_settings_cleanup_default (void)
+{
+ /* last-ditch attempt to save settings to disk */
+ MOUSEPAD_GSETTINGS_SYNC ();
+
+ /* cleanup the singleton settings instance */
+ if (MOUSEPAD_IS_SETTINGS (default_settings))
+ {
+ g_object_unref (default_settings);
+ default_settings = NULL;
+ }
+}
+
+
+
+static void
+mousepad_settings_update_gsettings_schema_dir (void)
+{
+ const gchar *old_value = g_getenv ("GSETTINGS_SCHEMA_DIR");
+ /* Append to existing env. var. */
+ if (old_value != NULL)
+ {
+ gchar *new_value;
+#ifndef G_OS_WIN32
+ const gchar *pathsep = ":";
+#else
+ const gchar *pathsep = ";";
+#endif
+ new_value = g_strdup_printf("%s%s" MOUSEPAD_GSETTINGS_SCHEMA_DIR, old_value, pathsep);
+ g_setenv ("GSETTINGS_SCHEMA_DIR", new_value, TRUE);
+ g_free (new_value);
+ }
+ /* Create a new env. var. */
+ else
+ {
+ g_setenv ("GSETTINGS_SCHEMA_DIR", MOUSEPAD_GSETTINGS_SCHEMA_DIR, FALSE);
+ }
+}
+
+
+
+/* The first time it's called, it constructs the singleton instance that
+ * is always returned and is automatically cleaned up during normal exit. */
+MousepadSettings *
+mousepad_settings_get_default (void)
+{
+ static gsize default_initialized = 0;
+
+ if (g_once_init_enter (&default_initialized))
+ {
+ gchar *conf_file;
+ GSettingsBackend *backend;
+
+ /* If we're installed in an unusual location, we still want to load
+ * the schema so enforce this with the relevant environment variable. */
+ mousepad_settings_update_gsettings_schema_dir ();
+
+ /* Path inside user's config directory */
+ conf_file = g_build_filename (g_get_user_config_dir (),
+ "mousepad",
+ "settings.conf",
+ NULL);
+
+ /* Always use the keyfile backend */
+ backend = g_keyfile_settings_backend_new (conf_file, "/", NULL);
+ g_free (conf_file);
+
+ /* Construct the singleton instance */
+ default_settings = g_object_new (
+ MOUSEPAD_TYPE_SETTINGS,
+ "backend", backend /* give ref to settings object */,
+ "schema-id", "org.xfce.Mousepad",
+ NULL);
+
+ /* Auto-cleanup at exit */
+ atexit (mousepad_settings_cleanup_default);
+
+ /* Never enter this block again */
+ g_once_init_leave (&default_initialized, 1);
+ }
+
+ g_warn_if_fail (MOUSEPAD_IS_SETTINGS (default_settings));
+
+ return default_settings;
+}
diff --git a/mousepad/mousepad-settings.h b/mousepad/mousepad-settings.h
new file mode 100644
index 0000000..8ccf4d7
--- /dev/null
+++ b/mousepad/mousepad-settings.h
@@ -0,0 +1,24 @@
+#ifndef MOUSEPAD_SETTINGS_H_
+#define MOUSEPAD_SETTINGS_H_ 1
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define MOUSEPAD_TYPE_SETTINGS (mousepad_settings_get_type ())
+#define MOUSEPAD_SETTINGS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MOUSEPAD_TYPE_SETTINGS, MousepadSettings))
+#define MOUSEPAD_SETTINGS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOUSEPAD_TYPE_SETTINGS, MousepadSettingsClass))
+#define MOUSEPAD_IS_SETTINGS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MOUSEPAD_TYPE_SETTINGS))
+#define MOUSEPAD_IS_SETTINGS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOUSEPAD_TYPE_SETTINGS))
+#define MOUSEPAD_SETTINGS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOUSEPAD_TYPE_SETTINGS, MousepadSettingsClass))
+
+typedef struct MousepadSettings_ MousepadSettings;
+typedef struct MousepadSettingsClass_ MousepadSettingsClass;
+
+GType mousepad_settings_get_type (void);
+
+MousepadSettings *mousepad_settings_get_default (void);
+
+G_END_DECLS
+
+#endif /* MOUSEPAD_SETTINGS_H_ */
diff --git a/mousepad/mousepad-settings.gschema.xml b/mousepad/org.xfce.Mousepad.gschema.xml
similarity index 100%
rename from mousepad/mousepad-settings.gschema.xml
rename to mousepad/org.xfce.Mousepad.gschema.xml
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the Xfce4-commits
mailing list