I created an Ubuntu application with Quickly, and I can see an XML file in the folder data/glib-2.0 of my project root and I am not exactly sure what this is for. The XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="sample-application">
<schema id="net.launchpad.sample-application" path="/net/launchpad/sample-application/">
<key name="example" type="s">
<default>''</default>
<summary>Sample setting</summary>
<description>Longer description of this sample setting. Talk about allowed values and what it does.</description>
</key>
</schema>
</schemalist>
Furthermore, in the default preferences dialog code that is created with a new application, I can see the following code:
settings = Gio.Settings("net.launchpad.sample-application")
widget = self.builder.get_object('example_entry')
settings.bind("example", widget, "text", Gio.SettingsBindFlags.DEFAULT)
and I am not sure what this is doing.
The XML file defines a set of keys you can use throughout your application to store user preferences.
In the element schema, you will notice two attributes: id and path. The id is what you use to refer to the schema in the code when instantiating the settings object. The path is where the keys are stored.
You can find the settings by installing the
dconf-tools
package, and runningdconf-editor
. Then navigating to the node defined above. The node in the XML in the question (/net/launchpad/sample-application) would be here:The schema is used against the GSettings API. You can see the GTK C based docs here: http://developer.gnome.org/gio/stable/GSettings.html. This is a part of the Gio module
As for the code.
settings = Gio.Settings("net.launchpad.sample-application")
This creates the GSettings object, that you can use to bind widgets to for storing settings. The parameter needs to match exactly the id attribute of the schema element in the XML.
settings.bind("example", widget, "text", Gio.SettingsBindFlags.DEFAULT)
This creates a binding between the
example
key in the schema, to thetext
property of thewidget
. The value that gets passed to the key is the text property of the specified widget. For GtkEntry, text is the most obvious property to use. Because it is being bound, whenever you change the value of the property in the preferences window, it is automatically updated. The final parameter determines which direction the binding works - see the docs for more information.The type you specify in the XML for the key should match one of the available options. Generally a single character, which you will see against the defined constant. E.g. for a boolean:
So boolean equates to
b
.So, just say you wanted a boolean key, you could add the following beneath schema element, alongside the other key:
Then bind the active property like so:
Settings can then just be accessed with the get function, specific to the data type. e.g.