2 <title>Migrating from GConf to GSettings</title>
5 <title>Before you start</title>
8 Converting individual applications and their settings from GConf to
9 GSettings can be done at will. But desktop-wide settings like font or
10 theme settings often have consumers in multiple modules. Therefore,
11 some consideration has to go into making sure that all users of a setting
12 are converted to GSettings at the same time or that the program
13 responsible for configuring that setting continues to update the value in
17 It is always a good idea to have a look at how others have handled
18 similar problems before.
23 <title>Conceptual differences</title>
26 Conceptually, GConf and GSettings are fairly similar. Both
27 have a concept of pluggable backends. Both keep information
28 about keys and their types in schemas. Both have a concept of
29 mandatory values, which lets you implement lock-down.
32 There are some differences in the approach to schemas. GConf
33 installs the schemas into the database and has API to handle
34 schema information (gconf_client_get_default_from_schema(),
35 gconf_value_get_schema(), etc). GSettings on the other hand
36 assumes that an application knows its own schemas, and does
37 not provide API to handle schema information at runtime.
38 GSettings is also more strict about requiring a schema whenever
39 you want to read or write a key. To deal with more free-form
40 information that would appear in schema-less entries in GConf,
41 GSettings allows for schemas to be 'relocatable'.
44 One difference in the way applications interact with their
45 settings is that with GConf you interact with a tree of
46 settings (ie the keys you pass to functions when reading
47 or writing values are actually paths with the actual name
48 of the key as the last element. With GSettings, you create
49 a GSettings object which has an implicit prefix that determines
50 where the settings get stored in the global tree of settings,
51 but the keys you pass when reading or writing values are just
52 the key names, not the full path.
57 <title>GConfClient (and GConfBridge) API conversion</title>
60 Most people use GConf via the high-level #GConfClient API.
61 The corresponding API is the #GSettings object. While not
62 every GConfClient function has a direct GSettings equivalent,
64 <table id="gconf-client-vs-gsettings">
67 <row><entry>GConfClient</entry><entry>GSettings</entry></row>
70 <row><entry>gconf_client_get_default()</entry><entry>no direct equivalent,
71 instead you call g_settings_new() for the schemas you use</entry></row>
72 <row><entry>gconf_client_set()</entry><entry>g_settings_set()</entry></row>
73 <row><entry>gconf_client_get()</entry><entry>g_settings_get()</entry></row>
74 <row><entry>gconf_client_get_bool()</entry><entry>g_settings_get_boolean()</entry></row>
75 <row><entry>gconf_client_set_bool()</entry><entry>g_settings_set_boolean()</entry></row>
76 <row><entry>gconf_client_get_int()</entry><entry>g_settings_get_int()</entry></row>
77 <row><entry>gconf_client_set_int()</entry><entry>g_settings_set_int()</entry></row>
78 <row><entry>gconf_client_get_float()</entry><entry>g_settings_get_double()</entry></row>
79 <row><entry>gconf_client_set_float()</entry><entry>g_settings_set_double()</entry></row>
80 <row><entry>gconf_client_get_string()</entry><entry>g_settings_get_string()</entry></row>
81 <row><entry>gconf_client_set_string()</entry><entry>g_settings_set_string()</entry></row>
82 <row><entry>gconf_client_get_list()</entry><entry>for string lists, see g_settings_get_strv(), else see g_settings_get_value() and #GVariant API</entry></row>
83 <row><entry>gconf_client_set_list()</entry><entry>for string lists, see g_settings_set_strv(), else see g_settings_set_value() and #GVariant API</entry></row>
84 <row><entry>gconf_entry_get_is_writable()</entry><entry>g_settings_is_writable()</entry></row>
85 <row><entry>gconf_client_notify_add()</entry><entry>not required, the #GSettings::changed signal is emitted automatically</entry></row>
86 <row><entry>gconf_client_add_dir()</entry><entry>not required, each GSettings instance automatically watches all keys in its path</entry></row>
87 <row><entry>#GConfChangeSet</entry><entry>g_settings_delay(), g_settings_apply()</entry></row>
88 <row><entry>gconf_client_get_default_from_schema()</entry><entry>no equivalent, applications are expected to know their schema</entry></row>
89 <row><entry>gconf_client_all_entries()</entry><entry>no equivalent, applications are expected to know their schema, and GSettings does not allow schema-less entries</entry></row>
90 <row><entry>gconf_client_get_without_default()</entry><entry>no equivalent</entry></row>
91 <row><entry>gconf_bridge_bind_property()</entry><entry>g_settings_bind()</entry></row>
92 <row><entry>gconf_bridge_bind_property_full()</entry><entry>g_settings_bind_with_mapping()</entry></row>
98 GConfBridge was a third-party library that used GConf to bind an object property
99 to a particular configuration key. GSettings offers this service itself.
102 There is a pattern that is sometimes used for GConf, where a setting can have
103 explicit 'value A', explicit 'value B' or 'use the system default'. With GConf,
104 'use the system default' is sometimes implemented by unsetting the user value.
107 This is not possible in GSettings, since it does not have API to determine if a value
108 is the default and does not let you unset values. The recommended way (and much
109 clearer) way in which this can be implemented in GSettings is to have a separate
110 'use-system-default' boolean setting.
115 <title>Change notification</title>
118 GConf requires you to call gconf_client_add_dir() and
119 gconf_client_notify_add() to get change notification. With
120 GSettings, this is not necessary; signals get emitted automatically
124 The #GSettings::changed signal is emitted for each changed key.
125 There is also a #GSettings::change-event signal that you can handle
126 if you need to see groups of keys that get changed at the same time.
129 GSettings also notifies you about changes in writability of keys,
130 with the #GSettings::writable-changed signal (and the
131 #GSettings::writable-change-event signal).
135 <section><title>Change sets</title>
137 GConf has a a concept of a set of changes which can be applied or reverted
138 at once: #GConfChangeSet (GConf doesn't actually apply changes atomically,
139 which is one of its shortcomings).
142 Instead of a separate object to represent a change set, GSettings has a
143 'delayed-apply' mode, which can be turned on for a GSettings object by
144 calling g_settings_delay(). In this mode, changes done to the GSettings
145 object are not applied - they are still visible when calling g_settings_get()
146 <emphasis>on the same object</emphasis>, but not to other GSettings instances
147 or even other processes.
150 To apply the pending changes all at once (GSettings <emphasis>does</emphasis>
151 atomicity here), call g_settings_apply(). To revert the pending changes,
152 call g_settings_revert() or just drop the reference to the #GSettings object.
157 <title>Schema conversion</title>
160 If you are porting your application from GConf, most likely you already
161 have a GConf schema. GConf comes with a commandline tool
162 gsettings-schema-convert that can help with the task of converting
163 a GConf schema into an equivalent GSettings schema. The tool is not
164 perfect and may need assistence in some cases.
166 <example><title>An example for using gsettings-schema-convert</title>
167 <para>Running <userinput>gsettings-schema-convert --gconf --xml --schema-id "org.gnome.font-rendering" --output org.gnome.font-rendering.gschema.xml destop_gnome_font_rendering.schemas</userinput> on the following <filename>desktop_gnome_font_rendering.schemas</filename> file:
170 <?xml version="1.0"?>
174 <key>/schemas/desktop/gnome/font_rendering/dpi</key>
175 <applyto>/desktop/gnome/font_rendering/dpi</applyto>
178 <default>96</default>
181 <long>The resolution used for converting font sizes to pixel sizes, in dots per inch.</long>
188 produces a <filename>org.gnome.font-rendering.gschema.xml</filename> file with the following content:
192 <schema id="org.gnome.font-rendering" path="/desktop/gnome/font_rendering/">
193 <key name="dpi" type="i">
194 <default>96</default>
195 <summary>DPI</summary>
196 <description>The resolution used for converting font sizes to pixel sizes, in dots per inch.</description>
206 GSettings schemas are identified at runtime by their id (as specified
207 in the XML source file). It is recommended to use a dotted name as schema
208 id, similar in style to a D-Bus bus name, e.g. "org.gnome.SessionManager".
209 In cases where the settings are general and not specific to one application,
210 the id should not use StudlyCaps, e.g. "org.gnome.font-rendering".
211 The filename used for the XML schema source is immaterial, but
212 schema compiler expects the files to have the extension
213 <filename>.gschema.xml</filename>. It is recommended to simply
214 use the schema id as the filename, followed by this extension,
215 e.g. <filename>org.gnome.SessionManager.gschema.xml</filename>.
219 The XML source file for your GSettings schema needs to get installed
220 into <filename>$datadir/glib-2.0/schemas</filename>, and needs to be
221 compiled into a binary form. At runtime, GSettings looks for compiled
222 schemas in the <filename>glib-2.0/schemas</filename> subdirectories
223 of all <envar>XDG_DATA_DIRS</envar> directories, so if you install
224 your schema in a different location, you need to set the
225 <envar>XDG_DATA_DIRS</envar> environment variable appropriately.
228 Schemas are compiled into binary form by the
229 <link linkend="glib-compile-schemas">glib-compile-schemas</link> utility.
230 GIO provides a <literal>glib_compile_schemas</literal>
231 variable for the schema compiler.
234 You can ignore all of this by using the provided m4 macros. To
235 do this, add to your <filename>configure.ac</filename>:
239 The corresponding <filename>Makefile.am</filename> fragment looks like
242 # gsettings_SCHEMAS is a list of all the schemas you want to install
243 gsettings_SCHEMAS = my.app.gschema.xml
245 # include the appropriate makefile rules for schema handling
251 This is not sufficient on its own. You need to mention what the source
252 of the <filename>my.app.gschema.xml</filename> file is. If the schema
253 file is distributed directly with your project's tarball then a mention
254 in <varname>EXTRA_DIST</varname> is appropriate. If the schema file is
255 generated from another source then you will need the appropriate rule
256 for that, plus probably an item in <varname>EXTRA_DIST</varname> for the
257 source files used by that rule.
261 One possible pitfall in doing schema conversion is that the default
262 values in GSettings schemas are parsed by the #GVariant parser.
263 This means that strings need to include quotes in the XML. Also note
264 that the types are now specified as #GVariant type strings.
268 <default>rgb</default>
274 <key name="rgba-order" type="s">
275 <default>'rgb'</default> <!-- note quotes -->
281 Another possible complication is that GConf specifies full paths
282 for each key, while a GSettings schema has a 'path' attribute that
283 contains the prefix for all the keys in the schema, and individual
284 keys just have a simple name. So
287 <key>/schemas/desktop/gnome/font_rendering/antialiasing</key>
293 <schema id="org.gnome.font" path="/desktop/gnome/font_rendering/">
294 <key name="antialiasing" type="s">
299 Default values can be localized in both GConf and GSettings schemas,
300 but GSettings uses gettext for the localization. You can specify
301 the gettext domain to use in the <tag class="attribute">gettext-domain</tag>
302 attribute. Therefore, when converting localized defaults in GConf,
305 <key>/schemas/apps/my_app/font_size</key>
307 <default>18</default>
310 <default>24</default>
318 <schema id="..." gettext-domain="your-domain">
320 <key name="font-size" type="i">
321 <default l10n="messages" context="font_size">18</default>
327 GSettings uses gettext for translation of default values.
328 The string that is translated is exactly the string that appears
329 inside of the <tag class='starttag'>default</tag> element. This
330 includes the quotation marks that appear around strings.
331 Default values must be marked with the <varname>l10n</varname>
332 attribute in the <tag class='starttag'>default</tag> tag, which
333 should be set as equal to <literal>'messages'</literal> or
334 <literal>'time'</literal> depending on the desired category. An
335 optional translation context can also be specified with the
336 <varname>context</varname> attribute, as in the example. This
337 is usually recommended, since the string "<literal>18</literal>"
338 is not particularly easy to translate without context. The
339 translated version of the default value should be stored in the
340 specified <varname>gettext-domain</varname>. Care must be taken
341 during translation to ensure that all translated values remain
342 syntactically valid; mistakes here will cause runtime errors.
345 GSettings schemas have optional <tag class="starttag">summary</tag> and
346 <tag class="starttag">description</tag> elements for each key which
347 correspond to the <tag class="starttag">short</tag> and
348 <tag class="starttag">long</tag> elements in the GConf schema and
349 will be used in similar ways by a future gsettings-editor, so you
350 should use the same conventions for them: The summary is just a short
351 label with no punctuation, the description can be one or more complete
352 sentences. If multiple paragraphs are desired for the description, the
353 paragraphs should be separated by a completely empty line.
356 Translations for these strings will also be handled
357 via gettext, so you should arrange for these strings to be
358 extracted into your gettext catalog. One way to do that is to use
359 intltool. Since intltool 0.50.1, schema files are
360 supported, so all you have to do is to add your .gschema.xml
361 files to <filename>POTFILES.in</filename> with a line like
363 [type: gettext/gsettings]data/org.foo.MyApp.gschema.xml
367 GSettings is a bit more restrictive about key names than GConf. Key
368 names in GSettings can be at most 32 characters long, and must only
369 consist of lowercase characters, numbers and dashes, with no
370 consecutive dashes. The first character must not be a number or dash,
371 and the last character cannot be '-'.
374 If you are using the GConf backend for GSettings during the
375 transition, you may want to keep your key names the same they
376 were in GConf, so that existing settings in the users GConf
377 database are preserved. You can achieve this by using the
378 <option>--allow-any-name</option> with the
379 <link linkend="glib-compile-schemas">glib-compile-schemas</link> schema
380 compiler. Note that this option is only meant
381 to ease the process of porting your application, allowing parts
382 of your application to continue to access GConf and parts to use
383 GSettings. By the time you have finished porting your application
384 you must ensure that all key names are valid.
388 <section><title>Data conversion</title>
390 GConf comes with a GSettings backend that can be used to
391 facility the transition to the GSettings API until you are
392 ready to make the jump to a different backend (most likely
393 dconf). To use it, you need to set the <envar>GSETTINGS_BACKEND</envar>
394 to 'gconf', e.g. by using
396 g_setenv ("GSETTINGS_BACKEND", "gconf", TRUE);
398 early on in your program. Note that this backend is meant purely
399 as a transition tool, and should not be used in production.
402 GConf also comes with a utility called
403 <command>gsettings-data-convert</command>, which is designed to help
404 with the task of migrating user settings from GConf into another
405 GSettings backend. It can be run manually, but it is designed to be
406 executed automatically, every time a user logs in. It keeps track of
407 the data migrations that it has already done, and it is harmless to
408 run it more than once.
411 To make use of this utility, you must install a keyfile in the
412 directory <filename>/usr/share/GConf/gsettings</filename> which
413 lists the GSettings keys and GConf paths to map to each other, for
414 each schema that you want to migrate user data for.
421 antialiasing = /desktop/gnome/font_rendering/antialiasing
422 dpi = /desktop/gnome/font_rendering/dpi
423 hinting = /desktop/gnome/font_rendering/hinting
424 rgba-order = /desktop/gnome/font_rendering/rgba_order
426 [apps.myapp:/path/to/myapps/]
427 some-odd-key1 = /apps/myapp/some_ODD-key1
430 The last key demonstrates that it may be necessary to modify the key
431 name to comply with stricter GSettings key name rules. Of course,
432 that means your application must use the new key names when looking
433 up settings in GSettings.
436 The last group in the example also shows how to handle the case
437 of 'relocatable' schemas, which don't have a fixed path. You can
438 specify the path to use in the group name, separated by a colon.
441 There are some limitations: <command>gsettings-data-convert</command>
442 does not do any transformation of the values. And it does not handle
443 complex GConf types other than lists of strings or integers.
446 Don't forget to require GConf 2.31.1 or newer in your configure
447 script if you are making use of the GConf backend or the conversion
452 If, as an application developer, you are interested in manually
453 ensuring that <command>gsettings-data-convert</command> has been
454 invoked (for example, to deal with the case where the user is
455 logged in during a distribution upgrade or for non-XDG desktop
456 environments which do not run the command as an autostart) you
457 may invoke it manually during your program initialisation. This
458 is not recommended for all application authors -- it is your
459 choice if this use case concerns you enough.
462 Internally, <command>gsettings-data-convert</command> uses a
463 keyfile to track which settings have been migrated. The
464 following code fragment will check that keyfile to see if your
465 data conversion script has been run yet and, if not, will
466 attempt to invoke the tool to run it. You should adapt it to
467 your application as you see fit.
473 ensure_migrated (const gchar *name)
475 gboolean needed = TRUE;
480 kf = g_key_file_new ();
482 g_key_file_load_from_data_dirs (kf, "gsettings-data-convert",
483 NULL, G_KEY_FILE_NONE, NULL);
484 list = g_key_file_get_string_list (kf, "State", "converted", &n, NULL);
488 for (i = 0; i < n; i++)
489 if (strcmp (list[i], name) == 0)
498 g_key_file_free (kf);
501 g_spawn_command_line_sync ("gsettings-data-convert",
502 NULL, NULL, NULL, NULL);
509 Although there is the possibility that the
510 <command>gsettings-data-convert</command> script will end up
511 running multiple times concurrently with this approach, it is
512 believed that this is safe.