]> Pileus Git - grits/blob - src/grits-prefs.c
Save prefs after changes
[grits] / src / grits-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:grits-prefs
20  * @short_description: Persistent preference handing
21  *
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
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 "grits-marshal.h"
37 #include "grits-prefs.h"
38
39 enum {
40         SIG_PREF_CHANGED,
41         NUM_SIGNALS,
42 };
43 static guint signals[NUM_SIGNALS];
44
45 /* Helper functions */
46 static void grits_prefs_save(GritsPrefs *prefs)
47 {
48         gsize length;
49         gchar *dir = g_path_get_dirname(prefs->key_path);
50         if (!g_file_test(dir, G_FILE_TEST_EXISTS))
51                 g_mkdir_with_parents(dir, 0755);
52         gchar *data = g_key_file_to_data(prefs->key_file, &length, NULL);
53         g_file_set_contents(prefs->key_path, data, length, NULL);
54         g_free(dir);
55         g_free(data);
56 }
57
58 /***********
59  * Methods *
60  ***********/
61 /**
62  * grits_prefs_new:
63  * @config:   the path to the config file
64  * @defaults: the path to the default config file
65  *
66  * Create a new preference object for the given @config. If the config does not
67  * exist the @defaults file is loaded.
68  *
69  * Returns: the new #GritsPrefs
70  */
71 GritsPrefs *grits_prefs_new(const gchar *config, const gchar *defaults)
72 {
73         g_debug("GritsPrefs: new - %s, %s", config, defaults);
74         GritsPrefs *prefs = g_object_new(GRITS_TYPE_PREFS, NULL);
75         if (config)
76                 prefs->key_path = g_strdup(config);
77         else
78                 prefs->key_path = g_build_filename(g_get_user_config_dir(),
79                                 PACKAGE, "config.ini", NULL);
80         GError *error = NULL;
81         g_key_file_load_from_file(prefs->key_file, prefs->key_path,
82                         G_KEY_FILE_KEEP_COMMENTS, &error);
83         if (error && defaults) {
84                 g_debug("GritsPrefs: new - Trying defaults");
85                 g_clear_error(&error);
86                 g_key_file_load_from_file(prefs->key_file, defaults,
87                                 G_KEY_FILE_KEEP_COMMENTS, &error);
88         }
89         if (error) {
90                 g_debug("GritsPrefs: new - Trying GRITS defaults");
91                 g_clear_error(&error);
92                 gchar *tmp = g_build_filename(PKGDATADIR, "defaults.ini", NULL);
93                 g_key_file_load_from_file(prefs->key_file, tmp,
94                                 G_KEY_FILE_KEEP_COMMENTS, &error);
95                 g_free(tmp);
96         }
97         if (error) {
98                 g_debug("GritsPrefs: new - Unable to load key file `%s': %s",
99                         prefs->key_path, error->message);
100         }
101         g_debug("GritsPrefs: new - using %s", prefs->key_path);
102         return prefs;
103 }
104
105 #define make_pref_type(name, c_type, g_type)                                         \
106 c_type grits_prefs_get_##name##_v(GritsPrefs *prefs,                                 \
107                 const gchar *group, const gchar *key, GError **_error)               \
108 {                                                                                    \
109         GError *error = NULL;                                                        \
110         c_type value = g_key_file_get_##name(prefs->key_file, group, key, &error);   \
111         if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND &&              \
112                      error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)                  \
113                 g_warning("GritsPrefs: get_"#name" - error getting key %s: %s\n",    \
114                                 key, error->message);                                \
115         if (error && _error)                                                         \
116                 *_error = error;                                                     \
117         return value;                                                                \
118 }                                                                                    \
119 c_type grits_prefs_get_##name(GritsPrefs *prefs, const gchar *key, GError **error)   \
120 {                                                                                    \
121         gchar **keys  = g_strsplit(key, "/", 2);                                     \
122         c_type value = grits_prefs_get_##name##_v(prefs, keys[0], keys[1], error);   \
123         g_strfreev(keys);                                                            \
124         return value;                                                                \
125 }                                                                                    \
126                                                                                      \
127 void grits_prefs_set_##name##_v(GritsPrefs *prefs,                                   \
128                 const gchar *group, const gchar *key, const c_type value)            \
129 {                                                                                    \
130         g_key_file_set_##name(prefs->key_file, group, key, value);                   \
131         gchar *all = g_strconcat(group, "/", key, NULL);                             \
132         g_signal_emit(prefs, signals[SIG_PREF_CHANGED], 0,                           \
133                         all, g_type, &value);                                        \
134         grits_prefs_save(prefs);                                                     \
135         g_free(all);                                                                 \
136 }                                                                                    \
137 void grits_prefs_set_##name(GritsPrefs *prefs, const gchar *key, const c_type value) \
138 {                                                                                    \
139         gchar **keys = g_strsplit(key, "/", 2);                                      \
140         grits_prefs_set_##name##_v(prefs, keys[0], keys[1], value);                  \
141         g_strfreev(keys);                                                            \
142 }                                                                                    \
143
144 make_pref_type(string,  gchar*,   G_TYPE_STRING)
145 make_pref_type(boolean, gboolean, G_TYPE_BOOLEAN)
146 make_pref_type(integer, gint,     G_TYPE_INT)
147 make_pref_type(double,  gdouble,  G_TYPE_DOUBLE)
148
149
150 /****************
151  * GObject code *
152  ****************/
153 G_DEFINE_TYPE(GritsPrefs, grits_prefs, G_TYPE_OBJECT);
154 static void grits_prefs_init(GritsPrefs *prefs)
155 {
156         g_debug("GritsPrefs: init");
157         prefs->key_file = g_key_file_new();
158 }
159 static void grits_prefs_dispose(GObject *_prefs)
160 {
161         g_debug("GritsPrefs: dispose");
162         GritsPrefs *prefs = GRITS_PREFS(_prefs);
163         if (prefs->key_file) {
164                 grits_prefs_save(prefs);
165                 g_key_file_free(prefs->key_file);
166                 g_free(prefs->key_path);
167                 prefs->key_file = NULL;
168         }
169         G_OBJECT_CLASS(grits_prefs_parent_class)->dispose(_prefs);
170 }
171 static void grits_prefs_class_init(GritsPrefsClass *klass)
172 {
173         g_debug("GritsPrefs: class_init");
174         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
175         gobject_class->dispose = grits_prefs_dispose;
176
177         /**
178          * GritsPrefs::pref-changed:
179          * @prefs: the preference store.
180          * @key:   the key to the preference.
181          * @type:  the type of the preference that changed.
182          * @value: a pointer to the value of the preference.
183          *
184          * The ::pref-changed signal is emitted each time a preference is
185          * changed.
186          */
187         signals[SIG_PREF_CHANGED] = g_signal_new(
188                         "pref-changed",
189                         G_TYPE_FROM_CLASS(gobject_class),
190                         G_SIGNAL_RUN_LAST,
191                         0,
192                         NULL,
193                         NULL,
194                         grits_cclosure_marshal_VOID__STRING_UINT_POINTER,
195                         G_TYPE_NONE,
196                         3,
197                         G_TYPE_STRING,
198                         G_TYPE_UINT,
199                         G_TYPE_POINTER);
200 }