diff --git a/src/app-utils/Makefile.am b/src/app-utils/Makefile.am index 0a4d01cb7e..9a2d72d8a4 100644 --- a/src/app-utils/Makefile.am +++ b/src/app-utils/Makefile.am @@ -54,6 +54,7 @@ libgncmod_app_utils_la_SOURCES = \ 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 @@ gncinclude_HEADERS = \ 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 \ diff --git a/src/app-utils/gnc-gsettings.c b/src/app-utils/gnc-gsettings.c new file mode 100644 index 0000000000..ffc1cea7c7 --- /dev/null +++ b/src/app-utils/gnc-gsettings.c @@ -0,0 +1,124 @@ +/********************************************************************\ + * gnc-gsettings.c -- utility functions for storing/retrieving * + * data in the GSettings database for GnuCash * + * Copyright (C) 2013 Geert Janssens * + * * + * 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@gnu.org * + * * +\********************************************************************/ + +#include "config.h" + +#include +#include +#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); +} diff --git a/src/app-utils/gnc-gsettings.h b/src/app-utils/gnc-gsettings.h new file mode 100644 index 0000000000..5c1a66563b --- /dev/null +++ b/src/app-utils/gnc-gsettings.h @@ -0,0 +1,113 @@ +/********************************************************************\ + * gnc-gsettings.h -- utility functions for storing/retrieving * + * data in the GSettings database for GnuCash * + * Copyright (C) 2013 Geert Janssens * + * * + * 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@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 + */ + + +#ifndef GNC_GSETIINGS_H +#define GNC_GSETTINGS_H + +#include + +/* 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 */ +/** @} */ +/** @} */ diff --git a/src/bin/gnucash-bin.c b/src/bin/gnucash-bin.c index 1387dfdf65..eca3c78e16 100644 --- a/src/bin/gnucash-bin.c +++ b/src/bin/gnucash-bin.c @@ -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_flags = NULL; 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; @@ -126,6 +128,13 @@ static GOptionEntry options[] = http://developer.gnome.org/doc/API/2.0/glib/glib-Commandline-option-parser.html */ 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"), @@ -575,6 +584,16 @@ gnc_parse_command_line(int *argc, char ***argv) } 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);