c++glibgnomegiogsettings

Edit gsettings in a C++


I'm trying to edit a gsetting through a C++ program. I've read this question and I'm able to get the value. If I try to set it (using set_uint method), change seems to be made (if I re-read it shows the new value) but, if I check manually, it is not so. Do I have to apply the edits? Or what else?

Example code:

#include <giomm/settings.h>
#include <iostream>

int main() {
    Glib::RefPtr<Gio::Settings> colorSetting = 
                  Gio::Settings::create("org.gnome.settings-daemon.plugins.color");
    Glib::ustring tempKey = "night-light-temperature";

    //Works!
    cout<<colorSetting->get_uint(tempKey)<<endl;

    //Seems to work
    colorSetting->set_uint(tempKey, (unsigned) 2300);

    //Reads new value correctly
    cout<<colorSetting->get_uint(tempKey)<<endl;

    return 0;
}

Thanks in advance.


Solution

  • Since your program is exiting almost immediately after setting the value, it’s likely that the asynchronous write machinery in GSettings has not written the new value to disk by the time your program exits.

    Try adding a g_settings_sync() call before exiting (I don’t know how it’s bound in giomm, but that’s what the call is in C). From the documentation for g_settings_sync():

    Writes made to a GSettings are handled asynchronously. For this reason, it is very unlikely that the changes have it to disk by the time g_settings_set() returns.

    To be clear, a g_settings_sync() call should not normally be necessary; it’s only necessary here because you’re not running a main loop.

    See also: G_Settings apply changes and Can't change dconf-entry with GSettings, which cover the same issue but from the perspective of C and JavaScript.