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