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 (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_static_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_static_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_static_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_static_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_write (GSettingsBackend *backend,
113 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
116 g_static_mutex_lock (&delayed->priv->lock);
117 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
118 g_tree_insert (delayed->priv->delayed, g_strdup (key),
119 g_variant_ref_sink (value));
120 g_static_mutex_unlock (&delayed->priv->lock);
122 g_settings_backend_changed (backend, key, origin_tag);
125 g_delayed_settings_backend_notify_unapplied (delayed);
131 add_to_tree (gpointer key,
135 g_tree_insert (user_data, g_strdup (key), g_variant_ref (value));
140 g_delayed_settings_backend_write_tree (GSettingsBackend *backend,
144 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
147 g_static_mutex_lock (&delayed->priv->lock);
148 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
150 g_tree_foreach (tree, add_to_tree, delayed->priv->delayed);
151 g_static_mutex_unlock (&delayed->priv->lock);
153 g_settings_backend_changed_tree (backend, tree, origin_tag);
156 g_delayed_settings_backend_notify_unapplied (delayed);
162 g_delayed_settings_backend_get_writable (GSettingsBackend *backend,
165 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
167 return g_settings_backend_get_writable (delayed->priv->backend, name);
171 g_delayed_settings_backend_reset (GSettingsBackend *backend,
175 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
178 g_static_mutex_lock (&delayed->priv->lock);
179 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
180 g_tree_insert (delayed->priv->delayed, g_strdup (key), NULL);
181 g_static_mutex_unlock (&delayed->priv->lock);
184 g_delayed_settings_backend_notify_unapplied (delayed);
188 g_delayed_settings_backend_subscribe (GSettingsBackend *backend,
191 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
193 g_settings_backend_subscribe (delayed->priv->backend, name);
197 g_delayed_settings_backend_unsubscribe (GSettingsBackend *backend,
200 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
202 g_settings_backend_unsubscribe (delayed->priv->backend, name);
206 g_delayed_settings_backend_get_permission (GSettingsBackend *backend,
209 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
211 return g_settings_backend_get_permission (delayed->priv->backend, path);
217 g_delayed_settings_backend_get_has_unapplied (GDelayedSettingsBackend *delayed)
219 /* we don't need to lock for this... */
221 return g_tree_nnodes (delayed->priv->delayed) > 0;
225 g_delayed_settings_backend_apply (GDelayedSettingsBackend *delayed)
227 if (g_tree_nnodes (delayed->priv->delayed) > 0)
232 g_static_mutex_lock (&delayed->priv->lock);
233 tmp = delayed->priv->delayed;
234 delayed->priv->delayed = g_settings_backend_create_tree ();
235 success = g_settings_backend_write_tree (delayed->priv->backend,
237 g_static_mutex_unlock (&delayed->priv->lock);
240 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed),
245 g_delayed_settings_backend_notify_unapplied (delayed);
250 g_delayed_settings_backend_revert (GDelayedSettingsBackend *delayed)
252 if (g_tree_nnodes (delayed->priv->delayed) > 0)
256 g_static_mutex_lock (&delayed->priv->lock);
257 tmp = delayed->priv->delayed;
258 delayed->priv->delayed = g_settings_backend_create_tree ();
259 g_static_mutex_unlock (&delayed->priv->lock);
260 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed), tmp, NULL);
263 g_delayed_settings_backend_notify_unapplied (delayed);
267 /* change notification */
269 delayed_backend_changed (GObject *target,
270 GSettingsBackend *backend,
274 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
276 if (origin_tag != delayed->priv)
277 g_settings_backend_changed (G_SETTINGS_BACKEND (delayed),
282 delayed_backend_keys_changed (GObject *target,
283 GSettingsBackend *backend,
285 const gchar * const *items,
288 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
290 if (origin_tag != delayed->priv)
291 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (delayed),
292 path, items, origin_tag);
296 delayed_backend_path_changed (GObject *target,
297 GSettingsBackend *backend,
301 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
303 if (origin_tag != delayed->priv)
304 g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed),
309 delayed_backend_writable_changed (GObject *target,
310 GSettingsBackend *backend,
313 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
314 gboolean last_one = FALSE;
316 g_static_mutex_lock (&delayed->priv->lock);
318 if (g_tree_lookup (delayed->priv->delayed, key) &&
319 !g_settings_backend_get_writable (delayed->priv->backend, key))
321 /* drop the key from our changeset if it just became read-only.
322 * no need to signal since the writable change below implies it.
324 g_tree_remove (delayed->priv->delayed, key);
326 /* if that was the only key... */
327 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
330 g_static_mutex_unlock (&delayed->priv->lock);
333 g_delayed_settings_backend_notify_unapplied (delayed);
335 g_settings_backend_writable_changed (G_SETTINGS_BACKEND (delayed), key);
338 /* slow method until we get foreach-with-remove in GTree
348 check_prefix (gpointer key,
352 CheckPrefixState *state = data;
354 if (g_str_has_prefix (key, state->path))
355 state->keys[state->index++] = key;
361 delayed_backend_path_writable_changed (GObject *target,
362 GSettingsBackend *backend,
365 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
366 gboolean last_one = FALSE;
369 g_static_mutex_lock (&delayed->priv->lock);
371 n_keys = g_tree_nnodes (delayed->priv->delayed);
375 CheckPrefixState state = { path, g_new (const gchar *, n_keys) };
378 /* collect a list of possibly-affected keys (ie: matching the path) */
379 g_tree_foreach (delayed->priv->delayed, check_prefix, &state);
381 /* drop the keys that have been affected */
382 for (i = 0; i < state.index; i++)
383 if (!g_settings_backend_get_writable (delayed->priv->backend,
385 g_tree_remove (delayed->priv->delayed, state.keys[i]);
389 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
392 g_static_mutex_unlock (&delayed->priv->lock);
395 g_delayed_settings_backend_notify_unapplied (delayed);
397 g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed),
402 g_delayed_settings_backend_finalize (GObject *object)
404 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (object);
406 g_static_mutex_free (&delayed->priv->lock);
407 g_object_unref (delayed->priv->backend);
408 g_tree_unref (delayed->priv->delayed);
410 /* if our owner is still alive, why are we finalizing? */
411 g_assert (delayed->priv->owner == NULL);
413 G_OBJECT_CLASS (g_delayed_settings_backend_parent_class)
418 g_delayed_settings_backend_class_init (GDelayedSettingsBackendClass *class)
420 GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
421 GObjectClass *object_class = G_OBJECT_CLASS (class);
423 g_type_class_add_private (class, sizeof (GDelayedSettingsBackendPrivate));
425 backend_class->read = g_delayed_settings_backend_read;
426 backend_class->write = g_delayed_settings_backend_write;
427 backend_class->write_tree = g_delayed_settings_backend_write_tree;
428 backend_class->reset = g_delayed_settings_backend_reset;
429 backend_class->get_writable = g_delayed_settings_backend_get_writable;
430 backend_class->subscribe = g_delayed_settings_backend_subscribe;
431 backend_class->unsubscribe = g_delayed_settings_backend_unsubscribe;
432 backend_class->get_permission = g_delayed_settings_backend_get_permission;
434 object_class->finalize = g_delayed_settings_backend_finalize;
438 g_delayed_settings_backend_init (GDelayedSettingsBackend *delayed)
441 G_TYPE_INSTANCE_GET_PRIVATE (delayed, G_TYPE_DELAYED_SETTINGS_BACKEND,
442 GDelayedSettingsBackendPrivate);
444 delayed->priv->delayed = g_settings_backend_create_tree ();
445 g_static_mutex_init (&delayed->priv->lock);
449 g_delayed_settings_backend_disown (gpointer data,
450 GObject *where_the_object_was)
452 GDelayedSettingsBackend *delayed = data;
454 g_static_mutex_lock (&delayed->priv->lock);
455 delayed->priv->owner_context = NULL;
456 delayed->priv->owner = NULL;
457 g_static_mutex_unlock (&delayed->priv->lock);
460 GDelayedSettingsBackend *
461 g_delayed_settings_backend_new (GSettingsBackend *backend,
463 GMainContext *owner_context)
465 static GSettingsListenerVTable vtable = {
466 delayed_backend_changed,
467 delayed_backend_path_changed,
468 delayed_backend_keys_changed,
469 delayed_backend_writable_changed,
470 delayed_backend_path_writable_changed
472 GDelayedSettingsBackend *delayed;
474 delayed = g_object_new (G_TYPE_DELAYED_SETTINGS_BACKEND, NULL);
475 delayed->priv->backend = g_object_ref (backend);
476 delayed->priv->owner_context = owner_context;
477 delayed->priv->owner = owner;
479 g_object_weak_ref (owner, g_delayed_settings_backend_disown, delayed);
481 g_settings_backend_watch (delayed->priv->backend,
482 &vtable, G_OBJECT (delayed), NULL);