Can't change dconf-entry with GSettings

2019-06-26 16:16发布

问题:

I'm currently building a simple application on Gjs, which should change the background-image of my gnome-shell. A solution on how this can be done using the gsettings-tool can be found here.

Since I want to build a desktop-application around it, I want to change the org.gnome.desktop.background.picture-uri-key by using Gio's GSettings-class. But using the set_X()-method does not change the value of the key.

This is my code to change the gsettings value:

var gio = imports.gi.Gio;

// Get the GSettings-object for the background-schema:
var background = new gio.Settings({schema: "org.gnome.desktop.background"});

// Read the current Background-Image:
print( "Current Background-Image: "+background.get_string("picture-uri") );

if (background.is_writable("picture-uri")){
    // Set a new Background-Image (should show up immediately):
    if (background.set_string("picture-uri", "file:///path/to/some/pic.jpg")){
        print("Success!");
    }
    else throw "Couldn't set the key!";
} else throw "The key is not writable";

Reading the value does work as expected, the is_writable()-method returns true and the set_string()-method also returns true.

I have checked that I'm not in "delay-apply" mode and the key has a GVariantType of string, so the set_string()-method should work.

Using the normal gsettings command-line tool (as explained in the linked post) works just fine.

I can't figure out what the problem is, is there any place I can look for logs or something?

回答1:

After not getting any responses here I asked the same question on the gjs-mailing list.

It turned out that the writes to dconf were not yet on the disk when my script exited, so they were never really applied.

The solution was to call the g_settings_sync() function (JsDoc) right after the set_string() function, to ensure that all writes had finished.

if (background.set_string("picture-uri", "file:///path/to/some/pic.jpg")){
    gio.Settings.sync()
    print("Success!");
}

Thanks to Johan Dahlin and his answer.