2 * Copyright (C) 2009-2011 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.
37 #include "grits-marshal.h"
38 #include "grits-prefs.h"
44 static guint signals[NUM_SIGNALS];
46 /* Helper functions */
47 static void grits_prefs_save(GritsPrefs *prefs)
49 g_debug("GritsPrefs: save");
51 gchar *dir = g_path_get_dirname(prefs->key_path);
52 if (!g_file_test(dir, G_FILE_TEST_EXISTS))
53 g_mkdir_with_parents(dir, 0755);
54 gchar *data = g_key_file_to_data(prefs->key_file, &length, NULL);
55 g_file_set_contents(prefs->key_path, data, length, NULL);
59 static gboolean grits_prefs_try_save(GritsPrefs *prefs)
61 const time_t interval = 1;
62 static time_t lastsave = 0;
63 static guint source = 0;
64 if (time(NULL) - lastsave > interval) {
65 grits_prefs_save(prefs);
66 lastsave = time(NULL);
68 } else if (source == 0) {
69 source = g_timeout_add_seconds(interval,
70 (GSourceFunc)grits_prefs_try_save, prefs);
80 * @config: the path to the config file
81 * @defaults: the path to the default config file
83 * Create a new preference object for the given @config. If the config does not
84 * exist the @defaults file is loaded.
86 * Returns: the new #GritsPrefs
88 GritsPrefs *grits_prefs_new(const gchar *config, const gchar *defaults)
90 g_debug("GritsPrefs: new - %s, %s", config, defaults);
91 GritsPrefs *prefs = g_object_new(GRITS_TYPE_PREFS, NULL);
93 prefs->key_path = g_strdup(config);
95 prefs->key_path = g_build_filename(g_get_user_config_dir(),
96 PACKAGE, "config.ini", NULL);
98 g_key_file_load_from_file(prefs->key_file, prefs->key_path,
99 G_KEY_FILE_KEEP_COMMENTS, &error);
100 if (error && defaults) {
101 g_debug("GritsPrefs: new - Trying defaults");
102 g_clear_error(&error);
103 g_key_file_load_from_file(prefs->key_file, defaults,
104 G_KEY_FILE_KEEP_COMMENTS, &error);
107 g_debug("GritsPrefs: new - Trying GRITS defaults");
108 g_clear_error(&error);
109 gchar *tmp = g_build_filename(PKGDATADIR, "defaults.ini", NULL);
110 g_key_file_load_from_file(prefs->key_file, tmp,
111 G_KEY_FILE_KEEP_COMMENTS, &error);
115 g_debug("GritsPrefs: new - Unable to load key file `%s': %s",
116 prefs->key_path, error->message);
118 g_debug("GritsPrefs: new - using %s", prefs->key_path);
122 #define make_pref_type(name, c_type, g_type) \
123 c_type grits_prefs_get_##name##_v(GritsPrefs *prefs, \
124 const gchar *group, const gchar *key, GError **_error) \
126 GError *error = NULL; \
127 c_type value = g_key_file_get_##name(prefs->key_file, group, key, &error); \
128 if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND && \
129 error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) \
130 g_warning("GritsPrefs: get_"#name" - error getting key %s: %s\n", \
131 key, error->message); \
132 if (error && _error) \
136 c_type grits_prefs_get_##name(GritsPrefs *prefs, const gchar *key, GError **error) \
138 gchar **keys = g_strsplit(key, "/", 2); \
139 c_type value = grits_prefs_get_##name##_v(prefs, keys[0], keys[1], error); \
144 void grits_prefs_set_##name##_v(GritsPrefs *prefs, \
145 const gchar *group, const gchar *key, const c_type value) \
147 g_key_file_set_##name(prefs->key_file, group, key, value); \
148 gchar *all = g_strconcat(group, "/", key, NULL); \
149 g_signal_emit(prefs, signals[SIG_PREF_CHANGED], 0, \
150 all, g_type, &value); \
151 grits_prefs_try_save(prefs); \
154 void grits_prefs_set_##name(GritsPrefs *prefs, const gchar *key, const c_type value) \
156 gchar **keys = g_strsplit(key, "/", 2); \
157 grits_prefs_set_##name##_v(prefs, keys[0], keys[1], value); \
161 make_pref_type(string, gchar*, G_TYPE_STRING)
162 make_pref_type(boolean, gboolean, G_TYPE_BOOLEAN)
163 make_pref_type(integer, gint, G_TYPE_INT)
164 make_pref_type(double, gdouble, G_TYPE_DOUBLE)
170 G_DEFINE_TYPE(GritsPrefs, grits_prefs, G_TYPE_OBJECT);
171 static void grits_prefs_init(GritsPrefs *prefs)
173 g_debug("GritsPrefs: init");
174 prefs->key_file = g_key_file_new();
176 static void grits_prefs_dispose(GObject *_prefs)
178 g_debug("GritsPrefs: dispose");
179 GritsPrefs *prefs = GRITS_PREFS(_prefs);
180 if (prefs->key_file) {
181 grits_prefs_save(prefs);
182 g_key_file_free(prefs->key_file);
183 g_free(prefs->key_path);
184 prefs->key_file = NULL;
186 G_OBJECT_CLASS(grits_prefs_parent_class)->dispose(_prefs);
188 static void grits_prefs_class_init(GritsPrefsClass *klass)
190 g_debug("GritsPrefs: class_init");
191 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
192 gobject_class->dispose = grits_prefs_dispose;
195 * GritsPrefs::pref-changed:
196 * @prefs: the preference store.
197 * @key: the key to the preference.
198 * @type: the type of the preference that changed.
199 * @value: a pointer to the value of the preference.
201 * The ::pref-changed signal is emitted each time a preference is
204 signals[SIG_PREF_CHANGED] = g_signal_new(
206 G_TYPE_FROM_CLASS(gobject_class),
211 grits_cclosure_marshal_VOID__STRING_UINT_POINTER,