Imported Upstream version 2.59.2
[platform/upstream/glib.git] / gio / gkeyfilesettingsbackend.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  * Copyright © 2010 Novell, Inc.
4  *
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.1 of the License, or (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Authors: Vincent Untz <vuntz@gnome.org>
19  *          Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <glibintl.h>
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "gfile.h"
31 #include "gfileinfo.h"
32 #include "gfileenumerator.h"
33 #include "gfilemonitor.h"
34 #include "gsimplepermission.h"
35 #include "gsettingsbackendinternal.h"
36 #include "giomodule-priv.h"
37 #include "gportalsupport.h"
38
39
40 #define G_TYPE_KEYFILE_SETTINGS_BACKEND      (g_keyfile_settings_backend_get_type ())
41 #define G_KEYFILE_SETTINGS_BACKEND(inst)     (G_TYPE_CHECK_INSTANCE_CAST ((inst),      \
42                                               G_TYPE_KEYFILE_SETTINGS_BACKEND,         \
43                                               GKeyfileSettingsBackend))
44 #define G_IS_KEYFILE_SETTINGS_BACKEND(inst)  (G_TYPE_CHECK_INSTANCE_TYPE ((inst),      \
45                                               G_TYPE_KEYFILE_SETTINGS_BACKEND))
46
47
48 typedef GSettingsBackendClass GKeyfileSettingsBackendClass;
49
50 typedef enum {
51   PROP_FILENAME = 1,
52   PROP_ROOT_PATH,
53   PROP_ROOT_GROUP,
54   PROP_DEFAULTS_DIR
55 } GKeyfileSettingsBackendProperty;
56
57 typedef struct
58 {
59   GSettingsBackend   parent_instance;
60
61   GKeyFile          *keyfile;
62   GPermission       *permission;
63   gboolean           writable;
64   char              *defaults_dir;
65   GKeyFile          *system_keyfile;
66   GHashTable        *system_locks; /* Used as a set, owning the strings it contains */
67
68   gchar             *prefix;
69   gint               prefix_len;
70   gchar             *root_group;
71   gint               root_group_len;
72
73   GFile             *file;
74   GFileMonitor      *file_monitor;
75   guint8             digest[32];
76   GFile             *dir;
77   GFileMonitor      *dir_monitor;
78 } GKeyfileSettingsBackend;
79
80 #ifdef G_OS_WIN32
81 #define EXTENSION_PRIORITY 10
82 #else
83 #define EXTENSION_PRIORITY (glib_should_use_portal () ? 110 : 10)
84 #endif
85
86 G_DEFINE_TYPE_WITH_CODE (GKeyfileSettingsBackend,
87                          g_keyfile_settings_backend,
88                          G_TYPE_SETTINGS_BACKEND,
89                          _g_io_modules_ensure_extension_points_registered ();
90                          g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
91                                                          g_define_type_id, "keyfile", EXTENSION_PRIORITY))
92
93 static void
94 compute_checksum (guint8        *digest,
95                   gconstpointer  contents,
96                   gsize          length)
97 {
98   GChecksum *checksum;
99   gsize len = 32;
100
101   checksum = g_checksum_new (G_CHECKSUM_SHA256);
102   g_checksum_update (checksum, contents, length);
103   g_checksum_get_digest (checksum, digest, &len);
104   g_checksum_free (checksum);
105   g_assert (len == 32);
106 }
107
108 static void
109 g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb)
110 {
111   gchar *contents;
112   gsize length;
113
114   contents = g_key_file_to_data (kfsb->keyfile, &length, NULL);
115   g_file_replace_contents (kfsb->file, contents, length, NULL, FALSE,
116                            G_FILE_CREATE_REPLACE_DESTINATION,
117                            NULL, NULL, NULL);
118
119   compute_checksum (kfsb->digest, contents, length);
120   g_free (contents);
121 }
122
123 static gboolean
124 group_name_matches (const gchar *group_name,
125                     const gchar *prefix)
126 {
127   /* sort of like g_str_has_prefix() except that it must be an exact
128    * match or the prefix followed by '/'.
129    *
130    * for example 'a' is a prefix of 'a' and 'a/b' but not 'ab'.
131    */
132   gint i;
133
134   for (i = 0; prefix[i]; i++)
135     if (prefix[i] != group_name[i])
136       return FALSE;
137
138   return group_name[i] == '\0' || group_name[i] == '/';
139 }
140
141 static gboolean
142 convert_path (GKeyfileSettingsBackend  *kfsb,
143               const gchar              *key,
144               gchar                   **group,
145               gchar                   **basename)
146 {
147   gint key_len = strlen (key);
148   gint i;
149
150   if (key_len < kfsb->prefix_len ||
151       memcmp (key, kfsb->prefix, kfsb->prefix_len) != 0)
152     return FALSE;
153
154   key_len -= kfsb->prefix_len;
155   key += kfsb->prefix_len;
156
157   for (i = key_len; i >= 0; i--)
158     if (key[i] == '/')
159       break;
160
161   if (kfsb->root_group)
162     {
163       /* if a root_group was specified, make sure the user hasn't given
164        * a path that ghosts that group name
165        */
166       if (i == kfsb->root_group_len && memcmp (key, kfsb->root_group, i) == 0)
167         return FALSE;
168     }
169   else
170     {
171       /* if no root_group was given, ensure that the user gave a path */
172       if (i == -1)
173         return FALSE;
174     }
175
176   if (group)
177     {
178       if (i >= 0)
179         {
180           *group = g_memdup (key, i + 1);
181           (*group)[i] = '\0';
182         }
183       else
184         *group = g_strdup (kfsb->root_group);
185     }
186
187   if (basename)
188     *basename = g_memdup (key + i + 1, key_len - i);
189
190   return TRUE;
191 }
192
193 static gboolean
194 path_is_valid (GKeyfileSettingsBackend *kfsb,
195                const gchar             *path)
196 {
197   return convert_path (kfsb, path, NULL, NULL);
198 }
199
200 static GVariant *
201 get_from_keyfile (GKeyfileSettingsBackend *kfsb,
202                   const GVariantType      *type,
203                   const gchar             *key)
204 {
205   GVariant *return_value = NULL;
206   gchar *group, *name;
207
208   if (convert_path (kfsb, key, &group, &name))
209     {
210       gchar *str;
211       gchar *sysstr;
212
213       g_assert (*name);
214
215       sysstr = g_key_file_get_value (kfsb->system_keyfile, group, name, NULL);
216       str = g_key_file_get_value (kfsb->keyfile, group, name, NULL);
217       if (sysstr &&
218           (g_hash_table_contains (kfsb->system_locks, key) ||
219            str == NULL))
220         {
221           g_free (str);
222           str = g_steal_pointer (&sysstr);
223         }
224
225       if (str)
226         {
227           return_value = g_variant_parse (type, str, NULL, NULL, NULL);
228
229           /* As a special case, support values of type %G_VARIANT_TYPE_STRING
230            * not being quoted, since users keep forgetting to do it and then
231            * getting confused. */
232           if (return_value == NULL &&
233               g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
234               str[0] != '\"')
235             {
236               GString *s = g_string_sized_new (strlen (str) + 2);
237               char *p = str;
238
239               g_string_append_c (s, '\"');
240               while (*p)
241                 {
242                   if (*p == '\"')
243                     g_string_append_c (s, '\\');
244                   g_string_append_c (s, *p);
245                   p++;
246                 }
247               g_string_append_c (s, '\"');
248               return_value = g_variant_parse (type, s->str, NULL, NULL, NULL);
249               g_string_free (s, TRUE);
250             }
251           g_free (str);
252         }
253
254       g_free (sysstr);
255
256       g_free (group);
257       g_free (name);
258     }
259
260   return return_value;
261 }
262
263 static gboolean
264 set_to_keyfile (GKeyfileSettingsBackend *kfsb,
265                 const gchar             *key,
266                 GVariant                *value)
267 {
268   gchar *group, *name;
269
270   if (g_hash_table_contains (kfsb->system_locks, key))
271     return FALSE;
272
273   if (convert_path (kfsb, key, &group, &name))
274     {
275       if (value)
276         {
277           gchar *str = g_variant_print (value, FALSE);
278           g_key_file_set_value (kfsb->keyfile, group, name, str);
279           g_variant_unref (g_variant_ref_sink (value));
280           g_free (str);
281         }
282       else
283         {
284           if (*name == '\0')
285             {
286               gchar **groups;
287               gint i;
288
289               groups = g_key_file_get_groups (kfsb->keyfile, NULL);
290
291               for (i = 0; groups[i]; i++)
292                 if (group_name_matches (groups[i], group))
293                   g_key_file_remove_group (kfsb->keyfile, groups[i], NULL);
294
295               g_strfreev (groups);
296             }
297           else
298             g_key_file_remove_key (kfsb->keyfile, group, name, NULL);
299         }
300
301       g_free (group);
302       g_free (name);
303
304       return TRUE;
305     }
306
307   return FALSE;
308 }
309
310 static GVariant *
311 g_keyfile_settings_backend_read (GSettingsBackend   *backend,
312                                  const gchar        *key,
313                                  const GVariantType *expected_type,
314                                  gboolean            default_value)
315 {
316   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
317
318   if (default_value)
319     return NULL;
320
321   return get_from_keyfile (kfsb, expected_type, key);
322 }
323
324 typedef struct
325 {
326   GKeyfileSettingsBackend *kfsb;
327   gboolean failed;
328 } WriteManyData;
329
330 static gboolean
331 g_keyfile_settings_backend_write_one (gpointer key,
332                                       gpointer value,
333                                       gpointer user_data)
334 {
335   WriteManyData *data = user_data;
336   gboolean success;
337
338   success = set_to_keyfile (data->kfsb, key, value);
339   g_assert (success);
340
341   return FALSE;
342 }
343
344 static gboolean
345 g_keyfile_settings_backend_check_one (gpointer key,
346                                       gpointer value,
347                                       gpointer user_data)
348 {
349   WriteManyData *data = user_data;
350
351   return data->failed = g_hash_table_contains (data->kfsb->system_locks, key) ||
352                         !path_is_valid (data->kfsb, key);
353 }
354
355 static gboolean
356 g_keyfile_settings_backend_write_tree (GSettingsBackend *backend,
357                                        GTree            *tree,
358                                        gpointer          origin_tag)
359 {
360   WriteManyData data = { G_KEYFILE_SETTINGS_BACKEND (backend) };
361
362   if (!data.kfsb->writable)
363     return FALSE;
364
365   g_tree_foreach (tree, g_keyfile_settings_backend_check_one, &data);
366
367   if (data.failed)
368     return FALSE;
369
370   g_tree_foreach (tree, g_keyfile_settings_backend_write_one, &data);
371   g_keyfile_settings_backend_keyfile_write (data.kfsb);
372
373   g_settings_backend_changed_tree (backend, tree, origin_tag);
374
375   return TRUE;
376 }
377
378 static gboolean
379 g_keyfile_settings_backend_write (GSettingsBackend *backend,
380                                   const gchar      *key,
381                                   GVariant         *value,
382                                   gpointer          origin_tag)
383 {
384   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
385   gboolean success;
386
387   if (!kfsb->writable)
388     return FALSE;
389
390   success = set_to_keyfile (kfsb, key, value);
391
392   if (success)
393     {
394       g_settings_backend_changed (backend, key, origin_tag);
395       g_keyfile_settings_backend_keyfile_write (kfsb);
396     }
397
398   return success;
399 }
400
401 static void
402 g_keyfile_settings_backend_reset (GSettingsBackend *backend,
403                                   const gchar      *key,
404                                   gpointer          origin_tag)
405 {
406   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
407
408   if (set_to_keyfile (kfsb, key, NULL))
409     g_keyfile_settings_backend_keyfile_write (kfsb);
410
411   g_settings_backend_changed (backend, key, origin_tag);
412 }
413
414 static gboolean
415 g_keyfile_settings_backend_get_writable (GSettingsBackend *backend,
416                                          const gchar      *name)
417 {
418   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
419
420   return kfsb->writable &&
421          !g_hash_table_contains (kfsb->system_locks, name) &&
422          path_is_valid (kfsb, name);
423 }
424
425 static GPermission *
426 g_keyfile_settings_backend_get_permission (GSettingsBackend *backend,
427                                            const gchar      *path)
428 {
429   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
430
431   return g_object_ref (kfsb->permission);
432 }
433
434 static void
435 keyfile_to_tree (GKeyfileSettingsBackend *kfsb,
436                  GTree                   *tree,
437                  GKeyFile                *keyfile,
438                  gboolean                 dup_check)
439 {
440   gchar **groups;
441   gint i;
442
443   groups = g_key_file_get_groups (keyfile, NULL);
444   for (i = 0; groups[i]; i++)
445     {
446       gboolean is_root_group;
447       gchar **keys;
448       gint j;
449
450       is_root_group = g_strcmp0 (kfsb->root_group, groups[i]) == 0;
451
452       /* reject group names that will form invalid key names */
453       if (!is_root_group &&
454           (g_str_has_prefix (groups[i], "/") ||
455            g_str_has_suffix (groups[i], "/") || strstr (groups[i], "//")))
456         continue;
457
458       keys = g_key_file_get_keys (keyfile, groups[i], NULL, NULL);
459       g_assert (keys != NULL);
460
461       for (j = 0; keys[j]; j++)
462         {
463           gchar *path, *value;
464
465           /* reject key names with slashes in them */
466           if (strchr (keys[j], '/'))
467             continue;
468
469           if (is_root_group)
470             path = g_strdup_printf ("%s%s", kfsb->prefix, keys[j]);
471           else
472             path = g_strdup_printf ("%s%s/%s", kfsb->prefix, groups[i], keys[j]);
473
474           value = g_key_file_get_value (keyfile, groups[i], keys[j], NULL);
475
476           if (dup_check && g_strcmp0 (g_tree_lookup (tree, path), value) == 0)
477             {
478               g_tree_remove (tree, path);
479               g_free (value);
480               g_free (path);
481             }
482           else
483             g_tree_insert (tree, path, value);
484         }
485
486       g_strfreev (keys);
487     }
488   g_strfreev (groups);
489 }
490
491 static void
492 g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
493 {
494   guint8 digest[32];
495   gchar *contents;
496   gsize length;
497
498   contents = NULL;
499   length = 0;
500
501   g_file_load_contents (kfsb->file, NULL, &contents, &length, NULL, NULL);
502   compute_checksum (digest, contents, length);
503
504   if (memcmp (kfsb->digest, digest, sizeof digest) != 0)
505     {
506       GKeyFile *keyfiles[2];
507       GTree *tree;
508
509       tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
510                               g_free, g_free);
511
512       keyfiles[0] = kfsb->keyfile;
513       keyfiles[1] = g_key_file_new ();
514
515       if (length > 0)
516         g_key_file_load_from_data (keyfiles[1], contents, length,
517                                    G_KEY_FILE_KEEP_COMMENTS |
518                                    G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
519
520       keyfile_to_tree (kfsb, tree, keyfiles[0], FALSE);
521       keyfile_to_tree (kfsb, tree, keyfiles[1], TRUE);
522       g_key_file_free (keyfiles[0]);
523       kfsb->keyfile = keyfiles[1];
524
525       if (g_tree_nnodes (tree) > 0)
526         g_settings_backend_changed_tree (&kfsb->parent_instance, tree, NULL);
527
528       g_tree_unref (tree);
529
530       memcpy (kfsb->digest, digest, sizeof digest);
531     }
532
533   g_free (contents);
534 }
535
536 static void
537 g_keyfile_settings_backend_keyfile_writable (GKeyfileSettingsBackend *kfsb)
538 {
539   GFileInfo *fileinfo;
540   gboolean writable;
541
542   fileinfo = g_file_query_info (kfsb->dir, "access::*", 0, NULL, NULL);
543
544   if (fileinfo)
545     {
546       writable =
547         g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE) &&
548         g_file_info_get_attribute_boolean (fileinfo, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE);
549       g_object_unref (fileinfo);
550     }
551   else
552     writable = FALSE;
553
554   if (writable != kfsb->writable)
555     {
556       kfsb->writable = writable;
557       g_settings_backend_path_writable_changed (&kfsb->parent_instance, "/");
558     }
559 }
560
561 static void
562 g_keyfile_settings_backend_finalize (GObject *object)
563 {
564   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
565
566   g_key_file_free (kfsb->keyfile);
567   g_object_unref (kfsb->permission);
568   g_key_file_unref (kfsb->system_keyfile);
569   g_hash_table_unref (kfsb->system_locks);
570   g_free (kfsb->defaults_dir);
571
572   g_file_monitor_cancel (kfsb->file_monitor);
573   g_object_unref (kfsb->file_monitor);
574   g_object_unref (kfsb->file);
575
576   g_file_monitor_cancel (kfsb->dir_monitor);
577   g_object_unref (kfsb->dir_monitor);
578   g_object_unref (kfsb->dir);
579
580   g_free (kfsb->root_group);
581   g_free (kfsb->prefix);
582
583   G_OBJECT_CLASS (g_keyfile_settings_backend_parent_class)
584     ->finalize (object);
585 }
586
587 static void
588 g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kfsb)
589 {
590 }
591
592 static void
593 file_changed (GFileMonitor      *monitor,
594               GFile             *file,
595               GFile             *other_file,
596               GFileMonitorEvent  event_type,
597               gpointer           user_data)
598 {
599   GKeyfileSettingsBackend *kfsb = user_data;
600
601   /* Ignore file deletions, let the GKeyFile content remain in tact. */
602   if (event_type != G_FILE_MONITOR_EVENT_DELETED)
603     g_keyfile_settings_backend_keyfile_reload (kfsb);
604 }
605
606 static void
607 dir_changed (GFileMonitor       *monitor,
608               GFile             *file,
609               GFile             *other_file,
610               GFileMonitorEvent  event_type,
611               gpointer           user_data)
612 {
613   GKeyfileSettingsBackend *kfsb = user_data;
614
615   g_keyfile_settings_backend_keyfile_writable (kfsb);
616 }
617
618 static void
619 load_system_settings (GKeyfileSettingsBackend *kfsb)
620 {
621   GError *error = NULL;
622   const char *dir = "/etc/glib-2.0/settings";
623   char *path;
624   char *contents;
625
626   kfsb->system_keyfile = g_key_file_new ();
627   kfsb->system_locks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
628
629   if (kfsb->defaults_dir)
630     dir = kfsb->defaults_dir;
631
632   path = g_build_filename (dir, "defaults", NULL);
633
634   /* The defaults are in the same keyfile format that we use for the settings.
635    * It can be produced from a dconf database using: dconf dump
636    */
637   if (!g_key_file_load_from_file (kfsb->system_keyfile, path, G_KEY_FILE_NONE, &error))
638     {
639       if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
640         g_warning ("Failed to read %s: %s", path, error->message);
641       g_clear_error (&error);
642     }
643   else
644     g_debug ("Loading default settings from %s", path);
645
646   g_free (path);
647
648   path = g_build_filename (dir, "locks", NULL);
649
650   /* The locks file is a text file containing a list paths to lock, one per line.
651    * It can be produced from a dconf database using: dconf list-locks
652    */
653   if (!g_file_get_contents (path, &contents, NULL, &error))
654     {
655       if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
656         g_warning ("Failed to read %s: %s", path, error->message);
657       g_clear_error (&error);
658     }
659   else
660     {
661       char **lines;
662       gsize i;
663
664       g_debug ("Loading locks from %s", path);
665
666       lines = g_strsplit (contents, "\n", 0);
667       for (i = 0; lines[i]; i++)
668         {
669           char *line = lines[i];
670           if (line[0] == '#' || line[0] == '\0')
671             {
672               g_free (line);
673               continue;
674             }
675
676           g_debug ("Locking key %s", line);
677           g_hash_table_add (kfsb->system_locks, g_steal_pointer (&line));
678         }
679
680       g_free (lines);
681     }
682   g_free (contents);
683
684   g_free (path);
685 }
686
687 static void
688 g_keyfile_settings_backend_constructed (GObject *object)
689 {
690   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
691
692   if (kfsb->file == NULL)
693     {
694       char *filename = g_build_filename (g_get_user_config_dir (),
695                                          "glib-2.0", "settings", "keyfile",
696                                          NULL);
697       kfsb->file = g_file_new_for_path (filename);
698       g_free (filename);
699     }
700
701   if (kfsb->prefix == NULL)
702     {
703       kfsb->prefix = g_strdup ("/");
704       kfsb->prefix_len = 1;
705     }
706   
707   kfsb->keyfile = g_key_file_new ();
708   kfsb->permission = g_simple_permission_new (TRUE);
709
710   kfsb->dir = g_file_get_parent (kfsb->file);
711   g_file_make_directory_with_parents (kfsb->dir, NULL, NULL);
712
713   kfsb->file_monitor = g_file_monitor (kfsb->file, G_FILE_MONITOR_NONE, NULL, NULL);
714   kfsb->dir_monitor = g_file_monitor (kfsb->dir, G_FILE_MONITOR_NONE, NULL, NULL);
715
716   compute_checksum (kfsb->digest, NULL, 0);
717
718   g_signal_connect (kfsb->file_monitor, "changed",
719                     G_CALLBACK (file_changed), kfsb);
720   g_signal_connect (kfsb->dir_monitor, "changed",
721                     G_CALLBACK (dir_changed), kfsb);
722
723   g_keyfile_settings_backend_keyfile_writable (kfsb);
724   g_keyfile_settings_backend_keyfile_reload (kfsb);
725
726   load_system_settings (kfsb);
727 }
728
729 static void
730 g_keyfile_settings_backend_set_property (GObject      *object,
731                                          guint         prop_id,
732                                          const GValue *value,
733                                          GParamSpec   *pspec)
734 {
735   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
736
737   switch ((GKeyfileSettingsBackendProperty)prop_id)
738     {
739     case PROP_FILENAME:
740       /* Construct only. */
741       g_assert (kfsb->file == NULL);
742       kfsb->file = g_file_new_for_path (g_value_get_string (value));
743       break;
744
745     case PROP_ROOT_PATH:
746       /* Construct only. */
747       g_assert (kfsb->prefix == NULL);
748       kfsb->prefix = g_value_dup_string (value);
749       if (kfsb->prefix)
750         kfsb->prefix_len = strlen (kfsb->prefix);
751       break;
752
753     case PROP_ROOT_GROUP:
754       /* Construct only. */
755       g_assert (kfsb->root_group == NULL);
756       kfsb->root_group = g_value_dup_string (value);
757       if (kfsb->root_group)
758         kfsb->root_group_len = strlen (kfsb->root_group);
759       break;
760
761     case PROP_DEFAULTS_DIR:
762       /* Construct only. */
763       g_assert (kfsb->defaults_dir == NULL);
764       kfsb->defaults_dir = g_value_dup_string (value);
765       break;
766
767     default:
768       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
769       break;
770     }
771 }
772
773 static void
774 g_keyfile_settings_backend_get_property (GObject    *object,
775                                          guint       prop_id,
776                                          GValue     *value,
777                                          GParamSpec *pspec)
778 {
779   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
780
781   switch ((GKeyfileSettingsBackendProperty)prop_id)
782     {
783     case PROP_FILENAME:
784       g_value_set_string (value, g_file_peek_path (kfsb->file));
785       break;
786
787     case PROP_ROOT_PATH:
788       g_value_set_string (value, kfsb->prefix);
789       break;
790
791     case PROP_ROOT_GROUP:
792       g_value_set_string (value, kfsb->root_group);
793       break;
794
795     case PROP_DEFAULTS_DIR:
796       g_value_set_string (value, kfsb->defaults_dir);
797       break;
798
799     default:
800       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
801       break;
802     }
803 }
804
805 static void
806 g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
807 {
808   GObjectClass *object_class = G_OBJECT_CLASS (class);
809
810   object_class->finalize = g_keyfile_settings_backend_finalize;
811   object_class->constructed = g_keyfile_settings_backend_constructed;
812   object_class->get_property = g_keyfile_settings_backend_get_property;
813   object_class->set_property = g_keyfile_settings_backend_set_property;
814
815   class->read = g_keyfile_settings_backend_read;
816   class->write = g_keyfile_settings_backend_write;
817   class->write_tree = g_keyfile_settings_backend_write_tree;
818   class->reset = g_keyfile_settings_backend_reset;
819   class->get_writable = g_keyfile_settings_backend_get_writable;
820   class->get_permission = g_keyfile_settings_backend_get_permission;
821   /* No need to implement subscribed/unsubscribe: the only point would be to
822    * stop monitoring the file when there's no GSettings anymore, which is no
823    * big win.
824    */
825
826   /**
827    * GKeyfileSettingsBackend:filename:
828    *
829    * The location where the settings are stored on disk.
830    *
831    * Defaults to `$XDG_CONFIG_HOME/glib-2.0/settings/keyfile`.
832    */
833   g_object_class_install_property (object_class,
834                                    PROP_FILENAME,
835                                    g_param_spec_string ("filename",
836                                                         P_("Filename"),
837                                                         P_("The filename"),
838                                                         NULL,
839                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
840                                                         G_PARAM_STATIC_STRINGS));
841
842   /**
843    * GKeyfileSettingsBackend:root-path:
844    *
845    * All settings read to or written from the backend must fall under the
846    * path given in @root_path (which must start and end with a slash and
847    * not contain two consecutive slashes).  @root_path may be "/".
848    * 
849    * Defaults to "/".
850    */
851   g_object_class_install_property (object_class,
852                                    PROP_ROOT_PATH,
853                                    g_param_spec_string ("root-path",
854                                                         P_("Root path"),
855                                                         P_("The root path"),
856                                                         NULL,
857                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
858                                                         G_PARAM_STATIC_STRINGS));
859
860   /**
861    * GKeyfileSettingsBackend:root-group:
862    *
863    * If @root_group is non-%NULL then it specifies the name of the keyfile
864    * group used for keys that are written directly below the root path.
865    *
866    * Defaults to NULL.
867    */
868   g_object_class_install_property (object_class,
869                                    PROP_ROOT_GROUP,
870                                    g_param_spec_string ("root-group",
871                                                         P_("Root group"),
872                                                         P_("The root group"),
873                                                         NULL,
874                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
875                                                         G_PARAM_STATIC_STRINGS));
876
877   /**
878    * GKeyfileSettingsBackend:default-dir:
879    *
880    * The directory where the system defaults and locks are located.
881    *
882    * Defaults to `/etc/glib-2.0/settings`.
883    */
884   g_object_class_install_property (object_class,
885                                    PROP_DEFAULTS_DIR,
886                                    g_param_spec_string ("defaults-dir",
887                                                         P_("Default dir"),
888                                                         P_("Defaults dir"),
889                                                         NULL,
890                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
891                                                         G_PARAM_STATIC_STRINGS));
892 }
893
894 /**
895  * g_keyfile_settings_backend_new:
896  * @filename: the filename of the keyfile
897  * @root_path: the path under which all settings keys appear
898  * @root_group: (nullable): the group name corresponding to
899  *              @root_path, or %NULL
900  *
901  * Creates a keyfile-backed #GSettingsBackend.
902  *
903  * The filename of the keyfile to use is given by @filename.
904  *
905  * All settings read to or written from the backend must fall under the
906  * path given in @root_path (which must start and end with a slash and
907  * not contain two consecutive slashes).  @root_path may be "/".
908  *
909  * If @root_group is non-%NULL then it specifies the name of the keyfile
910  * group used for keys that are written directly below @root_path.  For
911  * example, if @root_path is "/apps/example/" and @root_group is
912  * "toplevel", then settings the key "/apps/example/enabled" to a value
913  * of %TRUE will cause the following to appear in the keyfile:
914  *
915  * |[
916  *   [toplevel]
917  *   enabled=true
918  * ]|
919  *
920  * If @root_group is %NULL then it is not permitted to store keys
921  * directly below the @root_path.
922  *
923  * For keys not stored directly below @root_path (ie: in a sub-path),
924  * the name of the subpath (with the final slash stripped) is used as
925  * the name of the keyfile group.  To continue the example, if
926  * "/apps/example/profiles/default/font-size" were set to
927  * 12 then the following would appear in the keyfile:
928  *
929  * |[
930  *   [profiles/default]
931  *   font-size=12
932  * ]|
933  *
934  * The backend will refuse writes (and return writability as being
935  * %FALSE) for keys outside of @root_path and, in the event that
936  * @root_group is %NULL, also for keys directly under @root_path.
937  * Writes will also be refused if the backend detects that it has the
938  * inability to rewrite the keyfile (ie: the containing directory is not
939  * writable).
940  *
941  * There is no checking done for your key namespace clashing with the
942  * syntax of the key file format.  For example, if you have '[' or ']'
943  * characters in your path names or '=' in your key names you may be in
944  * trouble.
945  *
946  * The backend reads default values from a keyfile called `defaults` in
947  * the directory specified by the #GKeyfileSettingsBackend:defaults-dir property,
948  * and a list of locked keys from a text file with the name `locks` in
949  * the same location.
950  *
951  * Returns: (transfer full): a keyfile-backed #GSettingsBackend
952  **/
953 GSettingsBackend *
954 g_keyfile_settings_backend_new (const gchar *filename,
955                                 const gchar *root_path,
956                                 const gchar *root_group)
957 {
958   g_return_val_if_fail (filename != NULL, NULL);
959   g_return_val_if_fail (root_path != NULL, NULL);
960   g_return_val_if_fail (g_str_has_prefix (root_path, "/"), NULL);
961   g_return_val_if_fail (g_str_has_suffix (root_path, "/"), NULL);
962   g_return_val_if_fail (strstr (root_path, "//") == NULL, NULL);
963
964   return G_SETTINGS_BACKEND (g_object_new (G_TYPE_KEYFILE_SETTINGS_BACKEND,
965                                            "filename", filename,
966                                            "root-path", root_path,
967                                            "root-group", root_group,
968                                            NULL));
969 }