2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2010 Novell, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Vincent Untz <vuntz@gnome.org>
21 * Ryan Lortie <desrt@desrt.ca>
26 #include "gkeyfilesettingsbackend.h"
33 #include "giomodule.h"
35 #include "gfileinfo.h"
36 #include "gfilemonitor.h"
40 G_DEFINE_TYPE (GKeyfileSettingsBackend,
41 g_keyfile_settings_backend,
42 G_TYPE_SETTINGS_BACKEND)
44 struct _GKeyfileSettingsBackendPrivate
51 GFileMonitor *monitor;
55 g_keyfile_settings_backend_read (GSettingsBackend *backend,
57 const GVariantType *expected_type)
59 GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (backend);
62 value = g_hash_table_lookup (kf_backend->priv->table, key);
65 g_variant_ref (value);
71 g_keyfile_settings_backend_write_one (const gchar *key,
73 GKeyfileSettingsBackend *kf_backend)
76 const gchar *base_key;
79 g_hash_table_replace (kf_backend->priv->table,
80 g_strdup (key), g_variant_ref (value));
82 slash = strrchr (key, '/');
83 g_assert (slash != NULL);
84 base_key = (slash + 1);
85 path = g_strndup (key, slash - key + 1);
87 g_key_file_set_string (kf_backend->priv->keyfile,
88 path, base_key, g_variant_print (value, TRUE));
96 g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kf_backend)
103 dirname = g_path_get_dirname (kf_backend->priv->file_path);
104 if (!g_file_test (dirname, G_FILE_TEST_IS_DIR))
105 g_mkdir_with_parents (dirname, 0700);
108 contents = g_key_file_to_data (kf_backend->priv->keyfile, &length, NULL);
110 file = g_file_new_for_path (kf_backend->priv->file_path);
111 g_file_replace_contents (file, contents, length,
112 NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION,
114 g_object_unref (file);
116 g_free (kf_backend->priv->checksum);
117 kf_backend->priv->checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA256, contents, length);
123 g_keyfile_settings_backend_write (GSettingsBackend *backend,
128 GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (backend);
130 g_keyfile_settings_backend_write_one (key, value, kf_backend);
131 g_keyfile_settings_backend_keyfile_write (kf_backend);
133 g_settings_backend_changed (backend, key, origin_tag);
139 g_keyfile_settings_backend_write_keys (GSettingsBackend *backend,
143 GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (backend);
145 g_tree_foreach (tree, (GTraverseFunc) g_keyfile_settings_backend_write_one, backend);
146 g_keyfile_settings_backend_keyfile_write (kf_backend);
148 g_settings_backend_changed_tree (backend, tree, origin_tag);
154 g_keyfile_settings_backend_reset_path (GSettingsBackend *backend,
158 GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (backend);
159 GPtrArray *reset_array;
163 gchar **groups = NULL;
167 reset_array = g_ptr_array_new_with_free_func (g_free);
169 hash_keys = g_hash_table_get_keys (kf_backend->priv->table);
170 for (l = hash_keys; l != NULL; l = l->next)
172 if (g_str_has_prefix (l->data, path))
174 g_hash_table_remove (kf_backend->priv->table, l->data);
175 g_ptr_array_add (reset_array, g_strdup (l->data));
178 g_list_free (hash_keys);
181 groups = g_key_file_get_groups (kf_backend->priv->keyfile, &groups_nb);
182 for (i = 0; i < groups_nb; i++)
184 if (g_str_has_prefix (groups[i], path))
185 changed = g_key_file_remove_group (kf_backend->priv->keyfile, groups[i], NULL) || changed;
190 g_keyfile_settings_backend_keyfile_write (kf_backend);
192 if (reset_array->len > 0)
194 /* the array has to be NULL-terminated */
195 g_ptr_array_add (reset_array, NULL);
196 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (kf_backend),
198 (const gchar **) reset_array->pdata,
202 g_ptr_array_free (reset_array, TRUE);
206 g_keyfile_settings_backend_reset (GSettingsBackend *backend,
210 GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (backend);
213 const gchar *base_key;
216 had_key = g_hash_table_lookup_extended (kf_backend->priv->table, key, NULL, NULL);
218 g_hash_table_remove (kf_backend->priv->table, key);
220 slash = strrchr (key, '/');
221 g_assert (slash != NULL);
222 base_key = (slash + 1);
223 path = g_strndup (key, slash - key + 1);
225 if (g_key_file_remove_key (kf_backend->priv->keyfile, path, base_key, NULL))
226 g_keyfile_settings_backend_keyfile_write (kf_backend);
231 g_settings_backend_changed (G_SETTINGS_BACKEND (kf_backend), key, origin_tag);
235 g_keyfile_settings_backend_get_writable (GSettingsBackend *backend,
238 GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (backend);
240 return kf_backend->priv->writable;
244 g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kf_backend)
246 gchar *contents = NULL;
249 GHashTable *loaded_keys;
250 GPtrArray *changed_array;
251 gchar **groups = NULL;
254 GList *keys_l = NULL;
257 if (!g_file_get_contents (kf_backend->priv->file_path,
258 &contents, &length, NULL))
260 contents = g_strdup ("");
264 new_checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA256, contents, length);
266 if (g_strcmp0 (kf_backend->priv->checksum, new_checksum) == 0)
268 g_free (new_checksum);
272 if (kf_backend->priv->checksum != NULL)
273 g_free (kf_backend->priv->checksum);
274 kf_backend->priv->checksum = new_checksum;
276 if (kf_backend->priv->keyfile != NULL)
277 g_key_file_free (kf_backend->priv->keyfile);
279 kf_backend->priv->keyfile = g_key_file_new ();
281 /* we just silently ignore errors: there's not much we can do about them */
283 g_key_file_load_from_data (kf_backend->priv->keyfile, contents, length,
284 G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
286 loaded_keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
287 changed_array = g_ptr_array_new_with_free_func (g_free);
289 /* Load keys from the keyfile */
290 groups = g_key_file_get_groups (kf_backend->priv->keyfile, &groups_nb);
291 for (i = 0; i < groups_nb; i++)
297 keys = g_key_file_get_keys (kf_backend->priv->keyfile, groups[i], &keys_nb, NULL);
301 for (j = 0; j < keys_nb; j++)
305 GVariant *old_variant;
308 value = g_key_file_get_string (kf_backend->priv->keyfile, groups[i], keys[j], NULL);
312 variant = g_variant_new_parsed (value);
318 full_key = g_strjoin ("", groups[i], keys[j], NULL),
319 g_hash_table_insert (loaded_keys, full_key, GINT_TO_POINTER(TRUE));
321 old_variant = g_hash_table_lookup (kf_backend->priv->table, full_key);
323 if (old_variant == NULL || !g_variant_equal (old_variant, variant))
325 g_ptr_array_add (changed_array, g_strdup (full_key));
326 g_hash_table_replace (kf_backend->priv->table,
328 g_variant_ref_sink (variant));
331 g_variant_unref (variant);
338 /* Remove keys that were in the hashtable but not in the keyfile */
339 keys_l = g_hash_table_get_keys (kf_backend->priv->table);
340 for (l = keys_l; l != NULL; l = l->next)
342 gchar *key = l->data;
344 if (g_hash_table_lookup_extended (loaded_keys, key, NULL, NULL))
347 g_ptr_array_add (changed_array, g_strdup (key));
348 g_hash_table_remove (kf_backend->priv->table, key);
350 g_list_free (keys_l);
352 if (changed_array->len > 0)
354 /* the array has to be NULL-terminated */
355 g_ptr_array_add (changed_array, NULL);
356 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (kf_backend),
358 (const gchar **) changed_array->pdata,
362 g_hash_table_unref (loaded_keys);
363 g_ptr_array_free (changed_array, TRUE);
367 g_keyfile_settings_backend_keyfile_writable (GFile *file)
371 gboolean writable = FALSE;
374 fileinfo = g_file_query_info (file, "access::*",
375 G_FILE_QUERY_INFO_NONE, NULL, &error);
377 if (fileinfo == NULL)
379 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
383 parent = g_file_get_parent (file);
386 writable = g_keyfile_settings_backend_keyfile_writable (parent);
387 g_object_unref (parent);
391 g_error_free (error);
396 /* We don't want to mark the backend as writable if the file is not readable,
397 * since it means we won't be able to load the content of the file, and we'll
400 g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_READ) &&
401 g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
402 g_object_unref (fileinfo);
408 g_keyfile_settings_backend_keyfile_changed (GFileMonitor *monitor,
411 GFileMonitorEvent event_type,
414 GKeyfileSettingsBackend *kf_backend;
416 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
417 event_type != G_FILE_MONITOR_EVENT_CREATED &&
418 event_type != G_FILE_MONITOR_EVENT_DELETED &&
419 event_type != G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED)
422 kf_backend = G_KEYFILE_SETTINGS_BACKEND (user_data);
424 if (event_type == G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED)
428 writable = g_keyfile_settings_backend_keyfile_writable (file);
430 if (kf_backend->priv->writable == writable)
433 kf_backend->priv->writable = writable;
436 /* else: reload the file since it was possibly not readable before */
439 g_keyfile_settings_backend_keyfile_reload (kf_backend);
443 g_keyfile_settings_backend_finalize (GObject *object)
445 GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (object);
447 g_hash_table_unref (kf_backend->priv->table);
448 kf_backend->priv->table = NULL;
450 g_key_file_free (kf_backend->priv->keyfile);
451 kf_backend->priv->keyfile = NULL;
453 g_free (kf_backend->priv->file_path);
454 kf_backend->priv->file_path = NULL;
456 g_free (kf_backend->priv->checksum);
457 kf_backend->priv->checksum = NULL;
459 g_file_monitor_cancel (kf_backend->priv->monitor);
460 g_object_unref (kf_backend->priv->monitor);
461 kf_backend->priv->monitor = NULL;
463 G_OBJECT_CLASS (g_keyfile_settings_backend_parent_class)
468 g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kf_backend)
470 kf_backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (kf_backend,
471 G_TYPE_KEYFILE_SETTINGS_BACKEND,
472 GKeyfileSettingsBackendPrivate);
473 kf_backend->priv->table =
474 g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
475 (GDestroyNotify) g_variant_unref);
477 kf_backend->priv->keyfile = NULL;
478 kf_backend->priv->writable = FALSE;
479 kf_backend->priv->file_path = NULL;
480 kf_backend->priv->checksum = NULL;
481 kf_backend->priv->monitor = NULL;
485 g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
487 GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
488 GObjectClass *object_class = G_OBJECT_CLASS (class);
490 object_class->finalize = g_keyfile_settings_backend_finalize;
492 backend_class->read = g_keyfile_settings_backend_read;
493 backend_class->write = g_keyfile_settings_backend_write;
494 backend_class->write_keys = g_keyfile_settings_backend_write_keys;
495 backend_class->reset = g_keyfile_settings_backend_reset;
496 backend_class->reset_path = g_keyfile_settings_backend_reset_path;
497 backend_class->get_writable = g_keyfile_settings_backend_get_writable;
498 /* No need to implement subscribed/unsubscribe: the only point would be to
499 * stop monitoring the file when there's no GSettings anymore, which is no
502 g_type_class_add_private (class, sizeof (GKeyfileSettingsBackendPrivate));
505 static GKeyfileSettingsBackend *
506 g_keyfile_settings_backend_new (const gchar *filename)
508 GKeyfileSettingsBackend *kf_backend;
511 kf_backend = g_object_new (G_TYPE_KEYFILE_SETTINGS_BACKEND, NULL);
512 kf_backend->priv->file_path = g_strdup (filename);
514 file = g_file_new_for_path (kf_backend->priv->file_path);
516 kf_backend->priv->writable = g_keyfile_settings_backend_keyfile_writable (file);
518 kf_backend->priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_SEND_MOVED, NULL, NULL);
519 g_signal_connect (kf_backend->priv->monitor, "changed",
520 (GCallback)g_keyfile_settings_backend_keyfile_changed, kf_backend);
522 g_object_unref (file);
524 g_keyfile_settings_backend_keyfile_reload (kf_backend);
530 * g_settings_backend_setup_keyfile:
531 * @context: a context string (not %NULL or "")
532 * @filename: a filename
534 * Sets up a keyfile for use with #GSettings.
536 * If you create a #GSettings with its context property set to @context
537 * then the settings will be stored in the keyfile at @filename. See
538 * g_settings_new_with_context().
540 * The keyfile must be setup before any settings objects are created
541 * for the named context.
543 * It is not possible to specify a keyfile for the default context.
545 * If the path leading up to @filename does not exist, it will be
546 * recursively created with user-only permissions. If the keyfile is
547 * not writable, any #GSettings objects created using @context will
548 * return %FALSE for any calls to g_settings_is_writable() and any
549 * attempts to write will fail.
554 g_settings_backend_setup_keyfile (const gchar *context,
555 const gchar *filename)
557 GKeyfileSettingsBackend *kf_backend;
559 kf_backend = g_keyfile_settings_backend_new (filename);
560 g_settings_backend_setup (context, G_SETTINGS_BACKEND (kf_backend));
561 g_object_unref (kf_backend);
564 #define __G_KEYFILE_SETTINGS_BACKEND_C__
565 #include "gioaliasdef.c"