Relocatable Schema

何謂「Relocatable Schema」,請參考「這一頁」的裡面的一段說明。

最簡單的辨別,就是「<schema>」沒有設定「path=""」,就會被視為「Relocatable Schema」。

範例檔

產生 gschema.xml

執行下面指令,產生「org.example.app-relocatable-schema.gschema.xml」

cat > org.example.app-relocatable-schema.gschema.xml <<EOF
<schemalist>
    <schema id="org.example.app-relocatable-schema" gettext-domain="app-app-relocatable-schema">

        <key name="greeting" type="s">
            <default l10n="messages">"Hello"</default>
            <summary>A greeting</summary>
            <description>
                Greeting!
            </description>
        </key>

        <key name="count" type="i">
            <default>99</default>
            <summary>counter</summary>
            <description>
                Counter
            </description>
        </key>

        <key name="allow" type="b">
            <default>false</default>
            <summary>Is allowed?</summary>
            <description>
                Allow?
            </description>
        </key>


    </schema>
</schemalist>

EOF

執行下面指令,觀看「org.example.app-relocatable-schema.gschema.xml」的內容

$ cat org.example.app-relocatable-schema.gschema.xml

可以看到「org.example.app-relocatable-schema.gschema.xml」的內容如下

<schemalist>
    <schema id="org.example.app-relocatable-schema" gettext-domain="app-app-relocatable-schema">

        <key name="greeting" type="s">
            <default l10n="messages">"Hello"</default>
            <summary>A greeting</summary>
            <description>
                Greeting!
            </description>
        </key>

        <key name="count" type="i">
            <default>99</default>
            <summary>counter</summary>
            <description>
                Counter
            </description>
        </key>

        <key name="allow" type="b">
            <default>false</default>
            <summary>Is allowed?</summary>
            <description>
                Allow?
            </description>
        </key>


    </schema>
</schemalist>

放至「/usr/share/glib-2.0/schemas/」

執行下面指令,將「org.example.app-relocatable-schema.gschema.xml」放到「/usr/share/glib-2.0/schemas/」

$ sudo cp org.example.app-relocatable-schema.gschema.xml /usr/share/glib-2.0/schemas/

編譯 schemas

執行下面指令,將「/usr/share/glib-2.0/schemas/」裡面的「.gschema.xml」編譯成「/usr/share/glib-2.0/schemas/gschemas.compiled」

$ sudo glib-compile-schemas /usr/share/glib-2.0/schemas/

gsettings 使用範例

執行

$ gsettings list-relocatable-schemas | grep app-relocatable-schema

顯示

org.example.app-relocatable-schema

執行

$ gsettings list-recursively | grep app-relocatable-schema

沒有顯示任何結果

執行

$ gsettings list-keys org.example.app-relocatable-schema

顯示

Schema 'org.example.app-relocatable-schema' is relocatable (path must be specified)

執行

$ gsettings list-keys org.example.app-relocatable-schema:/

顯示

allow
count
greeting

執行

$ gsettings get org.example.app-relocatable-schema:/ greeting

顯示

'Hello'

執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-1 greeting

顯示

Path must end with a slash (/)

執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-1/ greeting

顯示

'Hello'

執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-2/ greeting

顯示

'Hello'

執行

$ gsettings set org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-1/ greeting hi_1

再執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-1/ greeting

顯示

'hi_1'

執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-2/ greeting

顯示

'Hello'

簡單的說,也就是「Relocatable Schema」根據「Path」可以有不同的「副本」。

dconf 使用範例

執行

$ gsettings set org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-2/ greeting hi_2

再執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-2/ greeting

顯示

'hi_2'

執行

$ dconf dump / | grep org/example/app-relocatable-schema -A 2

顯示

[org/example/app-relocatable-schema/demo-2]
greeting='hi_2'

[org/example/app-relocatable-schema/demo-1]
greeting='hi_1'

執行

$ dconf list /org/example/app-relocatable-schema/

顯示

demo-2/
demo-1/

執行

$ dconf list /org/example/app-relocatable-schema/demo-1/

顯示

greeting

執行

$ dconf read /org/example/app-relocatable-schema/demo-1/greeting

顯示

'hi_1'

執行

$ dconf read /org/example/app-relocatable-schema/demo-2/greeting

顯示

'hi_2'

執行

$ dconf write /org/example/app-relocatable-schema/demo-1/count 88888

執行

$ dconf read /org/example/app-relocatable-schema/demo-1/count

顯示

88888

執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-1/ count

顯示

88888

執行

$ dconf read /org/example/app-relocatable-schema/demo-3/count

沒有顯示任何結果

執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-3/ count

顯示

99

執行

$ dconf write /org/example/app-relocatable-schema/demo-3/count 77777

執行

$ gsettings get org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-3/ count

顯示

77777

關於 ~/.config/dconf/user

執行

$ strings ~/.config/dconf/user | grep count

顯示

count
count
icount

執行

$ strings ~/.config/dconf/user | grep greeting

顯示

igreeting
sgreeting
igreeting
bgreeting
$ grep hi ~/.config/dconf/user

顯示

Binary file /home/sam/.config/dconf/user matches

執行

$ dconf dump / | grep org/example/app-relocatable-schema -A 2

顯示

[org/example/app-relocatable-schema/demo-3]
count=77777

[org/example/app-relocatable-schema/demo-2]
greeting='hi_2'

[org/example/app-relocatable-schema/demo-1]
greeting='hi_1'
count=88888

執行

gsettings reset org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-1/ greeting
gsettings reset org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-1/ count
gsettings reset org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-2/ greeting
gsettings reset org.example.app-relocatable-schema:/org/example/app-relocatable-schema/demo-3/ count

然後

$ dconf dump / | grep org/example/app-relocatable-schema -A 2

沒有顯示任何結果,直接出現下一個提示字元。

$ strings /usr/share/glib-2.0/schemas/gschemas.compiled | grep greeting

顯示

greeting

解除 schema

執行

$ sudo rm /usr/share/glib-2.0/schemas/org.example.app-relocatable-schema.gschema.xml

再執行

$ sudo glib-compile-schemas /usr/share/glib-2.0/schemas/

測試是否解除

$ gsettings get org.example.app-relocatable-schema:/ greeting

顯示

No such schema 'org.example.app-relocatable-schema'

執行

$ gsettings list-relocatable-schemas | grep org.example.app-relocatable-schema

沒有顯示任何結果,直接出現下一個提示字元。

相關檔案

  • /usr/share/glib-2.0/schemas/org.example.app-relocatable-schema.gschema.xml
  • /usr/share/glib-2.0/schemas/gschemas.compiled
  • ~/.config/dconf/user