814127fbe7de55ca675fd3116b46ace17c774d60
[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
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 void sig_window_restore_item(WINDOW_REC *window, const char *type,
39                                     CONFIG_NODE *node)
40 {
41         char *name, *tag, *chat_type;
42
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);
46
47         if (name == NULL || tag == NULL)
48                 return;
49
50         if (g_strcasecmp(type, "CHANNEL") == 0) {
51                 /* bind channel to window */
52                 WINDOW_BIND_REC *rec = window_bind_add(window, tag, name);
53                 rec->sticky = TRUE;
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);
57         }
58 }
59
60 static void window_add_items(WINDOW_REC *window, CONFIG_NODE *node)
61 {
62         GSList *tmp;
63         char *type;
64
65         if (node == NULL)
66                 return;
67
68         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
69                 CONFIG_NODE *node = tmp->data;
70
71                 type = config_node_get_str(node, "type", NULL);
72                 if (type != NULL) {
73                         signal_emit("window restore item", 3,
74                                     window, type, node);
75                 }
76         }
77 }
78
79 void windows_layout_restore(void)
80 {
81         WINDOW_REC *window;
82         CONFIG_NODE *node;
83         GSList *tmp;
84
85         node = iconfig_node_traverse("windows", FALSE);
86         if (node == NULL) return;
87
88         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
89                 CONFIG_NODE *node = tmp->data;
90
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", "")));
96
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);
101
102                 window_add_items(window, config_node_section(node, "items", -1));
103                 signal_emit("window restore", 2, window, node);
104         }
105
106         signal_emit("windows restored", 0);
107 }
108
109 static void window_save_items(WINDOW_REC *window, CONFIG_NODE *node)
110 {
111         CONFIG_NODE *subnode;
112         GSList *tmp;
113         const char *type;
114
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;
119
120                 type = module_find_id_str("WINDOW ITEM TYPE", rec->type);
121                 if (type == NULL) continue;
122
123                 subnode = config_node_section(node, NULL, NODE_TYPE_BLOCK);
124
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);
129
130                 if (server != NULL)
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);
135                 }
136         }
137 }
138
139 static void window_save(WINDOW_REC *window, CONFIG_NODE *node)
140 {
141         char refnum[MAX_INT_STRLEN];
142
143         ltoa(refnum, window->refnum);
144         node = config_node_section(node, refnum, NODE_TYPE_BLOCK);
145
146         if (window->sticky_refnum)
147                 iconfig_node_set_bool(node, "sticky_refnum", TRUE);
148
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);
156                 g_free(level);
157         }
158         if (window->theme_name != NULL)
159                 iconfig_node_set_str(node, "theme", window->theme_name);
160
161         if (window->items != NULL)
162                 window_save_items(window, node);
163
164         signal_emit("window save", 2, window, node);
165 }
166
167 void windows_layout_save(void)
168 {
169         CONFIG_NODE *node;
170
171         iconfig_set_str(NULL, "windows", NULL);
172         node = iconfig_node_traverse("windows", TRUE);
173
174         g_slist_foreach(windows, (GFunc) window_save, node);
175         signal_emit("windows saved", 0);
176
177         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
178                     TXT_WINDOWS_LAYOUT_SAVED);
179 }
180
181 void windows_layout_reset(void)
182 {
183         iconfig_set_str(NULL, "windows", NULL);
184         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
185                     TXT_WINDOWS_LAYOUT_RESET);
186 }
187
188 void windows_layout_init(void)
189 {
190         signal_add("window restore item", (SIGNAL_FUNC) sig_window_restore_item);
191 }
192
193 void windows_layout_deinit(void)
194 {
195         signal_remove("window restore item", (SIGNAL_FUNC) sig_window_restore_item);
196 }