2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @short_description: Persistent preference handing
22 * #GisPrefs is used to store and access preferences in libgis. It is mostly a
23 * wrapper around a #GKeyFile. Preferences can be stored for the application
24 * using libgis, but may also be stored by libgis itself. An example of this are
25 * whether libgis is in online or offline mode. Many #GisPlugin<!-- -->s also
28 * There are two variants of preference functions. The normal variant takes
29 * group and a key separated by a "/" as they key to the preference. The "_v"
30 * variant takes the group and the key as separate parameters.
36 #include "gis-marshal.h"
37 #include "gis-prefs.h"
43 static guint signals[NUM_SIGNALS];
50 * @config: the path to the config file
51 * @defaults: the path to the default config file
53 * Create a new preference object for the given @config. If the config does not
54 * exist the @defaults file is loaded.
56 * Returns: the new #GisPrefs
58 GisPrefs *gis_prefs_new(const gchar *config, const gchar *defaults)
60 g_debug("GisPrefs: new - %s, %s", config, defaults);
61 GisPrefs *prefs = g_object_new(GIS_TYPE_PREFS, NULL);
63 prefs->key_path = g_strdup(config);
65 prefs->key_path = g_build_filename(g_get_user_config_dir(),
66 PACKAGE, "config.ini", NULL);
68 g_key_file_load_from_file(prefs->key_file, prefs->key_path,
69 G_KEY_FILE_KEEP_COMMENTS, &error);
70 if (error && defaults) {
71 g_debug("GisPrefs: new - Trying defaults");
72 g_clear_error(&error);
73 g_key_file_load_from_file(prefs->key_file, defaults,
74 G_KEY_FILE_KEEP_COMMENTS, &error);
77 g_debug("GisPrefs: new - Trying GIS defaults");
78 g_clear_error(&error);
79 gchar *tmp = g_build_filename(PKGDATADIR, "defaults.ini", NULL);
80 g_key_file_load_from_file(prefs->key_file, tmp,
81 G_KEY_FILE_KEEP_COMMENTS, &error);
85 g_debug("GisPrefs: new - Unable to load key file `%s': %s",
86 prefs->key_path, error->message);
88 g_debug("GisPrefs: new - using %s", prefs->key_path);
92 #define make_pref_type(name, c_type, g_type) \
93 c_type gis_prefs_get_##name##_v(GisPrefs *prefs, \
94 const gchar *group, const gchar *key, GError **_error) \
96 GError *error = NULL; \
97 c_type value = g_key_file_get_##name(prefs->key_file, group, key, &error); \
98 if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND) \
99 g_warning("GisPrefs: get_value_##name - error getting key %s: %s\n", \
100 key, error->message); \
101 if (error && _error) \
105 c_type gis_prefs_get_##name(GisPrefs *prefs, const gchar *key, GError **error) \
107 gchar **keys = g_strsplit(key, "/", 2); \
108 c_type value = gis_prefs_get_##name##_v(prefs, keys[0], keys[1], error); \
113 void gis_prefs_set_##name##_v(GisPrefs *prefs, \
114 const gchar *group, const gchar *key, const c_type value) \
116 g_key_file_set_##name(prefs->key_file, group, key, value); \
117 gchar *all = g_strconcat(group, "/", key, NULL); \
118 g_signal_emit(prefs, signals[SIG_PREF_CHANGED], 0, \
119 all, g_type, &value); \
122 void gis_prefs_set_##name(GisPrefs *prefs, const gchar *key, const c_type value) \
124 gchar **keys = g_strsplit(key, "/", 2); \
125 gis_prefs_set_##name##_v(prefs, keys[0], keys[1], value); \
129 make_pref_type(string, gchar*, G_TYPE_STRING)
130 make_pref_type(boolean, gboolean, G_TYPE_BOOLEAN)
131 make_pref_type(integer, gint, G_TYPE_INT)
132 make_pref_type(double, gdouble, G_TYPE_DOUBLE)
138 G_DEFINE_TYPE(GisPrefs, gis_prefs, G_TYPE_OBJECT);
139 static void gis_prefs_init(GisPrefs *prefs)
141 g_debug("GisPrefs: init");
142 prefs->key_file = g_key_file_new();
144 static void gis_prefs_dispose(GObject *_prefs)
146 g_debug("GisPrefs: dispose");
147 GisPrefs *prefs = GIS_PREFS(_prefs);
148 if (prefs->key_file) {
150 gchar *dir = g_path_get_dirname(prefs->key_path);
151 g_mkdir_with_parents(dir, 0755);
152 gchar *data = g_key_file_to_data(prefs->key_file, &length, NULL);
153 g_file_set_contents(prefs->key_path, data, length, NULL);
154 g_key_file_free(prefs->key_file);
155 g_free(prefs->key_path);
158 prefs->key_file = NULL;
160 G_OBJECT_CLASS(gis_prefs_parent_class)->dispose(_prefs);
162 static void gis_prefs_class_init(GisPrefsClass *klass)
164 g_debug("GisPrefs: class_init");
165 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
166 gobject_class->dispose = gis_prefs_dispose;
169 * GisPrefs::pref-changed:
170 * @prefs: the preference store.
171 * @key: the key to the preference.
172 * @type: the type of the preference that changed.
173 * @value: a pointer to the value of the preference.
175 * The ::pref-changed signal is emitted each time a preference is
178 signals[SIG_PREF_CHANGED] = g_signal_new(
180 G_TYPE_FROM_CLASS(gobject_class),
185 gis_cclosure_marshal_VOID__STRING_UINT_POINTER,