updates.
[runtime.git] / apps / irssi / src / fe-text / statusbar-config.c
index f9ea05f20a51d03481040ba0d8956549342494f8..81638d994d4032dc0dcde6aeb9a50f2785f15731 100644 (file)
 */
 
 #include "module.h"
+#include "module-formats.h"
 #include "signals.h"
+#include "commands.h"
 #include "settings.h"
+#include "levels.h"
 #include "lib-config/iconfig.h"
 
 #include "statusbar.h"
+#include "printtext.h"
 
 static void read_statusbar_config_from_node(CONFIG_NODE *node);
 
@@ -141,21 +145,11 @@ static void statusbar_read(STATUSBAR_GROUP_REC *group, CONFIG_NODE *node)
 {
        STATUSBAR_CONFIG_REC *bar;
         GSList *tmp;
-       int visible;
         const char *visible_str;
 
        bar = statusbar_config_find(group, node->key);
-       visible = bar == NULL ? STATUSBAR_VISIBLE_ALWAYS : bar->visible;
-
-        /* first make sure we want to see this statusbar */
-        visible_str = config_node_get_str(node, "visible", "");
-       if (strcmp(visible_str, "active") == 0)
-                visible = STATUSBAR_VISIBLE_ACTIVE;
-       else if (strcmp(visible_str, "inactive") == 0)
-               visible = STATUSBAR_VISIBLE_INACTIVE;
-       else if (strcmp(visible_str, "never") == 0) {
-               /* we don't want this statusbar -
-                  destroy it if it already exists */
+       if (config_node_get_bool(node, "disabled", FALSE)) {
+               /* disabled, destroy it if it already exists */
                if (bar != NULL)
                        statusbar_config_destroy(group, bar);
                 return;
@@ -167,11 +161,18 @@ static void statusbar_read(STATUSBAR_GROUP_REC *group, CONFIG_NODE *node)
                bar->placement = STATUSBAR_BOTTOM;
                bar->position = 0;
        }
-       bar->visible = visible;
 
-       if (strcmp(config_node_get_str(node, "type", ""), "window") == 0)
+        visible_str = config_node_get_str(node, "visible", "");
+       if (g_strcasecmp(visible_str, "active") == 0)
+                bar->visible = STATUSBAR_VISIBLE_ACTIVE;
+       else if (g_strcasecmp(visible_str, "inactive") == 0)
+               bar->visible = STATUSBAR_VISIBLE_INACTIVE;
+       else
+               bar->visible = STATUSBAR_VISIBLE_ALWAYS;
+
+       if (g_strcasecmp(config_node_get_str(node, "type", ""), "window") == 0)
                 bar->type = STATUSBAR_TYPE_WINDOW;
-       if (strcmp(config_node_get_str(node, "placement", ""), "top") == 0)
+       if (g_strcasecmp(config_node_get_str(node, "placement", ""), "top") == 0)
                 bar->placement = STATUSBAR_TOP;
        bar->position = config_node_get_int(node, "position", 0);
 
@@ -247,15 +248,353 @@ static void read_statusbar_config(void)
                read_statusbar_config_from_node(node);
 
         create_root_statusbars();
+        statusbars_create_window_bars();
+}
+
+static const char *sbar_get_type(STATUSBAR_CONFIG_REC *rec)
+{
+       return rec->type == STATUSBAR_TYPE_ROOT ? "root" :
+               rec->type == STATUSBAR_TYPE_WINDOW ? "window" : "??";
+}
+
+static const char *sbar_get_placement(STATUSBAR_CONFIG_REC *rec)
+{
+       return rec->placement == STATUSBAR_TOP ? "top" :
+               rec->placement == STATUSBAR_BOTTOM ? "bottom" : "??";
+}
+
+static const char *sbar_get_visibility(STATUSBAR_CONFIG_REC *rec)
+{
+       return rec->visible == STATUSBAR_VISIBLE_ALWAYS ? "always" :
+               rec->visible == STATUSBAR_VISIBLE_ACTIVE ? "active" :
+               rec->visible == STATUSBAR_VISIBLE_INACTIVE ? "inactive" : "??";
+}
+
+static void statusbar_list_items(STATUSBAR_CONFIG_REC *bar)
+{
+       GSList *tmp;
+
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                   TXT_STATUSBAR_INFO_ITEM_HEADER);
+
+       for (tmp = bar->items; tmp != NULL; tmp = tmp->next) {
+               SBAR_ITEM_CONFIG_REC *rec = tmp->data;
+
+               printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                           TXT_STATUSBAR_INFO_ITEM_NAME,
+                           rec->name, rec->priority,
+                           rec->right_alignment ? "right" : "left");
+       }
+
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                   TXT_STATUSBAR_INFO_ITEM_FOOTER);
+}
+
+static void statusbar_print(STATUSBAR_CONFIG_REC *rec)
+{
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                   TXT_STATUSBAR_INFO_NAME, rec->name);
+
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                   TXT_STATUSBAR_INFO_TYPE, sbar_get_type(rec));
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                   TXT_STATUSBAR_INFO_PLACEMENT,
+                   sbar_get_placement(rec));
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                   TXT_STATUSBAR_INFO_POSITION, rec->position);
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                   TXT_STATUSBAR_INFO_VISIBLE,
+                   sbar_get_visibility(rec));
+
+       if (rec->items != NULL)
+               statusbar_list_items(rec);
+}
+
+static void cmd_statusbar_list(void)
+{
+       GSList *tmp;
+
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_STATUSBAR_LIST_HEADER);
+
+        tmp = active_statusbar_group->config_bars;
+       for (; tmp != NULL; tmp = tmp->next) {
+               STATUSBAR_CONFIG_REC *rec = tmp->data;
+
+               printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+                           TXT_STATUSBAR_LIST, rec->name, sbar_get_type(rec),
+                           sbar_get_placement(rec), rec->position,
+                           sbar_get_visibility(rec));
+       }
+
+       printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_STATUSBAR_LIST_FOOTER);
+}
+
+static void cmd_statusbar_print_info(const char *name)
+{
+       GSList *tmp;
+
+        tmp = active_statusbar_group->config_bars;
+       for (; tmp != NULL; tmp = tmp->next) {
+               STATUSBAR_CONFIG_REC *rec = tmp->data;
+
+               if (g_strcasecmp(rec->name, name) == 0) {
+                        statusbar_print(rec);
+                       return;
+               }
+       }
+
+       printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
+                   TXT_STATUSBAR_NOT_FOUND, name);
+}
+
+/* SYNTAX: STATUSBAR <name> ENABLE */
+static void cmd_statusbar_enable(const char *data, void *server,
+                                void *item, CONFIG_NODE *node)
+{
+        iconfig_node_set_str(node, "disabled", NULL);
+}
+
+/* SYNTAX: STATUSBAR <name> DISABLE */
+static void cmd_statusbar_disable(const char *data, void *server,
+                                 void *item, CONFIG_NODE *node)
+{
+        iconfig_node_set_bool(node, "disabled", TRUE);
+}
+
+/* SYNTAX: STATUSBAR <name> RESET */
+static void cmd_statusbar_reset(const char *data, void *server,
+                               void *item, CONFIG_NODE *node)
+{
+       CONFIG_NODE *parent;
+
+       parent = iconfig_node_traverse("statusbar", TRUE);
+       parent = config_node_section(parent, active_statusbar_group->name,
+                                    NODE_TYPE_BLOCK);
+
+        iconfig_node_set_str(parent, node->key, NULL);
+}
+
+/* SYNTAX: STATUSBAR <name> TYPE window|root */
+static void cmd_statusbar_type(const char *data, void *server,
+                              void *item, CONFIG_NODE *node)
+{
+       if (g_strcasecmp(data, "window") == 0)
+               iconfig_node_set_str(node, "type", "window");
+        else if (g_strcasecmp(data, "root") == 0)
+               iconfig_node_set_str(node, "type", "root");
+       else {
+               printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
+                           TXT_STATUSBAR_UNKNOWN_TYPE, data);
+       }
+}
+
+/* SYNTAX: STATUSBAR <name> PLACEMENT top|bottom */
+static void cmd_statusbar_placement(const char *data, void *server,
+                                   void *item, CONFIG_NODE *node)
+{
+       if (g_strcasecmp(data, "top") == 0)
+               iconfig_node_set_str(node, "placement", "top");
+        else if (g_strcasecmp(data, "bottom") == 0)
+               iconfig_node_set_str(node, "placement", "bottom");
+       else {
+               printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
+                           TXT_STATUSBAR_UNKNOWN_PLACEMENT, data);
+       }
+}
+
+/* SYNTAX: STATUSBAR <name> POSITION <num> */
+static void cmd_statusbar_position(const char *data, void *server,
+                                   void *item, CONFIG_NODE *node)
+{
+       iconfig_node_set_int(node, "position", atoi(data));
+}
+
+/* SYNTAX: STATUSBAR <name> VISIBLE always|active|inactive */
+static void cmd_statusbar_visible(const char *data, void *server,
+                                 void *item, CONFIG_NODE *node)
+{
+       if (g_strcasecmp(data, "always") == 0)
+               iconfig_node_set_str(node, "visible", "always");
+        else if (g_strcasecmp(data, "active") == 0)
+               iconfig_node_set_str(node, "visible", "active");
+        else if (g_strcasecmp(data, "inactive") == 0)
+               iconfig_node_set_str(node, "visible", "inactive");
+       else {
+               printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
+                           TXT_STATUSBAR_UNKNOWN_VISIBILITY, data);
+       }
+}
+
+static CONFIG_NODE *statusbar_items_section(CONFIG_NODE *parent)
+{
+       STATUSBAR_CONFIG_REC *bar;
+        CONFIG_NODE *node;
+        GSList *tmp;
+
+       node = config_node_section(parent, "items", -1);
+       if (node != NULL)
+               return node;
+
+        /* find the statusbar configuration from memory */
+       bar = statusbar_config_find(active_statusbar_group, parent->key);
+       if (bar == NULL) {
+               printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
+                           TXT_STATUSBAR_NOT_FOUND, parent->key);
+                return NULL;
+       }
+
+       /* since items list in config file overrides defaults,
+          we'll need to copy the whole list. */
+       parent = config_node_section(parent, "items", NODE_TYPE_BLOCK);
+       for (tmp = bar->items; tmp != NULL; tmp = tmp->next) {
+               SBAR_ITEM_CONFIG_REC *rec = tmp->data;
+
+               node = config_node_section(parent, rec->name,
+                                          NODE_TYPE_BLOCK);
+               if (rec->priority != 0)
+                        iconfig_node_set_int(node, "priority", rec->priority);
+               if (rec->right_alignment)
+                        iconfig_node_set_str(node, "alignment", "right");
+       }
+
+        return parent;
+}
+
+/* SYNTAX: STATUSBAR <name> ADD [-before | -after <item>]
+           [-priority #] [-alignment left|right] <item> */
+static void cmd_statusbar_add(const char *data, void *server,
+                             void *item, CONFIG_NODE *node)
+{
+        GHashTable *optlist;
+        char *name, *value;
+       void *free_arg;
+        int index;
+
+       node = statusbar_items_section(node);
+       if (node == NULL)
+                return;
+
+       if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
+                           "statusbar add", &optlist, &name))
+               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 = config_node_section_index(node, name, index, NODE_TYPE_BLOCK);
+
+        /* set the options */
+        value = g_hash_table_lookup(optlist, "priority");
+        if (value != NULL) iconfig_node_set_int(node, "priority", atoi(value));
+
+       value = g_hash_table_lookup(optlist, "alignment");
+       if (value != NULL) {
+               iconfig_node_set_str(node, "alignment",
+                                    g_strcasecmp(value, "right") == 0 ?
+                                    "right" : NULL);
+       }
+
+       cmd_params_free(free_arg);
+}
+
+/* SYNTAX: STATUSBAR <name> REMOVE <item> */
+static void cmd_statusbar_remove(const char *data, void *server,
+                                void *item, CONFIG_NODE *node)
+{
+        node = statusbar_items_section(node);
+       if (node == NULL)
+                return;
+
+       if (config_node_section(node, data, -1) != NULL)
+               iconfig_node_set_str(node, data, NULL);
+       else {
+               printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
+                           TXT_STATUSBAR_ITEM_NOT_FOUND, data);
+       }
+}
+
+static void cmd_statusbar(const char *data)
+{
+        CONFIG_NODE *node;
+       char *name, *cmd, *params, *signal;
+       void *free_arg;
+
+       if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
+                           &name, &cmd, &params))
+               return;
+
+       if (*name == '\0') {
+               /* list all statusbars */
+                cmd_statusbar_list();
+               cmd_params_free(free_arg);
+                return;
+       }
+
+       if (*cmd == '\0') {
+               /* print statusbar info */
+                cmd_statusbar_print_info(name);
+               cmd_params_free(free_arg);
+                return;
+       }
+
+        /* lookup/create the statusbar node */
+       node = iconfig_node_traverse("statusbar", TRUE);
+       node = config_node_section(node, active_statusbar_group->name,
+                                  NODE_TYPE_BLOCK);
+       node = config_node_section(node, name, NODE_TYPE_BLOCK);
+
+       /* call the subcommand */
+       signal = g_strconcat("command statusbar ", cmd, NULL);
+        g_strdown(signal);
+       if (!signal_emit(signal, 4, params, NULL, NULL, node)) {
+               printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
+                           TXT_STATUSBAR_UNKNOWN_COMMAND, cmd);
+       } else {
+                read_statusbar_config();
+       }
+       g_free(signal);
+
+       cmd_params_free(free_arg);
 }
 
 void statusbar_config_init(void)
 {
         read_statusbar_config();
-       signal_add("setup reread", (SIGNAL_FUNC) read_statusbar_config);
+       signal_add_last("setup reread", (SIGNAL_FUNC) read_statusbar_config);
+       signal_add("theme changed", (SIGNAL_FUNC) read_statusbar_config);
+
+        command_bind("statusbar", NULL, (SIGNAL_FUNC) cmd_statusbar);
+        command_bind("statusbar enable", NULL, (SIGNAL_FUNC) cmd_statusbar_enable);
+        command_bind("statusbar disable", NULL, (SIGNAL_FUNC) cmd_statusbar_disable);
+        command_bind("statusbar reset", NULL, (SIGNAL_FUNC) cmd_statusbar_reset);
+        command_bind("statusbar add", NULL, (SIGNAL_FUNC) cmd_statusbar_add);
+        command_bind("statusbar remove", NULL, (SIGNAL_FUNC) cmd_statusbar_remove);
+        command_bind("statusbar type", NULL, (SIGNAL_FUNC) cmd_statusbar_type);
+        command_bind("statusbar placement", NULL, (SIGNAL_FUNC) cmd_statusbar_placement);
+        command_bind("statusbar position", NULL, (SIGNAL_FUNC) cmd_statusbar_position);
+        command_bind("statusbar visible", NULL, (SIGNAL_FUNC) cmd_statusbar_visible);
+
+       command_set_options("statusbar add", "+before +after +priority +alignment");
 }
 
 void statusbar_config_deinit(void)
 {
        signal_remove("setup reread", (SIGNAL_FUNC) read_statusbar_config);
+       signal_remove("theme changed", (SIGNAL_FUNC) read_statusbar_config);
+
+        command_unbind("statusbar", (SIGNAL_FUNC) cmd_statusbar);
+        command_unbind("statusbar enable", (SIGNAL_FUNC) cmd_statusbar_enable);
+        command_unbind("statusbar disable", (SIGNAL_FUNC) cmd_statusbar_disable);
+        command_unbind("statusbar reset", (SIGNAL_FUNC) cmd_statusbar_reset);
+        command_unbind("statusbar add", (SIGNAL_FUNC) cmd_statusbar_add);
+        command_unbind("statusbar remove", (SIGNAL_FUNC) cmd_statusbar_remove);
+        command_unbind("statusbar type", (SIGNAL_FUNC) cmd_statusbar_type);
+        command_unbind("statusbar placement", (SIGNAL_FUNC) cmd_statusbar_placement);
+        command_unbind("statusbar position", (SIGNAL_FUNC) cmd_statusbar_position);
+        command_unbind("statusbar visible", (SIGNAL_FUNC) cmd_statusbar_visible);
 }