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