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