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->bound_items == NULL &&
139 window_destroy(window);
142 void window_set_active(WINDOW_REC *window)
144 WINDOW_REC *old_window;
146 if (window == active_win)
149 old_window = active_win;
151 if (active_win != NULL) {
152 windows = g_slist_remove(windows, active_win);
153 windows = g_slist_prepend(windows, active_win);
156 if (active_win != NULL)
157 signal_emit("window changed", 2, active_win, old_window);
160 void window_change_server(WINDOW_REC *window, void *server)
162 window->active_server = server;
163 signal_emit("window server changed", 2, window, server);
166 void window_set_refnum(WINDOW_REC *window, int refnum)
171 g_return_if_fail(window != NULL);
172 g_return_if_fail(refnum >= 1);
173 if (window->refnum == refnum) return;
175 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
176 WINDOW_REC *rec = tmp->data;
178 if (rec->refnum == refnum) {
179 rec->refnum = window->refnum;
180 signal_emit("window refnum changed", 2, rec, GINT_TO_POINTER(refnum));
185 old_refnum = window->refnum;
186 window->refnum = refnum;
187 signal_emit("window refnum changed", 2, window, GINT_TO_POINTER(old_refnum));
190 void window_set_name(WINDOW_REC *window, const char *name)
192 g_free_not_null(window->name);
193 window->name = g_strdup(name);
195 signal_emit("window name changed", 1, window);
198 void window_set_history(WINDOW_REC *window, const char *name)
201 oldname = window->history_name;
203 if (name == NULL || *name == '\0')
204 window->history_name = NULL;
206 window->history_name = g_strdup(name);
208 signal_emit("window history changed", 1, window, oldname);
210 g_free_not_null(oldname);
213 void window_set_level(WINDOW_REC *window, int level)
215 g_return_if_fail(window != NULL);
217 window->level = level;
218 signal_emit("window level changed", 1, window);
221 /* return active item's name, or if none is active, window's name */
222 char *window_get_active_name(WINDOW_REC *window)
224 g_return_val_if_fail(window != NULL, NULL);
226 if (window->active != NULL)
227 return window->active->name;
232 WINDOW_REC *window_find_level(void *server, int level)
238 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
239 WINDOW_REC *rec = tmp->data;
241 if ((server == NULL || rec->active_server == server) &&
242 (rec->level & level)) {
243 if (server == NULL || rec->active_server == server)
252 WINDOW_REC *window_find_closest(void *server, const char *name, int level)
258 item = name == NULL ? NULL :
259 window_item_find(server, name);
261 return window_item_window(item);
264 if (level != MSGLEVEL_HILIGHT)
265 level &= ~(MSGLEVEL_HILIGHT | MSGLEVEL_NOHILIGHT);
266 window = window_find_level(server, level);
267 if (window != NULL) return window;
269 /* match by level - ignore server */
270 window = window_find_level(NULL, level);
271 if (window != NULL) return window;
273 /* fallback to active */
277 WINDOW_REC *window_find_refnum(int refnum)
281 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
282 WINDOW_REC *rec = tmp->data;
284 if (rec->refnum == refnum)
291 WINDOW_REC *window_find_name(const char *name)
295 g_return_val_if_fail(name != NULL, NULL);
297 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
298 WINDOW_REC *rec = tmp->data;
300 if (rec->name != NULL && g_strcasecmp(rec->name, name) == 0)
307 WINDOW_REC *window_find_item(SERVER_REC *server, const char *name)
312 g_return_val_if_fail(name != NULL, NULL);
314 rec = window_find_name(name);
315 if (rec != NULL) return rec;
317 item = server == NULL ? NULL :
318 window_item_find(server, name);
319 if (item == NULL && server == NULL) {
320 /* not found from the active server - any server? */
321 item = window_item_find(NULL, name);
327 /* still nothing? maybe user just left the # in front of
328 channel, try again with it.. */
329 chan = g_strdup_printf("#%s", name);
330 item = server == NULL ? NULL :
331 window_item_find(server, chan);
332 if (item == NULL) item = window_item_find(NULL, chan);
339 return window_item_window(item);
342 int window_refnum_prev(int refnum, int wrap)
348 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
349 WINDOW_REC *rec = tmp->data;
351 if (rec->refnum < refnum && (prev == -1 || rec->refnum > prev))
353 if (wrap && (max == -1 || rec->refnum > max))
357 return prev != -1 ? prev : max;
360 int window_refnum_next(int refnum, int wrap)
366 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
367 WINDOW_REC *rec = tmp->data;
369 if (rec->refnum > refnum && (next == -1 || rec->refnum < next))
371 if (wrap && (min == -1 || rec->refnum < min))
375 return next != -1 ? next : min;
378 int windows_refnum_last(void)
384 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
385 WINDOW_REC *rec = tmp->data;
387 if (rec->refnum > max)
394 int window_refnum_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
396 return w1->refnum < w2->refnum ? -1 : 1;
399 GSList *windows_get_sorted(void)
401 GSList *tmp, *sorted;
404 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
405 WINDOW_REC *rec = tmp->data;
407 sorted = g_slist_insert_sorted(sorted, rec, (GCompareFunc)
414 /* Add a new bind to window - if duplicate is found it's returned */
415 WINDOW_BIND_REC *window_bind_add(WINDOW_REC *window, const char *servertag,
418 WINDOW_BIND_REC *rec;
420 g_return_val_if_fail(window != NULL, NULL);
421 g_return_val_if_fail(servertag != NULL, NULL);
422 g_return_val_if_fail(name != NULL, NULL);
424 rec = window_bind_find(window, servertag, name);
428 rec = g_new0(WINDOW_BIND_REC, 1);
429 rec->name = g_strdup(name);
430 rec->servertag = g_strdup(servertag);
432 window->bound_items = g_slist_append(window->bound_items, rec);
436 void window_bind_destroy(WINDOW_REC *window, WINDOW_BIND_REC *rec)
438 g_return_if_fail(window != NULL);
439 g_return_if_fail(rec != NULL);
441 window->bound_items = g_slist_remove(window->bound_items, rec);
443 g_free(rec->servertag);
448 WINDOW_BIND_REC *window_bind_find(WINDOW_REC *window, const char *servertag,
453 g_return_val_if_fail(window != NULL, NULL);
454 g_return_val_if_fail(servertag != NULL, NULL);
455 g_return_val_if_fail(name != NULL, NULL);
457 for (tmp = window->bound_items; tmp != NULL; tmp = tmp->next) {
458 WINDOW_BIND_REC *rec = tmp->data;
460 if (g_strcasecmp(rec->name, name) == 0 &&
461 g_strcasecmp(rec->servertag, servertag) == 0)
468 void window_bind_remove_unsticky(WINDOW_REC *window)
472 for (tmp = window->bound_items; tmp != NULL; tmp = next) {
473 WINDOW_BIND_REC *rec = tmp->data;
477 window_bind_destroy(window, rec);
481 static void sig_server_looking(SERVER_REC *server)
485 g_return_if_fail(server != NULL);
487 /* Try to keep some server assigned to windows..
488 Also change active window's server if the window is empty */
489 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
490 WINDOW_REC *rec = tmp->data;
492 if ((rec->servertag == NULL ||
493 g_strcasecmp(rec->servertag, server->tag) == 0) &&
494 (rec->active_server == NULL ||
495 (rec == active_win && rec->items == NULL)))
496 window_change_server(rec, server);
500 static void sig_server_disconnected(SERVER_REC *server)
503 SERVER_REC *new_server;
505 g_return_if_fail(server != NULL);
507 new_server = servers == NULL ? NULL : servers->data;
508 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
509 WINDOW_REC *rec = tmp->data;
511 if (rec->active_server == server) {
512 window_change_server(rec, rec->servertag != NULL ?
518 static void window_print_daychange(WINDOW_REC *window, struct tm *tm)
522 char *format, str[256];
524 theme = active_win->theme != NULL ? active_win->theme : current_theme;
525 format_create_dest(&dest, NULL, NULL, MSGLEVEL_NEVER, window);
526 format = format_get_text_theme(theme, MODULE_NAME, &dest,
528 if (strftime(str, sizeof(str), format, tm) <= 0)
532 printtext_string_window(window, MSGLEVEL_NEVER, str);
535 static void sig_print_text(void)
543 if (tm->tm_hour != 0 || tm->tm_min != 0)
547 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
549 /* day changed, print notice about it to every window */
550 for (tmp = windows; tmp != NULL; tmp = tmp->next)
551 window_print_daychange(tmp->data, tm);
554 static int sig_check_daychange(void)
562 if (daycheck == 1 && tm->tm_hour == 0 && tm->tm_min == 0) {
567 if (tm->tm_hour != 23 || tm->tm_min != 59) {
575 signal_add("print text", (SIGNAL_FUNC) sig_print_text);
580 static void read_settings(void)
583 g_source_remove(daytag);
587 if (settings_get_bool("timestamps"))
588 daytag = g_timeout_add(30000, (GSourceFunc) sig_check_daychange, NULL);
591 void windows_init(void)
594 daycheck = 0; daytag = -1;
595 settings_add_bool("lookandfeel", "window_auto_change", FALSE);
596 settings_add_bool("lookandfeel", "windows_auto_renumber", TRUE);
599 signal_add("server looking", (SIGNAL_FUNC) sig_server_looking);
600 signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
601 signal_add("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
602 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
605 void windows_deinit(void)
607 if (daytag != -1) g_source_remove(daytag);
608 if (daycheck == 1) signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
610 signal_remove("server looking", (SIGNAL_FUNC) sig_server_looking);
611 signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
612 signal_remove("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
613 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);