可以将应用程序的配置信息放置在 schema 文件中,并使用 gsettings 命令查询/设置配置信息。
获取配置信息相关命令:
# gsettings list-schemas 显示系统已安装的不可重定位的schema
# gsettings list-relocatable-schemas 显示已安装的可重定位的schema
# gsettings list-children SCHEMA 显示指定schema的children,其中SCHEMA指xml文件中schema的id属性值,例如实例中的"org.lili.test.app.testgsettings"
# gsettings list-keys SCHEMA 显示指定schema的所有项(key)
# gsettings range SCHEMA KEY 查询指定schema的指定项KEY的有效取值范围
# gsettings get SCHEMA KEY 显示指定schema的指定项KEY的值
# gsettings list-recursively [SCHEMA]如果有SCHEMA参数,则递归显示指定schema的所有项(key)和值(value),如果没有SCHEMA参数,则递归显示所有schema的所有项(key)和值(value)
设置配置信息相关命令:
# gsettings set SCHEMA KEY VALUE 设置指定schema的指定项KEY的值为VALUE
# gsettings reset SCHEMA KEY 恢复指定schema的指定项KEY的值为默认值
# gsettings reset-recursively SCHEMA 恢复指定schema的所有key的值为默认值
schema 文件存放路径:
# echo $XDG_DATA_DIRS
/usr/local/share/:/usr/share/
# ls /usr/share/glib-2.0/schemas/
...
org.gnome.anjuta.cvs.gschema.xml
org.gnome.anjuta.document-manager.gschema.xml
org.gnome.anjuta.file-manager.gschema.xml
org.gnome.anjuta.gschema.xml
org.gnome.anjuta.plugins.build.gschema.xml
...
schema 文件特点:
1、文件扩展名为 gschema.xml ;
2、schema 文件范例:
<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="org.gnome.Weather">
<schema id="org.gnome.Weather" path="/org/gnome/Weather/">
<key name="locations" type="av">
<default>[]</default>
<summary>Configured cities to show weather for</summary>
<description>
The locations shown in the world view of gnome-weather. Each value is a
GVariant returned by gweather_location_serialize().
</description>
</key>
<key name="automatic-location" type="b">
<default>true</default>
<summary>Automatic location</summary>
<description>
The automatic location is the value of automatic-location switch which decides whether
to fetch current location or not.
</description>
</key>
</schema>
</schemalist>
3、schema 文件需要编译成二进制文件才能被 gsettings 使用,编译命令如下:
# glib-compile-schemas /usr/share/glib-2.0/schemas