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>
22 #include "gdelayedsettingsbackend.h"
23 #include "gsettingsbackendinternal.h"
28 struct _GDelayedSettingsBackendPrivate
30 GSettingsBackend
*backend
;
34 GMainContext
*owner_context
;
38 G_DEFINE_TYPE_WITH_PRIVATE (GDelayedSettingsBackend
,
39 g_delayed_settings_backend
,
40 G_TYPE_SETTINGS_BACKEND
)
43 invoke_notify_unapplied (gpointer data
)
45 g_object_notify (data
, "has-unapplied");
46 g_object_unref (data
);
52 g_delayed_settings_backend_notify_unapplied (GDelayedSettingsBackend
*delayed
)
54 GMainContext
*target_context
;
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
);
65 target_context
= NULL
;
68 g_mutex_unlock (&delayed
->priv
->lock
);
71 g_main_context_invoke (target_context
, invoke_notify_unapplied
, target
);
76 g_delayed_settings_backend_read (GSettingsBackend
*backend
,
78 const GVariantType
*expected_type
,
79 gboolean default_value
)
81 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
82 gpointer result
= NULL
;
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 */
91 g_variant_ref (result
);
95 g_mutex_unlock (&delayed
->priv
->lock
);
99 result
= g_settings_backend_read (delayed
->priv
->backend
, key
,
100 expected_type
, default_value
);
106 g_delayed_settings_backend_read_user_value (GSettingsBackend
*backend
,
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
);
122 g_variant_ref (result
);
123 g_mutex_unlock (&delayed
->priv
->lock
);
128 return g_settings_backend_read_user_value (delayed
->priv
->backend
, key
, expected_type
);
132 g_delayed_settings_backend_write (GSettingsBackend
*backend
,
137 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
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
);
149 g_delayed_settings_backend_notify_unapplied (delayed
);
155 add_to_tree (gpointer key
,
159 g_tree_insert (user_data
, g_strdup (key
), g_variant_ref (value
));
164 g_delayed_settings_backend_write_tree (GSettingsBackend
*backend
,
168 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
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
);
180 g_delayed_settings_backend_notify_unapplied (delayed
);
186 g_delayed_settings_backend_get_writable (GSettingsBackend
*backend
,
189 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
191 return g_settings_backend_get_writable (delayed
->priv
->backend
, name
);
195 g_delayed_settings_backend_reset (GSettingsBackend
*backend
,
199 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
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
);
208 g_delayed_settings_backend_notify_unapplied (delayed
);
212 g_delayed_settings_backend_subscribe (GSettingsBackend
*backend
,
215 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
217 g_settings_backend_subscribe (delayed
->priv
->backend
, name
);
221 g_delayed_settings_backend_unsubscribe (GSettingsBackend
*backend
,
224 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
226 g_settings_backend_unsubscribe (delayed
->priv
->backend
, name
);
230 g_delayed_settings_backend_get_permission (GSettingsBackend
*backend
,
233 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (backend
);
235 return g_settings_backend_get_permission (delayed
->priv
->backend
, path
);
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;
249 g_delayed_settings_backend_apply (GDelayedSettingsBackend
*delayed
)
251 if (g_tree_nnodes (delayed
->priv
->delayed
) > 0)
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
,
261 g_mutex_unlock (&delayed
->priv
->lock
);
264 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed
),
269 g_delayed_settings_backend_notify_unapplied (delayed
);
274 g_delayed_settings_backend_revert (GDelayedSettingsBackend
*delayed
)
276 if (g_tree_nnodes (delayed
->priv
->delayed
) > 0)
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
);
287 g_delayed_settings_backend_notify_unapplied (delayed
);
291 /* change notification */
293 delayed_backend_changed (GObject
*target
,
294 GSettingsBackend
*backend
,
298 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (target
);
300 if (origin_tag
!= delayed
->priv
)
301 g_settings_backend_changed (G_SETTINGS_BACKEND (delayed
),
306 delayed_backend_keys_changed (GObject
*target
,
307 GSettingsBackend
*backend
,
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
);
320 delayed_backend_path_changed (GObject
*target
,
321 GSettingsBackend
*backend
,
325 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (target
);
327 if (origin_tag
!= delayed
->priv
)
328 g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed
),
333 delayed_backend_writable_changed (GObject
*target
,
334 GSettingsBackend
*backend
,
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
);
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
377 check_prefix (gpointer key
,
381 CheckPrefixState
*state
= data
;
383 if (g_str_has_prefix (key
, state
->path
))
384 state
->keys
[state
->index
++] = key
;
390 delayed_backend_path_writable_changed (GObject
*target
,
391 GSettingsBackend
*backend
,
394 GDelayedSettingsBackend
*delayed
= G_DELAYED_SETTINGS_BACKEND (target
);
395 gboolean last_one
= FALSE
;
398 g_mutex_lock (&delayed
->priv
->lock
);
400 n_keys
= g_tree_nnodes (delayed
->priv
->delayed
);
404 CheckPrefixState state
= { path
, g_new (const gchar
*, n_keys
) };
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
,
417 g_tree_remove (delayed
->priv
->delayed
, state
.keys
[i
]);
421 last_one
= g_tree_nnodes (delayed
->priv
->delayed
) == 0;
424 g_mutex_unlock (&delayed
->priv
->lock
);
427 g_delayed_settings_backend_notify_unapplied (delayed
);
429 g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed
),
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
)
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
;
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
);
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
,
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
);