Add myself to the DOAP for GLib
[glib.git] / gio / gdelayedsettingsbackend.c
blobe41393f8e9471a610b1dd9c0e07fd146b14ad87d
1 /*
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.1 of the License, 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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #include "config.h"
22 #include "gdelayedsettingsbackend.h"
23 #include "gsettingsbackendinternal.h"
25 #include <string.h>
28 struct _GDelayedSettingsBackendPrivate
30 GSettingsBackend *backend;
31 GMutex lock;
32 GTree *delayed;
34 GMainContext *owner_context;
35 gpointer owner;
38 G_DEFINE_TYPE_WITH_PRIVATE (GDelayedSettingsBackend,
39 g_delayed_settings_backend,
40 G_TYPE_SETTINGS_BACKEND)
42 static gboolean
43 invoke_notify_unapplied (gpointer data)
45 g_object_notify (data, "has-unapplied");
46 g_object_unref (data);
48 return FALSE;
51 static void
52 g_delayed_settings_backend_notify_unapplied (GDelayedSettingsBackend *delayed)
54 GMainContext *target_context;
55 GObject *target;
57 g_mutex_lock (&delayed->priv->lock);
58 if (delayed->priv->owner)
60 target_context = delayed->priv->owner_context;
61 target = g_object_ref (delayed->priv->owner);
63 else
65 target_context = NULL;
66 target = NULL;
68 g_mutex_unlock (&delayed->priv->lock);
70 if (target != NULL)
71 g_main_context_invoke (target_context, invoke_notify_unapplied, target);
75 static GVariant *
76 g_delayed_settings_backend_read (GSettingsBackend *backend,
77 const gchar *key,
78 const GVariantType *expected_type,
79 gboolean default_value)
81 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
82 gpointer result = NULL;
84 if (!default_value)
86 g_mutex_lock (&delayed->priv->lock);
87 if (g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result))
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;
95 g_mutex_unlock (&delayed->priv->lock);
98 if (result == NULL)
99 result = g_settings_backend_read (delayed->priv->backend, key,
100 expected_type, default_value);
102 return result;
105 static GVariant *
106 g_delayed_settings_backend_read_user_value (GSettingsBackend *backend,
107 const gchar *key,
108 const GVariantType *expected_type)
110 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
111 gboolean value_found = FALSE;
112 gpointer result = NULL;
114 /* If we find an explicit NULL in our changeset then we want to return
115 * NULL (because the user value has been reset).
117 * Otherwise, chain up.
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);
125 if (value_found)
126 return result;
128 return g_settings_backend_read_user_value (delayed->priv->backend, key, expected_type);
131 static gboolean
132 g_delayed_settings_backend_write (GSettingsBackend *backend,
133 const gchar *key,
134 GVariant *value,
135 gpointer origin_tag)
137 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
138 gboolean was_empty;
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);
146 g_settings_backend_changed (backend, key, origin_tag);
148 if (was_empty)
149 g_delayed_settings_backend_notify_unapplied (delayed);
151 return TRUE;
154 static gboolean
155 add_to_tree (gpointer key,
156 gpointer value,
157 gpointer user_data)
159 g_tree_insert (user_data, g_strdup (key), g_variant_ref (value));
160 return FALSE;
163 static gboolean
164 g_delayed_settings_backend_write_tree (GSettingsBackend *backend,
165 GTree *tree,
166 gpointer origin_tag)
168 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
169 gboolean was_empty;
171 g_mutex_lock (&delayed->priv->lock);
172 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
174 g_tree_foreach (tree, add_to_tree, delayed->priv->delayed);
175 g_mutex_unlock (&delayed->priv->lock);
177 g_settings_backend_changed_tree (backend, tree, origin_tag);
179 if (was_empty)
180 g_delayed_settings_backend_notify_unapplied (delayed);
182 return TRUE;
185 static gboolean
186 g_delayed_settings_backend_get_writable (GSettingsBackend *backend,
187 const gchar *name)
189 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
191 return g_settings_backend_get_writable (delayed->priv->backend, name);
194 static void
195 g_delayed_settings_backend_reset (GSettingsBackend *backend,
196 const gchar *key,
197 gpointer origin_tag)
199 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
200 gboolean was_empty;
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);
207 if (was_empty)
208 g_delayed_settings_backend_notify_unapplied (delayed);
211 static void
212 g_delayed_settings_backend_subscribe (GSettingsBackend *backend,
213 const char *name)
215 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
217 g_settings_backend_subscribe (delayed->priv->backend, name);
220 static void
221 g_delayed_settings_backend_unsubscribe (GSettingsBackend *backend,
222 const char *name)
224 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
226 g_settings_backend_unsubscribe (delayed->priv->backend, name);
229 static GPermission *
230 g_delayed_settings_backend_get_permission (GSettingsBackend *backend,
231 const gchar *path)
233 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
235 return g_settings_backend_get_permission (delayed->priv->backend, path);
239 /* method calls */
240 gboolean
241 g_delayed_settings_backend_get_has_unapplied (GDelayedSettingsBackend *delayed)
243 /* we don't need to lock for this... */
245 return g_tree_nnodes (delayed->priv->delayed) > 0;
248 void
249 g_delayed_settings_backend_apply (GDelayedSettingsBackend *delayed)
251 if (g_tree_nnodes (delayed->priv->delayed) > 0)
253 gboolean success;
254 GTree *tmp;
256 g_mutex_lock (&delayed->priv->lock);
257 tmp = delayed->priv->delayed;
258 delayed->priv->delayed = g_settings_backend_create_tree ();
259 success = g_settings_backend_write_tree (delayed->priv->backend,
260 tmp, delayed->priv);
261 g_mutex_unlock (&delayed->priv->lock);
263 if (!success)
264 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed),
265 tmp, NULL);
267 g_tree_unref (tmp);
269 g_delayed_settings_backend_notify_unapplied (delayed);
273 void
274 g_delayed_settings_backend_revert (GDelayedSettingsBackend *delayed)
276 if (g_tree_nnodes (delayed->priv->delayed) > 0)
278 GTree *tmp;
280 g_mutex_lock (&delayed->priv->lock);
281 tmp = delayed->priv->delayed;
282 delayed->priv->delayed = g_settings_backend_create_tree ();
283 g_mutex_unlock (&delayed->priv->lock);
284 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed), tmp, NULL);
285 g_tree_unref (tmp);
287 g_delayed_settings_backend_notify_unapplied (delayed);
291 /* change notification */
292 static void
293 delayed_backend_changed (GObject *target,
294 GSettingsBackend *backend,
295 const gchar *key,
296 gpointer origin_tag)
298 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
300 if (origin_tag != delayed->priv)
301 g_settings_backend_changed (G_SETTINGS_BACKEND (delayed),
302 key, origin_tag);
305 static void
306 delayed_backend_keys_changed (GObject *target,
307 GSettingsBackend *backend,
308 const gchar *path,
309 gpointer origin_tag,
310 const gchar * const *items)
312 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
314 if (origin_tag != delayed->priv)
315 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (delayed),
316 path, items, origin_tag);
319 static void
320 delayed_backend_path_changed (GObject *target,
321 GSettingsBackend *backend,
322 const gchar *path,
323 gpointer origin_tag)
325 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
327 if (origin_tag != delayed->priv)
328 g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed),
329 path, origin_tag);
332 static void
333 delayed_backend_writable_changed (GObject *target,
334 GSettingsBackend *backend,
335 const gchar *key)
337 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
338 gboolean last_one = FALSE;
340 g_mutex_lock (&delayed->priv->lock);
342 if (g_tree_lookup (delayed->priv->delayed, key) != NULL &&
343 !g_settings_backend_get_writable (delayed->priv->backend, key))
345 /* drop the key from our changeset if it just became read-only.
346 * no need to signal since the writable change below implies it.
348 * note that the item in the tree may very well be set to NULL in
349 * the case that the user stored a reset. we intentionally don't
350 * drop the key in this case since a reset will always succeed
351 * (even against a non-writable key).
353 g_tree_remove (delayed->priv->delayed, key);
355 /* if that was the only key... */
356 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
359 g_mutex_unlock (&delayed->priv->lock);
361 if (last_one)
362 g_delayed_settings_backend_notify_unapplied (delayed);
364 g_settings_backend_writable_changed (G_SETTINGS_BACKEND (delayed), key);
367 /* slow method until we get foreach-with-remove in GTree
369 typedef struct
371 const gchar *path;
372 const gchar **keys;
373 gsize index;
374 } CheckPrefixState;
376 static gboolean
377 check_prefix (gpointer key,
378 gpointer value,
379 gpointer data)
381 CheckPrefixState *state = data;
383 if (g_str_has_prefix (key, state->path))
384 state->keys[state->index++] = key;
386 return FALSE;
389 static void
390 delayed_backend_path_writable_changed (GObject *target,
391 GSettingsBackend *backend,
392 const gchar *path)
394 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
395 gboolean last_one = FALSE;
396 gsize n_keys;
398 g_mutex_lock (&delayed->priv->lock);
400 n_keys = g_tree_nnodes (delayed->priv->delayed);
402 if (n_keys > 0)
404 CheckPrefixState state = { path, g_new (const gchar *, n_keys) };
405 gsize i;
407 /* collect a list of possibly-affected keys (ie: matching the path) */
408 g_tree_foreach (delayed->priv->delayed, check_prefix, &state);
410 /* drop the keys that have been affected.
412 * don't drop 'reset' keys (see above) */
413 for (i = 0; i < state.index; i++)
414 if (g_tree_lookup (delayed->priv->delayed, state.keys[i]) != NULL &&
415 !g_settings_backend_get_writable (delayed->priv->backend,
416 state.keys[i]))
417 g_tree_remove (delayed->priv->delayed, state.keys[i]);
419 g_free (state.keys);
421 last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
424 g_mutex_unlock (&delayed->priv->lock);
426 if (last_one)
427 g_delayed_settings_backend_notify_unapplied (delayed);
429 g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed),
430 path);
433 static void
434 g_delayed_settings_backend_finalize (GObject *object)
436 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (object);
438 g_mutex_clear (&delayed->priv->lock);
439 g_object_unref (delayed->priv->backend);
440 g_tree_unref (delayed->priv->delayed);
442 /* if our owner is still alive, why are we finalizing? */
443 g_assert (delayed->priv->owner == NULL);
445 G_OBJECT_CLASS (g_delayed_settings_backend_parent_class)
446 ->finalize (object);
449 static void
450 g_delayed_settings_backend_class_init (GDelayedSettingsBackendClass *class)
452 GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
453 GObjectClass *object_class = G_OBJECT_CLASS (class);
455 backend_class->read = g_delayed_settings_backend_read;
456 backend_class->read_user_value = g_delayed_settings_backend_read_user_value;
457 backend_class->write = g_delayed_settings_backend_write;
458 backend_class->write_tree = g_delayed_settings_backend_write_tree;
459 backend_class->reset = g_delayed_settings_backend_reset;
460 backend_class->get_writable = g_delayed_settings_backend_get_writable;
461 backend_class->subscribe = g_delayed_settings_backend_subscribe;
462 backend_class->unsubscribe = g_delayed_settings_backend_unsubscribe;
463 backend_class->get_permission = g_delayed_settings_backend_get_permission;
465 object_class->finalize = g_delayed_settings_backend_finalize;
468 static void
469 g_delayed_settings_backend_init (GDelayedSettingsBackend *delayed)
471 delayed->priv = g_delayed_settings_backend_get_instance_private (delayed);
472 delayed->priv->delayed = g_settings_backend_create_tree ();
473 g_mutex_init (&delayed->priv->lock);
476 static void
477 g_delayed_settings_backend_disown (gpointer data,
478 GObject *where_the_object_was)
480 GDelayedSettingsBackend *delayed = data;
482 g_mutex_lock (&delayed->priv->lock);
483 delayed->priv->owner_context = NULL;
484 delayed->priv->owner = NULL;
485 g_mutex_unlock (&delayed->priv->lock);
488 GDelayedSettingsBackend *
489 g_delayed_settings_backend_new (GSettingsBackend *backend,
490 gpointer owner,
491 GMainContext *owner_context)
493 static GSettingsListenerVTable vtable = {
494 delayed_backend_changed,
495 delayed_backend_path_changed,
496 delayed_backend_keys_changed,
497 delayed_backend_writable_changed,
498 delayed_backend_path_writable_changed
500 GDelayedSettingsBackend *delayed;
502 delayed = g_object_new (G_TYPE_DELAYED_SETTINGS_BACKEND, NULL);
503 delayed->priv->backend = g_object_ref (backend);
504 delayed->priv->owner_context = owner_context;
505 delayed->priv->owner = owner;
507 g_object_weak_ref (owner, g_delayed_settings_backend_disown, delayed);
509 g_settings_backend_watch (delayed->priv->backend,
510 &vtable, G_OBJECT (delayed), NULL);
512 return delayed;