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
This commit is contained in:
Allison Lortie 2017-08-02 11:08:17 +01:00 committed by Alberts Muktupāvels
parent c2e7f31697
commit 6ca449672d
3 changed files with 64 additions and 0 deletions

View File

@ -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;
@ -2646,6 +2653,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);

View File

@ -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);

View File

@ -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);