GApplication: parse command line options
[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.
19       </para>
20     </section>
21
22     <section>
23       <title>Conceptual differences</title>
24
25       <para>
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.
30       </para>
31       <para>
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'.
42       </para>
43       <para>
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.
53       </para>
54     </section>
55
56     <section>
57       <title>GConfClient (and GConfBridge) API conversion</title>
58
59       <para>
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,
63         many do:
64         <table id="gconf-client-vs-gsettings">
65           <tgroup cols="2">
66             <thead>
67               <row><entry>GConfClient</entry><entry>GSettings</entry></row>
68             </thead>
69             <tbody>
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>
93             </tbody>
94           </tgroup>
95         </table>
96       </para>
97       <para>
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.
100       </para>
101       <para>
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.
105       </para>
106       <para>
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.
111       </para>
112     </section>
113
114     <section>
115       <title>Change notification</title>
116
117       <para>
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
121         for every change.
122       </para>
123       <para>
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.
127       </para>
128       <para>
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).
132       </para>
133     </section>
134
135     <section><title>Change sets</title>
136       <para>
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).
140       </para>
141       <para>
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.
148       </para>
149       <para>
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.
153       </para>
154     </section>
155
156     <section>
157       <title>Schema conversion</title>
158
159       <para>
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.
165       </para>
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:
168         <programlisting>
169 <![CDATA[
170 <?xml version="1.0"?>
171 <gconfschemafile>
172     <schemalist>
173         <schema>
174             <key>/schemas/desktop/gnome/font_rendering/dpi</key>
175             <applyto>/desktop/gnome/font_rendering/dpi</applyto>
176             <owner>gnome</owner>
177             <type>int</type>
178             <default>96</default>
179             <locale name="C">
180                 <short>DPI</short>
181                 <long>The resolution used for converting font sizes to pixel sizes, in dots per inch.</long>
182             </locale>
183         </schema>
184     </schemalist>
185 </gconfschemafile>
186 ]]>
187 </programlisting>
188 produces a <filename>org.gnome.font-rendering.gschema.xml</filename> file with the following content:
189 <programlisting>
190 <![CDATA[
191 <schemalist>
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>
197     </key>
198   </schema>
199 </schemalist>
200 ]]>
201 </programlisting>
202 </para>
203       </example>
204
205       <para>
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>.
216       </para>
217
218       <para>
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.
226       </para>
227       <para>
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.
232       </para>
233       <para>
234         You can ignore all of this by using the provided m4 macros.  To
235         do this, add to your <filename>configure.ac</filename>:
236 <programlisting>
237 GLIB_GSETTINGS
238 </programlisting>
239         The corresponding <filename>Makefile.am</filename> fragment looks like
240         this:
241 <programlisting>
242 # gsettings_SCHEMAS is a list of all the schemas you want to install
243 gsettings_SCHEMAS = my.app.gschema.xml
244
245 # include the appropriate makefile rules for schema handling
246 @GSETTINGS_RULES@
247 </programlisting>
248       </para>
249
250       <para>
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.
258       </para>
259
260       <para>
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.
265         <programlisting>
266 <![CDATA[
267 <type>string</type>
268 <default>rgb</default>
269 ]]>
270         </programlisting>
271         becomes
272         <programlisting>
273 <![CDATA[
274 <key name="rgba-order" type="s">
275   <default>'rgb'</default> <!-- note quotes -->
276 </key>
277 ]]>
278         </programlisting>
279       </para>
280       <para>
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
285         <programlisting>
286 <![CDATA[
287 <key>/schemas/desktop/gnome/font_rendering/antialiasing</key>
288 ]]>
289         </programlisting>
290         becomes
291         <programlisting>
292 <![CDATA[
293 <schema id="org.gnome.font" path="/desktop/gnome/font_rendering/">
294   <key name="antialiasing" type="s">
295 ]]>
296         </programlisting>
297       </para>
298       <para>
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,
303         <programlisting>
304 <![CDATA[
305 <key>/schemas/apps/my_app/font_size</key>
306   <locale name="C">
307     <default>18</default>
308   </locale>
309   <locale name="be">
310     <default>24</default>
311   </locale>
312 </key>
313 ]]>
314         </programlisting>
315         becomes
316         <programlisting>
317 <![CDATA[
318 <schema id="..." gettext-domain="your-domain">
319  ...
320 <key name="font-size" type="i">
321   <default l10n="messages" context="font_size">18</default>
322 </key>
323 ]]>
324         </programlisting>
325       </para>
326       <para>
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.
343       </para>
344       <para>
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.
354       </para>
355       <para>
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
362         <programlisting>
363         [type: gettext/gsettings]data/org.foo.MyApp.gschema.xml
364         </programlisting>
365       </para>
366       <para>
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 '-'.
372       </para>
373       <para>
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.
385       </para>
386     </section>
387
388     <section><title>Data conversion</title>
389       <para>
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
395 <programlisting>
396   g_setenv ("GSETTINGS_BACKEND", "gconf", TRUE);
397 </programlisting>
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.
400       </para>
401       <para>
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.
409       </para>
410       <para>
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.
415       </para>
416       <para>
417         Here is an example:
418         <programlisting>
419 <![CDATA[
420 [org.gnome.fonts]
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
425
426 [apps.myapp:/path/to/myapps/]
427 some-odd-key1 = /apps/myapp/some_ODD-key1
428 ]]>
429         </programlisting>
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.
434       </para>
435       <para>
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.
439       </para>
440       <para>
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.
444       </para>
445       <para>
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
448         utility.
449       </para>
450
451       <para>
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.
460       </para>
461       <para>
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.
468       </para>
469       <para>
470         <programlisting>
471 <![CDATA[
472 static void
473 ensure_migrated (const gchar *name)
474 {
475   gboolean needed = TRUE;
476   GKeyFile *kf;
477   gchar **list;
478   gsize i, n;
479
480   kf = g_key_file_new ();
481
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);
485
486   if (list)
487     {
488       for (i = 0; i < n; i++)
489         if (strcmp (list[i], name) == 0)
490           {
491             needed = FALSE;
492             break;
493           }
494
495       g_strfreev (list);
496     }
497
498   g_key_file_free (kf);
499
500   if (needed)
501     g_spawn_command_line_sync ("gsettings-data-convert",
502                                NULL, NULL, NULL, NULL);
503 }
504
505 ]]>
506         </programlisting>
507       </para>
508       <para>
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.
513       </para>
514     </section>
515   </chapter>