GSettings – Part VII

#define G_SETTINGS_ENABLE_BACKEND

#include <gtk/gtk.h>
#include <gio/gsettingsbackend.h>

#define SCHEMA_ID "this.is.my.app.gschema.xml"
#define PATH      "/this/is/my/app/"

#define MY_KEY    "my-key"

static void changed_clbk ( GSettings *settings, const gchar *key, GSettingsBackend *backend )
{
    g_return_if_fail ( G_IS_SETTINGS ( settings ) );
    g_return_if_fail ( G_IS_SETTINGS_BACKEND ( backend ) );

    /// ***
    gchar *buf_key;

    /// ***
    ///g_print ( "Key = %s\n", key );

    /// ***
    buf_key = g_strdup_printf ( "/%s", key );

    /// ***
    g_settings_backend_changed ( backend, buf_key, NULL );

    /// ***
    g_free ( buf_key );
}

void list_schemas ( GSettingsSchemaSource *source );
void list_schemas ( GSettingsSchemaSource *source )
{
    /// ***
    gchar **schemas;

    /// ***
    g_settings_schema_source_list_schemas ( source, TRUE, &schemas, NULL );

    /// ***
    for ( int i = 0; schemas[i]; i++ )
    {
        ///g_print ( "Schemas = %s\n", schemas[i] );
    }

    /// ***
    g_strfreev ( schemas );
}

static GSettingsSchema *create_schema ( void )
{
    GSettingsSchema       *schema;
    GSettingsSchemaSource *default_source;

    /// ***
    default_source = g_settings_schema_source_get_default();

    /// ***
    list_schemas ( default_source );

    /// ***
    schema  = g_settings_schema_source_lookup ( default_source, SCHEMA_ID, TRUE );

    /// ***
    return schema;
}

static GSettingsBackend *create_backend ( void )
{
    GSettingsBackend *backend;
    g_autofree gchar *config_file;

    /// *** Create the CFG file
    config_file = g_build_filename ( g_get_home_dir(),
                                     ".config",
                                     "myApp",
                                     "cfg-settings",
                                     NULL );
    /// ***
    ///g_print ( "config_file = %s\n", config_file );

    /// ***
    if ( config_file )
    {
        /// *** Creates the keyfile-backed
        backend = g_keyfile_settings_backend_new (	config_file, PATH, "checked-group" );

        /// ***
        return backend;
    }

    /// ***
    return NULL;
}

void set_settings_value ( const gboolean value );
void set_settings_value ( const gboolean value )
{
    /// ***
    GSettings        *settings;
    GSettingsBackend *backend;
    GSettingsSchema  *schema;

    /// ***
    schema = create_schema();

    /// ***
    backend = create_backend();

    /// ***
    if ( backend )
    {
        /// ***
        settings = g_settings_new_full ( schema, backend, PATH );

        /// ***
        g_signal_connect ( settings, "changed", G_CALLBACK ( changed_clbk ), backend );

        /// ***
        g_settings_set_boolean ( settings, MY_KEY, value );

        /// ***
        g_print ( "The State is [%s]\n", value ? "Checked" : "Not checked" );
    }

    /// ***
    g_settings_sync ();

    /// ***
    g_object_unref ( settings );
}

void toggled_clbk ( GtkCheckButton * button );
void toggled_clbk ( GtkCheckButton * button )
{
    g_return_if_fail ( GTK_IS_CHECK_BUTTON ( button ) );

    /// ***
    gboolean active;

    /// ***
    active = gtk_check_button_get_active ( button );

    /// ***
    set_settings_value ( active );
}

GtkWidget *create_check_button ( const gchar * const label );
GtkWidget *create_check_button ( const gchar * const label )
{
    GtkWidget        *check_button;
    GSettings        *settings;
    GSettingsBackend *backend;
    GSettingsSchema  *schema;
    gboolean          value;

    /// ***
    schema = create_schema();

    /// ***
    backend = create_backend ();

    /// ***
    settings = g_settings_new_full ( schema, backend, PATH );

    /// ***
    value = g_settings_get_boolean ( settings, MY_KEY );

    /// ***
    check_button = gtk_check_button_new_with_label ( label );

    /// ***
    gtk_check_button_set_active ( GTK_CHECK_BUTTON ( check_button ), value );

    /// ***
    g_signal_connect ( check_button, "toggled", G_CALLBACK ( toggled_clbk ), settings );

    /// ***
    gboolean key_state = FALSE;
    g_settings_get ( settings, MY_KEY, "b", &key_state );
    g_autoptr ( GVariant ) variant_value_1 = g_settings_get_user_value ( settings, MY_KEY );
    g_autoptr ( GVariant ) variant_value_2 = g_settings_get_value ( settings, MY_KEY );

    g_print ( "Key State is = %d - %d - %d - %d - %d - %d\n",
              key_state,
              g_settings_get_boolean ( settings, MY_KEY ),
              g_settings_get_has_unapplied ( settings ),
              g_variant_get_boolean ( variant_value_1 ),
              g_variant_get_boolean ( variant_value_2 ),
              g_settings_is_writable ( settings, MY_KEY ) );

    /// ***
    ///g_settings_reset ( settings, MY_KEY );

    /// ***
    g_object_unref ( settings );

    /// ***
    return check_button;
}

static void activate ( GtkApplication * app )
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *check_button;

    /// ***
    window = gtk_application_window_new ( app );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );
    gtk_window_set_title ( GTK_WINDOW ( window ), "MyChat" );

    /// ***
    box = gtk_box_new ( GTK_ORIENTATION_HORIZONTAL, 5 );

    /// ***
    gtk_window_set_child ( GTK_WINDOW ( window ), box );

    /// ***
    check_button = create_check_button ( "Check me" );

    /// ***
    gtk_box_append ( GTK_BOX ( box ), check_button );

    /// ***
    gtk_window_present ( GTK_WINDOW ( window ) );
}

int main ( int argc, char *argv[] )
{
    GtkApplication *app;
    int status;

    /// ***
    app = gtk_application_new ( "this.is.my.app", G_APPLICATION_FLAGS_NONE );

    /// ***
    g_signal_connect_swapped ( app, "activate", G_CALLBACK ( activate ), app );

    /// ***
    status = g_application_run ( G_APPLICATION ( app ), argc, argv );

    /// ***
    return status;
}
Author: MichaelB

Leave a Reply

Your email address will not be published.