Read a gconf value and return a default value if missing

A function like gconf_client_get_bool returns FALSE if the value is set to FALSE or if the value is missing. Moreover, the err argument is not set, if the value is missing.

One way to return another default value if the key is missing is to use the function gconf_client_get that will return NULL in this case.

A function to get a boolean value can be written like:

static gboolean
get_bool_default (GConfClient *client, const gchar *key, gboolean def)
{
    gboolean value = def;
    GConfValue* val;

    val = gconf_client_get (client, key, NULL);
    if (val != NULL)
    {
        value = gconf_value_get_bool (val);
        gconf_value_free (val);
    }

    return value;
}

I haven’t found a way to remove a not user defined gconf key, so to check this code, my current solution is just to read another key.

Leave a Reply

Your email address will not be published. Required fields are marked *