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 * #GritsPrefs is used to store and access preferences in grits. It is mostly a
23 * wrapper around a #GKeyFile. Preferences can be stored for the application
24 * using grits, but may also be stored by grits itself. An example of this are
25 * whether grits is in online or offline mode. Many #GritsPlugin<!-- -->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 "grits-marshal.h"
37 #include "grits-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 #GritsPrefs
58 GritsPrefs *grits_prefs_new(const gchar *config, const gchar *defaults)
60 g_debug("GritsPrefs: new - %s, %s", config, defaults);
61 GritsPrefs *prefs = g_object_new(GRITS_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("GritsPrefs: 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("GritsPrefs: new - Trying GRITS 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("GritsPrefs: new - Unable to load key file `%s': %s",
86 prefs->key_path, error->message);
88 g_debug("GritsPrefs: new - using %s", prefs->key_path);
92 #define make_pref_type(name, c_type, g_type) \
93 c_type grits_prefs_get_##name##_v(GritsPrefs *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 error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) \
100 g_warning("GritsPrefs: get_"#name" - error getting key %s: %s\n", \
101 key, error->message); \
102 if (error && _error) \
106 c_type grits_prefs_get_##name(GritsPrefs *prefs, const gchar *key, GError **error) \
108 gchar **keys = g_strsplit(key, "/", 2); \
109 c_type value = grits_prefs_get_##name##_v(prefs, keys[0], keys[1], error); \
114 void grits_prefs_set_##name##_v(GritsPrefs *prefs, \
115 const gchar *group, const gchar *key, const c_type value) \
117 g_key_file_set_##name(prefs->key_file, group, key, value); \
118 gchar *all = g_strconcat(group, "/", key, NULL); \
119 g_signal_emit(prefs, signals[SIG_PREF_CHANGED], 0, \
120 all, g_type, &value); \
123 void grits_prefs_set_##name(GritsPrefs *prefs, const gchar *key, const c_type value) \
125 gchar **keys = g_strsplit(key, "/", 2); \
126 grits_prefs_set_##name##_v(prefs, keys[0], keys[1], value); \
130 make_pref_type(string, gchar*, G_TYPE_STRING)
131 make_pref_type(boolean, gboolean, G_TYPE_BOOLEAN)
132 make_pref_type(integer, gint, G_TYPE_INT)
133 make_pref_type(double, gdouble, G_TYPE_DOUBLE)
139 G_DEFINE_TYPE(GritsPrefs, grits_prefs, G_TYPE_OBJECT);
140 static void grits_prefs_init(GritsPrefs *prefs)
142 g_debug("GritsPrefs: init");
143 prefs->key_file = g_key_file_new();
145 static void grits_prefs_dispose(GObject *_prefs)
147 g_debug("GritsPrefs: dispose");
148 GritsPrefs *prefs = GRITS_PREFS(_prefs);
149 if (prefs->key_file) {
151 gchar *dir = g_path_get_dirname(prefs->key_path);
152 g_mkdir_with_parents(dir, 0755);
153 gchar *data = g_key_file_to_data(prefs->key_file, &length, NULL);
154 g_file_set_contents(prefs->key_path, data, length, NULL);
155 g_key_file_free(prefs->key_file);
156 g_free(prefs->key_path);
159 prefs->key_file = NULL;
161 G_OBJECT_CLASS(grits_prefs_parent_class)->dispose(_prefs);
163 static void grits_prefs_class_init(GritsPrefsClass *klass)
165 g_debug("GritsPrefs: class_init");
166 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
167 gobject_class->dispose = grits_prefs_dispose;
170 * GritsPrefs::pref-changed:
171 * @prefs: the preference store.
172 * @key: the key to the preference.
173 * @type: the type of the preference that changed.
174 * @value: a pointer to the value of the preference.
176 * The ::pref-changed signal is emitted each time a preference is
179 signals[SIG_PREF_CHANGED] = g_signal_new(
181 G_TYPE_FROM_CLASS(gobject_class),
186 grits_cclosure_marshal_VOID__STRING_UINT_POINTER,