]> Pileus Git - grits/blob - src/gis/gis-prefs.c
ea3f05a5a8611c54995c8b8ba8a19fbcdccd7ae8
[grits] / src / gis / gis-prefs.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  * 
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.
8  * 
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.
13  * 
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/>.
16  */
17
18 #include <glib.h>
19 #include "gis-marshal.h"
20 #include "gis-prefs.h"
21
22 /****************
23  * GObject code *
24  ****************/
25 enum {
26         SIG_PREF_CHANGED,
27         NUM_SIGNALS,
28 };
29 static guint signals[NUM_SIGNALS];
30
31 G_DEFINE_TYPE(GisPrefs, gis_prefs, G_TYPE_OBJECT);
32 static void gis_prefs_init(GisPrefs *self)
33 {
34         g_debug("GisPrefs: init");
35         self->key_file = g_key_file_new();
36 }
37 static GObject *gis_prefs_constructor(GType gtype, guint n_properties,
38                 GObjectConstructParam *properties)
39 {
40         g_debug("gis_prefs: constructor");
41         GObjectClass *parent_class = G_OBJECT_CLASS(gis_prefs_parent_class);
42         return  parent_class->constructor(gtype, n_properties, properties);
43 }
44 static void gis_prefs_dispose(GObject *_self)
45 {
46         g_debug("GisPrefs: dispose");
47         GisPrefs *self = GIS_PREFS(_self);
48         if (self->key_file) {
49                 gsize length;
50                 g_mkdir_with_parents(g_path_get_dirname(self->key_path), 0755);
51                 gchar *data = g_key_file_to_data(self->key_file, &length, NULL);
52                 g_file_set_contents(self->key_path, data, length, NULL);
53                 g_key_file_free(self->key_file);
54                 self->key_file = NULL;
55         }
56         G_OBJECT_CLASS(gis_prefs_parent_class)->dispose(_self);
57 }
58 static void gis_prefs_finalize(GObject *_self)
59 {
60         g_debug("GisPrefs: finalize");
61         G_OBJECT_CLASS(gis_prefs_parent_class)->finalize(_self);
62 }
63 static void gis_prefs_class_init(GisPrefsClass *klass)
64 {
65         g_debug("GisPrefs: class_init");
66         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
67         gobject_class->constructor  = gis_prefs_constructor;
68         gobject_class->dispose      = gis_prefs_dispose;
69         gobject_class->finalize     = gis_prefs_finalize;
70         signals[SIG_PREF_CHANGED] = g_signal_new(
71                         "pref-changed",
72                         G_TYPE_FROM_CLASS(gobject_class),
73                         G_SIGNAL_RUN_LAST,
74                         0,
75                         NULL,
76                         NULL,
77                         gis_cclosure_marshal_VOID__STRING_UINT_POINTER,
78                         G_TYPE_NONE,
79                         1,
80                         G_TYPE_STRING,
81                         G_TYPE_UINT,
82                         G_TYPE_POINTER);
83 }
84
85 /***********
86  * Methods *
87  ***********/
88 GisPrefs *gis_prefs_new(const gchar *prog)
89 {
90         g_debug("GisPrefs: new - %s", prog);
91         GisPrefs *self = g_object_new(GIS_TYPE_PREFS, NULL);
92         self->key_path = g_build_filename(g_get_user_config_dir(),
93                         prog, "config.ini", NULL);
94         GError *error = NULL;
95         g_key_file_load_from_file(self->key_file, self->key_path,
96                         G_KEY_FILE_KEEP_COMMENTS, &error);
97         if (error) {
98                 g_debug("GisPrefs: new - Trying %s defaults", prog);
99                 g_clear_error(&error);
100                 gchar *tmp = g_build_filename(DATADIR, prog, "defaults.ini", NULL);
101                 g_key_file_load_from_file(self->key_file, tmp,
102                                 G_KEY_FILE_KEEP_COMMENTS, &error);
103                 g_free(tmp);
104         }
105         if (error) {
106                 g_debug("GisPrefs: new - Trying GIS defaults");
107                 g_clear_error(&error);
108                 gchar *tmp = g_build_filename(DATADIR, "gis", "defaults.ini", NULL);
109                 g_key_file_load_from_file(self->key_file, tmp,
110                                 G_KEY_FILE_KEEP_COMMENTS, &error);
111                 g_free(tmp);
112         }
113         if (error) {
114                 g_clear_error(&error);
115                 g_warning("GisPrefs: new - Unable to load key file `%s': %s",
116                         self->key_path, error->message);
117         }
118         return self;
119 }
120
121 #define make_pref_type(name, c_type, g_type)                                         \
122 c_type gis_prefs_get_##name##_v(GisPrefs *self,                                      \
123                 const gchar *group, const gchar *key)                                \
124 {                                                                                    \
125         GError *error = NULL;                                                        \
126         c_type value = g_key_file_get_##name(self->key_file, group, key, &error);    \
127         if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND)                \
128                 g_warning("GisPrefs: get_value_##name - error getting key %s: %s\n", \
129                                 key, error->message);                                \
130         return value;                                                                \
131 }                                                                                    \
132 c_type gis_prefs_get_##name(GisPrefs *self, const gchar *key)                        \
133 {                                                                                    \
134         gchar **keys  = g_strsplit(key, "/", 2);                                     \
135         c_type value = gis_prefs_get_##name##_v(self, keys[0], keys[1]);             \
136         g_strfreev(keys);                                                            \
137         return value;                                                                \
138 }                                                                                    \
139                                                                                      \
140 void gis_prefs_set_##name##_v(GisPrefs *self,                                        \
141                 const gchar *group, const gchar *key, const c_type value)            \
142 {                                                                                    \
143         g_key_file_set_##name(self->key_file, group, key, value);                    \
144         gchar *all = g_strconcat(group, "/", key, NULL);                             \
145         g_signal_emit(self, signals[SIG_PREF_CHANGED], 0,                            \
146                         all, g_type, &value);                                        \
147         g_free(all);                                                                 \
148 }                                                                                    \
149 void gis_prefs_set_##name(GisPrefs *self, const gchar *key, const c_type value)      \
150 {                                                                                    \
151         gchar **keys = g_strsplit(key, "/", 2);                                      \
152         gis_prefs_set_##name##_v(self, keys[0], keys[1], value);                     \
153         g_strfreev(keys);                                                            \
154 }                                                                                    \
155
156 make_pref_type(string,  gchar*,   G_TYPE_STRING)
157 make_pref_type(boolean, gboolean, G_TYPE_BOOLEAN)
158 make_pref_type(integer, gint,     G_TYPE_INT)
159 make_pref_type(double,  gdouble,  G_TYPE_DOUBLE)