Merge remote branch 'gvdb/master'
[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/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         <link linkend="gsettings-schema-convert">gsettings-schema-convert</link>
164         that can help with the task of converting a GConf schema into
165         an equivalent GSettings schema. The tool is not perfect and
166         may need assistence in some cases.
167       </para>
168       <example><title>An example for using gsettings-schema-convert</title>
169         <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         <programlisting>
171 <![CDATA[
172 <?xml version="1.0"?>
173 <gconfschemafile>
174     <schemalist>
175         <schema>
176             <key>/schemas/desktop/gnome/font_rendering/dpi</key>
177             <applyto>/desktop/gnome/font_rendering/dpi</applyto>
178             <owner>gnome</owner>
179             <type>int</type>
180             <default>96</default>
181             <locale name="C">
182                 <short>DPI</short>
183                 <long>The resolution used for converting font sizes to pixel sizes, in dots per inch.</long>
184             </locale>
185         </schema>
186     </schemalist>
187 </gconfschemafile>
188 ]]>
189 </programlisting>
190 produces a <filename>org.gnome.font-rendering.gschema.xml</filename> file with the following content:
191 <programlisting>
192 <![CDATA[
193 <schemalist>
194   <schema id="org.gnome.font-rendering" path="/desktop/gnome/font_rendering/">
195     <key name="dpi" type="i">
196       <default>96</default>
197       <summary>DPI</summary>
198       <description>The resolution used for converting font sizes to pixel sizes, in dots per inch.</description>
199     </key>
200   </schema>
201 </schemalist>
202 ]]>
203 </programlisting>
204 </para>
205       </example>
206
207       <para>
208         GSettings schemas are identified at runtime by their id (as specified
209         in the XML source file). It is recommended to use a dotted name as schema
210         id, similar in style to a DBus bus name, 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.font-rendering.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. For that, you use <tag class="starttag">_summary</tag>
360         and <tag class="starttag">_description</tag> elements in a
361         .gschema.xml.in file and use
362         <literal>@<!-- -->INTLTOOL_XML_NOMERGE_RULE<!-- -->@</literal>
363         in your Makefile.am to produce the .gschema.xml file. The
364         <literal>NOMERGE</literal> part of the rule instructs intltool
365         to extract translatable strings, but not merge the translations
366         back into the generated xml file.
367       </para>
368       <para>
369         GSettings is a bit more restrictive about key names than GConf. Key
370         names in GSettings can be at most 32 characters long, and must only
371         consist of lowercase characters, numbers and dashes, with no
372         consecutive dashes. The first character must not be a number or dash,
373         and the last character cannot be '-'.
374       </para>
375       <para>
376         If you are using the GConf backend for GSettings during the
377         transition, you may want to keep your key names the same they
378         were in GConf, so that existing settings in the users GConf
379         database are preserved. You can achieve this by using the
380         <option>--allow-any-name</option> with the
381         <link linkend="glib-compile-schemas">glib-compile-schemas</link> schema
382         compiler. Note that this option is only meant
383         to ease the process of porting your application, allowing parts
384         of your application to continue to access GConf and parts to use
385         GSettings. By the time you have finished porting your application
386         you must ensure that all key names are valid.
387       </para>
388     </section>
389
390     <section><title>Data conversion</title>
391       <para>
392         GConf comes with a GSettings backend that can be used to
393         facility the transition to the GSettings API until you are
394         ready to make the jump to a different backend (most likely
395         dconf). To use it, you need to set the <envar>GSETTINGS_BACKEND</envar>
396         to 'gconf', e.g. by using
397 <programlisting>
398   g_setenv ("GSETTINGS_BACKEND", "gconf", TRUE);
399 </programlisting>
400         early on in your program. Note that this backend is meant purely
401         as a transition tool, and should not be used in production.
402       </para>
403       <para>
404         GConf also comes with a utility called
405         <command>gsettings-data-convert</command>, which is designed to help
406         with the task of migrating user settings from GConf into another
407         GSettings backend. It can be run manually, but it is designed to be
408         executed automatically, every time a user logs in. It keeps track of
409         the data migrations that it has already done, and it is harmless to
410         run it more than once.
411       </para>
412       <para>
413         To make use of this utility, you must install a keyfile in the
414         directory <filename>/usr/share/GConf/gsettings</filename> which
415         lists the GSettings keys and GConf paths to map to each other, for
416         each schema that you want to migrate user data for.
417       </para>
418       <para>
419         Here is an example:
420         <programlisting>
421 <![CDATA[
422 [org.gnome.fonts]
423 antialiasing = /desktop/gnome/font_rendering/antialiasing
424 dpi = /desktop/gnome/font_rendering/dpi
425 hinting = /desktop/gnome/font_rendering/hinting
426 rgba-order = /desktop/gnome/font_rendering/rgba_order
427
428 [apps.myapp:/path/to/myapps/]
429 some-odd-key1 = /apps/myapp/some_ODD-key1
430 ]]>
431         </programlisting>
432         The last key demonstrates that it may be necessary to modify the key
433         name to comply with stricter GSettings key name rules. Of course,
434         that means your application must use the new key names when looking
435         up settings in GSettings.
436       </para>
437       <para>
438         The last group in the example also shows how to handle the case
439         of 'relocatable' schemas, which don't have a fixed path. You can
440         specify the path to use in the group name, separated by a colon.
441       </para>
442       <para>
443         There are some limitations: <command>gsettings-data-convert</command>
444         does not do any transformation of the values. And it does not handle
445         complex GConf types other than lists of strings or integers.
446       </para>
447       <para>
448         Don't forget to require GConf 2.31.1 or newer in your configure
449         script if you are making use of the GConf backend or the conversion
450         utility.
451       </para>
452     </section>
453   </chapter>