Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / fe-text / statusbar-config.c
1 /*
2  statusbar-config.c : irssi
3
4     Copyright (C) 2001 Timo Sirainen
5
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.
10
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.
15
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.
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "settings.h"
26 #include "levels.h"
27 #include "lib-config/iconfig.h"
28 #include "misc.h"
29
30 #include "statusbar.h"
31 #include "printtext.h"
32
33 static void read_statusbar_config_from_node(CONFIG_NODE *node);
34
35 static STATUSBAR_CONFIG_REC *
36 statusbar_config_create(STATUSBAR_GROUP_REC *group, const char *name)
37 {
38         STATUSBAR_CONFIG_REC *bar;
39
40         g_return_val_if_fail(group != NULL, NULL);
41         g_return_val_if_fail(name != NULL, NULL);
42
43         bar = g_new0(STATUSBAR_CONFIG_REC, 1);
44         group->config_bars = g_slist_append(group->config_bars, bar);
45
46         bar->name = g_strdup(name);
47         return bar;
48 }
49
50 static SBAR_ITEM_CONFIG_REC *
51 statusbar_item_config_create(STATUSBAR_CONFIG_REC *bar, const char *name,
52                              int priority, int right_alignment)
53 {
54         SBAR_ITEM_CONFIG_REC *rec;
55
56         g_return_val_if_fail(bar != NULL, NULL);
57         g_return_val_if_fail(name != NULL, NULL);
58
59         rec = g_new0(SBAR_ITEM_CONFIG_REC, 1);
60         bar->items = g_slist_append(bar->items, rec);
61
62         rec->name = g_strdup(name);
63         rec->priority = priority;
64         rec->right_alignment = right_alignment;
65
66         return rec;
67 }
68
69 static void statusbar_config_item_destroy(STATUSBAR_CONFIG_REC *barconfig,
70                                           SBAR_ITEM_CONFIG_REC *itemconfig)
71 {
72         barconfig->items = g_slist_remove(barconfig->items, itemconfig);
73
74         g_free(itemconfig->name);
75         g_free(itemconfig);
76 }
77
78 void statusbar_config_destroy(STATUSBAR_GROUP_REC *group,
79                               STATUSBAR_CONFIG_REC *config)
80 {
81         group->config_bars = g_slist_remove(group->config_bars, config);
82
83         while (config->items != NULL)
84                 statusbar_config_item_destroy(config, config->items->data);
85
86         g_free(config->name);
87         g_free(config);
88 }
89
90 static STATUSBAR_CONFIG_REC *
91 statusbar_config_find(STATUSBAR_GROUP_REC *group, const char *name)
92 {
93         GSList *tmp;
94
95         for (tmp = group->config_bars; tmp != NULL; tmp = tmp->next) {
96                 STATUSBAR_CONFIG_REC *config = tmp->data;
97
98                 if (strcmp(config->name, name) == 0)
99                         return config;
100         }
101
102         return NULL;
103 }
104
105 static void statusbar_reset_defaults(void)
106 {
107         CONFIG_REC *config;
108         CONFIG_NODE *node;
109
110         while (statusbar_groups != NULL)
111                 statusbar_group_destroy(statusbar_groups->data);
112         active_statusbar_group = NULL;
113
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);
118         if (node != NULL)
119                 read_statusbar_config_from_node(node);
120         config_close(config);
121 }
122
123 static void statusbar_read_items(CONFIG_NODE *items)
124 {
125         GSList *tmp;
126
127         tmp = config_node_first(items->value);
128         for (; tmp != NULL; tmp = config_node_next(tmp)) {
129                 CONFIG_NODE *node = tmp->data;
130
131                 statusbar_item_register(node->key, node->value, NULL);
132         }
133 }
134
135 static void statusbar_read_item(STATUSBAR_CONFIG_REC *bar, CONFIG_NODE *node)
136 {
137         int priority, right_alignment;
138
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);
143 }
144
145 static void statusbar_read(STATUSBAR_GROUP_REC *group, CONFIG_NODE *node)
146 {
147         STATUSBAR_CONFIG_REC *bar;
148         GSList *tmp;
149         const char *visible_str;
150
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 */
154                 if (bar != NULL)
155                         statusbar_config_destroy(group, bar);
156                 return;
157         }
158
159         if (bar == NULL) {
160                 bar = statusbar_config_create(group, node->key);
161                 bar->type = STATUSBAR_TYPE_ROOT;
162                 bar->placement = STATUSBAR_BOTTOM;
163                 bar->position = 0;
164         }
165
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;
171         else
172                 bar->visible = STATUSBAR_VISIBLE_ALWAYS;
173
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);
179
180         node = config_node_section(node, "items", -1);
181         if (node != NULL) {
182                 /* we're overriding the items - destroy the old */
183                 while (bar->items != NULL)
184                         statusbar_config_item_destroy(bar, bar->items->data);
185
186                 tmp = config_node_first(node->value);
187                 for (; tmp != NULL; tmp = config_node_next(tmp))
188                         statusbar_read_item(bar, tmp->data);
189         }
190 }
191
192 static void statusbar_read_group(CONFIG_NODE *node)
193 {
194         STATUSBAR_GROUP_REC *group;
195         GSList *tmp;
196
197         group = statusbar_group_find(node->key);
198         if (group == NULL) {
199                 group = statusbar_group_create(node->key);
200                 if (active_statusbar_group == NULL)
201                         active_statusbar_group = group;
202         }
203
204         tmp = config_node_first(node->value);
205         for (; tmp != NULL; tmp = config_node_next(tmp))
206                 statusbar_read(group, tmp->data);
207 }
208
209 static void create_root_statusbars(void)
210 {
211         STATUSBAR_REC *bar;
212         GSList *tmp;
213
214         tmp = active_statusbar_group->config_bars;
215         for (; tmp != NULL; tmp = tmp->next) {
216                 STATUSBAR_CONFIG_REC *rec = tmp->data;
217
218                 if (rec->type == STATUSBAR_TYPE_ROOT) {
219                         bar = statusbar_create(active_statusbar_group, rec, NULL);
220                         statusbar_redraw(bar, TRUE);
221                 }
222         }
223 }
224
225 static void read_statusbar_config_from_node(CONFIG_NODE *node)
226 {
227         CONFIG_NODE *items;
228         GSList *tmp;
229
230         items = config_node_section(node, "items", -1);
231         if (items != NULL)
232                 statusbar_read_items(items);
233
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);
238         }
239 }
240
241 static void read_statusbar_config(void)
242 {
243         CONFIG_NODE *node;
244
245         statusbar_reset_defaults();
246
247         node = iconfig_node_traverse("statusbar", FALSE);
248         if (node != NULL)
249                 read_statusbar_config_from_node(node);
250
251         create_root_statusbars();
252         statusbars_create_window_bars();
253 }
254
255 static const char *sbar_get_type(STATUSBAR_CONFIG_REC *rec)
256 {
257         return rec->type == STATUSBAR_TYPE_ROOT ? "root" :
258                 rec->type == STATUSBAR_TYPE_WINDOW ? "window" : "??";
259 }
260
261 static const char *sbar_get_placement(STATUSBAR_CONFIG_REC *rec)
262 {
263         return rec->placement == STATUSBAR_TOP ? "top" :
264                 rec->placement == STATUSBAR_BOTTOM ? "bottom" : "??";
265 }
266
267 static const char *sbar_get_visibility(STATUSBAR_CONFIG_REC *rec)
268 {
269         return rec->visible == STATUSBAR_VISIBLE_ALWAYS ? "always" :
270                 rec->visible == STATUSBAR_VISIBLE_ACTIVE ? "active" :
271                 rec->visible == STATUSBAR_VISIBLE_INACTIVE ? "inactive" : "??";
272 }
273
274 static void statusbar_list_items(STATUSBAR_CONFIG_REC *bar)
275 {
276         GSList *tmp;
277
278         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
279                     TXT_STATUSBAR_INFO_ITEM_HEADER);
280
281         for (tmp = bar->items; tmp != NULL; tmp = tmp->next) {
282                 SBAR_ITEM_CONFIG_REC *rec = tmp->data;
283
284                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
285                             TXT_STATUSBAR_INFO_ITEM_NAME,
286                             rec->name, rec->priority,
287                             rec->right_alignment ? "right" : "left");
288         }
289
290         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
291                     TXT_STATUSBAR_INFO_ITEM_FOOTER);
292 }
293
294 static void statusbar_print(STATUSBAR_CONFIG_REC *rec)
295 {
296         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
297                     TXT_STATUSBAR_INFO_NAME, rec->name);
298
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));
309
310         if (rec->items != NULL)
311                 statusbar_list_items(rec);
312 }
313
314 static void cmd_statusbar_list(void)
315 {
316         GSList *tmp;
317
318         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_STATUSBAR_LIST_HEADER);
319
320         tmp = active_statusbar_group->config_bars;
321         for (; tmp != NULL; tmp = tmp->next) {
322                 STATUSBAR_CONFIG_REC *rec = tmp->data;
323
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));
328         }
329
330         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_STATUSBAR_LIST_FOOTER);
331 }
332
333 static void cmd_statusbar_print_info(const char *name)
334 {
335         GSList *tmp;
336
337         tmp = active_statusbar_group->config_bars;
338         for (; tmp != NULL; tmp = tmp->next) {
339                 STATUSBAR_CONFIG_REC *rec = tmp->data;
340
341                 if (g_strcasecmp(rec->name, name) == 0) {
342                         statusbar_print(rec);
343                         return;
344                 }
345         }
346
347         printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
348                     TXT_STATUSBAR_NOT_FOUND, name);
349 }
350
351 /* SYNTAX: STATUSBAR <name> ENABLE */
352 static void cmd_statusbar_enable(const char *data, void *server,
353                                  void *item, CONFIG_NODE *node)
354 {
355         iconfig_node_set_str(node, "disabled", NULL);
356 }
357
358 /* SYNTAX: STATUSBAR <name> DISABLE */
359 static void cmd_statusbar_disable(const char *data, void *server,
360                                   void *item, CONFIG_NODE *node)
361 {
362         iconfig_node_set_bool(node, "disabled", TRUE);
363 }
364
365 /* SYNTAX: STATUSBAR <name> RESET */
366 static void cmd_statusbar_reset(const char *data, void *server,
367                                 void *item, CONFIG_NODE *node)
368 {
369         CONFIG_NODE *parent;
370
371         parent = iconfig_node_traverse("statusbar", TRUE);
372         parent = config_node_section(parent, active_statusbar_group->name,
373                                      NODE_TYPE_BLOCK);
374
375         iconfig_node_set_str(parent, node->key, NULL);
376 }
377
378 /* SYNTAX: STATUSBAR <name> TYPE window|root */
379 static void cmd_statusbar_type(const char *data, void *server,
380                                void *item, CONFIG_NODE *node)
381 {
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");
386         else {
387                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
388                             TXT_STATUSBAR_UNKNOWN_TYPE, data);
389         }
390 }
391
392 /* SYNTAX: STATUSBAR <name> PLACEMENT top|bottom */
393 static void cmd_statusbar_placement(const char *data, void *server,
394                                     void *item, CONFIG_NODE *node)
395 {
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");
400         else {
401                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
402                             TXT_STATUSBAR_UNKNOWN_PLACEMENT, data);
403         }
404 }
405
406 /* SYNTAX: STATUSBAR <name> POSITION <num> */
407 static void cmd_statusbar_position(const char *data, void *server,
408                                    void *item, CONFIG_NODE *node)
409 {
410         iconfig_node_set_int(node, "position", atoi(data));
411 }
412
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)
416 {
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");
423         else {
424                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
425                             TXT_STATUSBAR_UNKNOWN_VISIBILITY, data);
426         }
427 }
428
429 static CONFIG_NODE *statusbar_items_section(CONFIG_NODE *parent)
430 {
431         STATUSBAR_CONFIG_REC *bar;
432         CONFIG_NODE *node;
433         GSList *tmp;
434
435         node = config_node_section(parent, "items", -1);
436         if (node != NULL)
437                 return node;
438
439         /* find the statusbar configuration from memory */
440         bar = statusbar_config_find(active_statusbar_group, parent->key);
441         if (bar == NULL) {
442                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
443                             TXT_STATUSBAR_NOT_FOUND, parent->key);
444                 return NULL;
445         }
446
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;
452
453                 node = config_node_section(parent, rec->name,
454                                            NODE_TYPE_BLOCK);
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");
459         }
460
461         return parent;
462 }
463
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)
468 {
469         GHashTable *optlist;
470         char *name, *value;
471         void *free_arg;
472         int index;
473
474         node = statusbar_items_section(node);
475         if (node == NULL)
476                 return;
477
478         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
479                             "statusbar add", &optlist, &name))
480                 return;
481
482         /* get the index */
483         index = -1;
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;
488
489         /* create/move item */
490         node = config_node_section_index(node, name, index, NODE_TYPE_BLOCK);
491
492         /* set the options */
493         value = g_hash_table_lookup(optlist, "priority");
494         if (value != NULL) iconfig_node_set_int(node, "priority", atoi(value));
495
496         value = g_hash_table_lookup(optlist, "alignment");
497         if (value != NULL) {
498                 iconfig_node_set_str(node, "alignment",
499                                      g_ascii_strcasecmp(value, "right") == 0 ?
500                                      "right" : NULL);
501         }
502
503         cmd_params_free(free_arg);
504 }
505
506 /* SYNTAX: STATUSBAR <name> REMOVE <item> */
507 static void cmd_statusbar_remove(const char *data, void *server,
508                                  void *item, CONFIG_NODE *node)
509 {
510         node = statusbar_items_section(node);
511         if (node == NULL)
512                 return;
513
514         if (config_node_section(node, data, -1) != NULL)
515                 iconfig_node_set_str(node, data, NULL);
516         else {
517                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
518                             TXT_STATUSBAR_ITEM_NOT_FOUND, data);
519         }
520 }
521
522 static void cmd_statusbar(const char *data)
523 {
524         CONFIG_NODE *node;
525         char *name, *cmd, *params, *signal;
526         void *free_arg;
527
528         if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
529                             &name, &cmd, &params))
530                 return;
531
532         if (*name == '\0') {
533                 /* list all statusbars */
534                 cmd_statusbar_list();
535                 cmd_params_free(free_arg);
536                 return;
537         }
538
539         if (*cmd == '\0') {
540                 /* print statusbar info */
541                 cmd_statusbar_print_info(name);
542                 cmd_params_free(free_arg);
543                 return;
544         }
545
546         /* lookup/create the statusbar node */
547         node = iconfig_node_traverse("statusbar", TRUE);
548         node = config_node_section(node, active_statusbar_group->name,
549                                    NODE_TYPE_BLOCK);
550         node = config_node_section(node, name, NODE_TYPE_BLOCK);
551
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);
558         } else {
559                 read_statusbar_config();
560         }
561         g_free(signal);
562
563         cmd_params_free(free_arg);
564 }
565
566 void statusbar_config_init(void)
567 {
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);
571
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);
582
583         command_set_options("statusbar add", "+before +after +priority +alignment");
584 }
585
586 void statusbar_config_deinit(void)
587 {
588         signal_remove("setup reread", (SIGNAL_FUNC) read_statusbar_config);
589         signal_remove("theme changed", (SIGNAL_FUNC) read_statusbar_config);
590
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);
601 }