2 statusbar-config.c : irssi
4 Copyright (C) 2001 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "module-formats.h"
27 #include "lib-config/iconfig.h"
30 #include "statusbar.h"
31 #include "printtext.h"
33 static void read_statusbar_config_from_node(CONFIG_NODE *node);
35 static STATUSBAR_CONFIG_REC *
36 statusbar_config_create(STATUSBAR_GROUP_REC *group, const char *name)
38 STATUSBAR_CONFIG_REC *bar;
40 g_return_val_if_fail(group != NULL, NULL);
41 g_return_val_if_fail(name != NULL, NULL);
43 bar = g_new0(STATUSBAR_CONFIG_REC, 1);
44 group->config_bars = g_slist_append(group->config_bars, bar);
46 bar->name = g_strdup(name);
50 static SBAR_ITEM_CONFIG_REC *
51 statusbar_item_config_create(STATUSBAR_CONFIG_REC *bar, const char *name,
52 int priority, int right_alignment)
54 SBAR_ITEM_CONFIG_REC *rec;
56 g_return_val_if_fail(bar != NULL, NULL);
57 g_return_val_if_fail(name != NULL, NULL);
59 rec = g_new0(SBAR_ITEM_CONFIG_REC, 1);
60 bar->items = g_slist_append(bar->items, rec);
62 rec->name = g_strdup(name);
63 rec->priority = priority;
64 rec->right_alignment = right_alignment;
69 static void statusbar_config_item_destroy(STATUSBAR_CONFIG_REC *barconfig,
70 SBAR_ITEM_CONFIG_REC *itemconfig)
72 barconfig->items = g_slist_remove(barconfig->items, itemconfig);
74 g_free(itemconfig->name);
78 void statusbar_config_destroy(STATUSBAR_GROUP_REC *group,
79 STATUSBAR_CONFIG_REC *config)
81 group->config_bars = g_slist_remove(group->config_bars, config);
83 while (config->items != NULL)
84 statusbar_config_item_destroy(config, config->items->data);
90 static STATUSBAR_CONFIG_REC *
91 statusbar_config_find(STATUSBAR_GROUP_REC *group, const char *name)
95 for (tmp = group->config_bars; tmp != NULL; tmp = tmp->next) {
96 STATUSBAR_CONFIG_REC *config = tmp->data;
98 if (strcmp(config->name, name) == 0)
105 static void statusbar_reset_defaults(void)
110 while (statusbar_groups != NULL)
111 statusbar_group_destroy(statusbar_groups->data);
112 active_statusbar_group = NULL;
114 /* read the default statusbar settings from internal config */
115 config = config_open(NULL, -1);
116 config_parse_data(config, default_config, "internal");
117 node = config_node_traverse(config, "statusbar", FALSE);
119 read_statusbar_config_from_node(node);
120 config_close(config);
123 static void statusbar_read_items(CONFIG_NODE *items)
127 tmp = config_node_first(items->value);
128 for (; tmp != NULL; tmp = config_node_next(tmp)) {
129 CONFIG_NODE *node = tmp->data;
131 statusbar_item_register(node->key, node->value, NULL);
135 static void statusbar_read_item(STATUSBAR_CONFIG_REC *bar, CONFIG_NODE *node)
137 int priority, right_alignment;
139 priority = config_node_get_int(node, "priority", 0);
140 right_alignment = strcmp(config_node_get_str(node, "alignment", ""), "right") == 0;
141 statusbar_item_config_create(bar, node->key,
142 priority, right_alignment);
145 static void statusbar_read(STATUSBAR_GROUP_REC *group, CONFIG_NODE *node)
147 STATUSBAR_CONFIG_REC *bar;
149 const char *visible_str;
151 bar = statusbar_config_find(group, node->key);
152 if (config_node_get_bool(node, "disabled", FALSE)) {
153 /* disabled, destroy it if it already exists */
155 statusbar_config_destroy(group, bar);
160 bar = statusbar_config_create(group, node->key);
161 bar->type = STATUSBAR_TYPE_ROOT;
162 bar->placement = STATUSBAR_BOTTOM;
166 visible_str = config_node_get_str(node, "visible", "");
167 if (g_ascii_strcasecmp(visible_str, "active") == 0)
168 bar->visible = STATUSBAR_VISIBLE_ACTIVE;
169 else if (g_ascii_strcasecmp(visible_str, "inactive") == 0)
170 bar->visible = STATUSBAR_VISIBLE_INACTIVE;
172 bar->visible = STATUSBAR_VISIBLE_ALWAYS;
174 if (g_ascii_strcasecmp(config_node_get_str(node, "type", ""), "window") == 0)
175 bar->type = STATUSBAR_TYPE_WINDOW;
176 if (g_ascii_strcasecmp(config_node_get_str(node, "placement", ""), "top") == 0)
177 bar->placement = STATUSBAR_TOP;
178 bar->position = config_node_get_int(node, "position", 0);
180 node = config_node_section(node, "items", -1);
182 /* we're overriding the items - destroy the old */
183 while (bar->items != NULL)
184 statusbar_config_item_destroy(bar, bar->items->data);
186 tmp = config_node_first(node->value);
187 for (; tmp != NULL; tmp = config_node_next(tmp))
188 statusbar_read_item(bar, tmp->data);
192 static void statusbar_read_group(CONFIG_NODE *node)
194 STATUSBAR_GROUP_REC *group;
197 group = statusbar_group_find(node->key);
199 group = statusbar_group_create(node->key);
200 if (active_statusbar_group == NULL)
201 active_statusbar_group = group;
204 tmp = config_node_first(node->value);
205 for (; tmp != NULL; tmp = config_node_next(tmp))
206 statusbar_read(group, tmp->data);
209 static void create_root_statusbars(void)
214 tmp = active_statusbar_group->config_bars;
215 for (; tmp != NULL; tmp = tmp->next) {
216 STATUSBAR_CONFIG_REC *rec = tmp->data;
218 if (rec->type == STATUSBAR_TYPE_ROOT) {
219 bar = statusbar_create(active_statusbar_group, rec, NULL);
220 statusbar_redraw(bar, TRUE);
225 static void read_statusbar_config_from_node(CONFIG_NODE *node)
230 items = config_node_section(node, "items", -1);
232 statusbar_read_items(items);
234 tmp = config_node_first(node->value);
235 for (; tmp != NULL; tmp = config_node_next(tmp)) {
236 if (tmp->data != items)
237 statusbar_read_group(tmp->data);
241 static void read_statusbar_config(void)
245 statusbar_reset_defaults();
247 node = iconfig_node_traverse("statusbar", FALSE);
249 read_statusbar_config_from_node(node);
251 create_root_statusbars();
252 statusbars_create_window_bars();
255 static const char *sbar_get_type(STATUSBAR_CONFIG_REC *rec)
257 return rec->type == STATUSBAR_TYPE_ROOT ? "root" :
258 rec->type == STATUSBAR_TYPE_WINDOW ? "window" : "??";
261 static const char *sbar_get_placement(STATUSBAR_CONFIG_REC *rec)
263 return rec->placement == STATUSBAR_TOP ? "top" :
264 rec->placement == STATUSBAR_BOTTOM ? "bottom" : "??";
267 static const char *sbar_get_visibility(STATUSBAR_CONFIG_REC *rec)
269 return rec->visible == STATUSBAR_VISIBLE_ALWAYS ? "always" :
270 rec->visible == STATUSBAR_VISIBLE_ACTIVE ? "active" :
271 rec->visible == STATUSBAR_VISIBLE_INACTIVE ? "inactive" : "??";
274 static void statusbar_list_items(STATUSBAR_CONFIG_REC *bar)
278 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
279 TXT_STATUSBAR_INFO_ITEM_HEADER);
281 for (tmp = bar->items; tmp != NULL; tmp = tmp->next) {
282 SBAR_ITEM_CONFIG_REC *rec = tmp->data;
284 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
285 TXT_STATUSBAR_INFO_ITEM_NAME,
286 rec->name, rec->priority,
287 rec->right_alignment ? "right" : "left");
290 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
291 TXT_STATUSBAR_INFO_ITEM_FOOTER);
294 static void statusbar_print(STATUSBAR_CONFIG_REC *rec)
296 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
297 TXT_STATUSBAR_INFO_NAME, rec->name);
299 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
300 TXT_STATUSBAR_INFO_TYPE, sbar_get_type(rec));
301 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
302 TXT_STATUSBAR_INFO_PLACEMENT,
303 sbar_get_placement(rec));
304 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
305 TXT_STATUSBAR_INFO_POSITION, rec->position);
306 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
307 TXT_STATUSBAR_INFO_VISIBLE,
308 sbar_get_visibility(rec));
310 if (rec->items != NULL)
311 statusbar_list_items(rec);
314 static void cmd_statusbar_list(void)
318 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_STATUSBAR_LIST_HEADER);
320 tmp = active_statusbar_group->config_bars;
321 for (; tmp != NULL; tmp = tmp->next) {
322 STATUSBAR_CONFIG_REC *rec = tmp->data;
324 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
325 TXT_STATUSBAR_LIST, rec->name, sbar_get_type(rec),
326 sbar_get_placement(rec), rec->position,
327 sbar_get_visibility(rec));
330 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_STATUSBAR_LIST_FOOTER);
333 static void cmd_statusbar_print_info(const char *name)
337 tmp = active_statusbar_group->config_bars;
338 for (; tmp != NULL; tmp = tmp->next) {
339 STATUSBAR_CONFIG_REC *rec = tmp->data;
341 if (g_strcasecmp(rec->name, name) == 0) {
342 statusbar_print(rec);
347 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
348 TXT_STATUSBAR_NOT_FOUND, name);
351 /* SYNTAX: STATUSBAR <name> ENABLE */
352 static void cmd_statusbar_enable(const char *data, void *server,
353 void *item, CONFIG_NODE *node)
355 iconfig_node_set_str(node, "disabled", NULL);
358 /* SYNTAX: STATUSBAR <name> DISABLE */
359 static void cmd_statusbar_disable(const char *data, void *server,
360 void *item, CONFIG_NODE *node)
362 iconfig_node_set_bool(node, "disabled", TRUE);
365 /* SYNTAX: STATUSBAR <name> RESET */
366 static void cmd_statusbar_reset(const char *data, void *server,
367 void *item, CONFIG_NODE *node)
371 parent = iconfig_node_traverse("statusbar", TRUE);
372 parent = config_node_section(parent, active_statusbar_group->name,
375 iconfig_node_set_str(parent, node->key, NULL);
378 /* SYNTAX: STATUSBAR <name> TYPE window|root */
379 static void cmd_statusbar_type(const char *data, void *server,
380 void *item, CONFIG_NODE *node)
382 if (g_ascii_strcasecmp(data, "window") == 0)
383 iconfig_node_set_str(node, "type", "window");
384 else if (g_ascii_strcasecmp(data, "root") == 0)
385 iconfig_node_set_str(node, "type", "root");
387 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
388 TXT_STATUSBAR_UNKNOWN_TYPE, data);
392 /* SYNTAX: STATUSBAR <name> PLACEMENT top|bottom */
393 static void cmd_statusbar_placement(const char *data, void *server,
394 void *item, CONFIG_NODE *node)
396 if (g_ascii_strcasecmp(data, "top") == 0)
397 iconfig_node_set_str(node, "placement", "top");
398 else if (g_ascii_strcasecmp(data, "bottom") == 0)
399 iconfig_node_set_str(node, "placement", "bottom");
401 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
402 TXT_STATUSBAR_UNKNOWN_PLACEMENT, data);
406 /* SYNTAX: STATUSBAR <name> POSITION <num> */
407 static void cmd_statusbar_position(const char *data, void *server,
408 void *item, CONFIG_NODE *node)
410 iconfig_node_set_int(node, "position", atoi(data));
413 /* SYNTAX: STATUSBAR <name> VISIBLE always|active|inactive */
414 static void cmd_statusbar_visible(const char *data, void *server,
415 void *item, CONFIG_NODE *node)
417 if (g_ascii_strcasecmp(data, "always") == 0)
418 iconfig_node_set_str(node, "visible", "always");
419 else if (g_ascii_strcasecmp(data, "active") == 0)
420 iconfig_node_set_str(node, "visible", "active");
421 else if (g_ascii_strcasecmp(data, "inactive") == 0)
422 iconfig_node_set_str(node, "visible", "inactive");
424 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
425 TXT_STATUSBAR_UNKNOWN_VISIBILITY, data);
429 static CONFIG_NODE *statusbar_items_section(CONFIG_NODE *parent)
431 STATUSBAR_CONFIG_REC *bar;
435 node = config_node_section(parent, "items", -1);
439 /* find the statusbar configuration from memory */
440 bar = statusbar_config_find(active_statusbar_group, parent->key);
442 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
443 TXT_STATUSBAR_NOT_FOUND, parent->key);
447 /* since items list in config file overrides defaults,
448 we'll need to copy the whole list. */
449 parent = config_node_section(parent, "items", NODE_TYPE_BLOCK);
450 for (tmp = bar->items; tmp != NULL; tmp = tmp->next) {
451 SBAR_ITEM_CONFIG_REC *rec = tmp->data;
453 node = config_node_section(parent, rec->name,
455 if (rec->priority != 0)
456 iconfig_node_set_int(node, "priority", rec->priority);
457 if (rec->right_alignment)
458 iconfig_node_set_str(node, "alignment", "right");
464 /* SYNTAX: STATUSBAR <name> ADD [-before | -after <item>]
465 [-priority #] [-alignment left|right] <item> */
466 static void cmd_statusbar_add(const char *data, void *server,
467 void *item, CONFIG_NODE *node)
474 node = statusbar_items_section(node);
478 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
479 "statusbar add", &optlist, &name))
484 value = g_hash_table_lookup(optlist, "before");
485 if (value != NULL) index = config_node_index(node, value);
486 value = g_hash_table_lookup(optlist, "after");
487 if (value != NULL) index = config_node_index(node, value)+1;
489 /* create/move item */
490 node = config_node_section_index(node, name, index, NODE_TYPE_BLOCK);
492 /* set the options */
493 value = g_hash_table_lookup(optlist, "priority");
494 if (value != NULL) iconfig_node_set_int(node, "priority", atoi(value));
496 value = g_hash_table_lookup(optlist, "alignment");
498 iconfig_node_set_str(node, "alignment",
499 g_ascii_strcasecmp(value, "right") == 0 ?
503 cmd_params_free(free_arg);
506 /* SYNTAX: STATUSBAR <name> REMOVE <item> */
507 static void cmd_statusbar_remove(const char *data, void *server,
508 void *item, CONFIG_NODE *node)
510 node = statusbar_items_section(node);
514 if (config_node_section(node, data, -1) != NULL)
515 iconfig_node_set_str(node, data, NULL);
517 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
518 TXT_STATUSBAR_ITEM_NOT_FOUND, data);
522 static void cmd_statusbar(const char *data)
525 char *name, *cmd, *params, *signal;
528 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
529 &name, &cmd, ¶ms))
533 /* list all statusbars */
534 cmd_statusbar_list();
535 cmd_params_free(free_arg);
540 /* print statusbar info */
541 cmd_statusbar_print_info(name);
542 cmd_params_free(free_arg);
546 /* lookup/create the statusbar node */
547 node = iconfig_node_traverse("statusbar", TRUE);
548 node = config_node_section(node, active_statusbar_group->name,
550 node = config_node_section(node, name, NODE_TYPE_BLOCK);
552 /* call the subcommand */
553 signal = g_strconcat("command statusbar ", cmd, NULL);
554 ascii_strdown(signal);
555 if (!signal_emit(signal, 4, params, NULL, NULL, node)) {
556 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
557 TXT_STATUSBAR_UNKNOWN_COMMAND, cmd);
559 read_statusbar_config();
563 cmd_params_free(free_arg);
566 void statusbar_config_init(void)
568 read_statusbar_config();
569 signal_add_last("setup reread", (SIGNAL_FUNC) read_statusbar_config);
570 signal_add("theme changed", (SIGNAL_FUNC) read_statusbar_config);
572 command_bind("statusbar", NULL, (SIGNAL_FUNC) cmd_statusbar);
573 command_bind("statusbar enable", NULL, (SIGNAL_FUNC) cmd_statusbar_enable);
574 command_bind("statusbar disable", NULL, (SIGNAL_FUNC) cmd_statusbar_disable);
575 command_bind("statusbar reset", NULL, (SIGNAL_FUNC) cmd_statusbar_reset);
576 command_bind("statusbar add", NULL, (SIGNAL_FUNC) cmd_statusbar_add);
577 command_bind("statusbar remove", NULL, (SIGNAL_FUNC) cmd_statusbar_remove);
578 command_bind("statusbar type", NULL, (SIGNAL_FUNC) cmd_statusbar_type);
579 command_bind("statusbar placement", NULL, (SIGNAL_FUNC) cmd_statusbar_placement);
580 command_bind("statusbar position", NULL, (SIGNAL_FUNC) cmd_statusbar_position);
581 command_bind("statusbar visible", NULL, (SIGNAL_FUNC) cmd_statusbar_visible);
583 command_set_options("statusbar add", "+before +after +priority +alignment");
586 void statusbar_config_deinit(void)
588 signal_remove("setup reread", (SIGNAL_FUNC) read_statusbar_config);
589 signal_remove("theme changed", (SIGNAL_FUNC) read_statusbar_config);
591 command_unbind("statusbar", (SIGNAL_FUNC) cmd_statusbar);
592 command_unbind("statusbar enable", (SIGNAL_FUNC) cmd_statusbar_enable);
593 command_unbind("statusbar disable", (SIGNAL_FUNC) cmd_statusbar_disable);
594 command_unbind("statusbar reset", (SIGNAL_FUNC) cmd_statusbar_reset);
595 command_unbind("statusbar add", (SIGNAL_FUNC) cmd_statusbar_add);
596 command_unbind("statusbar remove", (SIGNAL_FUNC) cmd_statusbar_remove);
597 command_unbind("statusbar type", (SIGNAL_FUNC) cmd_statusbar_type);
598 command_unbind("statusbar placement", (SIGNAL_FUNC) cmd_statusbar_placement);
599 command_unbind("statusbar position", (SIGNAL_FUNC) cmd_statusbar_position);
600 command_unbind("statusbar visible", (SIGNAL_FUNC) cmd_statusbar_visible);