5d53f1ae177204fe3396bb5a5e4f0fefdbc49e69
[crypto.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
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
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 "queries.h"
31
32 #include "module-formats.h"
33 #include "printtext.h"
34 #include "themes.h"
35 #include "fe-windows.h"
36 #include "window-items.h"
37
38 static WINDOW_REC *restore_win;
39
40 static void signal_query_created_curwin(QUERY_REC *query)
41 {
42         g_return_if_fail(IS_QUERY(query));
43
44         window_item_add(restore_win, (WI_ITEM_REC *) query, TRUE);
45 }
46
47 static void sig_layout_restore_item(WINDOW_REC *window, const char *type,
48                                     CONFIG_NODE *node)
49 {
50         char *name, *tag, *chat_type;
51
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);
55
56         if (name == NULL || tag == NULL)
57                 return;
58
59         if (g_strcasecmp(type, "CHANNEL") == 0) {
60                 /* bind channel to window */
61                 WINDOW_BIND_REC *rec = window_bind_add(window, tag, name);
62                 rec->sticky = TRUE;
63         } else if (g_strcasecmp(type, "QUERY") == 0 && chat_type != NULL) {
64                 /* create query immediately */
65                 signal_add("query created",
66                            (SIGNAL_FUNC) signal_query_created_curwin);
67
68                 restore_win = window;
69                 chat_protocol_find(chat_type)->query_create(tag, name, TRUE);
70
71                 signal_remove("query created",
72                               (SIGNAL_FUNC) signal_query_created_curwin);
73         }
74 }
75
76 static void window_add_items(WINDOW_REC *window, CONFIG_NODE *node)
77 {
78         GSList *tmp;
79         char *type;
80
81         if (node == NULL)
82                 return;
83
84         tmp = config_node_first(node->value);
85         for (; tmp != NULL; tmp = config_node_next(tmp)) {
86                 CONFIG_NODE *node = tmp->data;
87
88                 type = config_node_get_str(node, "type", NULL);
89                 if (type != NULL) {
90                         signal_emit("layout restore item", 3,
91                                     window, type, node);
92                 }
93         }
94 }
95
96 void windows_layout_restore(void)
97 {
98         signal_emit("layout restore", 0);
99 }
100
101 static void sig_layout_restore(void)
102 {
103         WINDOW_REC *window;
104         CONFIG_NODE *node;
105         GSList *tmp;
106
107         node = iconfig_node_traverse("windows", FALSE);
108         if (node == NULL) return;
109
110         tmp = config_node_first(node->value);
111         for (; tmp != NULL; tmp = config_node_next(tmp)) {
112                 CONFIG_NODE *node = tmp->data;
113
114                 window = window_find_refnum(atoi(node->key));
115                 if (window == NULL)
116                         window = window_create(NULL, TRUE);
117
118                 window_set_refnum(window, atoi(node->key));
119                 window->sticky_refnum = config_node_get_bool(node, "sticky_refnum", FALSE);
120                 window->immortal = config_node_get_bool(node, "immortal", FALSE);
121                 window_set_name(window, config_node_get_str(node, "name", NULL));
122                 window_set_history(window, config_node_get_str(node, "history_name", NULL));
123                 window_set_level(window, level2bits(config_node_get_str(node, "level", "")));
124
125                 window->servertag = g_strdup(config_node_get_str(node, "servertag", NULL));
126                 window->theme_name = g_strdup(config_node_get_str(node, "theme", NULL));
127                 if (window->theme_name != NULL)
128                         window->theme = theme_load(window->theme_name);
129
130                 window_add_items(window, config_node_section(node, "items", -1));
131                 signal_emit("layout restore window", 2, window, node);
132         }
133 }
134
135 static void window_save_items(WINDOW_REC *window, CONFIG_NODE *node)
136 {
137         CONFIG_NODE *subnode;
138         GSList *tmp;
139         const char *type;
140
141         node = config_node_section(node, "items", NODE_TYPE_LIST);
142         for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
143                 WI_ITEM_REC *rec = tmp->data;
144                 SERVER_REC *server = rec->server;
145
146                 type = module_find_id_str("WINDOW ITEM TYPE", rec->type);
147                 if (type == NULL) continue;
148
149                 subnode = config_node_section(node, NULL, NODE_TYPE_BLOCK);
150
151                 iconfig_node_set_str(subnode, "type", type);
152                 type = chat_protocol_find_id(rec->chat_type)->name;
153                 iconfig_node_set_str(subnode, "chat_type", type);
154                 iconfig_node_set_str(subnode, "name", rec->name);
155
156                 if (server != NULL)
157                         iconfig_node_set_str(subnode, "tag", server->tag);
158                 else if (IS_QUERY(rec)) {
159                         iconfig_node_set_str(subnode, "tag",
160                                              QUERY(rec)->server_tag);
161                 }
162         }
163 }
164
165 static void window_save(WINDOW_REC *window, CONFIG_NODE *node)
166 {
167         char refnum[MAX_INT_STRLEN];
168
169         ltoa(refnum, window->refnum);
170         node = config_node_section(node, refnum, NODE_TYPE_BLOCK);
171
172         if (window->sticky_refnum)
173                 iconfig_node_set_bool(node, "sticky_refnum", TRUE);
174
175         if (window->immortal)
176                 iconfig_node_set_bool(node, "immortal", TRUE);
177
178         if (window->name != NULL)
179                 iconfig_node_set_str(node, "name", window->name);
180
181         if (window->history_name != NULL)
182                 iconfig_node_set_str(node, "history_name", window->history_name);
183
184         if (window->servertag != NULL)
185                 iconfig_node_set_str(node, "servertag", window->servertag);
186         if (window->level != 0) {
187                 char *level = bits2level(window->level);
188                 iconfig_node_set_str(node, "level", level);
189                 g_free(level);
190         }
191         if (window->theme_name != NULL)
192                 iconfig_node_set_str(node, "theme", window->theme_name);
193
194         if (window->items != NULL)
195                 window_save_items(window, node);
196
197         signal_emit("layout save window", 2, window, node);
198 }
199
200 void windows_layout_save(void)
201 {
202         CONFIG_NODE *node;
203
204         iconfig_set_str(NULL, "windows", NULL);
205         node = iconfig_node_traverse("windows", TRUE);
206
207         g_slist_foreach(windows, (GFunc) window_save, node);
208         signal_emit("layout save", 0);
209
210         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
211                     TXT_WINDOWS_LAYOUT_SAVED);
212 }
213
214 void windows_layout_reset(void)
215 {
216         iconfig_set_str(NULL, "windows", NULL);
217         signal_emit("layout reset", 0);
218
219         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
220                     TXT_WINDOWS_LAYOUT_RESET);
221 }
222
223 void windows_layout_init(void)
224 {
225         signal_add("layout restore item", (SIGNAL_FUNC) sig_layout_restore_item);
226         signal_add("layout restore", (SIGNAL_FUNC) sig_layout_restore);
227 }
228
229 void windows_layout_deinit(void)
230 {
231         signal_remove("layout restore item", (SIGNAL_FUNC) sig_layout_restore_item);
232         signal_remove("layout restore", (SIGNAL_FUNC) sig_layout_restore);
233 }