gio/tests/file: skip the file monitor tests if using GPollFileMonitor
[platform/upstream/glib.git] / gio / gdelayedsettingsbackend.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gdelayedsettingsbackend.h"
25 #include "gsettingsbackendinternal.h"
26
27 #include <string.h>
28
29
30 struct _GDelayedSettingsBackendPrivate
31 {
32   GSettingsBackend *backend;
33   GMutex lock;
34   GTree *delayed;
35
36   GMainContext *owner_context;
37   gpointer owner;
38 };
39
40 G_DEFINE_TYPE (GDelayedSettingsBackend,
41                g_delayed_settings_backend,
42                G_TYPE_SETTINGS_BACKEND)
43
44 static gboolean
45 invoke_notify_unapplied (gpointer data)
46 {
47   g_object_notify (data, "has-unapplied");
48   g_object_unref (data);
49
50   return FALSE;
51 }
52
53 static void
54 g_delayed_settings_backend_notify_unapplied (GDelayedSettingsBackend *delayed)
55 {
56   GMainContext *target_context;
57   GObject *target;
58
59   g_mutex_lock (&delayed->priv->lock);
60   if (delayed->priv->owner)
61     {
62       target_context = delayed->priv->owner_context;
63       target = g_object_ref (delayed->priv->owner);
64     }
65   else
66     {
67       target_context = NULL;
68       target = NULL;
69     }
70   g_mutex_unlock (&delayed->priv->lock);
71
72   if (target != NULL)
73     g_main_context_invoke (target_context, invoke_notify_unapplied, target);
74 }
75
76
77 static GVariant *
78 g_delayed_settings_backend_read (GSettingsBackend   *backend,
79                                  const gchar        *key,
80                                  const GVariantType *expected_type,
81                                  gboolean            default_value)
82 {
83   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
84   gpointer result = NULL;
85
86   if (!default_value)
87     {
88       g_mutex_lock (&delayed->priv->lock);
89       if (g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result))
90         {
91           /* NULL in the tree means we should consult the default value */
92           if (result != NULL)
93             g_variant_ref (result);
94           else
95             default_value = TRUE;
96         }
97       g_mutex_unlock (&delayed->priv->lock);
98     }
99
100   if (result == NULL)
101     result = g_settings_backend_read (delayed->priv->backend, key,
102                                       expected_type, default_value);
103
104   return result;
105 }
106
107 static gboolean
108 g_delayed_settings_backend_write (GSettingsBackend *backend,
109                                   const gchar      *key,
110                                   GVariant         *value,
111                                   gpointer          origin_tag)
112 {
113   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
114   gboolean was_empty;
115
116   g_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_mutex_unlock (&delayed->priv->lock);
121
122   g_settings_backend_changed (backend, key, origin_tag);
123
124   if (was_empty)
125     g_delayed_settings_backend_notify_unapplied (delayed);
126
127   return TRUE;
128 }
129
130 static gboolean
131 add_to_tree (gpointer key,
132              gpointer value,
133              gpointer user_data)
134 {
135   g_tree_insert (user_data, g_strdup (key), g_variant_ref (value));
136   return FALSE;
137 }
138
139 static gboolean
140 g_delayed_settings_backend_write_tree (GSettingsBackend *backend,
141                                        GTree            *tree,
142                                        gpointer          origin_tag)
143 {
144   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
145   gboolean was_empty;
146
147   g_mutex_lock (&delayed->priv->lock);
148   was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
149
150   g_tree_foreach (tree, add_to_tree, delayed->priv->delayed);
151   g_mutex_unlock (&delayed->priv->lock);
152
153   g_settings_backend_changed_tree (backend, tree, origin_tag);
154
155   if (was_empty)
156     g_delayed_settings_backend_notify_unapplied (delayed);
157
158   return TRUE;
159 }
160
161 static gboolean
162 g_delayed_settings_backend_get_writable (GSettingsBackend *backend,
163                                          const gchar      *name)
164 {
165   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
166
167   return g_settings_backend_get_writable (delayed->priv->backend, name);
168 }
169
170 static void
171 g_delayed_settings_backend_reset (GSettingsBackend *backend,
172                                   const gchar      *key,
173                                   gpointer          origin_tag)
174 {
175   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
176   gboolean was_empty;
177
178   g_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_mutex_unlock (&delayed->priv->lock);
182
183   if (was_empty)
184     g_delayed_settings_backend_notify_unapplied (delayed);
185 }
186
187 static void
188 g_delayed_settings_backend_subscribe (GSettingsBackend *backend,
189                                       const char       *name)
190 {
191   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
192
193   g_settings_backend_subscribe (delayed->priv->backend, name);
194 }
195
196 static void
197 g_delayed_settings_backend_unsubscribe (GSettingsBackend *backend,
198                                         const char       *name)
199 {
200   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
201
202   g_settings_backend_unsubscribe (delayed->priv->backend, name);
203 }
204
205 static GPermission *
206 g_delayed_settings_backend_get_permission (GSettingsBackend *backend,
207                                            const gchar      *path)
208 {
209   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
210
211   return g_settings_backend_get_permission (delayed->priv->backend, path);
212 }
213
214
215 /* method calls */
216 gboolean
217 g_delayed_settings_backend_get_has_unapplied (GDelayedSettingsBackend *delayed)
218 {
219   /* we don't need to lock for this... */
220
221   return g_tree_nnodes (delayed->priv->delayed) > 0;
222 }
223
224 void
225 g_delayed_settings_backend_apply (GDelayedSettingsBackend *delayed)
226 {
227   if (g_tree_nnodes (delayed->priv->delayed) > 0)
228     {
229       gboolean success;
230       GTree *tmp;
231
232       g_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,
236                                                tmp, delayed->priv);
237       g_mutex_unlock (&delayed->priv->lock);
238
239       if (!success)
240         g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed),
241                                          tmp, NULL);
242
243       g_tree_unref (tmp);
244
245       g_delayed_settings_backend_notify_unapplied (delayed);
246     }
247 }
248
249 void
250 g_delayed_settings_backend_revert (GDelayedSettingsBackend *delayed)
251 {
252   if (g_tree_nnodes (delayed->priv->delayed) > 0)
253     {
254       GTree *tmp;
255
256       g_mutex_lock (&delayed->priv->lock);
257       tmp = delayed->priv->delayed;
258       delayed->priv->delayed = g_settings_backend_create_tree ();
259       g_mutex_unlock (&delayed->priv->lock);
260       g_settings_backend_changed_tree (G_SETTINGS_BACKEND (delayed), tmp, NULL);
261       g_tree_unref (tmp);
262
263       g_delayed_settings_backend_notify_unapplied (delayed);
264     }
265 }
266
267 /* change notification */
268 static void
269 delayed_backend_changed (GObject          *target,
270                          GSettingsBackend *backend,
271                          const gchar      *key,
272                          gpointer          origin_tag)
273 {
274   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
275
276   if (origin_tag != delayed->priv)
277     g_settings_backend_changed (G_SETTINGS_BACKEND (delayed),
278                                 key, origin_tag);
279 }
280
281 static void
282 delayed_backend_keys_changed (GObject             *target,
283                               GSettingsBackend    *backend,
284                               const gchar         *path,
285                               const gchar * const *items,
286                               gpointer             origin_tag)
287 {
288   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
289
290   if (origin_tag != delayed->priv)
291     g_settings_backend_keys_changed (G_SETTINGS_BACKEND (delayed),
292                                      path, items, origin_tag);
293 }
294
295 static void
296 delayed_backend_path_changed (GObject          *target,
297                               GSettingsBackend *backend,
298                               const gchar      *path,
299                               gpointer          origin_tag)
300 {
301   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
302
303   if (origin_tag != delayed->priv)
304     g_settings_backend_path_changed (G_SETTINGS_BACKEND (delayed),
305                                      path, origin_tag);
306 }
307
308 static void
309 delayed_backend_writable_changed (GObject          *target,
310                                   GSettingsBackend *backend,
311                                   const gchar      *key)
312 {
313   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
314   gboolean last_one = FALSE;
315
316   g_mutex_lock (&delayed->priv->lock);
317
318   if (g_tree_lookup (delayed->priv->delayed, key) != NULL &&
319       !g_settings_backend_get_writable (delayed->priv->backend, key))
320     {
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.
323        *
324        * note that the item in the tree may very well be set to NULL in
325        * the case that the user stored a reset.  we intentionally don't
326        * drop the key in this case since a reset will always succeed
327        * (even against a non-writable key).
328        */
329       g_tree_remove (delayed->priv->delayed, key);
330
331       /* if that was the only key... */
332       last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
333     }
334
335   g_mutex_unlock (&delayed->priv->lock);
336
337   if (last_one)
338     g_delayed_settings_backend_notify_unapplied (delayed);
339
340   g_settings_backend_writable_changed (G_SETTINGS_BACKEND (delayed), key);
341 }
342
343 /* slow method until we get foreach-with-remove in GTree
344  */
345 typedef struct
346 {
347   const gchar *path;
348   const gchar **keys;
349   gsize index;
350 } CheckPrefixState;
351
352 static gboolean
353 check_prefix (gpointer key,
354               gpointer value,
355               gpointer data)
356 {
357   CheckPrefixState *state = data;
358
359   if (g_str_has_prefix (key, state->path))
360     state->keys[state->index++] = key;
361
362   return FALSE;
363 }
364
365 static void
366 delayed_backend_path_writable_changed (GObject          *target,
367                                        GSettingsBackend *backend,
368                                        const gchar      *path)
369 {
370   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (target);
371   gboolean last_one = FALSE;
372   gsize n_keys;
373
374   g_mutex_lock (&delayed->priv->lock);
375
376   n_keys = g_tree_nnodes (delayed->priv->delayed);
377
378   if (n_keys > 0)
379     {
380       CheckPrefixState state = { path, g_new (const gchar *, n_keys) };
381       gsize i;
382
383       /* collect a list of possibly-affected keys (ie: matching the path) */
384       g_tree_foreach (delayed->priv->delayed, check_prefix, &state);
385
386       /* drop the keys that have been affected.
387        *
388        * don't drop 'reset' keys (see above) */
389       for (i = 0; i < state.index; i++)
390         if (g_tree_lookup (delayed->priv->delayed, state.keys[i]) != NULL &&
391             !g_settings_backend_get_writable (delayed->priv->backend,
392                                               state.keys[i]))
393           g_tree_remove (delayed->priv->delayed, state.keys[i]);
394
395       g_free (state.keys);
396
397       last_one = g_tree_nnodes (delayed->priv->delayed) == 0;
398     }
399
400   g_mutex_unlock (&delayed->priv->lock);
401
402   if (last_one)
403     g_delayed_settings_backend_notify_unapplied (delayed);
404
405   g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (delayed),
406                                             path);
407 }
408
409 static void
410 g_delayed_settings_backend_finalize (GObject *object)
411 {
412   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (object);
413
414   g_mutex_clear (&delayed->priv->lock);
415   g_object_unref (delayed->priv->backend);
416   g_tree_unref (delayed->priv->delayed);
417
418   /* if our owner is still alive, why are we finalizing? */
419   g_assert (delayed->priv->owner == NULL);
420
421   G_OBJECT_CLASS (g_delayed_settings_backend_parent_class)
422     ->finalize (object);
423 }
424
425 static void
426 g_delayed_settings_backend_class_init (GDelayedSettingsBackendClass *class)
427 {
428   GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
429   GObjectClass *object_class = G_OBJECT_CLASS (class);
430
431   g_type_class_add_private (class, sizeof (GDelayedSettingsBackendPrivate));
432
433   backend_class->read = g_delayed_settings_backend_read;
434   backend_class->write = g_delayed_settings_backend_write;
435   backend_class->write_tree = g_delayed_settings_backend_write_tree;
436   backend_class->reset = g_delayed_settings_backend_reset;
437   backend_class->get_writable = g_delayed_settings_backend_get_writable;
438   backend_class->subscribe = g_delayed_settings_backend_subscribe;
439   backend_class->unsubscribe = g_delayed_settings_backend_unsubscribe;
440   backend_class->get_permission = g_delayed_settings_backend_get_permission;
441
442   object_class->finalize = g_delayed_settings_backend_finalize;
443 }
444
445 static void
446 g_delayed_settings_backend_init (GDelayedSettingsBackend *delayed)
447 {
448   delayed->priv =
449     G_TYPE_INSTANCE_GET_PRIVATE (delayed, G_TYPE_DELAYED_SETTINGS_BACKEND,
450                                  GDelayedSettingsBackendPrivate);
451
452   delayed->priv->delayed = g_settings_backend_create_tree ();
453   g_mutex_init (&delayed->priv->lock);
454 }
455
456 static void
457 g_delayed_settings_backend_disown (gpointer  data,
458                                    GObject  *where_the_object_was)
459 {
460   GDelayedSettingsBackend *delayed = data;
461
462   g_mutex_lock (&delayed->priv->lock);
463   delayed->priv->owner_context = NULL;
464   delayed->priv->owner = NULL;
465   g_mutex_unlock (&delayed->priv->lock);
466 }
467
468 GDelayedSettingsBackend *
469 g_delayed_settings_backend_new (GSettingsBackend *backend,
470                                 gpointer          owner,
471                                 GMainContext     *owner_context)
472 {
473   static GSettingsListenerVTable vtable = {
474     delayed_backend_changed,
475     delayed_backend_path_changed,
476     delayed_backend_keys_changed,
477     delayed_backend_writable_changed,
478     delayed_backend_path_writable_changed
479   };
480   GDelayedSettingsBackend *delayed;
481
482   delayed = g_object_new (G_TYPE_DELAYED_SETTINGS_BACKEND, NULL);
483   delayed->priv->backend = g_object_ref (backend);
484   delayed->priv->owner_context = owner_context;
485   delayed->priv->owner = owner;
486
487   g_object_weak_ref (owner, g_delayed_settings_backend_disown, delayed);
488
489   g_settings_backend_watch (delayed->priv->backend,
490                             &vtable, G_OBJECT (delayed), NULL);
491
492   return delayed;
493 }