SettingsSchema


Object Hierarchy:

GLib.SettingsSchema GLib.SettingsSchema GLib.SettingsSchema

Description:

[ CCode ( ref_function = "g_settings_schema_ref" , type_id = "g_settings_schema_get_type ()" , unref_function = "g_settings_schema_unref" ) ]
[ Compact ]
[ Version ( since = "2.32" ) ]
public class SettingsSchema

The [struct@Gio.

SettingsSchemaSource] and `GSettingsSchema` APIs provide a mechanism for advanced control over the loading of schemas and a mechanism for introspecting their content.

Plugin loading systems that wish to provide plugins a way to access settings face the problem of how to make the schemas for these settings visible to GSettings. Typically, a plugin will want to ship the schema along with itself and it won't be installed into the standard system directories for schemas.

[struct@Gio.SettingsSchemaSource] provides a mechanism for dealing with this by allowing the creation of a new ‘schema source’ from which schemas can be acquired. This schema source can then become part of the metadata associated with the plugin and queried whenever the plugin requires access to some settings.

Consider the following example:

```c typedef struct { … GSettingsSchemaSource *schema_source; … } Plugin;

Plugin * initialise_plugin (const gchar *dir) { Plugin *plugin;

plugin->schema_source = g_settings_schema_source_new_from_directory (dir, g_settings_schema_source_get_default (), FALSE, NULL);

return plugin; }

GSettings * plugin_get_settings (Plugin *plugin, const gchar *schema_id) { GSettingsSchema *schema;

if (schema_id == NULL) schema_id = plugin->identifier;

schema = g_settings_schema_source_lookup (plugin->schema_source, schema_id, FALSE);

if (schema == NULL) { … disable the plugin or abort, etc … }

return g_settings_new_full (schema, NULL, NULL); } ```

The code above shows how hooks should be added to the code that initialises (or enables) the plugin to create the schema source and how an API can be added to the plugin system to provide a convenient way for the plugin to access its settings, using the schemas that it ships.

From the standpoint of the plugin, it would need to ensure that it ships a gschemas.compiled file as part of itself, and then simply do the following:

```c { GSettings *settings; gint some_value;

settings = plugin_get_settings (self, NULL); some_value = g_settings_get_int (settings, "some-value"); … } ```

It's also possible that the plugin system expects the schema source files (ie: `.gschema.xml` files) instead of a `gschemas.compiled` file. In that case, the plugin loading system must compile the schemas for itself before attempting to create the settings source.

Example: GSettings:

Note:

Use glib-compile-schemas [directory] to compile the gschema.

Note:

Use Control-C to quit the program.

<schemalist>
<schema id="org.example.glib-settings-schema-source" path="/org/example/my-app/" gettext-domain="my-app">

<key name="greeting" type="s">
<default l10n="messages">"Hello, earthlings"</default>
<summary>A greeting</summary>
<description>
Greeting of the invading martians
</description>
</key>

<key name="bottles-of-beer" type="i">
<default>99</default>
<summary>Bottles of beer</summary>
<description>
Number of bottles of beer on the wall
</description>
</key>

<key name="lighting" type="b">
<default>false</default>
<summary>Is the light switched on?</summary>
<description>
State of an imaginary light switch.
</description>
</key>

</schema>
</schemalist>

glib-compile-schemas .

public static int main (string[] args) {
try {
// Custom location:
string settings_dir = Path.get_dirname (args[0]);
SettingsSchemaSource sss = new SettingsSchemaSource.from_directory (settings_dir, null, false);
SettingsSchema schema = sss.lookup ("org.example.glib-settings-schema-source", false);
if (sss.lookup == null) {
print ("ID not found.");
return 0;
}

Settings settings = new Settings.full (schema, null, null);

// Default location: (XDG_DATA_DIRS)
// Settings settings = new Settings ("org.example.glib-settings-schema-source");



// Output: ``Hello, earthlings``
string greeting = settings.get_string ("greeting");
print ("%s\n", greeting);

// Output: ``99``
int bottles = settings.get_int ("bottles-of-beer");
print ("%d\n", bottles);

// Output: ``false``
bool lighting = settings.get_boolean ("lighting");
print ("%s\n", lighting.to_string ());



// Change notification for any key in the schema
settings.changed.connect ((key) => {
print ("Key '%s' changed\n", key);
});

// Change notification for a single key
settings.changed["greeting"].connect (() => {
print ("New greeting: %s\n", settings.get_string ("greeting"));
});


// Setting keys

// Output:
// ``Key 'bottles-of-beer' changed``
// ``Key 'lighting' changed``
// ``Key 'greeting' changed``
// ``New greeting: hello, world``
settings.set_int ("bottles-of-beer", bottles - 1);
settings.set_boolean ("lighting", !lighting);
settings.set_string ("greeting", "hello, world");


print ("\nPlease start 'dconf-editor' and edit keys in /org/example/my-app/\n");
new MainLoop ().run ();
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}

valac --pkg gio-2.0 GLib.Settings.vala


Namespace: GLib
Package: gio-2.0

Content:

Methods: