GSettings, glib-compile-schemas and Eclipse

1k Views Asked by At

I am building this Gtkmm3 application in Ubuntu and wanted to explore GSettings. All was going well while following the instructions at the 'Using GSettings' page and then it was time to configure the make files. I use Eclipse 2019-12 IDE with CDT (V9.10) and 'GNU Make Builder' as the builder. I'm totally perplexed as to how to introduce the macros listed in the GNOME page into the make files. I even tried changing the project to a 'C/C++ Autotools Project' using Eclipse but still the necessary make files to add the macros were missing. Creating a new project with GNU Autotools does create the necessary make files but I was not able to get pkg-config to work with it.

Can anyone point me to some resource which explains how to compile the schema and how & where to load the resultant binary file (externally if necessary). I'll consider myself blessed if someone has already made a Gtkmm3 C++ application with GSettings support using Eclipse IDE in Linux and can share the details.

1

There are 1 best solutions below

0
On BEST ANSWER

Finally I figured. Thought I'll share my findings here. Actually some one out there had explained this for python (link below).

Using GSettings with Python/PyGObject

Creating the schema

For the developer the work starts with defining a schema for the settings. A schema is an XML file that looks something like this.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE schemalist SYSTEM "gio_gschema.dtd" >
<schemalist>
    <schema id="org.gtk.skanray.emlibrary"
        path="/org/skanray/emlibrary/" gettext-domain="emlibrary">
        <key name="wave-pressure-ptrach-visible" type="b">
            <default>true</default>
            <summary>Set visibility of 'Ptrach' trace in pressure waveform.</summary>
            <description>The pressure waveform shows multiple traces where 'PAW' is always enabled and additionally 'Ptrach' can be displayed. This settings affects the visibility of the trachial pressure trace shown in this waveform channel.</description></key>
    </schema>
</schemalist>

The file name has to have a ‘.gschema.xml’ suffix. The schema file should be in the project path, only so that it gets pushed to SVN. Best would be to use an XML editor (e.g. Eclipse) that supports design of XML files from a DTD file. Use following DTD file.

gschema.dtd

It is possible to store anything derived from GVariant into GSettings. Refer to following page to understand the basic types and the ‘type’ attribute to be used in the schema.

GVariant Format Strings

Compiling the schema

With the schema ready, (sudo) copy it into /usr/share/glib-2.0/schemas/ then run,

> sudo glib-compile-schemas /usr/share/glib-2.0/schemas/

At this point, the newly added settings can be seen / modified using dconf editor.

Accessing GSettings from the application

Coming to the main event of the show, this is how an application can read ( and / or write) settings. It is not necessary that one needs to bind property of an object to a ‘key’ in GSettings, it may be queried and used as well. Refer to GSettings API reference for details.

Glib::RefPtr <Gio::Settings> refSettings = Gio::Settings::create(“org.gtk.skanray.emlibrary”);
CLineTrace * pTrace = NULL; // CLineTrace is derived from Gtk::Widget
…
pTrace = …
…
if(refSettings)
{
  refSettings->bind("wave-pressure-ptrach-visible",
                    pTrace,
                    "visible",
                    Gio::SETTINGS_BIND_DEFAULT);
}

Now you can fire up dconf editor and test the settings.

NOTE

Bindings are usually preferred to be made in class constructors. However binding to ‘visible’ property of a widget could be a bit tricky. Typically the top level window does a show_all() as the last line in its constructor. However constructors of the children of top level window would have completed executing including making the bindings. If there were settings that had stored ‘visibility’ as false then the top level window’s call to show_all() would mess up with that setting. In such cases it is advised to perform the bind one time in the on_map() handler of the respective class.