1 /*
2 * Copyright © 2009, 2010 Codethink Limited
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Ryan Lortie <desrt@desrt.ca>
18 */
19
20 #include "config.h"
21
22 #include "gdelayedsettingsbackend.h"
23 #include "gsettingsbackendinternal.h"
24
25 #include <string.h>
26
27
28 struct _GDelayedSettingsBackendPrivate
29 {
30 GSettingsBackend *backend;
31 GMutex lock;
32 GTree *delayed;
33
34 GMainContext *owner_context;
35 gpointer owner;
36 };
37
G_DEFINE_TYPE_WITH_PRIVATE(GDelayedSettingsBackend,g_delayed_settings_backend,G_TYPE_SETTINGS_BACKEND)38 G_DEFINE_TYPE_WITH_PRIVATE (GDelayedSettingsBackend,
39 g_delayed_settings_backend,
40 G_TYPE_SETTINGS_BACKEND)
41
42 static gboolean
43 invoke_notify_unapplied (gpointer data)
44 {
45 g_object_notify (data, "has-unapplied");
46 g_object_unref (data);
47
48 return FALSE;
49 }
50
51 static void
g_delayed_settings_backend_notify_unapplied(GDelayedSettingsBackend * delayed)52 g_delayed_settings_backend_notify_unapplied (GDelayedSettingsBackend *delayed)
53 {
54 GMainContext *target_context;
55 GObject *target;
56
57 g_mutex_lock (&delayed->priv->lock);
58 if (delayed->priv->owner)
59 {
60 target_context = delayed->priv->owner_context;
61 target = g_object_ref (delayed->priv->owner);
62 }
63 else
64 {
65 target_context = NULL;
66 target = NULL;
67 }
68 g_mutex_unlock (&delayed->priv->lock);
69
70 if (target != NULL)
71 g_main_context_invoke (target_context, invoke_notify_unapplied, target);
72 }
73
74
75 static GVariant *
g_delayed_settings_backend_read(GSettingsBackend * backend,const gchar * key,const GVariantType * expected_type,gboolean default_value)76 g_delayed_settings_backend_read (GSettingsBackend *backend,
77 const gchar *key,
78 const GVariantType *expected_type,
79 gboolean default_value)
80 {
81 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
82 gpointer result = NULL;
83
84 if (!default_value)
85 {
86 g_mutex_lock (&delayed->priv->lock);
87 if (g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result))
88 {
89 /* NULL in the tree means we should consult the default value */
90 if (result != NULL)
91 g_variant_ref (result);
92 else
93 default_value = TRUE;
94 }
95 g_mutex_unlock (&delayed->priv->lock);
96 }
97
98 if (result == NULL)
99 result = g_settings_backend_read (delayed->priv->backend, key,
100 expected_type, default_value);
101
102 return result;
103 }
104
105 static GVariant *
g_delayed_settings_backend_read_user_value(GSettingsBackend * backend,const gchar * key,const GVariantType * expected_type)106 g_delayed_settings_backend_read_user_value (GSettingsBackend *backend,
107 const gchar *key,
108 const GVariantType *expected_type)
109 {
110 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
111 gboolean value_found = FALSE;
112 gpointer result = NULL;
113
114 /* If we find an explicit NULL in our changeset then we want to return
115 * NULL (because the user value has been reset).
116 *
117 * Otherwise, chain up.
118 */
119 g_mutex_lock (&delayed->priv->lock);
120 value_found = g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result);
121 if (result)
122 g_variant_ref (result);
123 g_mutex_unlock (&delayed->priv->lock);
124
125 if (value_found)
126 return result;
127
128 return g_settings_backend_read_user_value (delayed->priv->backend, key, expected_type);
129 }
130
131 static gboolean
g_delayed_settings_backend_write(GSettingsBackend * backend,const gchar * key,GVariant * value,gpointer origin_tag)132 g_delayed_settings_backend_write (GSettingsBackend *backend,
133 const gchar *key,
134 GVariant *value,
135 gpointer origin_tag)
136 {
137 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
138 gboolean was_empty;
139
140 g_mutex_lock (&delayed->priv->lock);
141 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
142 g_tree_insert (delayed->priv->delayed, g_strdup (key),
143 g_variant_ref_sink (value));
144 g_mutex_unlock (&delayed->priv->lock);
145
146 g_settings_backend_changed (backend, key, origin_tag);
147
148 if (was_empty)
149 g_delayed_settings_backend_notify_unapplied (delayed);
150
151 return TRUE;
152 }
153
154 static gboolean
add_to_tree(gpointer key,gpointer value,gpointer user_data)155 add_to_tree (gpointer key,
156 gpointer value,
157 gpointer user_data)
158 {
159 g_tree_insert (user_data, g_strdup (key), g_variant_ref (value));
160 return FALSE;
161 }
162
163 static gboolean
g_delayed_settings_backend_write_tree(GSettingsBackend * backend,GTree * tree,gpointer origin_tag)164 g_delayed_settings_backend_write_tree (GSettingsBackend *backend,
165 GTree *tree,
166 gpointer origin_tag)
167 {
168 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
169 gboolean was_empty;
170
171 g_mutex_lock (&delayed->priv->lock);
172 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
173
174 g_tree_foreach (tree, add_to_tree, delayed->priv->delayed);
175 g_mutex_unlock (&delayed->priv->lock);
176
177 g_settings_backend_changed_tree (backend, tree, origin_tag);
178
179 if (was_empty)
180 g_delayed_settings_backend_notify_unapplied (delayed);
181
182 return TRUE;
183 }
184
185 static gboolean
g_delayed_settings_backend_get_writable(GSettingsBackend * backend,const gchar * name)186 g_delayed_settings_backend_get_writable (GSettingsBackend *backend,
187 const gchar *name)
188 {
189 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
190
191 return g_settings_backend_get_writable (delayed->priv->backend, name);
192 }
193
194 static void
g_delayed_settings_backend_reset(GSettingsBackend * backend,const gchar * key,gpointer origin_tag)195 g_delayed_settings_backend_reset (GSettingsBackend *backend,
196 const gchar *key,
197 gpointer origin_tag)
198 {
199 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
200 gboolean was_empty;
201
202 g_mutex_lock (&delayed->priv->lock);
203 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
204 g_tree_insert (delayed->priv->delayed, g_strdup (key), NULL);
205 g_mutex_unlock (&delayed->priv->lock);
206
207 g_settings_backend_changed (backend, key, origin_tag);
208
209 if (was_empty)
210 g_delayed_settings_backend_notify_unapplied (delayed);
211 }
212
213 static void
g_delayed_settings_backend_subscribe(GSettingsBackend * backend,const char * name)214 g_delayed_settings_backend_subscribe (GSettingsBackend *backend,
215 const char *name)
216 {
217 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
218
219 g_settings_backend_subscribe (delayed->priv->backend, name);
220 }
221
222 static void
g_delayed_settings_backend_unsubscribe(GSettingsBackend * backend,const char * name)223 g_delayed_settings_backend_unsubscribe (GSettingsBackend *backend,
224 const char *name)
225 {
226 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
227
228 g_settings_backend_unsubscribe (delayed->priv->backend, name);
229 }
230
231 static GPermission *
g_delayed_settings_backend_get_permission(GSettingsBackend * backend,const gchar * path)232 g_delayed_settings_backend_get_permission (GSettingsBackend *backend,
233 const gchar *path)
234 {
235 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
236
237 return g_settings_backend_get_permission (delayed->priv->backend, path);
238 }
239
240
241 /* method calls */
242 gboolean
g_delayed_settings_backend_get_has_unapplied(GDelayedSettingsBackend * delayed)243 g_delayed_settings_backend_get_has_unapplied (GDelayedSettingsBackend *delayed)
244 {
245 /* we don't need to lock for this... */
246
247 return g_tree_nnodes (delayed->priv->delayed) > 0;
248 }
249
250 void
g_delayed_settings_backend_apply(GDelayedSettingsBackend * delayed)251 g_delayed_settings_backend_apply (GDelayedSettingsBackend *delayed)
252 {
253 if (g_tree_nnodes (delayed->priv->delayed) > 0)
254 {
255 gboolean success;
256 GTree *tmp;
257
258 g_mutex_lock (&delayed->priv->lock);
259 tmp = delayed->priv->delayed;
260 delayed->priv->delayed = g_settings_backend_create_tree ();
261 success = g_settings_backend_write_tree (delayed->priv->backend,
262 tmp, delayed->priv);
263 g_mutex_unlock (&delayed->priv->lock);
264
265 if (!success)
266 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed),
267 tmp, NULL);
268
269 g_tree_unref (tmp);
270
271 g_delayed_settings_backend_notify_unapplied (delayed);
272 }
273 }
274
275 void
g_delayed_settings_backend_revert(GDelayedSettingsBackend * delayed)276 g_delayed_settings_backend_revert (GDelayedSettingsBackend *delayed)
277 {
278 if (g_tree_nnodes (delayed->priv->delayed) > 0)
279 {
280 GTree *tmp;
281
282 g_mutex_lock (&delayed->priv->lock);
283 tmp = delayed->priv->delayed;
284 delayed->priv->delayed = g_settings_backend_create_tree ();
285 g_mutex_unlock (&delayed->priv->lock);
286 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed), tmp, NULL);
287 g_tree_unref (tmp);
288
289 g_delayed_settings_backend_notify_unapplied (delayed);
290 }
291 }
292
293 /* change notification */
294 static void
delayed_backend_changed(GObject * target,GSettingsBackend * backend,const gchar * key,gpointer origin_tag)295 delayed_backend_changed (GObject *target,
296 GSettingsBackend *backend,
297 const gchar *key,
298 gpointer origin_tag)
299 {
300 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
301
302 if (origin_tag != delayed->priv)
303 g_settings_backend_changed (G_SETTINGS_BACKEND (delayed),
304 key, origin_tag);
305 }
306
307 static void
delayed_backend_keys_changed(GObject * target,GSettingsBackend * backend,const gchar * path,gpointer origin_tag,const gchar * const * items)308 delayed_backend_keys_changed (GObject *target,
309 GSettingsBackend *backend,
310 const gchar *path,
311 gpointer origin_tag,
312 const gchar * const *items)
313 {
314 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
315
316 if (origin_tag != delayed->priv)
317 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (delayed),
318 path, items, origin_tag);
319 }
320
321 static void
delayed_backend_path_changed(GObject * target,GSettingsBackend * backend,const gchar * path,gpointer origin_tag)322 delayed_backend_path_changed (GObject *target,
323 GSettingsBackend *backend,
324 const gchar *path,
325 gpointer origin_tag)
326 {
327 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
328
329 if (origin_tag != delayed->priv)
330 g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed),
331 path, origin_tag);
332 }
333
334 static void
delayed_backend_writable_changed(GObject * target,GSettingsBackend * backend,const gchar * key)335 delayed_backend_writable_changed (GObject *target,
336 GSettingsBackend *backend,
337 const gchar *key)
338 {
339 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
340 gboolean last_one = FALSE;
341
342 g_mutex_lock (&delayed->priv->lock);
343
344 if (g_tree_lookup (delayed->priv->delayed, key) != NULL &&
345 !g_settings_backend_get_writable (delayed->priv->backend, key))
346 {
347 /* drop the key from our changeset if it just became read-only.
348 * no need to signal since the writable change below implies it.
349 *
350 * note that the item in the tree may very well be set to NULL in
351 * the case that the user stored a reset. we intentionally don't
352 * drop the key in this case since a reset will always succeed
353 * (even against a non-writable key).
354 */
355 g_tree_remove (delayed->priv->delayed, key);
356
357 /* if that was the only key... */
358 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
359 }
360
361 g_mutex_unlock (&delayed->priv->lock);
362
363 if (last_one)
364 g_delayed_settings_backend_notify_unapplied (delayed);
365
366 g_settings_backend_writable_changed (G_SETTINGS_BACKEND (delayed), key);
367 }
368
369 /* slow method until we get foreach-with-remove in GTree
370 */
371 typedef struct
372 {
373 const gchar *path;
374 const gchar **keys;
375 gsize index;
376 } CheckPrefixState;
377
378 static gboolean
check_prefix(gpointer key,gpointer value,gpointer data)379 check_prefix (gpointer key,
380 gpointer value,
381 gpointer data)
382 {
383 CheckPrefixState *state = data;
384
385 if (g_str_has_prefix (key, state->path))
386 state->keys[state->index++] = key;
387
388 return FALSE;
389 }
390
391 static void
delayed_backend_path_writable_changed(GObject * target,GSettingsBackend * backend,const gchar * path)392 delayed_backend_path_writable_changed (GObject *target,
393 GSettingsBackend *backend,
394 const gchar *path)
395 {
396 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
397 gboolean last_one = FALSE;
398 gsize n_keys;
399
400 g_mutex_lock (&delayed->priv->lock);
401
402 n_keys = g_tree_nnodes (delayed->priv->delayed);
403
404 if (n_keys > 0)
405 {
406 CheckPrefixState state = { path, g_new (const gchar *, n_keys), 0 };
407 gsize i;
408
409 /* collect a list of possibly-affected keys (ie: matching the path) */
410 g_tree_foreach (delayed->priv->delayed, check_prefix, &state);
411
412 /* drop the keys that have been affected.
413 *
414 * don't drop 'reset' keys (see above) */
415 for (i = 0; i < state.index; i++)
416 if (g_tree_lookup (delayed->priv->delayed, state.keys[i]) != NULL &&
417 !g_settings_backend_get_writable (delayed->priv->backend,
418 state.keys[i]))
419 g_tree_remove (delayed->priv->delayed, state.keys[i]);
420
421 g_free (state.keys);
422
423 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
424 }
425
426 g_mutex_unlock (&delayed->priv->lock);
427
428 if (last_one)
429 g_delayed_settings_backend_notify_unapplied (delayed);
430
431 g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed),
432 path);
433 }
434
435 static void
g_delayed_settings_backend_finalize(GObject * object)436 g_delayed_settings_backend_finalize (GObject *object)
437 {
438 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (object);
439
440 g_mutex_clear (&delayed->priv->lock);
441 g_object_unref (delayed->priv->backend);
442 g_tree_unref (delayed->priv->delayed);
443
444 /* if our owner is still alive, why are we finalizing? */
445 g_assert (delayed->priv->owner == NULL);
446
447 G_OBJECT_CLASS (g_delayed_settings_backend_parent_class)
448 ->finalize (object);
449 }
450
451 static void
g_delayed_settings_backend_class_init(GDelayedSettingsBackendClass * class)452 g_delayed_settings_backend_class_init (GDelayedSettingsBackendClass *class)
453 {
454 GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
455 GObjectClass *object_class = G_OBJECT_CLASS (class);
456
457 backend_class->read = g_delayed_settings_backend_read;
458 backend_class->read_user_value = g_delayed_settings_backend_read_user_value;
459 backend_class->write = g_delayed_settings_backend_write;
460 backend_class->write_tree = g_delayed_settings_backend_write_tree;
461 backend_class->reset = g_delayed_settings_backend_reset;
462 backend_class->get_writable = g_delayed_settings_backend_get_writable;
463 backend_class->subscribe = g_delayed_settings_backend_subscribe;
464 backend_class->unsubscribe = g_delayed_settings_backend_unsubscribe;
465 backend_class->get_permission = g_delayed_settings_backend_get_permission;
466
467 object_class->finalize = g_delayed_settings_backend_finalize;
468 }
469
470 static void
g_delayed_settings_backend_init(GDelayedSettingsBackend * delayed)471 g_delayed_settings_backend_init (GDelayedSettingsBackend *delayed)
472 {
473 delayed->priv = g_delayed_settings_backend_get_instance_private (delayed);
474 delayed->priv->delayed = g_settings_backend_create_tree ();
475 g_mutex_init (&delayed->priv->lock);
476 }
477
478 static void
g_delayed_settings_backend_disown(gpointer data,GObject * where_the_object_was)479 g_delayed_settings_backend_disown (gpointer data,
480 GObject *where_the_object_was)
481 {
482 GDelayedSettingsBackend *delayed = data;
483
484 g_mutex_lock (&delayed->priv->lock);
485 delayed->priv->owner_context = NULL;
486 delayed->priv->owner = NULL;
487 g_mutex_unlock (&delayed->priv->lock);
488 }
489
490 GDelayedSettingsBackend *
g_delayed_settings_backend_new(GSettingsBackend * backend,gpointer owner,GMainContext * owner_context)491 g_delayed_settings_backend_new (GSettingsBackend *backend,
492 gpointer owner,
493 GMainContext *owner_context)
494 {
495 static GSettingsListenerVTable vtable = {
496 delayed_backend_changed,
497 delayed_backend_path_changed,
498 delayed_backend_keys_changed,
499 delayed_backend_writable_changed,
500 delayed_backend_path_writable_changed
501 };
502 GDelayedSettingsBackend *delayed;
503
504 delayed = g_object_new (G_TYPE_DELAYED_SETTINGS_BACKEND, NULL);
505 delayed->priv->backend = g_object_ref (backend);
506 delayed->priv->owner_context = owner_context;
507 delayed->priv->owner = owner;
508
509 g_object_weak_ref (owner, g_delayed_settings_backend_disown, delayed);
510
511 g_settings_backend_watch (delayed->priv->backend,
512 &vtable, G_OBJECT (delayed), NULL);
513
514 return delayed;
515 }
516