ci: Fix documentation build for `pages` job
[dconf.git] / gsettings / dconfsettingsbackend.c
blob752e013c696747167fa24993cb06993ac8839867
1 /*
2 * Copyright © 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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #include "config.h"
22 #define G_SETTINGS_ENABLE_BACKEND
23 #include <gio/gsettingsbackend.h>
24 #include "../engine/dconf-engine.h"
25 #include <gio/gio.h>
27 #include <string.h>
29 typedef GSettingsBackendClass DConfSettingsBackendClass;
31 typedef struct
33 GSettingsBackend backend;
34 DConfEngine *engine;
35 } DConfSettingsBackend;
37 static GType dconf_settings_backend_get_type (void);
38 G_DEFINE_TYPE (DConfSettingsBackend, dconf_settings_backend, G_TYPE_SETTINGS_BACKEND)
40 static GVariant *
41 dconf_settings_backend_read (GSettingsBackend *backend,
42 const gchar *key,
43 const GVariantType *expected_type,
44 gboolean default_value)
46 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
48 return dconf_engine_read (dcsb->engine,
49 default_value ? DCONF_READ_DEFAULT_VALUE : 0,
50 NULL, key);
53 static GVariant *
54 dconf_settings_backend_read_user_value (GSettingsBackend *backend,
55 const gchar *key,
56 const GVariantType *expected_type)
58 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
60 return dconf_engine_read (dcsb->engine, DCONF_READ_USER_VALUE, NULL, key);
63 static gboolean
64 dconf_settings_backend_write (GSettingsBackend *backend,
65 const gchar *key,
66 GVariant *value,
67 gpointer origin_tag)
69 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
70 DConfChangeset *change;
71 gboolean success;
73 change = dconf_changeset_new ();
74 dconf_changeset_set (change, key, value);
76 success = dconf_engine_change_fast (dcsb->engine, change, origin_tag, NULL);
77 dconf_changeset_unref (change);
79 return success;
82 static gboolean
83 dconf_settings_backend_add_to_changeset (gpointer key,
84 gpointer value,
85 gpointer data)
87 dconf_changeset_set (data, key, value);
89 return FALSE;
92 static gboolean
93 dconf_settings_backend_write_tree (GSettingsBackend *backend,
94 GTree *tree,
95 gpointer origin_tag)
97 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
98 DConfChangeset *change;
99 gboolean success;
101 if (g_tree_nnodes (tree) == 0)
102 return TRUE;
104 change = dconf_changeset_new ();
105 g_tree_foreach (tree, dconf_settings_backend_add_to_changeset, change);
106 success = dconf_engine_change_fast (dcsb->engine, change, origin_tag, NULL);
107 dconf_changeset_unref (change);
109 return success;
112 static void
113 dconf_settings_backend_reset (GSettingsBackend *backend,
114 const gchar *key,
115 gpointer origin_tag)
117 dconf_settings_backend_write (backend, key, NULL, origin_tag);
120 static gboolean
121 dconf_settings_backend_get_writable (GSettingsBackend *backend,
122 const gchar *name)
124 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
126 return dconf_engine_is_writable (dcsb->engine, name);
129 static void
130 dconf_settings_backend_subscribe (GSettingsBackend *backend,
131 const gchar *name)
133 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
135 dconf_engine_watch_fast (dcsb->engine, name);
138 static void
139 dconf_settings_backend_unsubscribe (GSettingsBackend *backend,
140 const gchar *name)
142 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
144 dconf_engine_unwatch_fast (dcsb->engine, name);
147 static void
148 dconf_settings_backend_sync (GSettingsBackend *backend)
150 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
152 dconf_engine_sync (dcsb->engine);
155 static void
156 dconf_settings_backend_free_weak_ref (gpointer data)
158 GWeakRef *weak_ref = data;
160 g_weak_ref_clear (weak_ref);
161 g_slice_free (GWeakRef, weak_ref);
164 static void
165 dconf_settings_backend_init (DConfSettingsBackend *dcsb)
167 GWeakRef *weak_ref;
169 weak_ref = g_slice_new (GWeakRef);
170 g_weak_ref_init (weak_ref, dcsb);
171 dcsb->engine = dconf_engine_new (NULL, weak_ref, dconf_settings_backend_free_weak_ref);
174 static void
175 dconf_settings_backend_finalize (GObject *object)
177 DConfSettingsBackend *dcsb = (DConfSettingsBackend *) object;
179 dconf_engine_unref (dcsb->engine);
181 G_OBJECT_CLASS (dconf_settings_backend_parent_class)
182 ->finalize (object);
185 static void
186 dconf_settings_backend_class_init (GSettingsBackendClass *class)
188 GObjectClass *object_class = G_OBJECT_CLASS (class);
190 object_class->finalize = dconf_settings_backend_finalize;
192 class->read = dconf_settings_backend_read;
193 class->read_user_value = dconf_settings_backend_read_user_value;
194 class->write = dconf_settings_backend_write;
195 class->write_tree = dconf_settings_backend_write_tree;
196 class->reset = dconf_settings_backend_reset;
197 class->get_writable = dconf_settings_backend_get_writable;
198 class->subscribe = dconf_settings_backend_subscribe;
199 class->unsubscribe = dconf_settings_backend_unsubscribe;
200 class->sync = dconf_settings_backend_sync;
203 void
204 g_io_module_load (GIOModule *module)
206 g_type_module_use (G_TYPE_MODULE (module));
207 g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
208 dconf_settings_backend_get_type (),
209 "dconf", 100);
212 void
213 g_io_module_unload (GIOModule *module)
215 g_assert_not_reached ();
218 gchar **
219 g_io_module_query (void)
221 return g_strsplit (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME, "!", 0);
224 void
225 dconf_engine_change_notify (DConfEngine *engine,
226 const gchar *prefix,
227 const gchar * const *changes,
228 const gchar *tag,
229 gboolean is_writability,
230 gpointer origin_tag,
231 gpointer user_data)
233 GWeakRef *weak_ref = user_data;
234 DConfSettingsBackend *dcsb;
236 dcsb = g_weak_ref_get (weak_ref);
238 if (dcsb == NULL)
239 return;
241 if (changes[0] == NULL)
242 return;
244 if (is_writability)
246 /* We know that the engine does it this way... */
247 g_assert (changes[0][0] == '\0' && changes[1] == NULL);
249 if (g_str_has_suffix (prefix, "/"))
250 g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (dcsb), prefix);
251 else
252 g_settings_backend_writable_changed (G_SETTINGS_BACKEND (dcsb), prefix);
255 /* We send the normal change notification even in the event that this
256 * was a writability notification because adding/removing a lock could
257 * impact the value that gets read.
259 if (changes[1] == NULL)
261 if (g_str_has_suffix (prefix, "/"))
262 g_settings_backend_path_changed (G_SETTINGS_BACKEND (dcsb), prefix, origin_tag);
263 else
264 g_settings_backend_changed (G_SETTINGS_BACKEND (dcsb), prefix, origin_tag);
266 else
267 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (dcsb), prefix, changes, origin_tag);