GSettings和GSettings-XML的整理记录

1 篇文章 0 订阅

@TOC

概览

编译
组成
创建实例时查找schema
获取keys信息
提交写入
mmap
检索反馈
修改请求
检索反馈
修改请求
检索反馈
修改请求
修改请求
直接修改
直接读取
*.gschema.xml
gschemas.compiled
dconf database
GSettings接口类
DBUS session
dconf-editor
gsetting
APPLICATION
dconf
  • KEY修改:所有经GSettings接口的修改请求都提交给DBus会话服务,DBus会话服务操作后sync到碰盘的数据库文件中。
  • KEY检索:on-disk上的dconf database(默认~/.config/dconf/usr)直接以只读的形式映射到当前进程的中,因此读操作的非常快。
  • DConf database:由gschemas.compiled文件描述构成的hashTable的二进制的dconf数据库(注:这里也可是其它格式的数据库),用于快速检索操作。
  • GSettings:只能修改和检索已有键值key,不能增删。因为数据库的创建是由gschemas.compiled文件形成的。
  • dconf工具:可对数据库直接读写的。
  • xml:修改过后需要重新编译生成gschemas.compiled才能生效,如果是新增key,可以直接运行看效果,如果是修改默认(缺省)值,因数据库中有值了,需删除数据库文件(eg:~/.config/dconf/usr)才能取到默认(缺省)值。

XML文件说明

<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
	<schema path="/io/elementary/switchboard/saved-state/" id="io.elementary.switchboard.saved-state">
    <key name="window-maximized" type="b">
      <default>false</default>
      <summary>Whether the window was maximized on last run</summary>
      <description>Whether the window was maximized on last run</description>
    </key>
    <key name="window-position" type="(ii)">
      <default>(-1, -1)</default>
      <summary>Window position</summary>
      <description>Most recent window position (x, y)</description>
    </key>
    <key name="window-size" type="(ii)">
      <default>(1024, 720)</default>
      <summary>Most recent window size</summary>
      <description>Most recent window size (width, height)</description>
    </key>
	</schema>
	<schema path="/io/elementary/switchboard/preferences/" id="io.elementary.switchboard.preferences">
		<key name="mapping-override" type="a{s(ss)}">
			<default>{}</default>
			<summary>Override (and extends) the default mapping with plugs</summary>
			<description>Contains a dictionary of path and two strings: the plug name and an argument. For example {'wallpaper': ('foo-desktop','wallpaper'), 'display': ('bar-display','')}</description>
		</key>
	</schema>
</schemalist>
  • 主要构成元素:

    • 一个xml文件可以有多个 每个必须指定path和id, id用作GSettings的参数指向一个schema;
    • 每个由一个以上的组成。
    • 一个 要指定name和类型。对应子项有缺省值, 摘要,描述
  • 注意点:

    • id与path同名,只是一个用“. ”,一个用“/”分隔。
    • 在用命令时,dconf和dconf-editor都是以path来检索,gsettings以id来检索操作
    • path必须以"/"开始和结束, 不能以“/apps/”, “/desktop/” 或 “/system/”开始作为命名起始
    • Key名只能取小写字母,数字和"-" , 但不能使用连续的"-"命名
    • key的只需填写默认值(缺省值),GSettings从Dconf database里检索到key如果没有值,则使用该值

    细则参考gio settings

GSettings

Gio.Settings VS Gtk.Settings
  1. Gio.Settings /GLib.Settings

The GSettings class provides a convenient API for storing and retrieving application settings.

When creating a GSettings instance, you have to specify a schema that describes the keys in your settings and their types and default values, as well as some other information.

创建时必须指定一个schema ID定位,去gschemas.compiled查找如何没有对应schema则失败。

  1. Gtk.Settings

GtkSettings provide a mechanism to share global settings between applications.

在X窗口系统中,它由XSettings manager实现,做为窗口环境的一部分,用于用户更改设置。每个screen只有一个Gtk.Settings实例。

本文所说的GSettings是指第一种

数据库文件--dconf database

dconf
  1. dconf is a simple key/value storage system that is heavily optimised for reading
  2. All preferences are stored in a single large binary file.
  3. dconf has a partial client/server architecture. It uses D-Bus. The server is only involved in writes
  4. Reads are performed by direct access (via mmap) to the on-disk database which is essentially a hashtable 读时直接从映射在内存中的hash表中读值,无需任何系统调用
  5. /etc/dconf/profile的配置文件中每一行指明了一个dconf database的名字

