2 windows-layout.c : irssi
4 Copyright (C) 2000-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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "lib-config/iconfig.h"
28 #include "chat-protocols.h"
32 #include "module-formats.h"
33 #include "printtext.h"
35 #include "fe-windows.h"
36 #include "window-items.h"
38 static void sig_window_restore_item(WINDOW_REC *window, const char *type,
41 char *name, *tag, *chat_type;
43 chat_type = config_node_get_str(node, "chat_type", NULL);
44 name = config_node_get_str(node, "name", NULL);
45 tag = config_node_get_str(node, "tag", NULL);
47 if (name == NULL || tag == NULL)
50 if (g_strcasecmp(type, "CHANNEL") == 0) {
51 /* bind channel to window */
52 WINDOW_BIND_REC *rec = window_bind_add(window, tag, name);
54 } else if (g_strcasecmp(type, "QUERY") == 0 && chat_type != NULL) {
55 /* create query immediately */
56 chat_protocol_find(chat_type)->query_create(tag, name, TRUE);
60 static void window_add_items(WINDOW_REC *window, CONFIG_NODE *node)
68 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
69 CONFIG_NODE *node = tmp->data;
71 type = config_node_get_str(node, "type", NULL);
73 signal_emit("window restore item", 3,
79 void windows_layout_restore(void)
85 node = iconfig_node_traverse("windows", FALSE);
86 if (node == NULL) return;
88 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
89 CONFIG_NODE *node = tmp->data;
91 window = window_create(NULL, TRUE);
92 window_set_refnum(window, atoi(node->key));
93 window->sticky_refnum = config_node_get_bool(node, "sticky_refnum", FALSE);
94 window_set_name(window, config_node_get_str(node, "name", NULL));
95 window_set_level(window, level2bits(config_node_get_str(node, "level", "")));
97 window->servertag = g_strdup(config_node_get_str(node, "servertag", NULL));
98 window->theme_name = g_strdup(config_node_get_str(node, "theme", NULL));
99 if (window->theme_name != NULL)
100 window->theme = theme_load(window->theme_name);
102 window_add_items(window, config_node_section(node, "items", -1));
103 signal_emit("window restore", 2, window, node);
106 signal_emit("windows restored", 0);
109 static void window_save_items(WINDOW_REC *window, CONFIG_NODE *node)
111 CONFIG_NODE *subnode;
115 node = config_node_section(node, "items", NODE_TYPE_LIST);
116 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
117 WI_ITEM_REC *rec = tmp->data;
118 SERVER_REC *server = rec->server;
120 type = module_find_id_str("WINDOW ITEM TYPE", rec->type);
121 if (type == NULL) continue;
123 subnode = config_node_section(node, NULL, NODE_TYPE_BLOCK);
125 iconfig_node_set_str(subnode, "type", type);
126 type = chat_protocol_find_id(rec->chat_type)->name;
127 iconfig_node_set_str(subnode, "chat_type", type);
128 iconfig_node_set_str(subnode, "name", rec->name);
131 iconfig_node_set_str(subnode, "tag", server->tag);
132 else if (IS_QUERY(rec)) {
133 iconfig_node_set_str(subnode, "tag",
134 QUERY(rec)->server_tag);
139 static void window_save(WINDOW_REC *window, CONFIG_NODE *node)
141 char refnum[MAX_INT_STRLEN];
143 ltoa(refnum, window->refnum);
144 node = config_node_section(node, refnum, NODE_TYPE_BLOCK);
146 if (window->sticky_refnum)
147 iconfig_node_set_bool(node, "sticky_refnum", TRUE);
149 if (window->name != NULL)
150 iconfig_node_set_str(node, "name", window->name);
151 if (window->servertag != NULL)
152 iconfig_node_set_str(node, "servertag", window->servertag);
153 if (window->level != 0) {
154 char *level = bits2level(window->level);
155 iconfig_node_set_str(node, "level", level);
158 if (window->theme_name != NULL)
159 iconfig_node_set_str(node, "theme", window->theme_name);
161 if (window->items != NULL)
162 window_save_items(window, node);
164 signal_emit("window save", 2, window, node);
167 void windows_layout_save(void)
171 iconfig_set_str(NULL, "windows", NULL);
172 node = iconfig_node_traverse("windows", TRUE);
174 g_slist_foreach(windows, (GFunc) window_save, node);
175 signal_emit("windows saved", 0);
177 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
178 TXT_WINDOWS_LAYOUT_SAVED);
181 void windows_layout_reset(void)
183 iconfig_set_str(NULL, "windows", NULL);
184 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
185 TXT_WINDOWS_LAYOUT_RESET);
188 void windows_layout_init(void)
190 signal_add("window restore item", (SIGNAL_FUNC) sig_window_restore_item);
193 void windows_layout_deinit(void)
195 signal_remove("window restore item", (SIGNAL_FUNC) sig_window_restore_item);