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"
31 struct _GDelayedSettingsBackendPrivate
33 GSettingsBackend *backend;
38 G_DEFINE_TYPE (GDelayedSettingsBackend,
39 g_delayed_settings_backend,
40 G_TYPE_SETTINGS_BACKEND)
43 g_delayed_settings_backend_read (GSettingsBackend *backend,
45 const GVariantType *expected_type)
47 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
50 if ((result = g_tree_lookup (delayed->priv->delayed, key)))
51 return g_variant_ref (result);
53 return g_settings_backend_read (delayed->priv->backend,
57 g_delayed_settings_backend_write (GSettingsBackend *backend,
62 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
65 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
66 g_tree_insert (delayed->priv->delayed, g_strdup (key),
67 g_variant_ref_sink (value));
68 g_settings_backend_changed (backend, key, origin_tag);
70 if (was_empty && delayed->priv->owner)
71 g_object_notify (delayed->priv->owner, "has-unapplied");
77 add_to_tree (gpointer key,
81 g_tree_insert (user_data, g_strdup (key), g_variant_ref (value));
86 g_delayed_settings_backend_write_keys (GSettingsBackend *backend,
90 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
93 was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
95 g_tree_foreach (tree, add_to_tree, delayed->priv->delayed);
97 g_settings_backend_changed_tree (backend, tree, origin_tag);
99 if (was_empty && delayed->priv->owner)
100 g_object_notify (delayed->priv->owner, "has-unapplied");
106 g_delayed_settings_backend_get_writable (GSettingsBackend *backend,
109 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
111 return g_settings_backend_get_writable (delayed->priv->backend, name);
115 g_delayed_settings_backend_reset (GSettingsBackend *backend,
119 /* deal with this... */
123 g_delayed_settings_backend_reset_path (GSettingsBackend *backend,
127 /* deal with this... */
131 g_delayed_settings_backend_subscribe (GSettingsBackend *backend,
134 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
136 g_settings_backend_subscribe (delayed->priv->backend, name);
140 g_delayed_settings_backend_unsubscribe (GSettingsBackend *backend,
143 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
145 g_settings_backend_unsubscribe (delayed->priv->backend, name);
151 g_delayed_settings_backend_get_has_unapplied (GDelayedSettingsBackend *delayed)
153 return g_tree_nnodes (delayed->priv->delayed) > 0;
157 g_delayed_settings_backend_apply (GDelayedSettingsBackend *delayed)
159 if (g_tree_nnodes (delayed->priv->delayed) > 0)
164 tmp = delayed->priv->delayed;
165 delayed->priv->delayed = g_settings_backend_create_tree ();
166 success = g_settings_backend_write_keys (delayed->priv->backend,
170 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed),
175 if (delayed->priv->owner)
176 g_object_notify (delayed->priv->owner, "has-unapplied");
181 g_delayed_settings_backend_revert (GDelayedSettingsBackend *delayed)
183 if (g_tree_nnodes (delayed->priv->delayed) > 0)
187 tmp = delayed->priv->delayed;
188 delayed->priv->delayed = g_settings_backend_create_tree ();
189 g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed), tmp, NULL);
192 if (delayed->priv->owner)
193 g_object_notify (delayed->priv->owner, "has-unapplied");
197 /* change notification */
199 delayed_backend_changed (GSettingsBackend *backend,
204 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (user_data);
206 if (origin_tag != delayed->priv)
207 g_settings_backend_changed (G_SETTINGS_BACKEND (delayed),
212 delayed_backend_keys_changed (GSettingsBackend *backend,
214 const gchar * const *items,
218 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (user_data);
220 if (origin_tag != delayed->priv)
221 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (delayed),
222 path, items, origin_tag);
226 delayed_backend_path_changed (GSettingsBackend *backend,
231 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (user_data);
233 if (origin_tag != delayed->priv)
234 g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed),
239 delayed_backend_writable_changed (GSettingsBackend *backend,
243 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (user_data);
245 if (g_tree_lookup (delayed->priv->delayed, key) &&
246 !g_settings_backend_get_writable (delayed->priv->backend, key))
248 /* drop the key from our changeset if it just became read-only.
249 * no need to signal this since the writable change implies it.
251 g_tree_remove (delayed->priv->delayed, key);
253 /* if that was the only key... */
254 if (delayed->priv->owner &&
255 g_tree_nnodes (delayed->priv->delayed) == 0)
256 g_object_notify (delayed->priv->owner, "has-unapplied");
259 g_settings_backend_writable_changed (G_SETTINGS_BACKEND (delayed), key);
262 /* slow method until we get foreach-with-remove in GTree
272 check_prefix (gpointer key,
276 CheckPrefixState *state = data;
278 if (g_str_has_prefix (key, state->path))
279 state->keys[state->index++] = key;
285 delayed_backend_path_writable_changed (GSettingsBackend *backend,
289 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (user_data);
292 n_keys = g_tree_nnodes (delayed->priv->delayed);
296 CheckPrefixState state = { path, g_new (const gchar *, n_keys) };
299 /* collect a list of possibly-affected keys (ie: matching the path) */
300 g_tree_foreach (delayed->priv->delayed, check_prefix, &state);
302 /* drop the keys that have been affected */
303 for (i = 0; i < state.index; i++)
304 if (!g_settings_backend_get_writable (delayed->priv->backend,
306 g_tree_remove (delayed->priv->delayed, state.keys[i]);
310 if (delayed->priv->owner &&
311 g_tree_nnodes (delayed->priv->delayed) == 0)
312 g_object_notify (delayed->priv->owner, "has-unapplied");
315 g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed),
320 g_delayed_settings_backend_finalize (GObject *object)
322 GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (object);
324 g_settings_backend_unwatch (delayed->priv->backend, delayed);
325 g_object_unref (delayed->priv->backend);
329 g_delayed_settings_backend_class_init (GDelayedSettingsBackendClass *class)
331 GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
332 GObjectClass *object_class = G_OBJECT_CLASS (class);
334 g_type_class_add_private (class, sizeof (GDelayedSettingsBackendPrivate));
336 backend_class->read = g_delayed_settings_backend_read;
337 backend_class->write = g_delayed_settings_backend_write;
338 backend_class->write_keys = g_delayed_settings_backend_write_keys;
339 backend_class->reset = g_delayed_settings_backend_reset;
340 backend_class->reset_path = g_delayed_settings_backend_reset_path;
341 backend_class->get_writable = g_delayed_settings_backend_get_writable;
342 backend_class->subscribe = g_delayed_settings_backend_subscribe;
343 backend_class->unsubscribe = g_delayed_settings_backend_unsubscribe;
345 object_class->finalize = g_delayed_settings_backend_finalize;
349 g_delayed_settings_backend_init (GDelayedSettingsBackend *delayed)
352 G_TYPE_INSTANCE_GET_PRIVATE (delayed, G_TYPE_DELAYED_SETTINGS_BACKEND,
353 GDelayedSettingsBackendPrivate);
355 delayed->priv->delayed = g_settings_backend_create_tree ();
358 GDelayedSettingsBackend *
359 g_delayed_settings_backend_new (GSettingsBackend *backend,
362 GDelayedSettingsBackend *delayed;
364 delayed = g_object_new (G_TYPE_DELAYED_SETTINGS_BACKEND, NULL);
365 delayed->priv->backend = g_object_ref (backend);
366 delayed->priv->owner = owner;
368 g_settings_backend_watch (delayed->priv->backend,
369 delayed_backend_changed,
370 delayed_backend_path_changed,
371 delayed_backend_keys_changed,
372 delayed_backend_writable_changed,
373 delayed_backend_path_writable_changed,