1
0
mirror of https://github.com/irssi/irssi.git synced 2024-11-03 04:27:19 -05:00

Merge pull request #492 from LemonBoy/enum

Enum setting implementation
This commit is contained in:
ailin-nemui 2016-06-13 21:47:39 +02:00 committed by GitHub
commit 52ced0728b
7 changed files with 138 additions and 40 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,20 @@ 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);
if (rec != NULL && strarray_find(rec->choices, value) < 0)
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 +410,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 +421,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 +433,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

@ -691,8 +691,20 @@ static void sig_complete_set(GList **list, WINDOW_REC *window,
SETTINGS_REC *rec = settings_get_record(line);
if (rec != NULL) {
char *value = settings_get_print(rec);
/* show the current option first */
if (value != NULL)
*list = g_list_append(*list, value);
/* show the whole list of valid options */
if (rec->type == SETTING_TYPE_CHOICE) {
char **tmp;
for (tmp = rec->choices; *tmp; tmp++) {
if (g_ascii_strcasecmp(*tmp, value) != 0)
*list = g_list_append(*list, g_strdup(*tmp));
}
}
}
}

View File

@ -67,6 +67,7 @@ static void set_print_pattern(const char *pattern)
static void set_boolean(const char *key, const char *value)
{
char *stripped_value;
stripped_value = g_strdup(value);
g_strstrip(stripped_value);
@ -79,7 +80,7 @@ static void set_boolean(const char *key, const char *value)
else
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_NOT_TOGGLE);
g_free(stripped_value);
g_free(stripped_value);
}
static void set_int(const char *key, const char *value)
@ -99,6 +100,24 @@ static void set_int(const char *key, const char *value)
settings_set_int(key, (int)longval);
}
static void set_choice(const char *key, const char *value)
{
char *stripped_value;
stripped_value = g_strdup(value);
g_strstrip(stripped_value);
if (settings_set_choice(key, stripped_value) == FALSE) {
SETTINGS_REC *rec = settings_get_record(key);
char *msg = g_strjoinv(", ", rec->choices);
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_INVALID_CHOICE, msg);
g_free(msg);
}
g_free(stripped_value);
}
/* SYNTAX: SET [-clear | -default] [<key> [<value>]] */
static void cmd_set(char *data)
{
@ -142,6 +161,12 @@ static void cmd_set(char *data)
else
set_int(key, value);
break;
case SETTING_TYPE_CHOICE:
if (clear || set_default)
settings_set_choice(key, rec->choices[rec->default_value.v_int]);
else
set_choice(key, value);
break;
case SETTING_TYPE_STRING:
settings_set_str(key, clear ? "" :
set_default ? rec->default_value.v_string :

View File

@ -221,6 +221,7 @@ FORMAT_REC fecommon_core_formats[] = {
{ "invalid_level", "Invalid message level", 0 },
{ "invalid_size", "Invalid size", 0 },
{ "invalid_charset", "Invalid charset: $0", 1, { 0 } },
{ "invalid_choice", "Invalid choice, must be one of $0", 1, { 0 } },
{ "eval_max_recurse", "/eval hit maximum recursion limit", 0 },
{ "program_not_found", "Could not find file or file is not executable", 0 },
{ "no_server_defined", "No servers defined for this network, see /help server for how to add one", 0 },

View File

@ -190,6 +190,7 @@ enum {
TXT_INVALID_LEVEL,
TXT_INVALID_SIZE,
TXT_INVALID_CHARSET,
TXT_INVALID_CHOICE,
TXT_EVAL_MAX_RECURSE,
TXT_PROGRAM_NOT_FOUND,
TXT_NO_SERVER_DEFINED,

View File

@ -623,39 +623,25 @@ char *irc_server_get_channels(IRC_SERVER_REC *server)
GString *chans, *keys;
char *ret;
int use_keys;
char *rejoin_channels_mode;
int rejoin_channels_mode;
g_return_val_if_fail(server != NULL, FALSE);
rejoin_channels_mode = g_strdup(settings_get_str("rejoin_channels_on_reconnect"));
rejoin_channels_mode = settings_get_choice("rejoin_channels_on_reconnect");
if (rejoin_channels_mode == NULL ||
(g_ascii_strcasecmp(rejoin_channels_mode, "on") != 0 &&
g_ascii_strcasecmp(rejoin_channels_mode, "off") != 0 &&
g_ascii_strcasecmp(rejoin_channels_mode, "auto") != 0)) {
g_warning("Invalid value for 'rejoin_channels_on_reconnect', valid values are 'on', 'off', 'auto', using 'on' as default value.");
g_free(rejoin_channels_mode);
rejoin_channels_mode = g_strdup("on");
}
/* do we want to rejoin channels in the first place? */
if(rejoin_channels_mode == 0)
return g_strdup("");
chans = g_string_new(NULL);
keys = g_string_new(NULL);
use_keys = FALSE;
/* do we want to rejoin channels in the first place? */
if(g_ascii_strcasecmp(rejoin_channels_mode, "off") == 0) {
g_string_free(chans, TRUE);
g_string_free(keys, TRUE);
g_free(rejoin_channels_mode);
return g_strdup("");
}
/* get currently joined channels */
for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
CHANNEL_REC *channel = tmp->data;
CHANNEL_SETUP_REC *setup = channel_setup_find(channel->name, channel->server->connrec->chatnet);
if ((setup != NULL && setup->autojoin && g_ascii_strcasecmp(rejoin_channels_mode, "auto") == 0) ||
g_ascii_strcasecmp(rejoin_channels_mode, "on") == 0) {
if ((setup != NULL && setup->autojoin && rejoin_channels_mode == 2) || rejoin_channels_mode == 1) {
g_string_append_printf(chans, "%s,", channel->name);
g_string_append_printf(keys, "%s,", channel->key == NULL ? "x" : channel->key);
if (channel->key != NULL)
@ -668,8 +654,7 @@ char *irc_server_get_channels(IRC_SERVER_REC *server)
REJOIN_REC *rec = tmp->data;
CHANNEL_SETUP_REC *setup = channel_setup_find(rec->channel, server->tag);
if ((setup != NULL && setup->autojoin && g_ascii_strcasecmp(rejoin_channels_mode, "auto") == 0) ||
g_ascii_strcasecmp(rejoin_channels_mode, "on") == 0) {
if ((setup != NULL && setup->autojoin && rejoin_channels_mode == 2) || rejoin_channels_mode == 1) {
g_string_append_printf(chans, "%s,", rec->channel);
g_string_append_printf(keys, "%s,", rec->key == NULL ? "x" :
rec->key);
@ -687,7 +672,6 @@ char *irc_server_get_channels(IRC_SERVER_REC *server)
ret = chans->str;
g_string_free(chans, FALSE);
g_string_free(keys, TRUE);
g_free(rejoin_channels_mode);
return ret;
}
@ -1033,7 +1017,7 @@ void irc_server_init_isupport(IRC_SERVER_REC *server)
void irc_servers_init(void)
{
settings_add_str("servers", "rejoin_channels_on_reconnect", "on");
settings_add_choice("servers", "rejoin_channels_on_reconnect", 1, "off;on;auto");
settings_add_str("misc", "usermode", DEFAULT_USER_MODE);
settings_add_str("misc", "split_line_start", "");
settings_add_str("misc", "split_line_end", "");