This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dg-settings.c
312 lines (253 loc) · 9.14 KB
/
dg-settings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* dg-settings.c
* Copyright (C) 2014 Adrian Perez <aperez@igalia.com>
*
* Distributed under terms of the MIT license.
*/
#include "dg-settings.h"
#include <gio/gio.h>
enum {
PROP_0,
PROP_SETTINGS_PATH,
PROP_SETTINGS_MONITORING_ENABLED,
PROP_N
};
typedef struct _DgSettingsPrivate DgSettingsPrivate;
struct _DgSettingsPrivate {
GFile *settings_path;
gboolean monitor_enabled;
GFileMonitor *monitor;
GValue **values;
};
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (DgSettings, dg_settings, G_TYPE_OBJECT)
static GParamSpec *properties[PROP_N] = { NULL, };
static gchar*
read_line (GFile *file)
{
g_assert (file != NULL);
g_autoptr(GError) error = NULL;
g_autoptr(GInputStream) stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
if (!stream && error)
return NULL;
g_autoptr(GDataInputStream) datain = g_data_input_stream_new (stream);
gsize read_length = 0;
return g_data_input_stream_read_line_utf8 (datain,
&read_length,
NULL,
&error);
}
static void
read_boolean (GFile *file, GValue *value)
{
g_autofree char *line = read_line (file);
if (line == NULL)
return;
g_value_set_boolean (value, g_ascii_strcasecmp ("false", line) != 0);
}
static void
read_uint (GFile *file, GValue *value)
{
g_autofree char *line = read_line (file);
if (line == NULL)
return;
guint64 uint_value = g_ascii_strtoull (line, NULL, 0);
if (uint_value <= G_MAXUINT)
g_value_set_uint (value, uint_value);
}
void
dg_settings__get_property__ (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
DgSettingsPrivate *priv = dg_settings_get_instance_private (DG_SETTINGS (object));
g_autoptr(GFile) setting_file = g_file_get_child (priv->settings_path,
g_param_spec_get_name (pspec));
/* Use the default value when the file does not exist */
g_value_copy (g_param_spec_get_default_value (pspec), value);
if (!g_file_query_exists (setting_file, NULL))
return;
/* Try to read the value from the file, converting as appropriate. */
switch (G_PARAM_SPEC_VALUE_TYPE (pspec)) {
case G_TYPE_BOOLEAN:
read_boolean (setting_file, value);
break;
case G_TYPE_UINT:
read_uint (setting_file, value);
break;
case G_TYPE_STRING:
g_value_take_string (value, read_line (setting_file));
break;
}
}
static void
write_line (GFile *file,
GParamSpec *pspec,
const gchar *line)
{
g_assert (file != NULL);
g_assert (pspec != NULL);
g_assert (line != NULL);
/* TODO: Handle errors */
g_autoptr(GOutputStream) stream =
G_OUTPUT_STREAM (g_file_open_readwrite (file, NULL, NULL));
if (stream == NULL)
return;
gsize bytes_written;
g_output_stream_printf (stream,
&bytes_written,
NULL,
NULL,
"%s\n\n%s\n",
line,
g_param_spec_get_blurb (pspec));
g_output_stream_close (stream,
NULL,
NULL);
}
void
dg_settings__set_property__ (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
DgSettingsPrivate *priv = dg_settings_get_instance_private (DG_SETTINGS (object));
g_autoptr(GFile) setting_file = g_file_get_child (priv->settings_path,
g_param_spec_get_name (pspec));
switch (G_PARAM_SPEC_VALUE_TYPE (pspec)) {
case G_TYPE_BOOLEAN:
write_line (setting_file, pspec,
g_value_get_boolean (value) ? "true" : "false");
break;
}
}
static void
dg_settings_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
DgSettingsPrivate *priv = dg_settings_get_instance_private (DG_SETTINGS (object));
switch (prop_id) {
case PROP_SETTINGS_PATH: {
g_value_take_string (value, g_file_get_path (priv->settings_path));
break;
}
case PROP_SETTINGS_MONITORING_ENABLED:
g_value_set_boolean (value, priv->monitor_enabled);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
dg_settings_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
DgSettingsPrivate *priv = dg_settings_get_instance_private (DG_SETTINGS (object));
switch (prop_id) {
case PROP_SETTINGS_PATH:
g_assert (!priv->settings_path);
priv->settings_path = g_file_new_for_path (g_value_get_string (value));
break;
case PROP_SETTINGS_MONITORING_ENABLED:
priv->monitor_enabled = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
dg_settings_finalize (GObject *object)
{
DgSettingsPrivate *priv = dg_settings_get_instance_private (DG_SETTINGS (object));
if (priv->monitor_enabled)
g_object_unref (priv->monitor);
g_object_unref (priv->settings_path);
G_OBJECT_CLASS (dg_settings_parent_class)->finalize (object);
}
static void
dg_settings_init (DgSettings *settings)
{
}
static void
dg_settings_monitor_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event,
DgSettings *settings)
{
switch (event) {
case G_FILE_MONITOR_EVENT_CHANGED:
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
case G_FILE_MONITOR_EVENT_UNMOUNTED:
case G_FILE_MONITOR_EVENT_MOVED:
case G_FILE_MONITOR_EVENT_RENAMED:
case G_FILE_MONITOR_EVENT_MOVED_IN:
case G_FILE_MONITOR_EVENT_MOVED_OUT:
case G_FILE_MONITOR_EVENT_CREATED:
return;
case G_FILE_MONITOR_EVENT_DELETED:
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT: {
g_autofree char *filename = g_file_get_basename (file);
GParamSpec *pspec =
g_object_class_find_property (G_OBJECT_GET_CLASS (settings),
filename);
if (pspec) g_object_notify_by_pspec (G_OBJECT (settings), pspec);
}
}
}
void
dg_settings__constructed__ (GObject *object)
{
DgSettingsPrivate *priv = dg_settings_get_instance_private (DG_SETTINGS (object));
g_assert (priv->settings_path);
if (priv->monitor_enabled) {
g_autoptr(GError) error = NULL;
priv->monitor = g_file_monitor_directory (priv->settings_path,
G_FILE_MONITOR_NONE,
NULL,
&error);
if (priv->monitor) {
g_file_monitor_set_rate_limit (priv->monitor, 500);
g_signal_connect (priv->monitor,
"changed",
G_CALLBACK (dg_settings_monitor_changed),
object);
} else {
g_autofree char *path = g_file_get_path (priv->settings_path);
g_warning ("DgSettings: cannot initialize monitoring for directory '%s'\n", path);
if (error) g_warning ("DgSettings: %s\n", error->message);
g_warning ("DgSettings: trying to continue anyway with monitoring disabled\n");
priv->monitor_enabled = FALSE;
}
}
}
static void
dg_settings_class_init (DgSettingsClass *klass)
{
GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
g_object_class->get_property = dg_settings_get_property;
g_object_class->set_property = dg_settings_set_property;
g_object_class->finalize = dg_settings_finalize;
properties[PROP_SETTINGS_PATH] =
g_param_spec_string ("settings-path",
"Settings path",
"Directory where the configuration files reside",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
properties[PROP_SETTINGS_MONITORING_ENABLED] =
g_param_spec_boolean ("settings-monitoring-enabled",
"Settings monitoring enabled",
"Whether changes to files backing up settings will be monitored",
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (g_object_class, PROP_N, properties);
}