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