gsettings

參考來源

此案例的程式碼來自於「Vala Settings Sample」。

其他相關網址

前置作業

準備「/usr/share/glib-2.0/schemas/org.example.my-app.gschema.xml」

create.sh

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

        <key name="greeting" type="s">
            <default l10n="messages">"Hello, earthlings"</default>
            <summary>A greeting</summary>
            <description>
                Greeting of the invading martians
            </description>
        </key>

        <key name="bottles-of-beer" type="i">
            <default>99</default>
            <summary>Bottles of beer</summary>
            <description>
                Number of bottles of beer on the wall
            </description>
        </key>

        <key name="lighting" type="b">
            <default>false</default>
            <summary>Is the light switched on?</summary>
            <description>
                State of an imaginary light switch.
            </description>
        </key>

    </schema>
</schemalist>

EOF

sudo cp org.example.my-app.gschema.xml /usr/share/glib-2.0/schemas/
sudo glib-compile-schemas /usr/share/glib-2.0/schemas/

主程式

gio-settings-demo.vala

void main () {
    var settings = new Settings ("org.example.my-app");

    // Getting keys
    var greeting = settings.get_string ("greeting");
    var bottles = settings.get_int ("bottles-of-beer");
    var lighting = settings.get_boolean ("lighting");

    print ("%s\n", greeting);
    print ("%d bottles of beer on the wall\n", bottles);
    print ("Is the light switched on? %s\n", lighting ? "yes" : "no");

    // Change notification for any key in the schema
    settings.changed.connect ((key) => {
        print ("Key '%s' changed\n", key);
    });

    // Change notification for a single key
    settings.changed["greeting"].connect (() => {
        print ("New greeting: %s\n", settings.get_string ("greeting"));
    });

    // Setting keys
    settings.set_int ("bottles-of-beer", bottles - 1);
    settings.set_boolean ("lighting", !lighting);
    settings.set_string ("greeting", "hello, world");

    print ("Please start 'dconf-editor' and edit keys in /org/example/my-app/\n");

    print ("Or use 'gsettings' and edit keys in /org/example/my-app/\n");

    print ("Ex:\n");

    print ("$ gsettings list-recursively | grep example\n");

    /*
    print ("org.example.my-app bottles-of-beer 98\n");
    print ("org.example.my-app greeting 'hello, world'\n");
    print ("org.example.my-app lighting true\n");
    */

    print ("$ gsettings set org.example.my-app greeting 'hi'\n");

    print ("$ gsettings list-recursively | grep example\n");

    new MainLoop ().run ();
}

編譯

編譯

build.sh

$ valac --pkg gio-2.0 gio-settings-demo.vala

執行

開一個「Termianl_1」

執行

run.sh

$ gio-settings-demo

顯示

hello, world
98 bottles of beer on the wall
Is the light switched on? no
Key 'bottles-of-beer' changed
Key 'lighting' changed
Key 'greeting' changed
New greeting: hello, world
Please start 'dconf-editor' and edit keys in /org/example/my-app/
Or use 'gsettings' and edit keys in /org/example/my-app/
Ex:
$ gsettings list-recursively | grep example
$ gsettings set org.example.my-app greeting 'hi'
$ gsettings list-recursively | grep example

測試

另外開一個「Termianl_2」

執行

$ gsettings list-recursively | grep example

顯示

org.example.my-app bottles-of-beer 98
org.example.my-app greeting 'hello, world'
org.example.my-app lighting true

執行

$ gsettings set org.example.my-app greeting 'hi'

在「Terminal_1」就會看到反應,多出下面兩行

Key 'greeting' changed
New greeting: hi

在「Termianl_2」

再次執行

$ gsettings list-recursively | grep example

顯示

org.example.my-app bottles-of-beer 98
org.example.my-app greeting 'hi'
org.example.my-app lighting true

執行

$ gsettings reset org.example.my-app greeting

再次執行

$ gsettings list-recursively | grep example

顯示

org.example.my-app bottles-of-beer 98
org.example.my-app greeting 'Hello, earthlings'
org.example.my-app lighting true