]> Pileus Git - grits/blob - src/gis-prefs.c
Miscellaneous bug fixes
[grits] / src / gis-prefs.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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 /**
19  * SECTION:gis-prefs
20  * @short_description: Persistent preference handing
21  *
22  * #GisPrefs is used to store and access preferences in libgis. It is mostly a
23  * wrapper around a #GKeyFile. Preferences can be stored for the application
24  * using libgis, but may also be stored by libgis itself. An example of this are
25  * whether libgis is in online or offline mode. Many #GisPlugin<!-- -->s also
26  * store preferences.
27  *
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.
31  */
32
33 #include <config.h>
34
35 #include <glib.h>
36 #include "gis-marshal.h"
37 #include "gis-prefs.h"
38
39 enum {
40         SIG_PREF_CHANGED,
41         NUM_SIGNALS,
42 };
43 static guint signals[NUM_SIGNALS];
44
45 /***********
46  * Methods *
47  ***********/
48 /**
49  * gis_prefs_new:
50  * @config:   the path to the config file
51  * @defaults: the path to the default config file
52  *
53  * Create a new preference object for the given @config. If the config does not
54  * exist the @defaults file is loaded.
55  *
56  * Returns: the new #GisPrefs
57  */
58 GisPrefs *gis_prefs_new(const gchar *config, const gchar *defaults)
59 {
60         g_debug("GisPrefs: new - %s, %s", config, defaults);
61         GisPrefs *prefs = g_object_new(GIS_TYPE_PREFS, NULL);
62         if (config)
63                 prefs->key_path = g_strdup(config);
64         else
65                 prefs->key_path = g_build_filename(g_get_user_config_dir(),
66                                 PACKAGE, "config.ini", NULL);
67         GError *error = 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("GisPrefs: 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);
75         }
76         if (error) {
77                 g_debug("GisPrefs: new - Trying GIS 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);
82                 g_free(tmp);
83         }
84         if (error) {
85                 g_debug("GisPrefs: new - Unable to load key file `%s': %s",
86                         prefs->key_path, error->message);
87         }
88         g_debug("GisPrefs: new - using %s", prefs->key_path);
89         return prefs;
90 }
91
92 #define make_pref_type(name, c_type, g_type)                                         \
93 c_type gis_prefs_get_##name##_v(GisPrefs *prefs,                                     \
94                 const gchar *group, const gchar *key, GError **_error)               \
95 {                                                                                    \
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("GisPrefs: get_"#name" - error getting key %s: %s\n",      \
101                                 key, error->message);                                \
102         if (error && _error)                                                         \
103                 *_error = error;                                                     \
104         return value;                                                                \
105 }                                                                                    \
106 c_type gis_prefs_get_##name(GisPrefs *prefs, const gchar *key, GError **error)       \
107 {                                                                                    \
108         gchar **keys  = g_strsplit(key, "/", 2);                                     \
109         c_type value = gis_prefs_get_##name##_v(prefs, keys[0], keys[1], error);     \
110         g_strfreev(keys);                                                            \
111         return value;                                                                \
112 }                                                                                    \
113                                                                                      \
114 void gis_prefs_set_##name##_v(GisPrefs *prefs,                                       \
115                 const gchar *group, const gchar *key, const c_type value)            \
116 {                                                                                    \
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);                                        \
121         g_free(all);                                                                 \
122 }                                                                                    \
123 void gis_prefs_set_##name(GisPrefs *prefs, const gchar *key, const c_type value)     \
124 {                                                                                    \
125         gchar **keys = g_strsplit(key, "/", 2);                                      \
126         gis_prefs_set_##name##_v(prefs, keys[0], keys[1], value);                    \
127         g_strfreev(keys);                                                            \
128 }                                                                                    \
129
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)
134
135
136 /****************
137  * GObject code *
138  ****************/
139 G_DEFINE_TYPE(GisPrefs, gis_prefs, G_TYPE_OBJECT);
140 static void gis_prefs_init(GisPrefs *prefs)
141 {
142         g_debug("GisPrefs: init");
143         prefs->key_file = g_key_file_new();
144 }
145 static void gis_prefs_dispose(GObject *_prefs)
146 {
147         g_debug("GisPrefs: dispose");
148         GisPrefs *prefs = GIS_PREFS(_prefs);
149         if (prefs->key_file) {
150                 gsize length;
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);
157                 g_free(dir);
158                 g_free(data);
159                 prefs->key_file = NULL;
160         }
161         G_OBJECT_CLASS(gis_prefs_parent_class)->dispose(_prefs);
162 }
163 static void gis_prefs_class_init(GisPrefsClass *klass)
164 {
165         g_debug("GisPrefs: class_init");
166         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
167         gobject_class->dispose = gis_prefs_dispose;
168
169         /**
170          * GisPrefs::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.
175          *
176          * The ::pref-changed signal is emitted each time a preference is
177          * changed.
178          */
179         signals[SIG_PREF_CHANGED] = g_signal_new(
180                         "pref-changed",
181                         G_TYPE_FROM_CLASS(gobject_class),
182                         G_SIGNAL_RUN_LAST,
183                         0,
184                         NULL,
185                         NULL,
186                         gis_cclosure_marshal_VOID__STRING_UINT_POINTER,
187                         G_TYPE_NONE,
188                         3,
189                         G_TYPE_STRING,
190                         G_TYPE_UINT,
191                         G_TYPE_POINTER);
192 }