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 WINDOW_REC *restore_win;
40 static void signal_query_created_curwin(QUERY_REC *query)
42 g_return_if_fail(IS_QUERY(query));
44 window_item_add(restore_win, (WI_ITEM_REC *) query, TRUE);
47 static void sig_layout_restore_item(WINDOW_REC *window, const char *type,
50 char *name, *tag, *chat_type;
52 chat_type = config_node_get_str(node, "chat_type", NULL);
53 name = config_node_get_str(node, "name", NULL);
54 tag = config_node_get_str(node, "tag", NULL);
56 if (name == NULL || tag == NULL)
59 if (g_strcasecmp(type, "CHANNEL") == 0) {
60 /* bind channel to window */
61 WINDOW_BIND_REC *rec = window_bind_add(window, tag, name);
63 } else if (g_strcasecmp(type, "QUERY") == 0 && chat_type != NULL) {
64 CHAT_PROTOCOL_REC *protocol;
65 /* create query immediately */
66 signal_add("query created",
67 (SIGNAL_FUNC) signal_query_created_curwin);
71 protocol = chat_protocol_find(chat_type);
72 if (protocol->query_create != NULL)
73 protocol->query_create(tag, name, TRUE);
77 query = g_new0(QUERY_REC, 1);
78 query->chat_type = chat_protocol_lookup(chat_type);
79 query->name = g_strdup(name);
80 query->server_tag = g_strdup(tag);
81 query_init(query, TRUE);
84 signal_remove("query created",
85 (SIGNAL_FUNC) signal_query_created_curwin);
89 static void window_add_items(WINDOW_REC *window, CONFIG_NODE *node)
97 tmp = config_node_first(node->value);
98 for (; tmp != NULL; tmp = config_node_next(tmp)) {
99 CONFIG_NODE *node = tmp->data;
101 type = config_node_get_str(node, "type", NULL);
103 signal_emit("layout restore item", 3,
109 void windows_layout_restore(void)
111 signal_emit("layout restore", 0);
114 static void sig_layout_restore(void)
120 node = iconfig_node_traverse("windows", FALSE);
121 if (node == NULL) return;
123 tmp = config_node_first(node->value);
124 for (; tmp != NULL; tmp = config_node_next(tmp)) {
125 CONFIG_NODE *node = tmp->data;
127 window = window_find_refnum(atoi(node->key));
129 window = window_create(NULL, TRUE);
131 window_set_refnum(window, atoi(node->key));
132 window->sticky_refnum = config_node_get_bool(node, "sticky_refnum", FALSE);
133 window->immortal = config_node_get_bool(node, "immortal", FALSE);
134 window_set_name(window, config_node_get_str(node, "name", NULL));
135 window_set_history(window, config_node_get_str(node, "history_name", NULL));
136 window_set_level(window, level2bits(config_node_get_str(node, "level", "")));
138 window->servertag = g_strdup(config_node_get_str(node, "servertag", NULL));
139 window->theme_name = g_strdup(config_node_get_str(node, "theme", NULL));
140 if (window->theme_name != NULL)
141 window->theme = theme_load(window->theme_name);
143 window_add_items(window, config_node_section(node, "items", -1));
144 signal_emit("layout restore window", 2, window, node);
148 static void sig_layout_save_item(WINDOW_REC *window, WI_ITEM_REC *item,
151 CONFIG_NODE *subnode;
152 CHAT_PROTOCOL_REC *proto;
155 type = module_find_id_str("WINDOW ITEM TYPE", item->type);
159 subnode = config_node_section(node, NULL, NODE_TYPE_BLOCK);
161 iconfig_node_set_str(subnode, "type", type);
162 proto = item->chat_type == 0 ? NULL :
163 chat_protocol_find_id(item->chat_type);
165 iconfig_node_set_str(subnode, "chat_type", proto->name);
166 iconfig_node_set_str(subnode, "name", item->visible_name);
168 if (item->server != NULL)
169 iconfig_node_set_str(subnode, "tag", item->server->tag);
170 else if (IS_QUERY(item)) {
171 iconfig_node_set_str(subnode, "tag", QUERY(item)->server_tag);
175 static void window_save_items(WINDOW_REC *window, CONFIG_NODE *node)
179 node = config_node_section(node, "items", NODE_TYPE_LIST);
180 for (tmp = window->items; tmp != NULL; tmp = tmp->next)
181 signal_emit("layout save item", 3, window, tmp->data, node);
184 static void window_save(WINDOW_REC *window, CONFIG_NODE *node)
186 char refnum[MAX_INT_STRLEN];
188 ltoa(refnum, window->refnum);
189 node = config_node_section(node, refnum, NODE_TYPE_BLOCK);
191 if (window->sticky_refnum)
192 iconfig_node_set_bool(node, "sticky_refnum", TRUE);
194 if (window->immortal)
195 iconfig_node_set_bool(node, "immortal", TRUE);
197 if (window->name != NULL)
198 iconfig_node_set_str(node, "name", window->name);
200 if (window->history_name != NULL)
201 iconfig_node_set_str(node, "history_name", window->history_name);
203 if (window->servertag != NULL)
204 iconfig_node_set_str(node, "servertag", window->servertag);
205 if (window->level != 0) {
206 char *level = bits2level(window->level);
207 iconfig_node_set_str(node, "level", level);
210 if (window->theme_name != NULL)
211 iconfig_node_set_str(node, "theme", window->theme_name);
213 if (window->items != NULL)
214 window_save_items(window, node);
216 signal_emit("layout save window", 2, window, node);
219 void windows_layout_save(void)
223 iconfig_set_str(NULL, "windows", NULL);
224 node = iconfig_node_traverse("windows", TRUE);
226 g_slist_foreach(windows, (GFunc) window_save, node);
227 signal_emit("layout save", 0);
229 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
230 TXT_WINDOWS_LAYOUT_SAVED);
233 void windows_layout_reset(void)
235 iconfig_set_str(NULL, "windows", NULL);
236 signal_emit("layout reset", 0);
238 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
239 TXT_WINDOWS_LAYOUT_RESET);
242 void windows_layout_init(void)
244 signal_add("layout restore item", (SIGNAL_FUNC) sig_layout_restore_item);
245 signal_add("layout restore", (SIGNAL_FUNC) sig_layout_restore);
246 signal_add("layout save item", (SIGNAL_FUNC) sig_layout_save_item);
249 void windows_layout_deinit(void)
251 signal_remove("layout restore item", (SIGNAL_FUNC) sig_layout_restore_item);
252 signal_remove("layout restore", (SIGNAL_FUNC) sig_layout_restore);
253 signal_remove("layout save item", (SIGNAL_FUNC) sig_layout_save_item);