gplugin/gplugin

Add more ranged settings to test plugin

16 months ago, Elliott Sales de Andrade
ec766bf5a520
Add more ranged settings to test plugin

Ranges are allowed to be any integral type or a double, and `GVariant` doesn't
allow accessing an integer from a differently-sized integer, so we need tests
for all versions.

Testing Done:
Opened plugin viewer and confirmed that all settings were added.

Reviewed at https://reviews.imfreedom.org/r/2152/
/*
* Copyright (C) 2022 Elliott Sales de Andrade <quantum.analyst@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*/
#include <gplugin.h>
#include <gplugin-gtk-plugin-settings-list.h>
/**
* GPluginGtkPluginSettingslist:
*
* A [class@Gtk.ListBox] widget that displays all the settings from a plugin.
*
* Since: 0.40.0
*/
/******************************************************************************
* Structs
*****************************************************************************/
struct _GPluginGtkPluginSettingsList {
GtkBox parent;
GSettings *settings;
GtkListBox *list_box;
GList *rows;
};
/******************************************************************************
* Enums
*****************************************************************************/
enum {
PROP_ZERO,
PROP_SETTINGS,
N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {
NULL,
};
/******************************************************************************
* Helpers
*****************************************************************************/
static GtkWidget *
gplugin_gtk_plugin_settings_row_new_for_key(
GSettings *settings,
const gchar *name,
GSettingsSchemaKey *key)
{
/* TODO: Make real widgets. */
GtkWidget *widget = NULL;
GtkWidget *label = NULL;
GtkWidget *entry = NULL;
GVariant *value = NULL;
char *value_str = NULL;
widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
label = gtk_label_new(g_settings_schema_key_get_name(key));
gtk_label_set_xalign(GTK_LABEL(label), 0);
gtk_box_append(GTK_BOX(widget), label);
value = g_settings_get_value(settings, name);
value_str = g_variant_print(value, TRUE);
g_variant_unref(value);
entry = gtk_entry_new();
gtk_editable_set_text(GTK_EDITABLE(entry), value_str);
gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
gtk_widget_set_hexpand(entry, TRUE);
gtk_box_append(GTK_BOX(widget), entry);
g_free(value_str);
return widget;
}
static void
gplugin_gtk_plugin_settings_list_refresh(
GPluginGtkPluginSettingsList *list,
GSettings *settings)
{
GSettingsSchema *schema = NULL;
gchar **names = NULL;
while(list->rows) {
gtk_list_box_remove(list->list_box, list->rows->data);
list->rows = g_list_delete_link(list->rows, list->rows);
}
if(settings == NULL) {
return;
}
g_object_get(G_OBJECT(settings), "settings-schema", &schema, NULL);
names = g_settings_schema_list_keys(schema);
for(gint i = 0; names[i] != NULL; i++) {
GSettingsSchemaKey *key = NULL;
GtkWidget *row = NULL;
GtkWidget *widget = NULL;
key = g_settings_schema_get_key(schema, names[i]);
widget = gplugin_gtk_plugin_settings_row_new_for_key(
settings,
names[i],
key);
row = gtk_list_box_row_new();
gtk_list_box_row_set_child(GTK_LIST_BOX_ROW(row), widget);
gtk_widget_set_focusable(row, FALSE);
gtk_list_box_append(list->list_box, row);
list->rows = g_list_append(list->rows, row);
g_settings_schema_key_unref(key);
}
g_strfreev(names);
g_settings_schema_unref(schema);
}
/******************************************************************************
* GObject Implementation
*****************************************************************************/
G_DEFINE_TYPE(
GPluginGtkPluginSettingsList,
gplugin_gtk_plugin_settings_list,
GTK_TYPE_BOX)
static void
gplugin_gtk_plugin_settings_list_set_property(
GObject *obj,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GPluginGtkPluginSettingsList *list = GPLUGIN_GTK_PLUGIN_SETTINGS_LIST(obj);
switch(prop_id) {
case PROP_SETTINGS:
gplugin_gtk_plugin_settings_list_set_settings(
list,
g_value_get_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
break;
}
}
static void
gplugin_gtk_plugin_settings_list_get_property(
GObject *obj,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GPluginGtkPluginSettingsList *list = GPLUGIN_GTK_PLUGIN_SETTINGS_LIST(obj);
switch(prop_id) {
case PROP_SETTINGS:
g_value_set_object(
value,
gplugin_gtk_plugin_settings_list_get_settings(list));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
break;
}
}
static void
gplugin_gtk_plugin_settings_list_finalize(GObject *obj)
{
GPluginGtkPluginSettingsList *list = GPLUGIN_GTK_PLUGIN_SETTINGS_LIST(obj);
g_clear_object(&list->settings);
g_clear_pointer(&list->rows, g_list_free);
G_OBJECT_CLASS(gplugin_gtk_plugin_settings_list_parent_class)
->finalize(obj);
}
static void
gplugin_gtk_plugin_settings_list_class_init(
GPluginGtkPluginSettingsListClass *klass)
{
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
obj_class->get_property = gplugin_gtk_plugin_settings_list_get_property;
obj_class->set_property = gplugin_gtk_plugin_settings_list_set_property;
obj_class->finalize = gplugin_gtk_plugin_settings_list_finalize;
gtk_widget_class_set_template_from_resource(
widget_class,
"/org/imfreedom/keep/gplugin/gplugin-gtk/plugin-settings-list.ui");
gtk_widget_class_bind_template_child(
widget_class,
GPluginGtkPluginSettingsList,
list_box);
/* properties */
/**
* GPluginGtkPluginSettingsList:settings:
*
* The [class@Gio.Settings] to display.
*/
properties[PROP_SETTINGS] = g_param_spec_object(
"settings",
"settings",
"The settings to display",
G_TYPE_SETTINGS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}
static void
gplugin_gtk_plugin_settings_list_init(GPluginGtkPluginSettingsList *self)
{
gtk_widget_init_template(GTK_WIDGET(self));
}
/******************************************************************************
* API
*****************************************************************************/
/**
* gplugin_gtk_plugin_settings_list_new:
*
* Creates a new [class@GPluginGtk4.PluginSettingsList].
*
* Returns: (transfer full): The new list.
*
* Since: 0.40.0
*/
GtkWidget *
gplugin_gtk_plugin_settings_list_new(void)
{
return g_object_new(GPLUGIN_GTK_TYPE_PLUGIN_SETTINGS_LIST, NULL);
}
/**
* gplugin_gtk_plugin_settings_list_set_settings:
* @list: The instance.
* @settings: (transfer none) (nullable): The plugin settings to display.
*
* This function will set which plugin settings to display.
*
* Since: 0.40.0
*/
void
gplugin_gtk_plugin_settings_list_set_settings(
GPluginGtkPluginSettingsList *list,
GSettings *settings)
{
g_return_if_fail(GPLUGIN_GTK_IS_PLUGIN_SETTINGS_LIST(list));
g_return_if_fail(G_IS_SETTINGS(settings) || settings == NULL);
if(g_set_object(&list->settings, settings)) {
gplugin_gtk_plugin_settings_list_refresh(list, settings);
g_object_notify(G_OBJECT(list), "settings");
}
}
/**
* gplugin_gtk_plugin_settings_list_get_settings:
* @list: The instance.
*
* Returns the plugin settings that are being displayed.
*
* Returns: (transfer none): The settings being displayed.
*
* Since: 0.40.0
*/
GSettings *
gplugin_gtk_plugin_settings_list_get_settings(
GPluginGtkPluginSettingsList *list)
{
g_return_val_if_fail(GPLUGIN_GTK_IS_PLUGIN_SETTINGS_LIST(list), NULL);
return list->settings;
}