From c06c5e214f77f79bb040d299ad156bd8fa452753 Mon Sep 17 00:00:00 2001 From: "Gergely POLONKAI (W00d5t0ck)" Date: Sat, 21 Sep 2013 14:03:56 +0200 Subject: [PATCH] Created AgSettings object to deal with GSettings --- src/Makefile.am | 9 +++--- src/ag-settings.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++ src/ag-settings.h | 41 +++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 src/ag-settings.c create mode 100644 src/ag-settings.h diff --git a/src/Makefile.am b/src/Makefile.am index a79fdcb..91dcdc2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,10 +12,11 @@ BUILT_SOURCES = \ $(NULL) astrognome_source_files = \ - ag-app.c \ - ag-window.c \ - ag-chart.c \ - astrognome.c \ + ag-app.c \ + ag-window.c \ + ag-chart.c \ + ag-settings.c \ + astrognome.c \ $(NULL) EXTRA_DIST = \ diff --git a/src/ag-settings.c b/src/ag-settings.c new file mode 100644 index 0000000..da7d191 --- /dev/null +++ b/src/ag-settings.c @@ -0,0 +1,78 @@ +#include "ag-settings.h" + +G_DEFINE_TYPE(AgSettings, ag_settings, G_TYPE_OBJECT); + +#define AG_SETTINGS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), AG_TYPE_SETTINGS, AgSettingsPrivate)) + +#define SETTINGS_SCHEMA_ID_WINDOW "eu.polonkai.gergely.Astrognome.state.window" +#define SETTINGS_SCHEMA_ID_CHART "eu.polonkai.gergely.Astrognome.state.chart" + +static AgSettings *singleton = NULL; + +struct _AgSettingsPrivate { + GSettings *settings_window; + GSettings *settings_chart; +}; + +static void +ag_settings_init(AgSettings *settings) +{ + settings->priv = AG_SETTINGS_GET_PRIVATE(settings); + settings->priv->settings_window = g_settings_new(SETTINGS_SCHEMA_ID_WINDOW); + settings->priv->settings_chart = g_settings_new(SETTINGS_SCHEMA_ID_CHART); +} + +static void +ag_settings_dispose(GObject *object) +{ + AgSettings *settings = AG_SETTINGS(object); + + g_clear_object(&settings->priv->settings_window); + g_clear_object(&settings->priv->settings_chart); + + G_OBJECT_CLASS(ag_settings_parent_class)->dispose(object); +} + +static void +ag_settings_finalize(GObject *object) +{ + singleton = NULL; + G_OBJECT_CLASS(ag_settings_parent_class)->finalize(object); +} + +static void +ag_settings_class_init(AgSettingsClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + + g_type_class_add_private(gobject_class, sizeof(AgSettingsPrivate)); + gobject_class->dispose = ag_settings_dispose; + gobject_class->finalize = ag_settings_finalize; +} + +AgSettings * +ag_settings_get(void) +{ + if (!singleton) { + singleton = AG_SETTINGS(g_object_new(AG_TYPE_SETTINGS, NULL)); + } else { + g_object_ref(singleton); + } + + g_assert(singleton); + + return singleton; +} + +GSettings * +ag_settings_peek_window_settings(AgSettings *settings) +{ + return settings->priv->settings_window; +} + +GSettings * +ag_settings_peek_chart_settings(AgSettings *settings) +{ + return settings->priv->settings_chart; +} + diff --git a/src/ag-settings.h b/src/ag-settings.h new file mode 100644 index 0000000..027ce82 --- /dev/null +++ b/src/ag-settings.h @@ -0,0 +1,41 @@ +#ifndef __AG_SETTINGS_H__ +#define __AG_SETTINGS_H__ + +#include +#include + +G_BEGIN_DECLS + +#define AG_TYPE_SETTINGS (ag_settings_get_type()) +#define AG_SETTINGS(o) (G_TYPE_CHECK_INSTANCE_CAST((o), AG_TYPE_SETTINGS, AgSettings)) +#define AG_SETTINGS_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), AG_TYPE_SETTINGS, AgSettingsClass)) +#define AG_IS_SETTINGS(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), AG_TYPE_SETTINGS)) +#define AG_IS_SETTINGS_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE((k), AG_TYPE_SETTINGS)) +#define AG_SETTINGS_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS((o), AG_TYPE_SETTINGS, AgSettingsClass)) + +typedef struct _AgSettings AgSettings; +typedef struct _AgSettingsClass AgSettingsClass; +typedef struct _AgSettingsPrivate AgSettingsPrivate; + +struct _AgSettings { + GObject parent_instance; + + /*< private >*/ + AgSettingsPrivate *priv; +}; + +struct _AgSettingsClass { + GObjectClass parent_class; +}; + +GType ag_settings_get_type(void); + +AgSettings *ag_settings_get(void); + +GSettings *ag_settings_peek_window_settings(AgSettings *settings); +GSettings *ag_settings_peek_chart_settings(AgSettings *settings); + +G_END_DECLS + +#endif /* __AG_SETTINGS_H__ */ +