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 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.
25 #include "lib-config/iconfig.h"
28 #include "chat-protocols.h"
33 #include "module-formats.h"
34 #include "printtext.h"
36 #include "fe-windows.h"
37 #include "window-items.h"
39 static WINDOW_REC *restore_win;
41 static void signal_query_created_curwin(QUERY_REC *query)
43 g_return_if_fail(IS_QUERY(query));
45 window_item_add(restore_win, (WI_ITEM_REC *) query, TRUE);
48 static void sig_layout_restore_item(WINDOW_REC *window, const char *type,
51 char *name, *tag, *chat_type;
53 chat_type = config_node_get_str(node, "chat_type", NULL);
54 name = config_node_get_str(node, "name", NULL);
55 tag = config_node_get_str(node, "tag", NULL);
57 if (name == NULL || tag == NULL)
60 if (g_ascii_strcasecmp(type, "CHANNEL") == 0) {
61 /* bind channel to window */
62 WINDOW_BIND_REC *rec = window_bind_add(window, tag, name);
64 } else if (g_ascii_strcasecmp(type, "QUERY") == 0 && chat_type != NULL) {
65 CHAT_PROTOCOL_REC *protocol;
66 /* create query immediately */
67 signal_add("query created",
68 (SIGNAL_FUNC) signal_query_created_curwin);
72 protocol = chat_protocol_find(chat_type);
73 if (protocol->query_create != NULL)
74 protocol->query_create(tag, name, TRUE);
78 query = g_new0(QUERY_REC, 1);
79 query->chat_type = chat_protocol_lookup(chat_type);
80 query->name = g_strdup(name);
81 query->server_tag = g_strdup(tag);
82 query_init(query, TRUE);
85 signal_remove("query created",
86 (SIGNAL_FUNC) signal_query_created_curwin);
90 static void window_add_items(WINDOW_REC *window, CONFIG_NODE *node)
98 tmp = config_node_first(node->value);
99 for (; tmp != NULL; tmp = config_node_next(tmp)) {
100 CONFIG_NODE *node = tmp->data;
102 type = config_node_get_str(node, "type", NULL);
104 signal_emit("layout restore item", 3,
110 void windows_layout_restore(void)
112 signal_emit("layout restore", 0);
115 static void sig_layout_restore(void)
121 node = iconfig_node_traverse("windows", FALSE);
122 if (node == NULL) return;
124 tmp = config_node_first(node->value);
125 for (; tmp != NULL; tmp = config_node_next(tmp)) {
126 CONFIG_NODE *node = tmp->data;
128 window = window_find_refnum(atoi(node->key));
130 window = window_create(NULL, TRUE);
132 window_set_refnum(window, atoi(node->key));
133 window->sticky_refnum = config_node_get_bool(node, "sticky_refnum", FALSE);
134 window->immortal = config_node_get_bool(node, "immortal", FALSE);
135 window_set_name(window, config_node_get_str(node, "name", NULL));
136 window_set_history(window, config_node_get_str(node, "history_name", NULL));
137 window_set_level(window, level2bits(config_node_get_str(node, "level", ""), NULL));
139 window->servertag = g_strdup(config_node_get_str(node, "servertag", NULL));
140 window->theme_name = g_strdup(config_node_get_str(node, "theme", NULL));
141 if (window->theme_name != NULL)
142 window->theme = theme_load(window->theme_name);
144 window_add_items(window, config_node_section(node, "items", -1));
145 signal_emit("layout restore window", 2, window, node);
149 static void sig_layout_save_item(WINDOW_REC *window, WI_ITEM_REC *item,
152 CONFIG_NODE *subnode;
153 CHAT_PROTOCOL_REC *proto;
155 WINDOW_BIND_REC *rec;
157 type = module_find_id_str("WINDOW ITEM TYPE", item->type);
161 subnode = config_node_section(node, NULL, NODE_TYPE_BLOCK);
163 iconfig_node_set_str(subnode, "type", type);
164 proto = item->chat_type == 0 ? NULL :
165 chat_protocol_find_id(item->chat_type);
167 iconfig_node_set_str(subnode, "chat_type", proto->name);
168 iconfig_node_set_str(subnode, "name", item->visible_name);
170 if (item->server != NULL) {
171 iconfig_node_set_str(subnode, "tag", item->server->tag);
172 if (IS_CHANNEL(item)) {
173 rec = window_bind_add(window, item->server->tag, item->visible_name);
177 } else if (IS_QUERY(item)) {
178 iconfig_node_set_str(subnode, "tag", QUERY(item)->server_tag);
182 static void window_save_items(WINDOW_REC *window, CONFIG_NODE *node)
186 node = config_node_section(node, "items", NODE_TYPE_LIST);
187 for (tmp = window->items; tmp != NULL; tmp = tmp->next)
188 signal_emit("layout save item", 3, window, tmp->data, node);
191 static void window_save(WINDOW_REC *window, CONFIG_NODE *node)
193 char refnum[MAX_INT_STRLEN];
195 ltoa(refnum, window->refnum);
196 node = config_node_section(node, refnum, NODE_TYPE_BLOCK);
198 if (window->sticky_refnum)
199 iconfig_node_set_bool(node, "sticky_refnum", TRUE);
201 if (window->immortal)
202 iconfig_node_set_bool(node, "immortal", TRUE);
204 if (window->name != NULL)
205 iconfig_node_set_str(node, "name", window->name);
207 if (window->history_name != NULL)
208 iconfig_node_set_str(node, "history_name", window->history_name);
210 if (window->servertag != NULL)
211 iconfig_node_set_str(node, "servertag", window->servertag);
212 if (window->level != 0) {
213 char *level = bits2level(window->level);
214 iconfig_node_set_str(node, "level", level);
217 if (window->theme_name != NULL)
218 iconfig_node_set_str(node, "theme", window->theme_name);
220 while (window->bound_items != NULL)
221 window_bind_destroy(window, window->bound_items->data);
222 if (window->items != NULL)
223 window_save_items(window, node);
225 signal_emit("layout save window", 2, window, node);
228 void windows_layout_save(void)
233 iconfig_set_str(NULL, "windows", NULL);
234 node = iconfig_node_traverse("windows", TRUE);
236 sorted = windows_get_sorted();
237 g_slist_foreach(sorted, (GFunc) window_save, node);
238 g_slist_free(sorted);
239 signal_emit("layout save", 0);
241 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
242 TXT_WINDOWS_LAYOUT_SAVED);
245 void windows_layout_reset(void)
249 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
250 WINDOW_REC *window = tmp->data;
251 while (window->bound_items != NULL)
252 window_bind_destroy(window, window->bound_items->data);
255 iconfig_set_str(NULL, "windows", NULL);
256 signal_emit("layout reset", 0);
258 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
259 TXT_WINDOWS_LAYOUT_RESET);
262 void windows_layout_init(void)
264 signal_add("layout restore item", (SIGNAL_FUNC) sig_layout_restore_item);
265 signal_add("layout restore", (SIGNAL_FUNC) sig_layout_restore);
266 signal_add("layout save item", (SIGNAL_FUNC) sig_layout_save_item);
269 void windows_layout_deinit(void)
271 signal_remove("layout restore item", (SIGNAL_FUNC) sig_layout_restore_item);
272 signal_remove("layout restore", (SIGNAL_FUNC) sig_layout_restore);
273 signal_remove("layout save item", (SIGNAL_FUNC) sig_layout_save_item);