2d3b65
From 5634fd61f17d28dfc05cd47cfbd2bd2f21e6d2b1 Mon Sep 17 00:00:00 2001
2d3b65
From: Allison Lortie <desrt@desrt.ca>
2d3b65
Date: Wed, 2 Aug 2017 11:06:03 +0100
2d3b65
Subject: [PATCH 1/4] gsettings: cleanup default value lookup
2d3b65
2d3b65
There are a couple of different ways (and soon one more) to access the
2d3b65
default value of a key. Clean up the various places that access this to
2d3b65
avoid duplication.
2d3b65
2d3b65
https://bugzilla.gnome.org/show_bug.cgi?id=746592
2d3b65
---
2d3b65
 gio/gsettings.c | 20 ++++----------------
2d3b65
 1 file changed, 4 insertions(+), 16 deletions(-)
2d3b65
2d3b65
diff --git a/gio/gsettings.c b/gio/gsettings.c
2d3b65
index 10d394d69..5e5816d57 100644
2d3b65
--- a/gio/gsettings.c
2d3b65
+++ b/gio/gsettings.c
2d3b65
@@ -1204,10 +1204,7 @@ g_settings_get_value (GSettings   *settings,
2d3b65
   value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
2d3b65
 
2d3b65
   if (value == NULL)
2d3b65
-    value = g_settings_schema_key_get_translated_default (&skey);
2d3b65
-
2d3b65
-  if (value == NULL)
2d3b65
-    value = g_variant_ref (skey.default_value);
2d3b65
+    value = g_settings_schema_key_get_default_value (&skey);
2d3b65
 
2d3b65
   g_settings_schema_key_clear (&skey);
2d3b65
 
2d3b65
@@ -1304,10 +1301,7 @@ g_settings_get_default_value (GSettings   *settings,
2d3b65
   value = g_settings_read_from_backend (settings, &skey, FALSE, TRUE);
2d3b65
 
2d3b65
   if (value == NULL)
2d3b65
-    value = g_settings_schema_key_get_translated_default (&skey);
2d3b65
-
2d3b65
-  if (value == NULL)
2d3b65
-    value = g_variant_ref (skey.default_value);
2d3b65
+    value = g_settings_schema_key_get_default_value (&skey);
2d3b65
 
2d3b65
   g_settings_schema_key_clear (&skey);
2d3b65
 
2d3b65
@@ -1360,10 +1354,7 @@ g_settings_get_enum (GSettings   *settings,
2d3b65
   value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
2d3b65
 
2d3b65
   if (value == NULL)
2d3b65
-    value = g_settings_schema_key_get_translated_default (&skey);
2d3b65
-
2d3b65
-  if (value == NULL)
2d3b65
-    value = g_variant_ref (skey.default_value);
2d3b65
+    value = g_settings_schema_key_get_default_value (&skey);
2d3b65
 
2d3b65
   result = g_settings_schema_key_to_enum (&skey, value);
2d3b65
   g_settings_schema_key_clear (&skey);
2d3b65
@@ -1473,10 +1464,7 @@ g_settings_get_flags (GSettings   *settings,
2d3b65
   value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
2d3b65
 
2d3b65
   if (value == NULL)
2d3b65
-    value = g_settings_schema_key_get_translated_default (&skey);
2d3b65
-
2d3b65
-  if (value == NULL)
2d3b65
-    value = g_variant_ref (skey.default_value);
2d3b65
+    value = g_settings_schema_key_get_default_value (&skey);
2d3b65
 
2d3b65
   result = g_settings_schema_key_to_flags (&skey, value);
2d3b65
   g_settings_schema_key_clear (&skey);
2d3b65
-- 
2d3b65
2.21.0
2d3b65
2d3b65
2d3b65
From 89c6e8f4a0bcda4b58dbaea713e62be01cfc2087 Mon Sep 17 00:00:00 2001
2d3b65
From: Allison Lortie <desrt@desrt.ca>
2d3b65
Date: Wed, 2 Aug 2017 11:08:17 +0100
2d3b65
Subject: [PATCH 2/4] gsettingsschema: Allow per-desktop overrides
2d3b65
MIME-Version: 1.0
2d3b65
Content-Type: text/plain; charset=UTF-8
2d3b65
Content-Transfer-Encoding: 8bit
2d3b65
2d3b65
Recognise a new 'd' option in schema keys which gives a dictionary of
2d3b65
per-desktop default values. This dictionary is searched for the items
2d3b65
found in XDG_CURRENT_DESKTOP, in the order. If nothing matches (or if
2d3b65
the option is missing) then the default value is used as before.
2d3b65
2d3b65
This feature was requested by Alberts Muktupāvels and this patch is
2d3b65
based on an approach devised by them.
2d3b65
2d3b65
https://bugzilla.gnome.org/show_bug.cgi?id=746592
2d3b65
---
2d3b65
 gio/gsettings.c                | 21 +++++++++++++++++
2d3b65
 gio/gsettingsschema-internal.h |  2 ++
2d3b65
 gio/gsettingsschema.c          | 41 ++++++++++++++++++++++++++++++++++
2d3b65
 3 files changed, 64 insertions(+)
2d3b65
2d3b65
diff --git a/gio/gsettings.c b/gio/gsettings.c
2d3b65
index 5e5816d57..f1130c095 100644
2d3b65
--- a/gio/gsettings.c
2d3b65
+++ b/gio/gsettings.c
2d3b65
@@ -1739,6 +1739,13 @@ g_settings_get_mapped (GSettings           *settings,
2d3b65
       if (okay) goto okay;
2d3b65
     }
2d3b65
 
2d3b65
+  if ((value = g_settings_schema_key_get_per_desktop_default (&skey)))
2d3b65
+    {
2d3b65
+      okay = mapping (value, &result, user_data);
2d3b65
+      g_variant_unref (value);
2d3b65
+      if (okay) goto okay;
2d3b65
+    }
2d3b65
+
2d3b65
   if (mapping (skey.default_value, &result, user_data))
2d3b65
     goto okay;
2d3b65
 
2d3b65
@@ -2647,6 +2654,20 @@ g_settings_binding_key_changed (GSettings   *settings,
2d3b65
         }
2d3b65
     }
2d3b65
 
2d3b65
+  if (variant == NULL)
2d3b65
+    {
2d3b65
+      variant = g_settings_schema_key_get_per_desktop_default (&binding->key);
2d3b65
+      if (variant &&
2d3b65
+          !binding->get_mapping (&value, variant, binding->user_data))
2d3b65
+        {
2d3b65
+          g_error ("Per-desktop default value for key '%s' in schema '%s' "
2d3b65
+                   "was rejected by the binding mapping function.",
2d3b65
+                   binding->key.name, g_settings_schema_get_id (binding->key.schema));
2d3b65
+          g_variant_unref (variant);
2d3b65
+          variant = NULL;
2d3b65
+        }
2d3b65
+    }
2d3b65
+
2d3b65
   if (variant == NULL)
2d3b65
     {
2d3b65
       variant = g_variant_ref (binding->key.default_value);
2d3b65
diff --git a/gio/gsettingsschema-internal.h b/gio/gsettingsschema-internal.h
2d3b65
index f54de3b34..5f996b4bc 100644
2d3b65
--- a/gio/gsettingsschema-internal.h
2d3b65
+++ b/gio/gsettingsschema-internal.h
2d3b65
@@ -37,6 +37,7 @@ struct _GSettingsSchemaKey
2d3b65
   const GVariantType *type;
2d3b65
   GVariant *minimum, *maximum;
2d3b65
   GVariant *default_value;
2d3b65
+  GVariant *desktop_overrides;
2d3b65
 
2d3b65
   gint ref_count;
2d3b65
 };
2d3b65
@@ -58,6 +59,7 @@ gboolean                g_settings_schema_key_type_check                (GSettin
2d3b65
 GVariant *              g_settings_schema_key_range_fixup               (GSettingsSchemaKey *key,
2d3b65
                                                                          GVariant           *value);
2d3b65
 GVariant *              g_settings_schema_key_get_translated_default    (GSettingsSchemaKey *key);
2d3b65
+GVariant *              g_settings_schema_key_get_per_desktop_default   (GSettingsSchemaKey *key);
2d3b65
 
2d3b65
 gint                    g_settings_schema_key_to_enum                   (GSettingsSchemaKey *key,
2d3b65
                                                                          GVariant           *value);
2d3b65
diff --git a/gio/gsettingsschema.c b/gio/gsettingsschema.c
2d3b65
index f1274a369..17b7e3b01 100644
2d3b65
--- a/gio/gsettingsschema.c
2d3b65
+++ b/gio/gsettingsschema.c
2d3b65
@@ -27,6 +27,7 @@
2d3b65
 #include <glibintl.h>
2d3b65
 #include <locale.h>
2d3b65
 #include <string.h>
2d3b65
+#include <stdlib.h>
2d3b65
 
2d3b65
 /**
2d3b65
  * SECTION:gsettingsschema
2d3b65
@@ -1283,6 +1284,11 @@ g_settings_schema_key_init (GSettingsSchemaKey *key,
2d3b65
           endian_fixup (&key->maximum);
2d3b65
           break;
2d3b65
 
2d3b65
+        case 'd':
2d3b65
+          g_variant_get (data, "@a{sv}", &key->desktop_overrides);
2d3b65
+          endian_fixup (&key->desktop_overrides);
2d3b65
+          break;
2d3b65
+
2d3b65
         default:
2d3b65
           g_warning ("unknown schema extension '%c'", code);
2d3b65
           break;
2d3b65
@@ -1303,6 +1309,9 @@ g_settings_schema_key_clear (GSettingsSchemaKey *key)
2d3b65
   if (key->maximum)
2d3b65
     g_variant_unref (key->maximum);
2d3b65
 
2d3b65
+  if (key->desktop_overrides)
2d3b65
+    g_variant_unref (key->desktop_overrides);
2d3b65
+
2d3b65
   g_variant_unref (key->default_value);
2d3b65
 
2d3b65
   g_settings_schema_unref (key->schema);
2d3b65
@@ -1410,6 +1419,35 @@ g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
2d3b65
   return value;
2d3b65
 }
2d3b65
 
2d3b65
+GVariant *
2d3b65
+g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey *key)
2d3b65
+{
2d3b65
+  static const gchar * const *current_desktops;
2d3b65
+  GVariant *value = NULL;
2d3b65
+  gint i;
2d3b65
+
2d3b65
+  if (!key->desktop_overrides)
2d3b65
+    return NULL;
2d3b65
+
2d3b65
+  if (g_once_init_enter (&current_desktops))
2d3b65
+    {
2d3b65
+      const gchar *xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
2d3b65
+      gchar **tmp;
2d3b65
+
2d3b65
+      if (xdg_current_desktop != NULL && xdg_current_desktop[0] != '\0')
2d3b65
+        tmp = g_strsplit (xdg_current_desktop, G_SEARCHPATH_SEPARATOR_S, -1);
2d3b65
+      else
2d3b65
+        tmp = g_new0 (gchar *, 0 + 1);
2d3b65
+
2d3b65
+      g_once_init_leave (&current_desktops, (const gchar **) tmp);
2d3b65
+    }
2d3b65
+
2d3b65
+  for (i = 0; value == NULL && current_desktops[i] != NULL; i++)
2d3b65
+    value = g_variant_lookup_value (key->desktop_overrides, current_desktops[i], NULL);
2d3b65
+
2d3b65
+  return value;
2d3b65
+}
2d3b65
+
2d3b65
 gint
2d3b65
 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
2d3b65
                                GVariant           *value)
2d3b65
@@ -1698,6 +1736,9 @@ g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
2d3b65
 
2d3b65
   value = g_settings_schema_key_get_translated_default (key);
2d3b65
 
2d3b65
+  if (!value)
2d3b65
+    value = g_settings_schema_key_get_per_desktop_default (key);
2d3b65
+
2d3b65
   if (!value)
2d3b65
     value = g_variant_ref (key->default_value);
2d3b65
 
2d3b65
-- 
2d3b65
2.21.0
2d3b65
2d3b65
2d3b65
From 3710e830de015829c086c69181a8703645d577ec Mon Sep 17 00:00:00 2001
2d3b65
From: Allison Lortie <desrt@desrt.ca>
2d3b65
Date: Wed, 2 Aug 2017 11:10:18 +0100
2d3b65
Subject: [PATCH 3/4] glib-compile-schemas: Handle per-desktop overrides
2d3b65
2d3b65
Add a new syntax to override files: if the group name has a ':' in it,
2d3b65
it indicates that we want to override the default values of keys for
2d3b65
only one desktop. For example:
2d3b65
2d3b65
[org.gnome.desktop.interface:Unity]
2d3b65
font-name='Ubuntu 12'
2d3b65
2d3b65
Will override the settings, only if "Unity" is found in
2d3b65
XDG_CURRENT_DESKTOP. Multiple per-desktop overrides can be specified
2d3b65
for a given key: the one which comes first in XDG_CURRENT_DESKTOP will
2d3b65
be used.
2d3b65
2d3b65
https://bugzilla.gnome.org/show_bug.cgi?id=746592
2d3b65
---
2d3b65
 gio/glib-compile-schemas.c | 83 ++++++++++++++++++++++++++++++++++----
2d3b65
 1 file changed, 75 insertions(+), 8 deletions(-)
2d3b65
2d3b65
diff --git a/gio/glib-compile-schemas.c b/gio/glib-compile-schemas.c
2d3b65
index 2dc8c7171..59fb68ee7 100644
2d3b65
--- a/gio/glib-compile-schemas.c
2d3b65
+++ b/gio/glib-compile-schemas.c
2d3b65
@@ -179,6 +179,8 @@ typedef struct
2d3b65
   GString      *unparsed_default_value;
2d3b65
   GVariant     *default_value;
2d3b65
 
2d3b65
+  GVariantDict *desktop_overrides;
2d3b65
+
2d3b65
   GString      *strinfo;
2d3b65
   gboolean      is_enum;
2d3b65
   gboolean      is_flags;
2d3b65
@@ -731,6 +733,11 @@ key_state_serialise (KeyState *state)
2d3b65
             g_variant_builder_add (&builder, "(y(**))", 'r',
2d3b65
                                    state->minimum, state->maximum);
2d3b65
 
2d3b65
+          /* per-desktop overrides */
2d3b65
+          if (state->desktop_overrides)
2d3b65
+            g_variant_builder_add (&builder, "(y@a{sv})", 'd',
2d3b65
+                                   g_variant_dict_end (state->desktop_overrides));
2d3b65
+
2d3b65
           state->serialised = g_variant_builder_end (&builder);
2d3b65
         }
2d3b65
 
2d3b65
@@ -768,6 +775,9 @@ key_state_free (gpointer data)
2d3b65
   if (state->serialised)
2d3b65
     g_variant_unref (state->serialised);
2d3b65
 
2d3b65
+  if (state->desktop_overrides)
2d3b65
+    g_variant_dict_unref (state->desktop_overrides);
2d3b65
+
2d3b65
   g_slice_free (KeyState, state);
2d3b65
 }
2d3b65
 
2d3b65
@@ -1878,6 +1888,8 @@ set_overrides (GHashTable  *schema_table,
2d3b65
       gchar **groups;
2d3b65
       gint i;
2d3b65
 
2d3b65
+      g_debug ("Processing override file '%s'", filename);
2d3b65
+
2d3b65
       key_file = g_key_file_new ();
2d3b65
       if (!g_key_file_load_from_file (key_file, filename, 0, &error))
2d3b65
         {
2d3b65
@@ -1900,18 +1912,31 @@ set_overrides (GHashTable  *schema_table,
2d3b65
       for (i = 0; groups[i]; i++)
2d3b65
         {
2d3b65
           const gchar *group = groups[i];
2d3b65
+          const gchar *schema_name;
2d3b65
+          const gchar *desktop_id;
2d3b65
           SchemaState *schema;
2d3b65
+          gchar **pieces;
2d3b65
           gchar **keys;
2d3b65
           gint j;
2d3b65
 
2d3b65
-          schema = g_hash_table_lookup (schema_table, group);
2d3b65
+          pieces = g_strsplit (group, ":", 2);
2d3b65
+          schema_name = pieces[0];
2d3b65
+          desktop_id = pieces[1];
2d3b65
+
2d3b65
+          g_debug ("Processing group '%s' (schema '%s', %s)",
2d3b65
+                   group, schema_name, desktop_id ? desktop_id : "all desktops");
2d3b65
+
2d3b65
+          schema = g_hash_table_lookup (schema_table, schema_name);
2d3b65
 
2d3b65
           if (schema == NULL)
2d3b65
-            /* Having the schema not be installed is expected to be a
2d3b65
-             * common case.  Don't even emit an error message about
2d3b65
-             * that.
2d3b65
-             */
2d3b65
-            continue;
2d3b65
+            {
2d3b65
+              /* Having the schema not be installed is expected to be a
2d3b65
+               * common case.  Don't even emit an error message about
2d3b65
+               * that.
2d3b65
+               */
2d3b65
+              g_strfreev (pieces);
2d3b65
+              continue;
2d3b65
+            }
2d3b65
 
2d3b65
           keys = g_key_file_get_keys (key_file, group, NULL, NULL);
2d3b65
           g_assert (keys != NULL);
2d3b65
@@ -1939,6 +1964,32 @@ set_overrides (GHashTable  *schema_table,
2d3b65
 
2d3b65
                   fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2d3b65
                   g_key_file_free (key_file);
2d3b65
+                  g_strfreev (pieces);
2d3b65
+                  g_strfreev (groups);
2d3b65
+                  g_strfreev (keys);
2d3b65
+
2d3b65
+                  return FALSE;
2d3b65
+                }
2d3b65
+
2d3b65
+              if (desktop_id != NULL && state->l10n)
2d3b65
+                {
2d3b65
+                  /* Let's avoid the n*m case of per-desktop localised
2d3b65
+                   * default values, and just forbid it.
2d3b65
+                   */
2d3b65
+                  fprintf (stderr,
2d3b65
+                           _("cannot provide per-desktop overrides for localised "
2d3b65
+                             "key '%s' in schema '%s' (override file '%s')"),
2d3b65
+                           key, group, filename);
2d3b65
+
2d3b65
+                  if (!strict)
2d3b65
+                    {
2d3b65
+                      fprintf (stderr, _("; ignoring override for this key.\n"));
2d3b65
+                      continue;
2d3b65
+                    }
2d3b65
+
2d3b65
+                  fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2d3b65
+                  g_key_file_free (key_file);
2d3b65
+                  g_strfreev (pieces);
2d3b65
                   g_strfreev (groups);
2d3b65
                   g_strfreev (keys);
2d3b65
 
2d3b65
@@ -1969,6 +2020,7 @@ set_overrides (GHashTable  *schema_table,
2d3b65
 
2d3b65
                   fprintf (stderr, _("--strict was specified; exiting.\n"));
2d3b65
                   g_key_file_free (key_file);
2d3b65
+                  g_strfreev (pieces);
2d3b65
                   g_strfreev (groups);
2d3b65
                   g_strfreev (keys);
2d3b65
 
2d3b65
@@ -1997,6 +2049,7 @@ set_overrides (GHashTable  *schema_table,
2d3b65
 
2d3b65
                       fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2d3b65
                       g_key_file_free (key_file);
2d3b65
+                      g_strfreev (pieces);
2d3b65
                       g_strfreev (groups);
2d3b65
                       g_strfreev (keys);
2d3b65
 
2d3b65
@@ -2025,6 +2078,7 @@ set_overrides (GHashTable  *schema_table,
2d3b65
 
2d3b65
                       fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2d3b65
                       g_key_file_free (key_file);
2d3b65
+                      g_strfreev (pieces);
2d3b65
                       g_strfreev (groups);
2d3b65
                       g_strfreev (keys);
2d3b65
 
2d3b65
@@ -2032,11 +2086,24 @@ set_overrides (GHashTable  *schema_table,
2d3b65
                     }
2d3b65
                 }
2d3b65
 
2d3b65
-              g_variant_unref (state->default_value);
2d3b65
-              state->default_value = value;
2d3b65
+              if (desktop_id != NULL)
2d3b65
+                {
2d3b65
+                  if (state->desktop_overrides == NULL)
2d3b65
+                    state->desktop_overrides = g_variant_dict_new (NULL);
2d3b65
+
2d3b65
+                  g_variant_dict_insert_value (state->desktop_overrides, desktop_id, value);
2d3b65
+                  g_variant_unref (value);
2d3b65
+                }
2d3b65
+              else
2d3b65
+                {
2d3b65
+                  g_variant_unref (state->default_value);
2d3b65
+                  state->default_value = value;
2d3b65
+                }
2d3b65
+
2d3b65
               g_free (string);
2d3b65
             }
2d3b65
 
2d3b65
+          g_strfreev (pieces);
2d3b65
           g_strfreev (keys);
2d3b65
         }
2d3b65
 
2d3b65
-- 
2d3b65
2.21.0
2d3b65
2d3b65
2d3b65
From 2ca9218fb46f32fa02bed43c6e60243c8c5d656f Mon Sep 17 00:00:00 2001
2d3b65
From: =?UTF-8?q?Alberts=20Muktup=C4=81vels?= <alberts.muktupavels@gmail.com>
2d3b65
Date: Tue, 19 Jun 2018 23:39:24 +0300
2d3b65
Subject: [PATCH 4/4] Add a test for per-desktop overrides
2d3b65
2d3b65
---
2d3b65
 gio/glib-compile-schemas.c                   |   1 +
2d3b65
 gio/tests/Makefile.am                        |   2 +
2d3b65
 gio/tests/gsettings.c                        | 106 ++++++++++++++++++-
2d3b65
 gio/tests/org.gtk.test.gschema.override.orig |   2 +
2d3b65
 gio/tests/org.gtk.test.gschema.xml.orig      |   6 ++
2d3b65
 5 files changed, 116 insertions(+), 1 deletion(-)
2d3b65
 create mode 100644 gio/tests/org.gtk.test.gschema.override.orig
2d3b65
2d3b65
diff --git a/gio/glib-compile-schemas.c b/gio/glib-compile-schemas.c
2d3b65
index 59fb68ee7..00dd64146 100644
2d3b65
--- a/gio/glib-compile-schemas.c
2d3b65
+++ b/gio/glib-compile-schemas.c
2d3b65
@@ -2139,6 +2139,7 @@ main (int argc, char **argv)
2d3b65
 
2d3b65
     /* These options are only for use in the gschema-compile tests */
2d3b65
     { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2d3b65
+    { "override-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &override_files, NULL, NULL },
2d3b65
     { NULL }
2d3b65
   };
2d3b65
 
2d3b65
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
2d3b65
index 49a19bf4a..b41317ad9 100644
2d3b65
--- a/gio/tests/Makefile.am
2d3b65
+++ b/gio/tests/Makefile.am
2d3b65
@@ -367,12 +367,14 @@ test.mo: de.po
2d3b65
 EXTRA_DIST += de.po
2d3b65
 dist_uninstalled_test_data += \
2d3b65
 	org.gtk.test.gschema.xml.orig		\
2d3b65
+	org.gtk.test.gschema.override.orig	\
2d3b65
 	org.gtk.schemasourcecheck.gschema.xml	\
2d3b65
 	testenum.h				\
2d3b65
 	enums.xml.template
2d3b65
 # Generated while running the testcase itself...
2d3b65
 CLEANFILES += \
2d3b65
 	org.gtk.test.gschema.xml	\
2d3b65
+	org.gtk.test.gschema.override	\
2d3b65
 	org.gtk.test.enums.xml		\
2d3b65
 	gsettings.store			\
2d3b65
 	gschemas.compiled		\
2d3b65
diff --git a/gio/tests/gsettings.c b/gio/tests/gsettings.c
2d3b65
index 2be4122fe..acdeead4c 100644
2d3b65
--- a/gio/tests/gsettings.c
2d3b65
+++ b/gio/tests/gsettings.c
2d3b65
@@ -2192,6 +2192,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
2d3b65
                             "org.gtk.test.range.direct",
2d3b65
                             "org.gtk.test.mapped",
2d3b65
                             "org.gtk.test.descriptions",
2d3b65
+                            "org.gtk.test.per-desktop",
2d3b65
                             NULL));
2d3b65
 }
2d3b65
 
2d3b65
@@ -2583,6 +2584,100 @@ test_default_value (void)
2d3b65
   g_object_unref (settings);
2d3b65
 }
2d3b65
 
2d3b65
+static gboolean
2d3b65
+string_map_func (GVariant *value,
2d3b65
+                 gpointer *result,
2d3b65
+                 gpointer  user_data)
2d3b65
+{
2d3b65
+  const gchar *str;
2d3b65
+
2d3b65
+  str = g_variant_get_string (value, NULL);
2d3b65
+  *result = g_variant_new_string (str);
2d3b65
+
2d3b65
+  return TRUE;
2d3b65
+}
2d3b65
+
2d3b65
+/* Test that per-desktop values from org.gtk.test.gschema.override
2d3b65
+ * does not change default value if current desktop is not listed in
2d3b65
+ * $XDG_CURRENT_DESKTOP.
2d3b65
+ */
2d3b65
+static void
2d3b65
+test_per_desktop (void)
2d3b65
+{
2d3b65
+  GSettings *settings;
2d3b65
+  TestObject *obj;
2d3b65
+  gpointer p;
2d3b65
+  gchar *str;
2d3b65
+
2d3b65
+  settings = g_settings_new ("org.gtk.test.per-desktop");
2d3b65
+  obj = test_object_new ();
2d3b65
+
2d3b65
+  if (!g_test_subprocess ())
2d3b65
+    {
2d3b65
+      g_test_trap_subprocess ("/gsettings/per-desktop/subprocess", 0, 0);
2d3b65
+      g_test_trap_assert_passed ();
2d3b65
+    }
2d3b65
+
2d3b65
+  str = g_settings_get_string (settings, "desktop");
2d3b65
+  g_assert_cmpstr (str, ==, "GNOME");
2d3b65
+  g_free (str);
2d3b65
+
2d3b65
+  p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
2d3b65
+
2d3b65
+  str = g_variant_dup_string (p, NULL);
2d3b65
+  g_assert_cmpstr (str, ==, "GNOME");
2d3b65
+  g_free (str);
2d3b65
+
2d3b65
+  g_variant_unref (p);
2d3b65
+
2d3b65
+  g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
2d3b65
+
2d3b65
+  g_object_get (obj, "string", &str, NULL);
2d3b65
+  g_assert_cmpstr (str, ==, "GNOME");
2d3b65
+  g_free (str);
2d3b65
+
2d3b65
+  g_object_unref (settings);
2d3b65
+  g_object_unref (obj);
2d3b65
+}
2d3b65
+
2d3b65
+/* Test that per-desktop values from org.gtk.test.gschema.override
2d3b65
+ * are successfully loaded based on the value of $XDG_CURRENT_DESKTOP.
2d3b65
+ */
2d3b65
+static void
2d3b65
+test_per_desktop_subprocess (void)
2d3b65
+{
2d3b65
+  GSettings *settings;
2d3b65
+  TestObject *obj;
2d3b65
+  gpointer p;
2d3b65
+  gchar *str;
2d3b65
+
2d3b65
+  g_setenv ("XDG_CURRENT_DESKTOP", "GNOME-Classic:GNOME", TRUE);
2d3b65
+
2d3b65
+  settings = g_settings_new ("org.gtk.test.per-desktop");
2d3b65
+  obj = test_object_new ();
2d3b65
+
2d3b65
+  str = g_settings_get_string (settings, "desktop");
2d3b65
+  g_assert_cmpstr (str, ==, "GNOME Classic");
2d3b65
+  g_free (str);
2d3b65
+
2d3b65
+  p = g_settings_get_mapped (settings, "desktop", string_map_func, NULL);
2d3b65
+
2d3b65
+  str = g_variant_dup_string (p, NULL);
2d3b65
+  g_assert_cmpstr (str, ==, "GNOME Classic");
2d3b65
+  g_free (str);
2d3b65
+
2d3b65
+  g_variant_unref (p);
2d3b65
+
2d3b65
+  g_settings_bind (settings, "desktop", obj, "string", G_SETTINGS_BIND_DEFAULT);
2d3b65
+
2d3b65
+  g_object_get (obj, "string", &str, NULL);
2d3b65
+  g_assert_cmpstr (str, ==, "GNOME Classic");
2d3b65
+  g_free (str);
2d3b65
+
2d3b65
+  g_object_unref (settings);
2d3b65
+  g_object_unref (obj);
2d3b65
+}
2d3b65
+
2d3b65
 static void
2d3b65
 test_extended_schema (void)
2d3b65
 {
2d3b65
@@ -2603,6 +2698,7 @@ int
2d3b65
 main (int argc, char *argv[])
2d3b65
 {
2d3b65
   gchar *schema_text;
2d3b65
+  gchar *override_text;
2d3b65
   gchar *enums;
2d3b65
   gint result;
2d3b65
 
2d3b65
@@ -2625,6 +2721,7 @@ main (int argc, char *argv[])
2d3b65
       g_setenv ("XDG_DATA_DIRS", ".", TRUE);
2d3b65
       g_setenv ("XDG_DATA_HOME", ".", TRUE);
2d3b65
       g_setenv ("GSETTINGS_SCHEMA_DIR", ".", TRUE);
2d3b65
+      g_setenv ("XDG_CURRENT_DESKTOP", "", TRUE);
2d3b65
 
2d3b65
       if (!backend_set)
2d3b65
         g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
2d3b65
@@ -2647,6 +2744,10 @@ main (int argc, char *argv[])
2d3b65
       g_assert (g_file_set_contents ("org.gtk.test.gschema.xml", schema_text, -1, NULL));
2d3b65
       g_free (schema_text);
2d3b65
 
2d3b65
+      g_assert (g_file_get_contents (SRCDIR "/org.gtk.test.gschema.override.orig", &override_text, NULL, NULL));
2d3b65
+      g_assert (g_file_set_contents ("org.gtk.test.gschema.override", override_text, -1, NULL));
2d3b65
+      g_free (override_text);
2d3b65
+
2d3b65
 /* Meson build defines this, autotools build does not */
2d3b65
 #ifndef GLIB_COMPILE_SCHEMAS
2d3b65
 #define GLIB_COMPILE_SCHEMAS "../glib-compile-schemas"
2d3b65
@@ -2655,7 +2756,8 @@ main (int argc, char *argv[])
2d3b65
       g_remove ("gschemas.compiled");
2d3b65
       g_assert (g_spawn_command_line_sync (GLIB_COMPILE_SCHEMAS " --targetdir=. "
2d3b65
                                            "--schema-file=org.gtk.test.enums.xml "
2d3b65
-                                           "--schema-file=org.gtk.test.gschema.xml",
2d3b65
+                                           "--schema-file=org.gtk.test.gschema.xml "
2d3b65
+                                           "--override-file=org.gtk.test.gschema.override",
2d3b65
                                            NULL, NULL, &result, NULL));
2d3b65
       g_assert (result == 0);
2d3b65
 
2d3b65
@@ -2736,6 +2838,8 @@ main (int argc, char *argv[])
2d3b65
   g_test_add_func ("/gsettings/read-descriptions", test_read_descriptions);
2d3b65
   g_test_add_func ("/gsettings/test-extended-schema", test_extended_schema);
2d3b65
   g_test_add_func ("/gsettings/default-value", test_default_value);
2d3b65
+  g_test_add_func ("/gsettings/per-desktop", test_per_desktop);
2d3b65
+  g_test_add_func ("/gsettings/per-desktop/subprocess", test_per_desktop_subprocess);
2d3b65
 
2d3b65
   result = g_test_run ();
2d3b65
 
2d3b65
diff --git a/gio/tests/org.gtk.test.gschema.override.orig b/gio/tests/org.gtk.test.gschema.override.orig
2d3b65
new file mode 100644
2d3b65
index 000000000..6694baace
2d3b65
--- /dev/null
2d3b65
+++ b/gio/tests/org.gtk.test.gschema.override.orig
2d3b65
@@ -0,0 +1,2 @@
2d3b65
+[org.gtk.test.per-desktop:GNOME-Classic]
2d3b65
+desktop = "GNOME Classic"
2d3b65
diff --git a/gio/tests/org.gtk.test.gschema.xml.orig b/gio/tests/org.gtk.test.gschema.xml.orig
2d3b65
index c07558335..fbcdce683 100644
2d3b65
--- a/gio/tests/org.gtk.test.gschema.xml.orig
2d3b65
+++ b/gio/tests/org.gtk.test.gschema.xml.orig
2d3b65
@@ -209,4 +209,10 @@
2d3b65
     </key>
2d3b65
   </schema>
2d3b65
 
2d3b65
+  <schema id="org.gtk.test.per-desktop" path="/tests/per-desktop/">
2d3b65
+    <key name="desktop" type="s">
2d3b65
+      <default>"GNOME"</default>
2d3b65
+    </key>
2d3b65
+  </schema>
2d3b65
+
2d3b65
 </schemalist>
2d3b65
-- 
2d3b65
2.21.0
2d3b65