[glib/mcatanzaro/glib-2-56-rhel8: 7/45] gsettingsschema: Allow per-desktop overrides




commit 4ab36dac09acdb0a98531afc633f2f35dc41cb4c
Author: Allison Lortie <desrt desrt ca>
Date:   Wed Aug 2 11:08:17 2017 +0100

    gsettingsschema: Allow per-desktop overrides
    
    Recognise a new 'd' option in schema keys which gives a dictionary of
    per-desktop default values. This dictionary is searched for the items
    found in XDG_CURRENT_DESKTOP, in the order. If nothing matches (or if
    the option is missing) then the default value is used as before.
    
    This feature was requested by Alberts Muktupāvels and this patch is
    based on an approach devised by them.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=746592

 gio/gsettings.c                | 21 +++++++++++++++++++++
 gio/gsettingsschema-internal.h |  2 ++
 gio/gsettingsschema.c          | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)
---
diff --git a/gio/gsettings.c b/gio/gsettings.c
index 5e5816d57..f1130c095 100644
--- a/gio/gsettings.c
+++ b/gio/gsettings.c
@@ -1739,6 +1739,13 @@ g_settings_get_mapped (GSettings           *settings,
       if (okay) goto okay;
     }
 
+  if ((value = g_settings_schema_key_get_per_desktop_default (&skey)))
+    {
+      okay = mapping (value, &result, user_data);
+      g_variant_unref (value);
+      if (okay) goto okay;
+    }
+
   if (mapping (skey.default_value, &result, user_data))
     goto okay;
 
@@ -2647,6 +2654,20 @@ g_settings_binding_key_changed (GSettings   *settings,
         }
     }
 
+  if (variant == NULL)
+    {
+      variant = g_settings_schema_key_get_per_desktop_default (&binding->key);
+      if (variant &&
+          !binding->get_mapping (&value, variant, binding->user_data))
+        {
+          g_error ("Per-desktop default value for key '%s' in schema '%s' "
+                   "was rejected by the binding mapping function.",
+                   binding->key.name, g_settings_schema_get_id (binding->key.schema));
+          g_variant_unref (variant);
+          variant = NULL;
+        }
+    }
+
   if (variant == NULL)
     {
       variant = g_variant_ref (binding->key.default_value);
diff --git a/gio/gsettingsschema-internal.h b/gio/gsettingsschema-internal.h
index f54de3b34..5f996b4bc 100644
--- a/gio/gsettingsschema-internal.h
+++ b/gio/gsettingsschema-internal.h
@@ -37,6 +37,7 @@ struct _GSettingsSchemaKey
   const GVariantType *type;
   GVariant *minimum, *maximum;
   GVariant *default_value;
+  GVariant *desktop_overrides;
 
   gint ref_count;
 };
@@ -58,6 +59,7 @@ gboolean                g_settings_schema_key_type_check                (GSettin
 GVariant *              g_settings_schema_key_range_fixup               (GSettingsSchemaKey *key,
                                                                          GVariant           *value);
 GVariant *              g_settings_schema_key_get_translated_default    (GSettingsSchemaKey *key);
+GVariant *              g_settings_schema_key_get_per_desktop_default   (GSettingsSchemaKey *key);
 
 gint                    g_settings_schema_key_to_enum                   (GSettingsSchemaKey *key,
                                                                          GVariant           *value);
diff --git a/gio/gsettingsschema.c b/gio/gsettingsschema.c
index f1274a369..17b7e3b01 100644
--- a/gio/gsettingsschema.c
+++ b/gio/gsettingsschema.c
@@ -27,6 +27,7 @@
 #include <glibintl.h>
 #include <locale.h>
 #include <string.h>
+#include <stdlib.h>
 
 /**
  * SECTION:gsettingsschema
@@ -1283,6 +1284,11 @@ g_settings_schema_key_init (GSettingsSchemaKey *key,
           endian_fixup (&key->maximum);
           break;
 
+        case 'd':
+          g_variant_get (data, "@a{sv}", &key->desktop_overrides);
+          endian_fixup (&key->desktop_overrides);
+          break;
+
         default:
           g_warning ("unknown schema extension '%c'", code);
           break;
@@ -1303,6 +1309,9 @@ g_settings_schema_key_clear (GSettingsSchemaKey *key)
   if (key->maximum)
     g_variant_unref (key->maximum);
 
+  if (key->desktop_overrides)
+    g_variant_unref (key->desktop_overrides);
+
   g_variant_unref (key->default_value);
 
   g_settings_schema_unref (key->schema);
@@ -1410,6 +1419,35 @@ g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
   return value;
 }
 
+GVariant *
+g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey *key)
+{
+  static const gchar * const *current_desktops;
+  GVariant *value = NULL;
+  gint i;
+
+  if (!key->desktop_overrides)
+    return NULL;
+
+  if (g_once_init_enter (&current_desktops))
+    {
+      const gchar *xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
+      gchar **tmp;
+
+      if (xdg_current_desktop != NULL && xdg_current_desktop[0] != '\0')
+        tmp = g_strsplit (xdg_current_desktop, G_SEARCHPATH_SEPARATOR_S, -1);
+      else
+        tmp = g_new0 (gchar *, 0 + 1);
+
+      g_once_init_leave (&current_desktops, (const gchar **) tmp);
+    }
+
+  for (i = 0; value == NULL && current_desktops[i] != NULL; i++)
+    value = g_variant_lookup_value (key->desktop_overrides, current_desktops[i], NULL);
+
+  return value;
+}
+
 gint
 g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
                                GVariant           *value)
@@ -1698,6 +1736,9 @@ g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
 
   value = g_settings_schema_key_get_translated_default (key);
 
+  if (!value)
+    value = g_settings_schema_key_get_per_desktop_default (key);
+
   if (!value)
     value = g_variant_ref (key->default_value);
 


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