详情参见man 7 dconf

ann@B460M-d5c6e3b7:~$ cat /etc/dconf/profile/ibus 
user-db:user
system-db:ibus

ann@B460M-d5c6e3b7:~$ file /etc/dconf/db/ibus
/etc/dconf/db/ibus: GVariant Database file, version 0
ann@B460M-d5c6e3b7:~$ file ~/.config/dconf/user 
/home/ann/.config/dconf/user: GVariant Database file, version 0
存放的缓存位置

见上小节,一般放在/etc/dconf/profile, 用户的数据库位置如下

~/.config/dconf/user

另记下,要是找不到可用此方法gsettings cache文件所在位置的查找方法,lsof列出某个进程的打开文件。


xml 编译

glib-compile-schemas

将GSettings XML 模式文件编译成二进制格式的文件供GSettings快速检索

ann@B460M-d5c6e3b7:usr$ sudo glib-compile-schemas /usr/share/glib-2.0/schemas/
ann@B460M-d5c6e3b7:usr$ file /usr/share/glib-2.0/schemas/gschemas.compiled 
/usr/share/glib-2.0/schemas/gschemas.compiled: GVariant Database file, version 0

将 DIRECTORY 中的所有 GSettings XML schema文件编译成一个名为gschemas.compiled的二进制文件,该文件供GSettings 使用 。XML 架构文件必须具有.gschema.xml的文件扩展名。
在运行时,GSettings 在环境变量XDG_DATA_DIRS 指定的所有目录的子目录 glib-2.0/schemas 中搜索所有schemas。 schemas文件的通常安装位置是 /usr/share/glib-2.0/schemas.

手动修改过的XML文件重新编译后,需删除原数据库才会重新加载默认配置值,新增项直接使用默认值,不用删。


查看和修改工具

  1. dconf-editor--图形界面,以SCHEMA PATH为查找对象

一个有图形界面的程序,用来编辑其它应用程序调用glib库中的gsettings函数修改或存储在dconf数据库中的设置项

打开后搜索 /io/elementary/switchboard/saved-state/即可
在这里插入图片描述

  1. gsettings --以ID为操作对象

gsettings offers a simple commandline interface to GSettings. It lets you get, set or monitor an individual key for changes.
Note that gsettings needs a D-Bus session bus connection to write changes to the dconf database

  • gsettings list-schemas : 列出所有schemas
  • gsettings get/set : 获取设置值
  • gsettings list-keys : 列出schemas的所有KEY
ann@B460M-d5c6e3b7:icons$ gsettings list-keys io.elementary.switchboard.saved-state
window-size
window-position
window-maximized
ann@B460M-d5c6e3b7:icons$ gsettings get  io.elementary.switchboard.saved-state window-size
(1024, 720)
ann@B460M-d5c6e3b7:~$ gsettings list-recursively io.elementary.switchboard.saved-state
io.elementary.switchboard.saved-state window-size (890, 563)
io.elementary.switchboard.saved-state window-position (388, 171)
io.elementary.switchboard.saved-state window-maximized false
  1. dconf --以PATH为操作对象

Simple tool for manipulating a dconf database, This tool operates on dconf directly

ann@B460M-d5c6e3b7:icons$ dconf list /io/elementary/switchboard/saved-state/
window-maximized
window-position
window-size
ann@B460M-d5c6e3b7:icons$ dconf read /io/elementary/switchboard/saved-state/window-size 
(1024, 720)
ann@B460M-d5c6e3b7:~$ dconf watch /io/elementary/switchboard/saved-state/window-size 
/io/elementary/switchboard/saved-state/window-size
 (905, 620)

/io/elementary/switchboard/saved-state/window-size
 (910, 564)

/io/elementary/switchboard/saved-state/window-size
 (890, 563)


Meson.build

指定schemas文件的安装目录

schemas_dir = get_option('prefix') / get_option('datadir') / 'glib-2.0' / 'schemas'
settings_schemas = [ 'org.gtk.Test' ]

install_data(settings_schemas, install_dir: schemas_dir)

在当前目录下编译schemas文件,只能临时测试

gnome = import('gnome')
gnome.compile_schemas(depend_files: files(settings_schemas))

如果要在安装过程中时编译schemas文件,需要加入一个post-install 脚本如下:

meson.add_install_script('glib-compile-schemas', schemas_dir)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值