r23217 - gnucash/trunk/src - Add some initialization code for GSettings
Geert Janssens
gjanssens at code.gnucash.org
Mon Oct 7 10:04:20 EDT 2013
Author: gjanssens
Date: 2013-10-07 10:04:19 -0400 (Mon, 07 Oct 2013)
New Revision: 23217
Trac: http://svn.gnucash.org/trac/changeset/23217
Added:
gnucash/trunk/src/app-utils/gnc-gsettings.c
gnucash/trunk/src/app-utils/gnc-gsettings.h
Modified:
gnucash/trunk/src/app-utils/Makefile.am
gnucash/trunk/src/bin/gnucash-bin.c
Log:
Add some initialization code for GSettings
Modified: gnucash/trunk/src/app-utils/Makefile.am
===================================================================
--- gnucash/trunk/src/app-utils/Makefile.am 2013-10-07 14:03:59 UTC (rev 23216)
+++ gnucash/trunk/src/app-utils/Makefile.am 2013-10-07 14:04:19 UTC (rev 23217)
@@ -54,6 +54,7 @@
gnc-exp-parser.c \
gnc-gconf-utils.c \
gnc-gettext-util.c \
+ gnc-gsettings.c \
gnc-helpers.c \
gnc-prefs.c \
gnc-sx-instance-model.c \
@@ -80,6 +81,7 @@
gnc-exp-parser.h \
gnc-gconf-utils.h \
gnc-gettext-util.h \
+ gnc-gsettings.h \
gnc-help-utils.h \
gnc-helpers.h \
gnc-prefs.h \
Added: gnucash/trunk/src/app-utils/gnc-gsettings.c
===================================================================
--- gnucash/trunk/src/app-utils/gnc-gsettings.c (rev 0)
+++ gnucash/trunk/src/app-utils/gnc-gsettings.c 2013-10-07 14:04:19 UTC (rev 23217)
@@ -0,0 +1,124 @@
+/********************************************************************\
+ * gnc-gsettings.c -- utility functions for storing/retrieving *
+ * data in the GSettings database for GnuCash *
+ * Copyright (C) 2013 Geert Janssens <geert at kobaltwit.be> *
+ * *
+ * 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 Software Foundation; either version 2 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact: *
+ * *
+ * Free Software Foundation Voice: +1-617-542-5942 *
+ * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
+ * Boston, MA 02110-1301, USA gnu at gnu.org *
+ * *
+\********************************************************************/
+
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include "gnc-gsettings.h"
+
+#define CLIENT_TAG "%s-%s-client"
+#define NOTIFY_TAG "%s-%s-notify_id"
+
+static GHashTable *schema_hash = NULL;
+static const gchar *gsettings_prefix;
+
+/************************************************************/
+/* Internal helper functions */
+/************************************************************/
+static gboolean gnc_gsettings_is_valid_key(GSettings *settings, const gchar *key)
+{
+ gchar **keys = NULL;
+ gint i = 0;
+ gboolean found = FALSE;
+
+ // Check if the key is valid key within settings
+ if (!G_IS_SETTINGS(settings))
+ return FALSE;
+
+ // Get list of keys
+ keys = g_settings_list_keys(settings);
+
+ while (keys && keys[i])
+ {
+ if (!g_strcmp0(key, keys[i]))
+ {
+ found = TRUE;
+ break;
+ }
+ i++;
+ }
+
+ // Free keys
+ g_strfreev(keys);
+
+ return found;
+}
+
+static GSettings * gnc_gsettings_get_schema_ptr (const gchar *schema_str)
+{
+ GSettings *gset = NULL;
+ gchar *full_name = gnc_gsettings_normalize_schema_name (schema_str);
+
+ if (!schema_hash)
+ schema_hash = g_hash_table_new (g_str_hash, g_str_equal);
+
+ gset = g_hash_table_lookup (schema_hash, full_name);
+ if (!gset)
+ {
+ gset = g_settings_new (full_name);
+ if (G_IS_SETTINGS(gset))
+ g_hash_table_insert (schema_hash, full_name, gset);
+ else
+ PWARN ("Ignoring attempt to access unknown gsettings schema %s", full_name);
+ }
+
+ g_free (full_name);
+ return gset;
+}
+
+
+
+/************************************************************/
+/* GSettings Utilities */
+/************************************************************/
+
+void
+gnc_gsettings_set_prefix (const gchar *prefix)
+{
+ gsettings_prefix = prefix;
+}
+
+const gchar *
+gnc_gsettings_get_prefix (void)
+{
+ return gsettings_prefix;
+}
+
+gchar *
+gnc_gsettings_normalize_schema_name (const gchar *name)
+{
+ if (name == NULL)
+ {
+ /* Need to return a newly allocated string */
+ return g_strdup(gnc_gsettings_get_prefix());
+ }
+ if (g_str_has_prefix (name, gnc_gsettings_get_prefix ()))
+ {
+ /* Need to return a newly allocated string */
+ return g_strdup(name);
+ }
+
+ return g_strjoin(".", gnc_gsettings_get_prefix(), name, NULL);
+}
Added: gnucash/trunk/src/app-utils/gnc-gsettings.h
===================================================================
--- gnucash/trunk/src/app-utils/gnc-gsettings.h (rev 0)
+++ gnucash/trunk/src/app-utils/gnc-gsettings.h 2013-10-07 14:04:19 UTC (rev 23217)
@@ -0,0 +1,113 @@
+/********************************************************************\
+ * gnc-gsettings.h -- utility functions for storing/retrieving *
+ * data in the GSettings database for GnuCash *
+ * Copyright (C) 2013 Geert Janssens <geert at kobaltwit.be> *
+ * *
+ * 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 Software Foundation; either version 2 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact: *
+ * *
+ * Free Software Foundation Voice: +1-617-542-5942 *
+ * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
+ * Boston, MA 02110-1301, USA gnu at gnu.org *
+ * *
+\********************************************************************/
+
+/** @addtogroup GLib
+ @{ */
+/** @addtogroup GSettings GSettings Utilities
+
+ The API in this file is designed to make it easy to use the GSettings
+ system from within Gnucash. GSettings is a shared key/value storage
+ system.
+
+ The main benefits of these routines are that they
+ -# maintain a list of GSettings objects (one per schema),
+ -# convert gnucash internal schema names into full gsettings schema id's, and
+ -# optionally take care of error checking on return values.
+
+ @{ */
+/** @file gnc-gsettings.h
+ * @brief GSettings helper routines.
+ * @author Copyright (C) 2013 Geert Janssens <geert at kobaltwit.be>
+ */
+
+
+#ifndef GNC_GSETIINGS_H
+#define GNC_GSETTINGS_H
+
+#include <gio/gio.h>
+
+/* Schema ids used across multiple modules */
+#define GSET_SCHEMA_PREFIX "org.gnucash"
+#define GSET_SCHEMA_GENERAL "general"
+#define GSET_SCHEMA_GENERAL_REGISTER "general.register"
+#define GSET_SCHEMA_GENERAL_REPORT "general.report"
+#define GSET_SCHEMA_WARNINGS "general.warnings"
+#define GSET_SCHEMA_WARNINGS_TEMP "general.warnings.temporary"
+#define GSET_SCHEMA_WARNINGS_PERM "general.warnings.permanent"
+
+/* Keys used across multiple modules */
+/* Currently the first one conflicts with same definition in gnc-gconf-utils.h
+ * Only load it if gnc-gconf-utils.h isn't loaded yet.
+ */
+#ifndef GNC_GCONF_UTILS_H
+#define DESKTOP_GNOME_INTERFACE "/desktop/gnome/interface"
+#endif /* GNC_GCONF_UTILS_H */
+#define GSET_KEY_TOOLBAR_STYLE "toolbar_style"
+#define GSET_KEY_SAVE_GEOMETRY "save_window_geometry"
+#define GSET_KEY_LAST_PATH "last_path"
+#define GSET_KEY_USE_NEW "use_new_window"
+#define GSET_KEY_ACCOUNTING_LABELS "use_accounting_labels"
+#define GSET_KEY_ACCOUNT_SEPARATOR "account_separator"
+#define GSET_KEY_NEGATIVE_IN_RED "negative_in_red"
+#define GSET_KEY_NUM_SOURCE "num_source"
+#define GSET_KEY_ENABLE_EURO "enable_euro"
+#define GSET_KEY_DATE_FORMAT "date_format"
+#define GSET_KEY_DATE_COMPLETION "date_completion"
+#define GSET_KEY_DATE_BACKMONTHS "date_backmonths"
+#define GSET_KEY_SHOW_LEAF_ACCT_NAMES "show_leaf_account_names"
+
+/** Convert a partial schema name into a complete gsettings schema name.
+ *
+ * This function takes a partial gsettings schema name and converts
+ * it into a fully qualified gsettings schema name. It does this
+ * by prepending the standard prefix for all gnucash schemas.
+ * If the schema is already fully qualified (i.e. begins with the
+ * default schema prefix, this routine will not change it.
+ *
+ * @param name A partial schema name. The default prefix is
+ * prepended to this name to produce a fully qualified schema
+ * name.
+ *
+ * @return This function returns a string pointer to the fully
+ * qualified schema name. It is the caller's responsibility to
+ * free this string.
+ */
+gchar *gnc_gsettings_normalize_schema_name (const gchar *name);
+
+
+/** Set the default gsettings schema prefix. This is
+ * used to generate complete schema id's if only
+ * partial id's are passed.
+ */
+void gnc_gsettings_set_prefix (const gchar *prefix);
+
+/** Get the default gsettings schema prefix.
+ * If none was set explicitly, this defaults to
+ * "org.gnucash"
+ */
+const gchar *gnc_gsettings_get_prefix (void);
+
+#endif /* GNC_GSETTINGS_H */
+/** @} */
+/** @} */
Modified: gnucash/trunk/src/bin/gnucash-bin.c
===================================================================
--- gnucash/trunk/src/bin/gnucash-bin.c 2013-10-07 14:03:59 UTC (rev 23216)
+++ gnucash/trunk/src/bin/gnucash-bin.c 2013-10-07 14:04:19 UTC (rev 23217)
@@ -43,6 +43,7 @@
#include "gfec.h"
#include "gnc-commodity.h"
#include "gnc-core-prefs.h"
+#include "gnc-gsettings.h"
#include "gnc-main-window.h"
#include "gnc-splash.h"
#include "gnc-gnome-utils.h"
@@ -81,6 +82,7 @@
static gchar *log_to_filename = NULL;
static int nofile = 0;
static const gchar *gconf_path = NULL;
+static const gchar *gsettings_prefix = NULL;
static const char *add_quotes_file = NULL;
static char *namespace_regexp = NULL;
static const char *file_to_load = NULL;
@@ -127,6 +129,13 @@
N_("GCONFPATH")
},
{
+ "gsettings-prefix", '\0', 0, G_OPTION_ARG_STRING, &gsettings_prefix,
+ N_("Set the prefix for gsettings schemas for gsettings queries. This can be useful to have a different settings tree while debugging."),
+ /* Translators: Argument description for autohelp; see
+ http://developer.gnome.org/doc/API/2.0/glib/glib-Commandline-option-parser.html */
+ N_("GSETTINGSPREFIX")
+ },
+ {
"add-price-quotes", '\0', 0, G_OPTION_ARG_STRING, &add_quotes_file,
N_("Add price quotes to given GnuCash datafile"),
/* Translators: Argument description for autohelp; see
@@ -575,6 +584,16 @@
}
gnc_gconf_set_path_prefix(g_strdup(gconf_path));
+ if (!gsettings_prefix)
+ {
+ const char *prefix = g_getenv("GNC_GSETTINGS_PREFIX");
+ if (prefix)
+ gsettings_prefix = prefix;
+ else
+ gsettings_prefix = GSET_SCHEMA_PREFIX;
+ }
+ gnc_gsettings_set_prefix(g_strdup(gsettings_prefix));
+
if (namespace_regexp)
gnc_core_prefs_set_namespace_regexp(namespace_regexp);
More information about the gnucash-changes
mailing list