2 * Copyright © 2009, 2010 Codethink Limited
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 of the licence, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gdelayedsettingsbackend.h"
25 #include "gsettingsbackendinternal.h"
30 struct _GDelayedSettingsBackendPrivate
32 GSettingsBackend *backend;
36 GMainContext *owner_context;
40 G_DEFINE_TYPE_WITH_PRIVATE (GDelayedSettingsBackend,
41 g_delayed_settings_backend,
42 G_TYPE_SETTINGS_BACKEND)
45 invoke_notify_unapplied (gpointer data)
47 g_object_notify (data, "has-unapplied");
48 g_object_unref (data);
54 g_delayed_settings_backend_notify_unapplied (GDelayedSettingsBackend *delayed)
56 GMainContext *target_context;
59 g_mutex_lock (&delayed->priv->lock);
60 if (delayed->priv->owner)
62 target_context = delayed->priv->owner_context;
63 target = g_object_ref (delayed->priv->owner);
67 target_context = NULL;
70 g_mutex_unlock (&delayed->priv->lock);
73 g_main_context_invoke (target_context, invoke_notify_unapplied, target);
78 g_delayed_settings_backend_read (GSettingsBackend *backend,
80 const GVariantType *expected_type,
81 gboolean default_value)
83 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
84 gpointer result = NULL;
88 g_mutex_lock (&delayed->priv->lock);
89 if (g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result))
91 /* NULL in the tree means we should consult the default value */
93 g_variant_ref (result);
97 g_mutex_unlock (&delayed->priv->lock);
101 result = g_settings_backend_read (delayed->priv->backend, key,
102 expected_type, default_value);
108 g_delayed_settings_backend_read_user_value (GSettingsBackend *backend,
110 const GVariantType *expected_type)
112 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
113 gboolean value_found = FALSE;
114 gpointer result = NULL;
116 /* If we find an explicit NULL in our changeset then we want to return
117 * NULL (because the user value has been reset).
119 * Otherwise, chain up.
121 g_mutex_lock (&delayed->priv->lock);
122 value_found = g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result);
124 g_variant_ref (result);
125 g_mutex_unlock (&delayed->priv->lock);
130 return g_settings_backend_read_user_value (delayed->priv->backend, key, expected_type);
134 g_delayed_settings_backend_write (GSettingsBackend *backend,
139 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
142 g_mutex_lock (&delayed->priv->lock);
143 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
144 g_tree_insert (delayed->priv->delayed, g_strdup (key),
145 g_variant_ref_sink (value));
146 g_mutex_unlock (&delayed->priv->lock);
148 g_settings_backend_changed (backend, key, origin_tag);
151 g_delayed_settings_backend_notify_unapplied (delayed);
157 add_to_tree (gpointer key,
161 g_tree_insert (user_data, g_strdup (key), g_variant_ref (value));
166 g_delayed_settings_backend_write_tree (GSettingsBackend *backend,
170 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
173 g_mutex_lock (&delayed->priv->lock);
174 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
176 g_tree_foreach (tree, add_to_tree, delayed->priv->delayed);
177 g_mutex_unlock (&delayed->priv->lock);
179 g_settings_backend_changed_tree (backend, tree, origin_tag);
182 g_delayed_settings_backend_notify_unapplied (delayed);
188 g_delayed_settings_backend_get_writable (GSettingsBackend *backend,
191 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
193 return g_settings_backend_get_writable (delayed->priv->backend, name);
197 g_delayed_settings_backend_reset (GSettingsBackend *backend,
201 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
204 g_mutex_lock (&delayed->priv->lock);
205 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
206 g_tree_insert (delayed->priv->delayed, g_strdup (key), NULL);
207 g_mutex_unlock (&delayed->priv->lock);
210 g_delayed_settings_backend_notify_unapplied (delayed);
214 g_delayed_settings_backend_subscribe (GSettingsBackend *backend,
217 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
219 g_settings_backend_subscribe (delayed->priv->backend, name);
223 g_delayed_settings_backend_unsubscribe (GSettingsBackend *backend,
226 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
228 g_settings_backend_unsubscribe (delayed->priv->backend, name);
232 g_delayed_settings_backend_get_permission (GSettingsBackend *backend,
235 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
237 return g_settings_backend_get_permission (delayed->priv->backend, path);
243 g_delayed_settings_backend_get_has_unapplied (GDelayedSettingsBackend *delayed)
245 /* we don't need to lock for this... */
247 return g_tree_nnodes (delayed->priv->delayed) > 0;
251 g_delayed_settings_backend_apply (GDelayedSettingsBackend *delayed)
253 if (g_tree_nnodes (delayed->priv->delayed) > 0)
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,
263 g_mutex_unlock (&delayed->priv->lock);
266 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed),
271 g_delayed_settings_backend_notify_unapplied (delayed);
276 g_delayed_settings_backend_revert (GDelayedSettingsBackend *delayed)
278 if (g_tree_nnodes (delayed->priv->delayed) > 0)
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);
289 g_delayed_settings_backend_notify_unapplied (delayed);
293 /* change notification */
295 delayed_backend_changed (GObject *target,
296 GSettingsBackend *backend,
300 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
302 if (origin_tag != delayed->priv)
303 g_settings_backend_changed (G_SETTINGS_BACKEND (delayed),
308 delayed_backend_keys_changed (GObject *target,
309 GSettingsBackend *backend,
311 const gchar * const *items,
314 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
316 if (origin_tag != delayed->priv)
317 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (delayed),
318 path, items, origin_tag);
322 delayed_backend_path_changed (GObject *target,
323 GSettingsBackend *backend,
327 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
329 if (origin_tag != delayed->priv)
330 g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed),
335 delayed_backend_writable_changed (GObject *target,
336 GSettingsBackend *backend,
339 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
340 gboolean last_one = FALSE;
342 g_mutex_lock (&delayed->priv->lock);
344 if (g_tree_lookup (delayed->priv->delayed, key) != NULL &&
345 !g_settings_backend_get_writable (delayed->priv->backend, key))
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.
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).
355 g_tree_remove (delayed->priv->delayed, key);
357 /* if that was the only key... */
358 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
361 g_mutex_unlock (&delayed->priv->lock);
364 g_delayed_settings_backend_notify_unapplied (delayed);
366 g_settings_backend_writable_changed (G_SETTINGS_BACKEND (delayed), key);
369 /* slow method until we get foreach-with-remove in GTree
379 check_prefix (gpointer key,
383 CheckPrefixState *state = data;
385 if (g_str_has_prefix (key, state->path))
386 state->keys[state->index++] = key;
392 delayed_backend_path_writable_changed (GObject *target,
393 GSettingsBackend *backend,
396 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
397 gboolean last_one = FALSE;
400 g_mutex_lock (&delayed->priv->lock);
402 n_keys = g_tree_nnodes (delayed->priv->delayed);
406 CheckPrefixState state = { path, g_new (const gchar *, n_keys) };
409 /* collect a list of possibly-affected keys (ie: matching the path) */
410 g_tree_foreach (delayed->priv->delayed, check_prefix, &state);
412 /* drop the keys that have been affected.
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,
419 g_tree_remove (delayed->priv->delayed, state.keys[i]);
423 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
426 g_mutex_unlock (&delayed->priv->lock);
429 g_delayed_settings_backend_notify_unapplied (delayed);
431 g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed),
436 g_delayed_settings_backend_finalize (GObject *object)
438 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (object);
440 g_mutex_clear (&delayed->priv->lock);
441 g_object_unref (delayed->priv->backend);
442 g_tree_unref (delayed->priv->delayed);
444 /* if our owner is still alive, why are we finalizing? */
445 g_assert (delayed->priv->owner == NULL);
447 G_OBJECT_CLASS (g_delayed_settings_backend_parent_class)
452 g_delayed_settings_backend_class_init (GDelayedSettingsBackendClass *class)
454 GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
455 GObjectClass *object_class = G_OBJECT_CLASS (class);
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;
467 object_class->finalize = g_delayed_settings_backend_finalize;
471 g_delayed_settings_backend_init (GDelayedSettingsBackend *delayed)
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);
479 g_delayed_settings_backend_disown (gpointer data,
480 GObject *where_the_object_was)
482 GDelayedSettingsBackend *delayed = data;
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);
490 GDelayedSettingsBackend *
491 g_delayed_settings_backend_new (GSettingsBackend *backend,
493 GMainContext *owner_context)
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
502 GDelayedSettingsBackend *delayed;
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;
509 g_object_weak_ref (owner, g_delayed_settings_backend_disown, delayed);
511 g_settings_backend_watch (delayed->priv->backend,
512 &vtable, G_OBJECT (delayed), NULL);