Schema conversion

If you are porting your application from GConf, most likely you already have a GConf schema. GIO comes with a commandline tool gsettings-schema-convert that can help with the task of converting a GConf schema into an equivalent GSettings schema. The tool is not perfect and may need assistence in some cases.

GSettings schemas are described by XML files that need to get installed into $datadir/glib-2.0/schemas, and need to be compiled into a binary form by the gschema-compile utility. GIO provides variables gsettingsschemadir and gsettingsupdateschemacache for the location and the command, which can be used in configure.in as follows:

AC_SUBST(gsettingsschemadir, `pkg-config --variable gsettingsschemadir gio-2.0`)
AC_SUBST(gsettingsupdateschemacache, `pkg-config --variable gsettingsupdateschemacache gio-2.0`)

The corresponding Makefile.am fragment looks like this:

gsettingsschema_DATA = my.app.gschema.xml
install-data-hook:
        $(gsettingsupdateschemacache) $(gsettingsschemadir)

One possible pitfall in doing schema conversion is that the default values in GSettings schemas are parsed by the GVariant parser. This means that strings need to include quotes in the XML. Also note that the types are now specified as GVariant type strings.


<type>string</type>
<default>rgb</default>

        

becomes


<key name="rgba-order" type="s">
  <default>'rgb'</default> <!-- note quotes -->
</key>

        

Another possible complication is that GConf specifies full paths for each key, while a GSettings schema has a 'path' attribute that contains the prefix for all the keys in the schema, and individual keys just have a simple name. So


<key>/schemas/desktop/gnome/font_rendering/antialiasing</key>

        

becomes


<schema id="org.gnome.font" path="/desktop/gnome/font_rendering/">
  <key name="antialiasing" type="s">

        

Default values can be localized in both GConf and GSettings schemas, but GSettings uses gettext for the localization, so


<key>/schemas/apps/my_app/font_size</key>
  <locale name="C">
    <default>18</default>
  </locale>
  <locale name="be">
    <default>24</default>
  </locale>
</key>

        

becomes


<key name="font-size" type="i">
  <default l10n="messages" context="font_size">18</default>
</key>

        

Note how we used the context attribute to add msgctxt - "18" is not a good string to look up in gettext by itself. Also note that the value 24 is not present in the schema anymore. It has to be added to the gettext catalog for "be" instead.

GSettings schemas have more stringent restrictions on key names than GConf. Key names in GSettings are restricted to at most 32 characters, and must only consist of lowercase characters, numbers and dashes, with no consecutive dashes. The first character must not be a number or dash, and the last character cannot be '-'. The gschema-compile schema compiler has a --allow-any-name that lets you ignore these restrictions. Note that this option is only meant to ease the process of porting your application, allowing parts of your application to continue to access GConf and parts to use GSettings. By the time you have finished porting your application you must ensure that all key names are valid.