summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorailin-nemui <ailin-nemui@users.noreply.github.com>2018-03-20 17:53:03 +0100
committerailin-nemui <ailin-nemui@users.noreply.github.com>2018-03-20 17:53:03 +0100
commit977c2c0a0d911e3b7b43387ed19c5bfee565c617 (patch)
tree8fe62fa087c1dde4ad555aa4c2dc4d92edcd6197 /src
parent501c150ec21b1dceeecc0c201d22fc1bad161cf4 (diff)
downloadirssi-977c2c0a0d911e3b7b43387ed19c5bfee565c617.zip
refactor common parts
Diffstat (limited to 'src')
-rw-r--r--src/fe-text/statusbar-config.c139
1 files changed, 44 insertions, 95 deletions
diff --git a/src/fe-text/statusbar-config.c b/src/fe-text/statusbar-config.c
index a6ec81fe..0c6070fa 100644
--- a/src/fe-text/statusbar-config.c
+++ b/src/fe-text/statusbar-config.c
@@ -568,30 +568,16 @@ static CONFIG_NODE *statusbar_copy_config(CONFIG_REC *config, CONFIG_NODE *sourc
return parent;
}
-/* SYNTAX: STATUSBAR ADDITEM|MODIFYITEM [-before | -after <item>]
- [-priority #] [-alignment left|right] <item> <statusbar> */
-static void cmd_statusbar_additem_modifyitem(const char *data, void *server, void *witem)
+static CONFIG_NODE *sbar_find_item_with_defaults(const char *statusbar, const char *item,
+ gboolean create)
{
CONFIG_REC *config, *close_config;
CONFIG_NODE *node;
- GHashTable *optlist;
- char *item, *statusbar, *value;
- void *free_arg;
- int index;
- int additem = GPOINTER_TO_INT(signal_get_user_data());
-
- if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS | PARAM_FLAG_STRIP_TRAILING_WS,
- "statusbar additem", &optlist, &item, &statusbar))
- return;
-
- if (*statusbar == '\0') {
- cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
- }
close_config = NULL;
config = mainconfig;
-
node = sbar_node(statusbar, FALSE);
+
if (node == NULL) {
/* we are looking up defaults from the internal config */
close_config = config = config_open(NULL, -1);
@@ -601,57 +587,65 @@ static void cmd_statusbar_additem_modifyitem(const char *data, void *server, voi
if (node == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_STATUSBAR_NOT_FOUND, statusbar);
- if (close_config != NULL) {
- config_close(close_config);
- }
- cmd_params_free(free_arg);
- return;
- }
-
- node = config_sbar_items_section(config, node, additem);
-
- if (node == NULL) {
- printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_STATUSBAR_ITEM_NOT_FOUND, item);
- if (close_config != NULL) {
+ if (close_config != NULL)
config_close(close_config);
- }
- cmd_params_free(free_arg);
- return;
+ return NULL;
}
- /* get the index */
- index = -1;
- value = g_hash_table_lookup(optlist, "before");
- if (value != NULL) index = config_node_index(node, value);
- value = g_hash_table_lookup(optlist, "after");
- if (value != NULL) index = config_node_index(node, value)+1;
+ node = config_sbar_items_section(config, node, create);
- if (!additem && config_node_section(config, node, item, -1) == NULL) {
+ if (node == NULL || (!create && config_node_section(config, node, item, -1) == NULL)) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_STATUSBAR_ITEM_NOT_FOUND, item);
- if (close_config != NULL) {
+ if (close_config != NULL)
config_close(close_config);
- }
- cmd_params_free(free_arg);
- return;
+ return NULL;
}
if (config != mainconfig) {
/* we need to copy default to user config */
node = statusbar_copy_config(config, node, sbar_node(statusbar, TRUE));
- config = mainconfig;
}
- if (close_config != NULL) {
+ if (close_config != NULL)
config_close(close_config);
- close_config = NULL;
+
+ return node;
+}
+
+/* SYNTAX: STATUSBAR ADDITEM|MODIFYITEM [-before | -after <item>]
+ [-priority #] [-alignment left|right] <item> <statusbar> */
+static void cmd_statusbar_additem_modifyitem(const char *data, void *server, void *witem)
+{
+ CONFIG_NODE *node;
+ GHashTable *optlist;
+ char *item, *statusbar, *value;
+ void *free_arg;
+ int index;
+ int additem = GPOINTER_TO_INT(signal_get_user_data());
+
+ if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS | PARAM_FLAG_STRIP_TRAILING_WS,
+ "statusbar additem", &optlist, &item, &statusbar))
+ return;
+
+ if (*statusbar == '\0') {
+ cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
}
+ node = sbar_find_item_with_defaults(statusbar, item, additem);
if (node == NULL) {
- g_warning("node not found");
cmd_params_free(free_arg);
return;
}
+ /* get the index */
+ index = -1;
+ value = g_hash_table_lookup(optlist, "before");
+ if (value != NULL)
+ index = config_node_index(node, value);
+ value = g_hash_table_lookup(optlist, "after");
+ if (value != NULL)
+ index = config_node_index(node, value) + 1;
+
/* create/move item */
node = iconfig_node_section_index(node, item, index, NODE_TYPE_BLOCK);
@@ -684,55 +678,10 @@ static void cmd_statusbar_removeitem(const char *data, void *server, void *witem
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
}
- close_config = NULL;
- config = mainconfig;
-
- node = sbar_node(statusbar, FALSE);
- if (node == NULL) {
- /* we are looking up defaults from the internal config */
- close_config = config = config_open(NULL, -1);
- config_parse_data(config, default_config, "internal");
- node = config_sbar_node(config, statusbar, FALSE);
- }
+ node = sbar_find_item_with_defaults(statusbar, item, FALSE);
- if (node == NULL) {
- printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_STATUSBAR_NOT_FOUND, statusbar);
- if (close_config != NULL) {
- config_close(close_config);
- }
- cmd_params_free(free_arg);
- return;
- }
-
- node = config_sbar_items_section(config, node, FALSE);
-
- if (node == NULL || config_node_section(config, node, item, -1) == NULL) {
- printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_STATUSBAR_ITEM_NOT_FOUND, item);
- if (close_config != NULL) {
- config_close(close_config);
- }
- cmd_params_free(free_arg);
- return;
- }
-
- if (config != mainconfig) {
- /* we need to copy default to user config */
- node = statusbar_copy_config(config, node, sbar_node(statusbar, TRUE));
- config = mainconfig;
- }
-
- if (close_config != NULL) {
- config_close(close_config);
- close_config = NULL;
- }
-
- if (node == NULL) {
- g_warning("node not found");
- cmd_params_free(free_arg);
- return;
- }
-
- iconfig_node_set_str(node, item, NULL);
+ if (node != NULL)
+ iconfig_node_set_str(node, item, NULL);
read_statusbar_config();
cmd_params_free(free_arg);