GSettings – Part VI

#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 list_schemas ( GSettingsSchemaSource *source )
{
    /// ***
    gchar **schemas;

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

    /// ***
    for ( int i = 0; schemas[i]; i++ )
    {
        if ( g_ascii_strcasecmp ( SCHEMA_ID, schemas[i] ) == 0 )
        {
            g_print ( "Found Schema = %s\n", schemas[i] );
        }
    }

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

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

    /// ***
    gboolean key_state = FALSE;

    /// ***
    g_settings_get ( settings, key, "b", &key_state );

    /// ***
    g_autoptr ( GVariant ) variant_value_1 = g_settings_get_user_value ( settings, key );

    /// ***
    g_autoptr ( GVariant ) variant_value_2 = g_settings_get_value ( settings, key );

    /// ***
    g_print ( "Key State is = %d - %d - %d - %d - %d - %d\n",
              key_state,
              g_settings_get_boolean       ( settings, 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, key ) );
  
    /// ***
    list_schemas ( g_settings_schema_source_get_default ()  );
}

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

    /// ***
    settings = g_settings_new ( SCHEMA_ID );

    /// ***
    g_signal_connect_swapped ( settings, "changed", G_CALLBACK ( changed_clbk ), settings );

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

    /// ***
    settings = g_settings_new ( SCHEMA_ID );

    /// ***
    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 );

    /// ***
    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.