gsettings命令使用简介

    xiaoxiao2021-12-01  27

    1.gsettings创建项

    应用程序可以使用gsettings来保存配置信息,可以通过代码在程序中进行设置、修改gsettings的已有的项,但是不能通过程序代码创建新的gsettings项,gsettings的项的在一个叫做schema的规范文件中创建,schema文档其实是一个规范的xml文档。其实例如下:

    <?xml version="1.0" encoding="UTF-8"?> <schemalist> <schema path="/org/lili/test/app/testgsettings/" id="org.lili.test.app.testgsettings"> <key type="b" name="enabled"> <default>true</default> <summary>Enable Testgsetting</summary> <description>Globally enable or disable the TestApp,Setting it to "false" will disable TestApp.</description> </key> <key type="i" name="brightness"> <default>30</default> <summary>The brightness of the screen</summary> <description>This is the laptop panel screen brightness used when the session is idle.</description> </key> </schema> </schemalist>

    对于schema文件的注意事项:

    path两头必须都有/,否则会验证失败schema文件的扩展名必须是gschema.xml,否则这个规则文件将无法被正确编译安装,最终无法被gsettings使用

    如果想让gsettings能被dconf-editor所读取,则必须指定path属性

    2.schema文件的编译安装

    schema文件并不能直接被gsettings使用,需要用glib-compile-schemas编译器将schema文件编译为二进制文件才能被gsettings所使用。GSettings 会读取 XDG_DATA_DIRS 下的 glib-2.0/schemas路径,所以通常将schema文件放到环境变量XDG_DATA_DIRS/glib-2.0/schemas/路径下,一般为/usr/share/glib-2.0/schemas 和 /usr/local/share/glib-2.0/schemas。例如,我们将schema文件拷贝到/usr/share/glib-2.0/schemas 路径下,我们可以在命令行运行以下命令编译刷新系统的gsettings:

    #glib-compile-schemas /usr/share/glib-2.0/schemas

    执行完上述命令后就可以发现自己定义的schema已经生效:

    3.常用的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 set SCHEMA KEY VALUE 设置指定schema的指定项KEY的值为VALUE #gsettings reset SCHEMA KEY 恢复指定schema的指定项KEY的值为默认值 #gsettings reset-recursively SCHEMA 恢复指定schema的所有key的值为默认值 #gsettings list-recursively [SCHEMA]如果有SCHEMA参数,则递归显示指定schema的所有项(key)和值(value),如果没有SCHEMA参数,则递归显示所有schema的所有项(key)和值(value)

    4.使用实例

    4.1显示系统都安装了哪些不可重定位的schema

    4.2查看org.cinnamon.settings-daemon.plugs都有哪些子schema

    4.3查看org.cinnamon.settings-daemon.plugs.power的schema下都有哪些项(key)

    4.4查看org.cinnamon.settings-daemon.plugs.power的schema下所有项的取值

    4.5查看org.cinnamon.settings-daemon.plugs.power的schema下的项button-hibernate的值

    4.6查看org.cinnamon.settings-daemon.plugs.power的schema下的项button-hibernate的取值范围

    4.7修改设置org.cinnamon.settings-daemon.plugs.power的schema下的项button-hibernate的值为shutdown

    4.8恢复org.cinnamon.settings-daemon.plugs.power的schema下的项button-hibernate的值为默认值

    4.9修改org.cinnamon.settings-daemon.plugs.power的schema下的多项值后,恢复整个schema的所有项为默认值

    4.9.1修改前配置截图

    4.9.2修改后配置截图

    4.9.3恢复后的配置截图

    感谢:本文前半部分参考了http://blog.csdn.net/zhgn2/article/details/8834339

    转载请注明原文地址: https://ju.6miu.com/read-679315.html

    最新回复(0)