4 Copyright (C) 1999-2000 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
22 #include "module-formats.h"
32 #include "printtext.h"
33 #include "fe-windows.h"
34 #include "window-items.h"
36 GSList *windows; /* first in the list is the active window,
37 next is the last active, etc. */
38 WINDOW_REC *active_win;
41 static int daycheck; /* 0 = don't check, 1 = time is 00:00, check,
42 2 = time is 00:00, already checked */
44 static int window_get_new_refnum(void)
55 if (refnum != win->refnum) {
67 WINDOW_REC *window_create(WI_ITEM_REC *item, int automatic)
71 rec = g_new0(WINDOW_REC, 1);
72 rec->refnum = window_get_new_refnum();
74 windows = g_slist_prepend(windows, rec);
75 signal_emit("window created", 2, rec, GINT_TO_POINTER(automatic));
77 if (item != NULL) window_item_add(rec, item, automatic);
78 if (windows->next == NULL || !automatic || settings_get_bool("window_auto_change")) {
79 if (automatic && windows->next != NULL)
80 signal_emit("window changed automatic", 1, rec);
81 window_set_active(rec);
86 /* removed_refnum was removed from the windows list, pack the windows so
87 there won't be any holes. If there is any holes after removed_refnum,
88 leave the windows behind it alone. */
89 static void windows_pack(int removed_refnum)
94 for (refnum = removed_refnum+1;; refnum++) {
95 window = window_find_refnum(refnum);
96 if (window == NULL || window->sticky_refnum)
99 window_set_refnum(window, refnum-1);
103 void window_destroy(WINDOW_REC *window)
105 g_return_if_fail(window != NULL);
107 if (window->destroying) return;
108 window->destroying = TRUE;
109 windows = g_slist_remove(windows, window);
111 if (active_win == window && windows != NULL) {
112 active_win = NULL; /* it's corrupted */
113 window_set_active(windows->data);
116 while (window->items != NULL)
117 window_item_destroy(window->items->data);
119 if (settings_get_bool("windows_auto_renumber"))
120 windows_pack(window->refnum);
122 signal_emit("window destroyed", 1, window);
124 while (window->bound_items != NULL)
125 window_bind_destroy(window, window->bound_items->data);
127 g_free_not_null(window->hilight_color);
128 g_free_not_null(window->servertag);
129 g_free_not_null(window->theme_name);
130 g_free_not_null(window->name);
134 void window_auto_destroy(WINDOW_REC *window)
136 if (settings_get_bool("autoclose_windows") && windows->next != NULL &&
137 window->items == NULL && window->level == 0)
138 window_destroy(window);
141 void window_set_active(WINDOW_REC *window)
143 WINDOW_REC *old_window;
145 if (window == active_win)
148 old_window = active_win;
150 if (active_win != NULL) {
151 windows = g_slist_remove(windows, active_win);
152 windows = g_slist_prepend(windows, active_win);
155 if (active_win != NULL)
156 signal_emit("window changed", 2, active_win, old_window);
159 void window_change_server(WINDOW_REC *window, void *server)
161 window->active_server = server;
162 signal_emit("window server changed", 2, window, server);
165 void window_set_refnum(WINDOW_REC *window, int refnum)
170 g_return_if_fail(window != NULL);
171 g_return_if_fail(refnum >= 1);
172 if (window->refnum == refnum) return;
174 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
175 WINDOW_REC *rec = tmp->data;
177 if (rec->refnum == refnum) {
178 rec->refnum = window->refnum;
179 signal_emit("window refnum changed", 2, rec, GINT_TO_POINTER(refnum));
184 old_refnum = window->refnum;
185 window->refnum = refnum;
186 signal_emit("window refnum changed", 2, window, GINT_TO_POINTER(old_refnum));
189 void window_set_name(WINDOW_REC *window, const char *name)
191 g_free_not_null(window->name);
192 window->name = g_strdup(name);
194 signal_emit("window name changed", 1, window);
197 void window_set_level(WINDOW_REC *window, int level)
199 g_return_if_fail(window != NULL);
201 window->level = level;
202 signal_emit("window level changed", 1, window);
205 /* return active item's name, or if none is active, window's name */
206 char *window_get_active_name(WINDOW_REC *window)
208 g_return_val_if_fail(window != NULL, NULL);
210 if (window->active != NULL)
211 return window->active->name;
216 WINDOW_REC *window_find_level(void *server, int level)
222 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
223 WINDOW_REC *rec = tmp->data;
225 if ((server == NULL || rec->active_server == server) &&
226 (rec->level & level)) {
227 if (server == NULL || rec->active_server == server)
236 WINDOW_REC *window_find_closest(void *server, const char *name, int level)
242 item = name == NULL ? NULL :
243 window_item_find(server, name);
245 return window_item_window(item);
248 if (level != MSGLEVEL_HILIGHT)
249 level &= ~(MSGLEVEL_HILIGHT | MSGLEVEL_NOHILIGHT);
250 window = window_find_level(server, level);
251 if (window != NULL) return window;
253 /* match by level - ignore server */
254 window = window_find_level(NULL, level);
255 if (window != NULL) return window;
257 /* fallback to active */
261 WINDOW_REC *window_find_refnum(int refnum)
265 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
266 WINDOW_REC *rec = tmp->data;
268 if (rec->refnum == refnum)
275 WINDOW_REC *window_find_name(const char *name)
279 g_return_val_if_fail(name != NULL, NULL);
281 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
282 WINDOW_REC *rec = tmp->data;
284 if (rec->name != NULL && g_strcasecmp(rec->name, name) == 0)
291 WINDOW_REC *window_find_item(SERVER_REC *server, const char *name)
296 g_return_val_if_fail(name != NULL, NULL);
298 rec = window_find_name(name);
299 if (rec != NULL) return rec;
301 item = server == NULL ? NULL :
302 window_item_find(server, name);
303 if (item == NULL && server == NULL) {
304 /* not found from the active server - any server? */
305 item = window_item_find(NULL, name);
311 /* still nothing? maybe user just left the # in front of
312 channel, try again with it.. */
313 chan = g_strdup_printf("#%s", name);
314 item = server == NULL ? NULL :
315 window_item_find(server, chan);
316 if (item == NULL) item = window_item_find(NULL, chan);
323 return window_item_window(item);
326 int window_refnum_prev(int refnum, int wrap)
332 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
333 WINDOW_REC *rec = tmp->data;
335 if (rec->refnum < refnum && (prev == -1 || rec->refnum > prev))
337 if (wrap && (max == -1 || rec->refnum > max))
341 return prev != -1 ? prev : max;
344 int window_refnum_next(int refnum, int wrap)
350 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
351 WINDOW_REC *rec = tmp->data;
353 if (rec->refnum > refnum && (next == -1 || rec->refnum < next))
355 if (wrap && (min == -1 || rec->refnum < min))
359 return next != -1 ? next : min;
362 int windows_refnum_last(void)
368 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
369 WINDOW_REC *rec = tmp->data;
371 if (rec->refnum > max)
378 static int window_refnum_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
380 return w1->refnum < w2->refnum ? -1 : 1;
383 GSList *windows_get_sorted(void)
385 GSList *tmp, *sorted;
388 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
389 WINDOW_REC *rec = tmp->data;
391 sorted = g_slist_insert_sorted(sorted, rec, (GCompareFunc)
398 WINDOW_BIND_REC *window_bind_add(WINDOW_REC *window, const char *servertag,
401 WINDOW_BIND_REC *rec;
403 g_return_val_if_fail(window != NULL, NULL);
404 g_return_val_if_fail(servertag != NULL, NULL);
405 g_return_val_if_fail(name != NULL, NULL);
407 rec = g_new0(WINDOW_BIND_REC, 1);
408 rec->name = g_strdup(name);
409 rec->servertag = g_strdup(servertag);
411 window->bound_items = g_slist_append(window->bound_items, rec);
415 void window_bind_destroy(WINDOW_REC *window, WINDOW_BIND_REC *rec)
417 g_return_if_fail(window != NULL);
418 g_return_if_fail(rec != NULL);
420 window->bound_items = g_slist_remove(window->bound_items, rec);
422 g_free(rec->servertag);
427 WINDOW_BIND_REC *window_bind_find(WINDOW_REC *window, const char *servertag,
432 g_return_val_if_fail(window != NULL, NULL);
433 g_return_val_if_fail(servertag != NULL, NULL);
434 g_return_val_if_fail(name != NULL, NULL);
436 for (tmp = window->bound_items; tmp != NULL; tmp = tmp->next) {
437 WINDOW_BIND_REC *rec = tmp->data;
439 if (g_strcasecmp(rec->name, name) == 0 &&
440 g_strcasecmp(rec->servertag, servertag) == 0)
447 void window_bind_remove_unsticky(WINDOW_REC *window)
451 for (tmp = window->bound_items; tmp != NULL; tmp = next) {
452 WINDOW_BIND_REC *rec = tmp->data;
456 window_bind_destroy(window, rec);
460 static void sig_server_looking(SERVER_REC *server)
464 g_return_if_fail(server != NULL);
466 /* Try to keep some server assigned to windows..
467 Also change active window's server if the window is empty */
468 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
469 WINDOW_REC *rec = tmp->data;
471 if ((rec->servertag == NULL ||
472 g_strcasecmp(rec->servertag, server->tag) == 0) &&
473 (rec->active_server == NULL ||
474 (rec == active_win && rec->items == NULL)))
475 window_change_server(rec, server);
479 static void sig_server_disconnected(SERVER_REC *server)
482 SERVER_REC *new_server;
484 g_return_if_fail(server != NULL);
486 new_server = servers == NULL ? NULL : servers->data;
487 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
488 WINDOW_REC *rec = tmp->data;
490 if (rec->active_server == server) {
491 window_change_server(rec, rec->servertag != NULL ?
497 static void sig_print_text(void)
506 if (strftime(month, sizeof(month), "%b", tm) <= 0)
509 if (tm->tm_hour != 0 || tm->tm_min != 0)
513 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
515 /* day changed, print notice about it to every window */
516 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
517 printformat_window(tmp->data, MSGLEVEL_NEVER, TXT_DAYCHANGE,
518 tm->tm_mday, tm->tm_mon+1,
519 1900+tm->tm_year, month);
523 static int sig_check_daychange(void)
531 if (daycheck == 1 && tm->tm_hour == 0 && tm->tm_min == 0) {
536 if (tm->tm_hour != 23 || tm->tm_min != 59) {
544 signal_add("print text", (SIGNAL_FUNC) sig_print_text);
549 static void read_settings(void)
552 g_source_remove(daytag);
556 if (settings_get_bool("timestamps"))
557 daytag = g_timeout_add(30000, (GSourceFunc) sig_check_daychange, NULL);
560 void windows_init(void)
563 daycheck = 0; daytag = -1;
564 settings_add_bool("lookandfeel", "window_auto_change", FALSE);
565 settings_add_bool("lookandfeel", "windows_auto_renumber", TRUE);
568 signal_add("server looking", (SIGNAL_FUNC) sig_server_looking);
569 signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
570 signal_add("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
571 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
574 void windows_deinit(void)
576 if (daytag != -1) g_source_remove(daytag);
577 if (daycheck == 1) signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
579 signal_remove("server looking", (SIGNAL_FUNC) sig_server_looking);
580 signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
581 signal_remove("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
582 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);