6d8ed86d1377c17a42a6b4b68e307057d79ef0ee
[platform/upstream/glib.git] / docs / reference / gio / migrating-gconf.xml
1   <chapter>
2     <title>Migrating from GConf to GSettings</title>
3
4     <section>
5       <title>Before you start</title>
6
7       <para>
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
14         both places.
15       </para>
16       <para>
17         It is always a good idea to have a look at how others have handled
18         similar problems before.  An examplaric conversion can be found e.g.
19         in the <ulink url="http://git.gnome.org/browse/archive/gnome-utils/log/?h=gsettings-tutorial">gsettings-tutorial</ulink> branch of gnome-utils.
20       </para>
21     </section>
22
23     <section>
24       <title>Conceptual differences</title>
25
26       <para>
27         Conceptually, GConf and GSettings are fairly similar. Both
28         have a concept of pluggable backends. Both keep information
29         about keys and their types in schemas. Both have a concept of
30         mandatory values, which lets you implement lock-down.
31       </para>
32       <para>
33         There are some differences in the approach to schemas. GConf
34         installs the schemas into the database and has API to handle
35         schema information (gconf_client_get_default_from_schema(),
36         gconf_value_get_schema(), etc). GSettings on the other hand
37         assumes that an application knows its own schemas, and does
38         not provide API to handle schema information at runtime.
39         GSettings is also more strict about requiring a schema whenever
40         you want to read or write a key. To deal with more free-form
41         information that would appear in schema-less entries in GConf,
42         GSettings allows for schemas to be 'relocatable'.
43       </para>
44       <para>
45         One difference in the way applications interact with their
46         settings is that with GConf you interact with a tree of
47         settings (ie the keys you pass to functions when reading
48         or writing values are actually paths with the actual name
49         of the key as the last element. With GSettings, you create
50         a GSettings object which has an implicit prefix that determines
51         where the settings get stored in the global tree of settings,
52         but the keys you pass when reading or writing values are just
53         the key names, not the full path.
54       </para>
55     </section>
56
57     <section>
58       <title>GConfClient (and GConfBridge) API conversion</title>
59
60       <para>
61         Most people use GConf via the high-level #GConfClient API.
62         The corresponding API is the #GSettings object. While not
63         every GConfClient function has a direct GSettings equivalent,
64         many do:
65         <table id="gconf-client-vs-gsettings">
66           <tgroup cols="2">
67             <thead>
68               <row><entry>GConfClient</entry><entry>GSettings</entry></row>
69             </thead>
70             <tbody>
71               <row><entry>gconf_client_get_default()</entry><entry>no direct equivalent,
72                                                                    instead you call g_settings_new() for the schemas you use</entry></row>
73               <row><entry>gconf_client_set()</entry><entry>g_settings_set()</entry></row>
74               <row><entry>gconf_client_get()</entry><entry>g_settings_get()</entry></row>
75               <row><entry>gconf_client_get_bool()</entry><entry>g_settings_get_boolean()</entry></row>
76               <row><entry>gconf_client_set_bool()</entry><entry>g_settings_set_boolean()</entry></row>
77               <row><entry>gconf_client_get_int()</entry><entry>g_settings_get_int()</entry></row>
78               <row><entry>gconf_client_set_int()</entry><entry>g_settings_set_int()</entry></row>
79               <row><entry>gconf_client_get_float()</entry><entry>g_settings_get_double()</entry></row>
80               <row><entry>gconf_client_set_float()</entry><entry>g_settings_set_double()</entry></row>
81               <row><entry>gconf_client_get_string()</entry><entry>g_settings_get_string()</entry></row>
82               <row><entry>gconf_client_set_string()</entry><entry>g_settings_set_string()</entry></row>
83               <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>
84               <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>
85               <row><entry>gconf_entry_get_is_writable()</entry><entry>g_settings_is_writable()</entry></row>
86               <row><entry>gconf_client_notify_add()</entry><entry>not required, the #GSettings::changed signal is emitted automatically</entry></row>
87               <row><entry>gconf_client_add_dir()</entry><entry>not required, each GSettings instance automatically watches all keys in its path</entry></row>
88               <row><entry>#GConfChangeSet</entry><entry>g_settings_delay(), g_settings_apply()</entry></row>
89               <row><entry>gconf_client_get_default_from_schema()</entry><entry>no equivalent, applications are expected to know their schema</entry></row>
90               <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>
91               <row><entry>gconf_client_get_without_default()</entry><entry>no equivalent</entry></row>
92               <row><entry>gconf_bridge_bind_property()</entry><entry>g_settings_bind()</entry></row>
93               <row><entry>gconf_bridge_bind_property_full()</entry><entry>g_settings_bind_with_mapping()</entry></row>
94             </tbody>
95           </tgroup>
96         </table>
97       </para>
98       <para>
99         GConfBridge was a third-party library that used GConf to bind an object property
100         to a particular configuration key. GSettings offers this service itself.
101       </para>
102       <para>
103         There is a pattern that is sometimes used for GConf, where a setting can have
104         explicit 'value A', explicit 'value B' or 'use the system default'. With GConf,
105         'use the system default' is sometimes implemented by unsetting the user value.
106       </para>
107       <para>
108         This is not possible in GSettings, since it does not have API to determine if a value
109         is the default and does not let you unset values. The recommended way (and much
110         clearer) way in which this can be implemented in GSettings is to have a separate
111         'use-system-default' boolean setting.
112       </para>
113     </section>
114
115     <section>
116       <title>Change notification</title>
117
118       <para>
119         GConf requires you to call gconf_client_add_dir() and
120         gconf_client_notify_add() to get change notification. With
121         GSettings, this is not necessary; signals get emitted automatically
122         for every change.
123       </para>
124       <para>
125         The #GSettings::changed signal is emitted for each changed key.
126         There is also a #GSettings::change-event signal that you can handle
127         if you need to see groups of keys that get changed at the same time.
128       </para>
129       <para>
130         GSettings also notifies you about changes in writability of keys,
131         with the #GSettings::writable-changed signal (and the
132         #GSettings::writable-change-event signal).
133       </para>
134     </section>
135
136     <section><title>Change sets</title>
137       <para>
138         GConf has a a concept of a set of changes which can be applied or reverted
139         at once: #GConfChangeSet (GConf doesn't actually apply changes atomically,
140         which is one of its shortcomings).
141       </para>
142       <para>
143         Instead of a separate object to represent a change set, GSettings has a
144         'delayed-apply' mode, which can be turned on for a GSettings object by
145         calling g_settings_delay(). In this mode, changes done to the GSettings
146         object are not applied - they are still visible when calling g_settings_get()
147         <emphasis>on the same object</emphasis>, but not to other GSettings instances
148         or even other processes.
149       </para>
150       <para>
151         To apply the pending changes all at once (GSettings <emphasis>does</emphasis>
152         atomicity here), call g_settings_apply(). To revert the pending changes,
153         call g_settings_revert() or just drop the reference to the #GSettings object.
154       </para>
155     </section>
156
157     <section>
158       <title>Schema conversion</title>
159
160       <para>
161         If you are porting your application from GConf, most likely you already
162         have a GConf schema. GConf comes with a commandline tool
163         gsettings-schema-convert that can help with the task of converting
164         a GConf schema into an equivalent GSettings schema. The tool is not
165         perfect and may need assistence in some cases.
166       </para>
167       <example><title>An example for using gsettings-schema-convert</title>
168         <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:
169         <programlisting>
170 <![CDATA[
171 <?xml version="1.0"?>
172 <gconfschemafile>
173     <schemalist>
174         <schema>
175             <key>/schemas/desktop/gnome/font_rendering/dpi</key>
176             <applyto>/desktop/gnome/font_rendering/dpi</applyto>
177             <owner>gnome</owner>
178             <type>int</type>
179             <default>96</default>
180             <locale name="C">
181                 <short>DPI</short>
182                 <long>The resolution used for converting font sizes to pixel sizes, in dots per inch.</long>
183             </locale>
184         </schema>
185     </schemalist>
186 </gconfschemafile>
187 ]]>
188 </programlisting>
189 produces a <filename>org.gnome.font-rendering.gschema.xml</filename> file with the following content:
190 <programlisting>
191 <![CDATA[
192 <schemalist>
193   <schema id="org.gnome.font-rendering" path="/desktop/gnome/font_rendering/">
194     <key name="dpi" type="i">
195       <default>96</default>
196       <summary>DPI</summary>
197       <description>The resolution used for converting font sizes to pixel sizes, in dots per inch.</description>
198     </key>
199   </schema>
200 </schemalist>
201 ]]>
202 </programlisting>
203 </para>
204       </example>
205
206       <para>
207        GSettings schemas are identified at runtime by their id (as specified
208        in the XML source file). It is recommended to use a dotted name as schema
209        id, similar in style to a D-Bus bus name, e.g. "org.gnome.SessionManager".
210        In cases where the settings are general and not specific to one application,
211        the id should not use StudlyCaps, e.g. "org.gnome.font-rendering".
212        The filename used for the XML schema source is immaterial, but
213        schema compiler expects the files to have the extension
214        <filename>.gschema.xml</filename>. It is recommended to simply
215        use the schema id as the filename, followed by this extension,
216        e.g. <filename>org.gnome.SessionManager.gschema.xml</filename>.
217       </para>
218
219       <para>
220         The XML source file for your GSettings schema needs to get installed
221         into <filename>$datadir/glib-2.0/schemas</filename>, and needs to be
222         compiled into a binary form. At runtime, GSettings looks for compiled
223         schemas in the <filename>glib-2.0/schemas</filename> subdirectories
224         of all <envar>XDG_DATA_DIRS</envar> directories, so if you install
225         your schema in a different location, you need to set the
226         <envar>XDG_DATA_DIRS</envar> environment variable appropriately.
227       </para>
228       <para>
229         Schemas are compiled into binary form by the
230         <link linkend="glib-compile-schemas">glib-compile-schemas</link> utility.
231         GIO provides a <literal>glib_compile_schemas</literal>
232         variable for the schema compiler.
233       </para>
234       <para>
235         You can ignore all of this by using the provided m4 macros.  To
236         do this, add to your <filename>configure.ac</filename>:
237 <programlisting>
238 GLIB_GSETTINGS
239 </programlisting>
240         The corresponding <filename>Makefile.am</filename> fragment looks like
241         this:
242 <programlisting>
243 # gsettings_SCHEMAS is a list of all the schemas you want to install
244 gsettings_SCHEMAS = my.app.gschema.xml
245
246 # include the appropriate makefile rules for schema handling
247 @GSETTINGS_RULES@
248 </programlisting>
249       </para>
250
251       <para>
252         This is not sufficient on its own.  You need to mention what the source
253         of the <filename>my.app.gschema.xml</filename> file is.  If the schema
254         file is distributed directly with your project's tarball then a mention
255         in <varname>EXTRA_DIST</varname> is appropriate.  If the schema file is
256         generated from another source then you will need the appropriate rule
257         for that, plus probably an item in <varname>EXTRA_DIST</varname> for the
258         source files used by that rule.
259       </para>
260
261       <para>
262         One possible pitfall in doing schema conversion is that the default
263         values in GSettings schemas are parsed by the #GVariant parser.
264         This means that strings need to include quotes in the XML.  Also note
265         that the types are now specified as #GVariant type strings.
266         <programlisting>
267 <![CDATA[
268 <type>string</type>
269 <default>rgb</default>
270 ]]>
271         </programlisting>
272         becomes
273         <programlisting>
274 <![CDATA[
275 <key name="rgba-order" type="s">
276   <default>'rgb'</default> <!-- note quotes -->
277 </key>
278 ]]>
279         </programlisting>
280       </para>
281       <para>
282         Another possible complication is that GConf specifies full paths
283         for each key, while a GSettings schema has a 'path' attribute that
284         contains the prefix for all the keys in the schema, and individual
285         keys just have a simple name. So
286         <programlisting>
287 <![CDATA[
288 <key>/schemas/desktop/gnome/font_rendering/antialiasing</key>
289 ]]>
290         </programlisting>
291         becomes
292         <programlisting>
293 <![CDATA[
294 <schema id="org.gnome.font" path="/desktop/gnome/font_rendering/">
295   <key name="antialiasing" type="s">
296 ]]>
297         </programlisting>
298       </para>
299       <para>
300         Default values can be localized in both GConf and GSettings schemas,
301         but GSettings uses gettext for the localization. You can specify
302         the gettext domain to use in the <tag class="attribute">gettext-domain</tag>
303         attribute. Therefore, when converting localized defaults in GConf,
304         <programlisting>
305 <![CDATA[
306 <key>/schemas/apps/my_app/font_size</key>
307   <locale name="C">
308     <default>18</default>
309   </locale>
310   <locale name="be">
311     <default>24</default>
312   </locale>
313 </key>
314 ]]>
315         </programlisting>
316         becomes
317         <programlisting>
318 <![CDATA[
319 <schema id="..." gettext-domain="your-domain">
320  ...
321 <key name="font-size" type="i">
322   <default l10n="messages" context="font_size">18</default>
323 </key>
324 ]]>
325         </programlisting>
326       </para>
327       <para>
328         GSettings uses gettext for translation of default values.
329         The string that is translated is exactly the string that appears
330         inside of the <tag class='starttag'>default</tag> element.  This
331         includes the quotation marks that appear around strings.
332         Default values must be marked with the <varname>l10n</varname>
333         attribute in the <tag class='starttag'>default</tag> tag, which
334         should be set as equal to <literal>'messages'</literal> or
335         <literal>'time'</literal> depending on the desired category.  An
336         optional translation context can also be specified with the
337         <varname>context</varname> attribute, as in the example.  This
338         is usually recommended, since the string "<literal>18</literal>"
339         is not particularly easy to translate without context.  The
340         translated version of the default value should be stored in the
341         specified <varname>gettext-domain</varname>.  Care must be taken
342         during translation to ensure that all translated values remain
343         syntactically valid; mistakes here will cause runtime errors.
344       </para>
345       <para>
346         GSettings schemas have optional <tag class="starttag">summary</tag> and
347         <tag class="starttag">description</tag> elements for each key which
348         correspond to the <tag class="starttag">short</tag> and
349         <tag class="starttag">long</tag> elements in the GConf schema and
350         will be used in similar ways by a future gsettings-editor, so you
351         should use the same conventions for them: The summary is just a short
352         label with no punctuation, the description can be one or more complete
353         sentences. If multiple paragraphs are desired for the description, the
354         paragraphs should be separated by a completely empty line.
355       </para>
356       <para>
357         Translations for these strings will also be handled
358         via gettext, so you should arrange for these strings to be
359         extracted into your gettext catalog. One way to do that is to use
360         intltool. Since intltool 0.50.1, schema files are
361         supported, so all you have to do is to add your .gschema.xml
362         files to <filename>POTFILES.in</filename> with a line like
363         <programlisting>
364         [type: gettext/gsettings]data/org.foo.MyApp.gschema.xml
365         </programlisting>
366       </para>
367       <para>
368         GSettings is a bit more restrictive about key names than GConf. Key
369         names in GSettings can be at most 32 characters long, and must only
370         consist of lowercase characters, numbers and dashes, with no
371         consecutive dashes. The first character must not be a number or dash,
372         and the last character cannot be '-'.
373       </para>
374       <para>
375         If you are using the GConf backend for GSettings during the
376         transition, you may want to keep your key names the same they
377         were in GConf, so that existing settings in the users GConf
378         database are preserved. You can achieve this by using the
379         <option>--allow-any-name</option> with the
380         <link linkend="glib-compile-schemas">glib-compile-schemas</link> schema
381         compiler. Note that this option is only meant
382         to ease the process of porting your application, allowing parts
383         of your application to continue to access GConf and parts to use
384         GSettings. By the time you have finished porting your application
385         you must ensure that all key names are valid.
386       </para>
387     </section>
388
389     <section><title>Data conversion</title>
390       <para>
391         GConf comes with a GSettings backend that can be used to
392         facility the transition to the GSettings API until you are
393         ready to make the jump to a different backend (most likely
394         dconf). To use it, you need to set the <envar>GSETTINGS_BACKEND</envar>
395         to 'gconf', e.g. by using
396 <programlisting>
397   g_setenv ("GSETTINGS_BACKEND", "gconf", TRUE);
398 </programlisting>
399         early on in your program. Note that this backend is meant purely
400         as a transition tool, and should not be used in production.
401       </para>
402       <para>
403         GConf also comes with a utility called
404         <command>gsettings-data-convert</command>, which is designed to help
405         with the task of migrating user settings from GConf into another
406         GSettings backend. It can be run manually, but it is designed to be
407         executed automatically, every time a user logs in. It keeps track of
408         the data migrations that it has already done, and it is harmless to
409         run it more than once.
410       </para>
411       <para>
412         To make use of this utility, you must install a keyfile in the
413         directory <filename>/usr/share/GConf/gsettings</filename> which
414         lists the GSettings keys and GConf paths to map to each other, for
415         each schema that you want to migrate user data for.
416       </para>
417       <para>
418         Here is an example:
419         <programlisting>
420 <![CDATA[
421 [org.gnome.fonts]
422 antialiasing = /desktop/gnome/font_rendering/antialiasing
423 dpi = /desktop/gnome/font_rendering/dpi
424 hinting = /desktop/gnome/font_rendering/hinting
425 rgba-order = /desktop/gnome/font_rendering/rgba_order
426
427 [apps.myapp:/path/to/myapps/]
428 some-odd-key1 = /apps/myapp/some_ODD-key1
429 ]]>
430         </programlisting>
431         The last key demonstrates that it may be necessary to modify the key
432         name to comply with stricter GSettings key name rules. Of course,
433         that means your application must use the new key names when looking
434         up settings in GSettings.
435       </para>
436       <para>
437         The last group in the example also shows how to handle the case
438         of 'relocatable' schemas, which don't have a fixed path. You can
439         specify the path to use in the group name, separated by a colon.
440       </para>
441       <para>
442         There are some limitations: <command>gsettings-data-convert</command>
443         does not do any transformation of the values. And it does not handle
444         complex GConf types other than lists of strings or integers.
445       </para>
446       <para>
447         Don't forget to require GConf 2.31.1 or newer in your configure
448         script if you are making use of the GConf backend or the conversion
449         utility.
450       </para>
451
452       <para>
453         If, as an application developer, you are interested in manually
454         ensuring that <command>gsettings-data-convert</command> has been
455         invoked (for example, to deal with the case where the user is
456         logged in during a distribution upgrade or for non-XDG desktop
457         environments which do not run the command as an autostart) you
458         may invoke it manually during your program initialisation.  This
459         is not recommended for all application authors -- it is your
460         choice if this use case concerns you enough.
461       </para>
462       <para>
463         Internally, <command>gsettings-data-convert</command> uses a
464         keyfile to track which settings have been migrated.  The
465         following code fragment will check that keyfile to see if your
466         data conversion script has been run yet and, if not, will
467         attempt to invoke the tool to run it.  You should adapt it to
468         your application as you see fit.
469       </para>
470       <para>
471         <programlisting>
472 <![CDATA[
473 static void
474 ensure_migrated (const gchar *name)
475 {
476   gboolean needed = TRUE;
477   GKeyFile *kf;
478   gchar **list;
479   gsize i, n;
480
481   kf = g_key_file_new ();
482
483   g_key_file_load_from_data_dirs (kf, "gsettings-data-convert",
484                                   NULL, G_KEY_FILE_NONE, NULL);
485   list = g_key_file_get_string_list (kf, "State", "converted", &n, NULL);
486
487   if (list)
488     {
489       for (i = 0; i < n; i++)
490         if (strcmp (list[i], name) == 0)
491           {
492             needed = FALSE;
493             break;
494           }
495
496       g_strfreev (list);
497     }
498
499   g_key_file_free (kf);
500
501   if (needed)
502     g_spawn_command_line_sync ("gsettings-data-convert",
503                                NULL, NULL, NULL, NULL);
504 }
505
506 ]]>
507         </programlisting>
508       </para>
509       <para>
510         Although there is the possibility that the
511         <command>gsettings-data-convert</command> script will end up
512         running multiple times concurrently with this approach, it is
513         believed that this is safe.
514       </para>
515     </section>
516   </chapter>