Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / fe-common / core / windows-layout.c
1 /*
2  windows-layout.c : irssi
3
4     Copyright (C) 2000-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 "signals.h"
23 #include "misc.h"
24 #include "levels.h"
25 #include "lib-config/iconfig.h"
26 #include "settings.h"
27
28 #include "chat-protocols.h"
29 #include "servers.h"
30 #include "channels.h"
31 #include "queries.h"
32
33 #include "module-formats.h"
34 #include "printtext.h"
35 #include "themes.h"
36 #include "fe-windows.h"
37 #include "window-items.h"
38
39 static WINDOW_REC *restore_win;
40
41 static void signal_query_created_curwin(QUERY_REC *query)
42 {
43         g_return_if_fail(IS_QUERY(query));
44
45         window_item_add(restore_win, (WI_ITEM_REC *) query, TRUE);
46 }
47
48 static void sig_layout_restore_item(WINDOW_REC *window, const char *type,
49                                     CONFIG_NODE *node)
50 {
51         char *name, *tag, *chat_type;
52
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);
56
57         if (name == NULL || tag == NULL)
58                 return;
59
60         if (g_ascii_strcasecmp(type, "CHANNEL") == 0) {
61                 /* bind channel to window */
62                 WINDOW_BIND_REC *rec = window_bind_add(window, tag, name);
63                 rec->sticky = TRUE;
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);
69
70                 restore_win = window;
71                 
72                 protocol = chat_protocol_find(chat_type);
73                 if (protocol->query_create != NULL)
74                         protocol->query_create(tag, name, TRUE);
75                 else {
76                         QUERY_REC *query;
77
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);
83                 }
84
85                 signal_remove("query created",
86                               (SIGNAL_FUNC) signal_query_created_curwin);
87         }
88 }
89
90 static void window_add_items(WINDOW_REC *window, CONFIG_NODE *node)
91 {
92         GSList *tmp;
93         char *type;
94
95         if (node == NULL)
96                 return;
97
98         tmp = config_node_first(node->value);
99         for (; tmp != NULL; tmp = config_node_next(tmp)) {
100                 CONFIG_NODE *node = tmp->data;
101
102                 type = config_node_get_str(node, "type", NULL);
103                 if (type != NULL) {
104                         signal_emit("layout restore item", 3,
105                                     window, type, node);
106                 }
107         }
108 }
109
110 void windows_layout_restore(void)
111 {
112         signal_emit("layout restore", 0);
113 }
114
115 static void sig_layout_restore(void)
116 {
117         WINDOW_REC *window;
118         CONFIG_NODE *node;
119         GSList *tmp;
120
121         node = iconfig_node_traverse("windows", FALSE);
122         if (node == NULL) return;
123
124         tmp = config_node_first(node->value);
125         for (; tmp != NULL; tmp = config_node_next(tmp)) {
126                 CONFIG_NODE *node = tmp->data;
127
128                 window = window_find_refnum(atoi(node->key));
129                 if (window == NULL)
130                         window = window_create(NULL, TRUE);
131
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));
138
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);
143
144                 window_add_items(window, config_node_section(node, "items", -1));
145                 signal_emit("layout restore window", 2, window, node);
146         }
147 }
148
149 static void sig_layout_save_item(WINDOW_REC *window, WI_ITEM_REC *item,
150                                  CONFIG_NODE *node)
151 {
152         CONFIG_NODE *subnode;
153         CHAT_PROTOCOL_REC *proto;
154         const char *type;
155         WINDOW_BIND_REC *rec;
156
157         type = module_find_id_str("WINDOW ITEM TYPE", item->type);
158         if (type == NULL)
159                 return;
160
161         subnode = config_node_section(node, NULL, NODE_TYPE_BLOCK);
162
163         iconfig_node_set_str(subnode, "type", type);
164         proto = item->chat_type == 0 ? NULL :
165                 chat_protocol_find_id(item->chat_type);
166         if (proto != NULL)
167                 iconfig_node_set_str(subnode, "chat_type", proto->name);
168         iconfig_node_set_str(subnode, "name", item->visible_name);
169
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);
174                         if (rec != NULL)
175                                 rec->sticky = TRUE;
176                 }
177         } else if (IS_QUERY(item)) {
178                 iconfig_node_set_str(subnode, "tag", QUERY(item)->server_tag);
179         }
180 }
181
182 static void window_save_items(WINDOW_REC *window, CONFIG_NODE *node)
183 {
184         GSList *tmp;
185
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);
189 }
190
191 static void window_save(WINDOW_REC *window, CONFIG_NODE *node)
192 {
193         char refnum[MAX_INT_STRLEN];
194
195         ltoa(refnum, window->refnum);
196         node = config_node_section(node, refnum, NODE_TYPE_BLOCK);
197
198         if (window->sticky_refnum)
199                 iconfig_node_set_bool(node, "sticky_refnum", TRUE);
200
201         if (window->immortal)
202                 iconfig_node_set_bool(node, "immortal", TRUE);
203
204         if (window->name != NULL)
205                 iconfig_node_set_str(node, "name", window->name);
206
207         if (window->history_name != NULL)
208                 iconfig_node_set_str(node, "history_name", window->history_name);
209
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);
215                 g_free(level);
216         }
217         if (window->theme_name != NULL)
218                 iconfig_node_set_str(node, "theme", window->theme_name);
219
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);
224
225         signal_emit("layout save window", 2, window, node);
226 }
227
228 void windows_layout_save(void)
229 {
230         CONFIG_NODE *node;
231         GSList *sorted;
232
233         iconfig_set_str(NULL, "windows", NULL);
234         node = iconfig_node_traverse("windows", TRUE);
235
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);
240
241         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
242                     TXT_WINDOWS_LAYOUT_SAVED);
243 }
244
245 void windows_layout_reset(void)
246 {
247         GSList *tmp;
248
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);
253         }
254
255         iconfig_set_str(NULL, "windows", NULL);
256         signal_emit("layout reset", 0);
257
258         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
259                     TXT_WINDOWS_LAYOUT_RESET);
260 }
261
262 void windows_layout_init(void)
263 {
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);
267 }
268
269 void windows_layout_deinit(void)
270 {
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);
274 }