1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

Add a CHOICE type to the settings system.

This is useful to let the user choose an option between a finite set of
valid alternatives.
This commit is contained in:
LemonBoy 2016-06-12 15:26:07 +02:00
parent 0fad2cd842
commit bf9d9494db
4 changed files with 113 additions and 18 deletions

View File

@ -148,11 +148,34 @@ int settings_get_size(const char *key)
return str == NULL ? 0 : bytes;
}
int settings_get_choice(const char *key)
{
SETTINGS_REC *rec;
CONFIG_NODE *node;
char *str;
int idx;
rec = settings_get(key, SETTING_TYPE_CHOICE);
if (rec == NULL) return -1;
node = iconfig_node_traverse("settings", FALSE);
node = node == NULL ? NULL : iconfig_node_section(node, rec->module, -1);
str = node == NULL ? rec->default_value.v_string :
config_node_get_str(node, key, rec->default_value.v_string);
idx = strarray_find(rec->choices, str);
return (idx < 0) ? rec->default_value.v_int : idx;
}
char *settings_get_print(SETTINGS_REC *rec)
{
char *value = NULL;
switch(rec->type) {
case SETTING_TYPE_CHOICE:
value = g_strdup(rec->choices[settings_get_choice(rec->key)]);
break;
case SETTING_TYPE_BOOLEAN:
value = g_strdup(settings_get_bool(rec->key) ? "ON" : "OFF");
break;
@ -172,13 +195,31 @@ char *settings_get_print(SETTINGS_REC *rec)
static void settings_add(const char *module, const char *section,
const char *key, SettingType type,
const SettingValue *default_value)
const SettingValue *default_value,
const char *choices)
{
SETTINGS_REC *rec;
char **choices_vec = NULL;
g_return_if_fail(key != NULL);
g_return_if_fail(section != NULL);
if (type == SETTING_TYPE_CHOICE) {
if (choices == NULL) {
g_warning("Trying to add setting '%s' with no choices.", key);
return;
}
choices_vec = g_strsplit(choices, ",", -1);
/* validate the default value */
if (default_value->v_int < 0 || default_value->v_int >= g_strv_length(choices_vec)) {
g_warning("Trying to add setting '%s' with an invalid default value.", key);
g_strfreev(choices_vec);
return;
}
}
rec = g_hash_table_lookup(settings, key);
if (rec != NULL) {
/* Already exists, make sure it's correct type */
@ -197,6 +238,7 @@ static void settings_add(const char *module, const char *section,
rec->type = type;
rec->default_value = *default_value;
rec->choices = choices_vec;
g_hash_table_insert(settings, rec->key, rec);
}
}
@ -208,7 +250,17 @@ void settings_add_str_module(const char *module, const char *section,
memset(&default_value, 0, sizeof(default_value));
default_value.v_string = g_strdup(def);
settings_add(module, section, key, SETTING_TYPE_STRING, &default_value);
settings_add(module, section, key, SETTING_TYPE_STRING, &default_value, NULL);
}
void settings_add_choice_module(const char *module, const char *section,
const char *key, int def, const char *choices)
{
SettingValue default_value;
memset(&default_value, 0, sizeof(default_value));
default_value.v_int = def;
settings_add(module, section, key, SETTING_TYPE_CHOICE, &default_value, choices);
}
void settings_add_int_module(const char *module, const char *section,
@ -218,7 +270,7 @@ void settings_add_int_module(const char *module, const char *section,
memset(&default_value, 0, sizeof(default_value));
default_value.v_int = def;
settings_add(module, section, key, SETTING_TYPE_INT, &default_value);
settings_add(module, section, key, SETTING_TYPE_INT, &default_value, NULL);
}
void settings_add_bool_module(const char *module, const char *section,
@ -228,8 +280,7 @@ void settings_add_bool_module(const char *module, const char *section,
memset(&default_value, 0, sizeof(default_value));
default_value.v_bool = def;
settings_add(module, section, key, SETTING_TYPE_BOOLEAN,
&default_value);
settings_add(module, section, key, SETTING_TYPE_BOOLEAN, &default_value, NULL);
}
void settings_add_time_module(const char *module, const char *section,
@ -239,7 +290,7 @@ void settings_add_time_module(const char *module, const char *section,
memset(&default_value, 0, sizeof(default_value));
default_value.v_string = g_strdup(def);
settings_add(module, section, key, SETTING_TYPE_TIME, &default_value);
settings_add(module, section, key, SETTING_TYPE_TIME, &default_value, NULL);
}
void settings_add_level_module(const char *module, const char *section,
@ -249,7 +300,7 @@ void settings_add_level_module(const char *module, const char *section,
memset(&default_value, 0, sizeof(default_value));
default_value.v_string = g_strdup(def);
settings_add(module, section, key, SETTING_TYPE_LEVEL, &default_value);
settings_add(module, section, key, SETTING_TYPE_LEVEL, &default_value, NULL);
}
void settings_add_size_module(const char *module, const char *section,
@ -259,14 +310,16 @@ void settings_add_size_module(const char *module, const char *section,
memset(&default_value, 0, sizeof(default_value));
default_value.v_string = g_strdup(def);
settings_add(module, section, key, SETTING_TYPE_SIZE, &default_value);
settings_add(module, section, key, SETTING_TYPE_SIZE, &default_value, NULL);
}
static void settings_destroy(SETTINGS_REC *rec)
{
if (rec->type != SETTING_TYPE_INT &&
rec->type != SETTING_TYPE_BOOLEAN)
rec->type != SETTING_TYPE_BOOLEAN &&
rec->type != SETTING_TYPE_CHOICE)
g_free(rec->default_value.v_string);
g_strfreev(rec->choices);
g_free(rec->module);
g_free(rec->section);
g_free(rec->key);
@ -328,6 +381,24 @@ static CONFIG_NODE *settings_get_node(const char *key)
return iconfig_node_section(node, rec->module, NODE_TYPE_BLOCK);
}
gboolean settings_set_choice(const char *key, const char *value)
{
SETTINGS_REC *rec;
rec = settings_get_record(key);
/* XXX: The leading/trailing whitespace makes the test fail */
if (rec != NULL && g_strv_contains((const char **)rec->choices, value) == FALSE) {
char *msg = g_strjoinv(",", rec->choices);
g_warning("Invalid value for '%s', must be one of: %s", key, msg);
g_free(msg);
return FALSE;
}
settings_set_str(key, value);
return TRUE;
}
void settings_set_str(const char *key, const char *value)
{
iconfig_node_set_str(settings_get_node(key), key, value);
@ -343,7 +414,7 @@ void settings_set_bool(const char *key, int value)
iconfig_node_set_bool(settings_get_node(key), key, value);
}
int settings_set_time(const char *key, const char *value)
gboolean settings_set_time(const char *key, const char *value)
{
int msecs;
@ -354,7 +425,7 @@ int settings_set_time(const char *key, const char *value)
return TRUE;
}
int settings_set_level(const char *key, const char *value)
gboolean settings_set_level(const char *key, const char *value)
{
int iserror;
@ -366,7 +437,7 @@ int settings_set_level(const char *key, const char *value)
return TRUE;
}
int settings_set_size(const char *key, const char *value)
gboolean settings_set_size(const char *key, const char *value)
{
int size;

View File

@ -8,6 +8,7 @@ typedef enum {
SETTING_TYPE_TIME,
SETTING_TYPE_LEVEL,
SETTING_TYPE_SIZE,
SETTING_TYPE_CHOICE,
SETTING_TYPE_ANY
} SettingType;
@ -26,6 +27,7 @@ typedef struct {
SettingType type;
SettingValue default_value;
char **choices;
} SETTINGS_REC;
/* macros for handling the default Irssi configuration */
@ -58,6 +60,7 @@ int settings_get_bool(const char *key);
int settings_get_time(const char *key); /* as milliseconds */
int settings_get_level(const char *key);
int settings_get_size(const char *key); /* as bytes */
int settings_get_choice(const char *key);
char *settings_get_print(SETTINGS_REC *rec);
/* Functions to add/remove settings */
@ -73,6 +76,8 @@ void settings_add_level_module(const char *module, const char *section,
const char *key, const char *def);
void settings_add_size_module(const char *module, const char *section,
const char *key, const char *def);
void settings_add_choice_module(const char *module, const char *section,
const char *key, int def, const char *choices);
void settings_remove(const char *key);
void settings_remove_module(const char *module);
@ -88,13 +93,16 @@ void settings_remove_module(const char *module);
settings_add_level_module(MODULE_NAME, section, key, def)
#define settings_add_size(section, key, def) \
settings_add_size_module(MODULE_NAME, section, key, def)
#define settings_add_choice(section, key, def, choices) \
settings_add_choice_module(MODULE_NAME, section, key, def, choices)
void settings_set_str(const char *key, const char *value);
void settings_set_int(const char *key, int value);
void settings_set_bool(const char *key, int value);
int settings_set_time(const char *key, const char *value);
int settings_set_level(const char *key, const char *value);
int settings_set_size(const char *key, const char *value);
gboolean settings_set_time(const char *key, const char *value);
gboolean settings_set_level(const char *key, const char *value);
gboolean settings_set_size(const char *key, const char *value);
gboolean settings_set_choice(const char *key, const char *value);
/* Get the type (SETTING_TYPE_xxx) of `key' */
SettingType settings_get_type(const char *key);

View File

@ -690,9 +690,20 @@ static void sig_complete_set(GList **list, WINDOW_REC *window,
else if (*line != '\0' && *word == '\0') {
SETTINGS_REC *rec = settings_get_record(line);
if (rec != NULL) {
char *value = settings_get_print(rec);
if (value != NULL)
*list = g_list_append(*list, value);
/* show the whole list of valid options */
if (rec->type == SETTING_TYPE_CHOICE) {
char **tmp = rec->choices;
while (*tmp)
*list = g_list_append(*list, g_strdup(*tmp++));
}
/* show the current option */
else {
char *value = settings_get_print(rec);
if (value != NULL)
*list = g_list_append(*list, value);
}
}
}

View File

@ -142,6 +142,11 @@ static void cmd_set(char *data)
else
set_int(key, value);
break;
case SETTING_TYPE_CHOICE:
settings_set_choice(key, clear ? "" :
set_default ? rec->choices[rec->default_value.v_int] :
value);
break;
case SETTING_TYPE_STRING:
settings_set_str(key, clear ? "" :
set_default ? rec->default_value.v_string :