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>
30 #include "gfileinfo.h"
31 #include "gfilemonitor.h"
32 #include "gsimplepermission.h"
33 #include "gsettingsbackend.h"
37 #define G_TYPE_KEYFILE_SETTINGS_BACKEND (g_keyfile_settings_backend_get_type ())
38 #define G_KEYFILE_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
39 G_TYPE_KEYFILE_SETTINGS_BACKEND, \
40 GKeyfileSettingsBackend))
41 #define G_IS_KEYFILE_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
42 G_TYPE_KEYFILE_SETTINGS_BACKEND))
45 typedef GSettingsBackendClass GKeyfileSettingsBackendClass;
49 GSettingsBackend parent_instance;
52 GPermission *permission;
61 GFileMonitor *file_monitor;
64 GFileMonitor *dir_monitor;
65 } GKeyfileSettingsBackend;
67 static GType g_keyfile_settings_backend_get_type (void);
68 G_DEFINE_TYPE (GKeyfileSettingsBackend,
69 g_keyfile_settings_backend,
70 G_TYPE_SETTINGS_BACKEND)
73 compute_checksum (guint8 *digest,
74 gconstpointer contents,
80 checksum = g_checksum_new (G_CHECKSUM_SHA256);
81 g_checksum_update (checksum, contents, length);
82 g_checksum_get_digest (checksum, digest, &len);
83 g_checksum_free (checksum);
88 g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb)
93 contents = g_key_file_to_data (kfsb->keyfile, &length, NULL);
94 g_file_replace_contents (kfsb->file, contents, length, NULL, FALSE,
95 G_FILE_CREATE_REPLACE_DESTINATION,
98 compute_checksum (kfsb->digest, contents, length);
103 group_name_matches (const gchar *group_name,
106 /* sort of like g_str_has_prefix() except that it must be an exact
107 * match or the prefix followed by '/'.
109 * for example 'a' is a prefix of 'a' and 'a/b' but not 'ab'.
113 for (i = 0; prefix[i]; i++)
114 if (prefix[i] != group_name[i])
117 return group_name[i] == '\0' || group_name[i] == '/';
121 convert_path (GKeyfileSettingsBackend *kfsb,
126 gint key_len = strlen (key);
129 if (key_len < kfsb->prefix_len ||
130 memcmp (key, kfsb->prefix, kfsb->prefix_len) != 0)
133 key_len -= kfsb->prefix_len;
134 key += kfsb->prefix_len;
136 for (i = key_len; i >= 0; i--)
140 if (kfsb->root_group)
142 /* if a root_group was specified, make sure the user hasn't given
143 * a path that ghosts that group name
145 if (i == kfsb->root_group_len && memcmp (key, kfsb->root_group, i) == 0)
150 /* if no root_group was given, ensure that the user gave a path */
159 *group = g_memdup (key, i + 1);
163 *group = g_strdup (kfsb->root_group);
167 *basename = g_memdup (key + i + 1, key_len - i);
173 path_is_valid (GKeyfileSettingsBackend *kfsb,
176 return convert_path (kfsb, path, NULL, NULL);
180 get_from_keyfile (GKeyfileSettingsBackend *kfsb,
181 const GVariantType *type,
184 GVariant *return_value = NULL;
187 if (convert_path (kfsb, key, &group, &name))
193 str = g_key_file_get_value (kfsb->keyfile, group, name, NULL);
197 return_value = g_variant_parse (type, str, NULL, NULL, NULL);
209 set_to_keyfile (GKeyfileSettingsBackend *kfsb,
215 if (convert_path (kfsb, key, &group, &name))
219 gchar *str = g_variant_print (value, FALSE);
220 g_key_file_set_value (kfsb->keyfile, group, name, str);
221 g_variant_unref (g_variant_ref_sink (value));
231 groups = g_key_file_get_groups (kfsb->keyfile, NULL);
233 for (i = 0; groups[i]; i++)
234 if (group_name_matches (groups[i], group))
235 g_key_file_remove_group (kfsb->keyfile, groups[i], NULL);
240 g_key_file_remove_key (kfsb->keyfile, group, name, NULL);
253 g_keyfile_settings_backend_read (GSettingsBackend *backend,
255 const GVariantType *expected_type,
256 gboolean default_value)
258 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
263 return get_from_keyfile (kfsb, expected_type, key);
268 GKeyfileSettingsBackend *kfsb;
273 g_keyfile_settings_backend_write_one (gpointer key,
277 WriteManyData *data = user_data;
280 success = set_to_keyfile (data->kfsb, key, value);
287 g_keyfile_settings_backend_check_one (gpointer key,
291 WriteManyData *data = user_data;
293 return data->failed = !path_is_valid (data->kfsb, key);
297 g_keyfile_settings_backend_write_many (GSettingsBackend *backend,
301 WriteManyData data = { G_KEYFILE_SETTINGS_BACKEND (backend) };
303 if (!data.kfsb->writable)
306 g_tree_foreach (tree, g_keyfile_settings_backend_check_one, &data);
311 g_tree_foreach (tree, g_keyfile_settings_backend_write_one, &data);
312 g_keyfile_settings_backend_keyfile_write (data.kfsb);
314 g_settings_backend_changed_tree (backend, tree, origin_tag);
320 g_keyfile_settings_backend_write (GSettingsBackend *backend,
325 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
331 success = set_to_keyfile (kfsb, key, value);
335 g_settings_backend_changed (backend, key, origin_tag);
336 g_keyfile_settings_backend_keyfile_write (kfsb);
343 g_keyfile_settings_backend_reset_path (GSettingsBackend *backend,
347 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
349 if (set_to_keyfile (kfsb, path, NULL))
350 g_keyfile_settings_backend_keyfile_write (kfsb);
352 g_settings_backend_path_changed (backend, path, origin_tag);
356 g_keyfile_settings_backend_reset (GSettingsBackend *backend,
360 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
362 if (set_to_keyfile (kfsb, key, NULL))
363 g_keyfile_settings_backend_keyfile_write (kfsb);
365 g_settings_backend_changed (backend, key, origin_tag);
369 g_keyfile_settings_backend_get_writable (GSettingsBackend *backend,
372 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
374 return kfsb->writable && path_is_valid (kfsb, name);
378 g_keyfile_settings_backend_get_permission (GSettingsBackend *backend,
381 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
383 return g_object_ref (kfsb->permission);
387 keyfile_to_tree (GKeyfileSettingsBackend *kfsb,
395 groups = g_key_file_get_groups (keyfile, NULL);
396 for (i = 0; groups[i]; i++)
398 gboolean is_root_group;
402 is_root_group = g_strcmp0 (kfsb->root_group, groups[i]) == 0;
403 keys = g_key_file_get_keys (keyfile, groups[i], NULL, NULL);
405 for (j = 0; keys[j]; j++)
410 path = g_strdup_printf ("%s%s", kfsb->prefix, keys[j]);
412 path = g_strdup_printf ("%s%s/%s", kfsb->prefix, groups[i], keys[j]);
414 value = g_key_file_get_value (keyfile, groups[i], keys[j], NULL);
416 if (dup_check && g_strcmp0 (g_tree_lookup (tree, path), value) == 0)
418 g_tree_remove (tree, path);
423 g_tree_insert (tree, path, value);
432 g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
441 g_file_load_contents (kfsb->file, NULL, &contents, &length, NULL, NULL);
442 compute_checksum (digest, contents, length);
444 if (memcmp (kfsb->digest, digest, sizeof digest) != 0)
446 GKeyFile *keyfiles[2];
449 tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
452 keyfiles[0] = kfsb->keyfile;
453 keyfiles[1] = g_key_file_new ();
456 g_key_file_load_from_data (keyfiles[1], contents, length,
457 G_KEY_FILE_KEEP_COMMENTS |
458 G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
460 keyfile_to_tree (kfsb, tree, keyfiles[0], FALSE);
461 keyfile_to_tree (kfsb, tree, keyfiles[1], TRUE);
462 g_key_file_free (keyfiles[0]);
463 kfsb->keyfile = keyfiles[1];
465 if (g_tree_nnodes (tree) > 0)
466 g_settings_backend_changed_tree (&kfsb->parent_instance, tree, NULL);
470 memcpy (kfsb->digest, digest, sizeof digest);
477 g_keyfile_settings_backend_keyfile_writable (GKeyfileSettingsBackend *kfsb)
482 fileinfo = g_file_query_info (kfsb->dir, "access::*", 0, NULL, NULL);
487 g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE) &&
488 g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE);
489 g_object_unref (fileinfo);
494 if (writable != kfsb->writable)
496 kfsb->writable = writable;
497 g_settings_backend_path_writable_changed (&kfsb->parent_instance, "/");
502 g_keyfile_settings_backend_finalize (GObject *object)
504 GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
506 g_key_file_free (kfsb->keyfile);
507 g_object_unref (kfsb->permission);
509 g_file_monitor_cancel (kfsb->file_monitor);
510 g_object_unref (kfsb->file_monitor);
511 g_object_unref (kfsb->file);
513 g_file_monitor_cancel (kfsb->dir_monitor);
514 g_object_unref (kfsb->dir_monitor);
515 g_object_unref (kfsb->dir);
517 g_free (kfsb->root_group);
518 g_free (kfsb->prefix);
520 G_OBJECT_CLASS (g_keyfile_settings_backend_parent_class)
525 g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kfsb)
530 g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
532 GObjectClass *object_class = G_OBJECT_CLASS (class);
534 object_class->finalize = g_keyfile_settings_backend_finalize;
536 class->read = g_keyfile_settings_backend_read;
537 class->write = g_keyfile_settings_backend_write;
538 class->write_keys = g_keyfile_settings_backend_write_many;
539 class->reset = g_keyfile_settings_backend_reset;
540 class->reset_path = g_keyfile_settings_backend_reset_path;
541 class->get_writable = g_keyfile_settings_backend_get_writable;
542 class->get_permission = g_keyfile_settings_backend_get_permission;
543 /* No need to implement subscribed/unsubscribe: the only point would be to
544 * stop monitoring the file when there's no GSettings anymore, which is no
549 file_changed (GFileMonitor *monitor,
552 GFileMonitorEvent event_type,
555 GKeyfileSettingsBackend *kfsb = user_data;
557 g_keyfile_settings_backend_keyfile_reload (kfsb);
561 dir_changed (GFileMonitor *monitor,
564 GFileMonitorEvent event_type,
567 GKeyfileSettingsBackend *kfsb = user_data;
569 g_keyfile_settings_backend_keyfile_writable (kfsb);
573 * g_keyfile_settings_backend_new:
574 * @filename: the filename of the keyfile
575 * @root_path: the path under which all settings keys appear
576 * @root_group: (allow-none): the group name corresponding to
577 * @root_path, or %NULL
578 * Returns: a keyfile-backed #GSettingsBackend
580 * Creates a keyfile-backed #GSettingsBackend.
582 * The filename of the keyfile to use is given by @filename.
584 * All settings read to or written from the backend must fall under the
585 * path given in @root_path (which must start and end with a slash and
586 * not contain two consecutive slashes). @root_path may be "/".
588 * If @root_group is non-%NULL then it specifies the name of the keyfile
589 * group used for keys that are written directly below @root_path. For
590 * example, if @root_path is "/apps/example/" and @root_group is
591 * "toplevel", then settings the key "/apps/example/enabled" to a value
592 * of %TRUE will cause the following to appear in the keyfile:
599 * If @root_group is %NULL then it is not permitted to store keys
600 * directly below the @root_path.
602 * For keys not stored directly below @root_path (ie: in a sub-path),
603 * the name of the subpath (with the final slash stripped) is used as
604 * the name of the keyfile group. To continue the example, if
605 * "/apps/example/profiles/default/font-size" were set to
606 * 12 then the following would appear in the keyfile:
613 * The backend will refuse writes (and return writability as being
614 * %FALSE) for keys outside of @root_path and, in the event that
615 * @root_group is %NULL, also for keys directly under @root_path.
616 * Writes will also be refused if the backend detects that it has the
617 * inability to rewrite the keyfile (ie: the containing directory is not
620 * There is no checking done for your key namespace clashing with the
621 * syntax of the key file format. For example, if you have '[' or ']'
622 * characters in your path names or '=' in your key names you may be in
626 g_keyfile_settings_backend_new (const gchar *filename,
627 const gchar *root_path,
628 const gchar *root_group)
630 GKeyfileSettingsBackend *kfsb;
632 g_return_val_if_fail (filename != NULL, NULL);
633 g_return_val_if_fail (root_path != NULL, NULL);
634 g_return_val_if_fail (g_str_has_prefix (root_path, "/"), NULL);
635 g_return_val_if_fail (g_str_has_suffix (root_path, "/"), NULL);
636 g_return_val_if_fail (strstr (root_path, "//") == NULL, NULL);
638 kfsb = g_object_new (G_TYPE_KEYFILE_SETTINGS_BACKEND, NULL);
639 kfsb->keyfile = g_key_file_new ();
640 kfsb->permission = g_simple_permission_new (TRUE);
642 kfsb->file = g_file_new_for_path (filename);
643 kfsb->dir = g_file_get_parent (kfsb->file);
644 g_file_make_directory_with_parents (kfsb->dir, NULL, NULL);
646 kfsb->file_monitor = g_file_monitor_file (kfsb->file, 0, NULL, NULL);
647 kfsb->dir_monitor = g_file_monitor_file (kfsb->dir, 0, NULL, NULL);
649 kfsb->prefix_len = strlen (root_path);
650 kfsb->prefix = g_strdup (root_path);
654 kfsb->root_group_len = strlen (root_group);
655 kfsb->root_group = g_strdup (root_group);
658 compute_checksum (kfsb->digest, NULL, 0);
660 g_signal_connect (kfsb->file_monitor, "changed",
661 G_CALLBACK (file_changed), kfsb);
662 g_signal_connect (kfsb->dir_monitor, "changed",
663 G_CALLBACK (dir_changed), kfsb);
665 g_keyfile_settings_backend_keyfile_writable (kfsb);
666 g_keyfile_settings_backend_keyfile_reload (kfsb);
668 return G_SETTINGS_BACKEND (kfsb);
671 #define __G_KEYFILE_SETTINGS_BACKEND_C__
672 #include "gioaliasdef.c"