2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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/>.
21 #include "gis-marshal.h"
22 #include "gis-prefs.h"
28 static guint signals[NUM_SIGNALS];
33 GisPrefs *gis_prefs_new(const gchar *config, const gchar *defaults)
35 g_debug("GisPrefs: new - %s, %s", config, defaults);
36 GisPrefs *self = g_object_new(GIS_TYPE_PREFS, NULL);
38 self->key_path = g_strdup(config);
40 self->key_path = g_build_filename(g_get_user_config_dir(),
41 PACKAGE, "config.ini", NULL);
43 g_key_file_load_from_file(self->key_file, self->key_path,
44 G_KEY_FILE_KEEP_COMMENTS, &error);
45 if (error && defaults) {
46 g_debug("GisPrefs: new - Trying defaults");
47 g_clear_error(&error);
48 g_key_file_load_from_file(self->key_file, defaults,
49 G_KEY_FILE_KEEP_COMMENTS, &error);
52 g_debug("GisPrefs: new - Trying GIS defaults");
53 g_clear_error(&error);
54 gchar *tmp = g_build_filename(PKGDATADIR, "defaults.ini", NULL);
55 g_key_file_load_from_file(self->key_file, tmp,
56 G_KEY_FILE_KEEP_COMMENTS, &error);
60 g_warning("GisPrefs: new - Unable to load key file `%s': %s",
61 self->key_path, error->message);
66 #define make_pref_type(name, c_type, g_type) \
67 c_type gis_prefs_get_##name##_v(GisPrefs *self, \
68 const gchar *group, const gchar *key, GError **_error) \
70 GError *error = NULL; \
71 c_type value = g_key_file_get_##name(self->key_file, group, key, &error); \
72 if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND) \
73 g_warning("GisPrefs: get_value_##name - error getting key %s: %s\n", \
74 key, error->message); \
75 if (error && _error) \
79 c_type gis_prefs_get_##name(GisPrefs *self, const gchar *key, GError **error) \
81 gchar **keys = g_strsplit(key, "/", 2); \
82 c_type value = gis_prefs_get_##name##_v(self, keys[0], keys[1], error); \
87 void gis_prefs_set_##name##_v(GisPrefs *self, \
88 const gchar *group, const gchar *key, const c_type value) \
90 g_key_file_set_##name(self->key_file, group, key, value); \
91 gchar *all = g_strconcat(group, "/", key, NULL); \
92 g_signal_emit(self, signals[SIG_PREF_CHANGED], 0, \
93 all, g_type, &value); \
96 void gis_prefs_set_##name(GisPrefs *self, const gchar *key, const c_type value) \
98 gchar **keys = g_strsplit(key, "/", 2); \
99 gis_prefs_set_##name##_v(self, keys[0], keys[1], value); \
103 make_pref_type(string, gchar*, G_TYPE_STRING)
104 make_pref_type(boolean, gboolean, G_TYPE_BOOLEAN)
105 make_pref_type(integer, gint, G_TYPE_INT)
106 make_pref_type(double, gdouble, G_TYPE_DOUBLE)
112 G_DEFINE_TYPE(GisPrefs, gis_prefs, G_TYPE_OBJECT);
113 static void gis_prefs_init(GisPrefs *self)
115 g_debug("GisPrefs: init");
116 self->key_file = g_key_file_new();
118 static void gis_prefs_dispose(GObject *_self)
120 g_debug("GisPrefs: dispose");
121 GisPrefs *self = GIS_PREFS(_self);
122 if (self->key_file) {
124 gchar *dir = g_path_get_dirname(self->key_path);
125 g_mkdir_with_parents(dir, 0755);
126 gchar *data = g_key_file_to_data(self->key_file, &length, NULL);
127 g_file_set_contents(self->key_path, data, length, NULL);
128 g_key_file_free(self->key_file);
129 g_free(self->key_path);
132 self->key_file = NULL;
134 G_OBJECT_CLASS(gis_prefs_parent_class)->dispose(_self);
136 static void gis_prefs_class_init(GisPrefsClass *klass)
138 g_debug("GisPrefs: class_init");
139 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
140 gobject_class->dispose = gis_prefs_dispose;
141 signals[SIG_PREF_CHANGED] = g_signal_new(
143 G_TYPE_FROM_CLASS(gobject_class),
148 gis_cclosure_marshal_VOID__STRING_UINT_POINTER,