[glib/keyfile-backend-strings] keyfile settings: Accept unquoted strings



commit 3b4938b748f0c54512150e275db16a9e03bd8b75
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jan 21 22:55:45 2019 -0500

    keyfile settings: Accept unquoted strings
    
    It is hard for users to remember that strings have to be explicitly
    quoted in the keyfile. Be lenient and accept strings that lack those
    quotes.

 gio/gkeyfilesettingsbackend.c | 20 ++++++++++++++++++++
 gio/tests/gsettings.c         | 17 +++++++++++++++++
 2 files changed, 37 insertions(+)
---
diff --git a/gio/gkeyfilesettingsbackend.c b/gio/gkeyfilesettingsbackend.c
index 398cb053a..a0684b6b0 100644
--- a/gio/gkeyfilesettingsbackend.c
+++ b/gio/gkeyfilesettingsbackend.c
@@ -225,6 +225,26 @@ get_from_keyfile (GKeyfileSettingsBackend *kfsb,
       if (str)
         {
           return_value = g_variant_parse (type, str, NULL, NULL, NULL);
+          if (return_value == NULL &&
+              g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
+              str[0] != '\"')
+            {
+              GString *s = g_string_sized_new (strlen (str) + 2);
+              char *p = str;
+
+              g_string_append_c (s, '\"');
+              while (*p)
+                {
+                  gunichar ch = g_utf8_get_char (p);
+                  if (ch == (gunichar)'\"')
+                    g_string_append_c (s, '\\');
+                  g_string_append_unichar (s, ch);
+                  p = g_utf8_next_char (p);
+                }
+              g_string_append_c (s, '\"');
+              return_value = g_variant_parse (type, s->str, NULL, NULL, NULL);
+              g_string_free (s, TRUE);
+            }
           g_free (str);
         }
 
diff --git a/gio/tests/gsettings.c b/gio/tests/gsettings.c
index 58217e982..57df7b80e 100644
--- a/gio/tests/gsettings.c
+++ b/gio/tests/gsettings.c
@@ -1770,6 +1770,23 @@ test_keyfile (void)
   g_assert_cmpstr (str, ==, "howdy");
   g_free (str);
 
+  /* Now check setting a string without quotes */
+  called = FALSE;
+  g_signal_connect (settings, "changed::greeting", G_CALLBACK (key_changed_cb), &called);
+
+  g_key_file_set_string (keyfile, "tests", "greeting", "he\"lau");
+  g_free (data);
+  data = g_key_file_to_data (keyfile, &len, NULL);
+  g_file_set_contents ("keyfile/gsettings.store", data, len, &error);
+  g_assert_no_error (error);
+  while (!called)
+    g_main_context_iteration (NULL, FALSE);
+  g_signal_handlers_disconnect_by_func (settings, key_changed_cb, &called);
+
+  str = g_settings_get_string (settings, "greeting");
+  g_assert_cmpstr (str, ==, "he\"lau");
+  g_free (str);
+
   g_settings_set (settings, "farewell", "s", "cheerio");
   
   called = FALSE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]