GSettings – Part IV

#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 );
}

static GSettingsBackend *create_backend ( void )
{
    GSettingsBackend *backend;
    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" );

        /// ***
        g_free ( config_file );

        /// ***
        return backend;
    }

    /// ***
    return NULL;
}

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

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

    /// ***
    if ( backend )
    {
        /// ***
        settings = g_settings_new_with_backend_and_path ( SCHEMA_ID, 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" );
    }
}

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;
    gboolean          value;

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

    /// ***
    settings = g_settings_new_with_backend_and_path ( SCHEMA_ID, 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_swapped ( check_button, "toggled", G_CALLBACK ( toggled_clbk ), check_button );

    /// ***
    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. Required fields are marked *