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();
73 rec->level = level2bits(settings_get_str("window_default_level"));
75 windows = g_slist_prepend(windows, rec);
76 signal_emit("window created", 2, rec, GINT_TO_POINTER(automatic));
78 if (item != NULL) window_item_add(rec, item, automatic);
79 if (windows->next == NULL || !automatic || settings_get_bool("window_auto_change")) {
80 if (automatic && windows->next != NULL)
81 signal_emit("window changed automatic", 1, rec);
82 window_set_active(rec);
87 /* removed_refnum was removed from the windows list, pack the windows so
88 there won't be any holes. If there is any holes after removed_refnum,
89 leave the windows behind it alone. */
90 static void windows_pack(int removed_refnum)
95 for (refnum = removed_refnum+1;; refnum++) {
96 window = window_find_refnum(refnum);
97 if (window == NULL || window->sticky_refnum)
100 window_set_refnum(window, refnum-1);
104 void window_destroy(WINDOW_REC *window)
106 g_return_if_fail(window != NULL);
108 if (window->destroying) return;
109 window->destroying = TRUE;
110 windows = g_slist_remove(windows, window);
112 if (active_win == window) {
113 active_win = NULL; /* it's corrupted */
115 window_set_active(windows->data);
118 while (window->items != NULL)
119 window_item_destroy(window->items->data);
121 if (settings_get_bool("windows_auto_renumber"))
122 windows_pack(window->refnum);
124 signal_emit("window destroyed", 1, window);
126 while (window->bound_items != NULL)
127 window_bind_destroy(window, window->bound_items->data);
129 g_free_not_null(window->hilight_color);
130 g_free_not_null(window->servertag);
131 g_free_not_null(window->theme_name);
132 g_free_not_null(window->name);
136 void window_auto_destroy(WINDOW_REC *window)
138 if (settings_get_bool("autoclose_windows") && windows->next != NULL &&
139 window->items == NULL && window->bound_items == NULL &&
140 window->level == 0 && !window->immortal)
141 window_destroy(window);
144 void window_set_active(WINDOW_REC *window)
146 WINDOW_REC *old_window;
148 if (window == active_win)
151 old_window = active_win;
153 if (active_win != NULL) {
154 windows = g_slist_remove(windows, active_win);
155 windows = g_slist_prepend(windows, active_win);
158 if (active_win != NULL)
159 signal_emit("window changed", 2, active_win, old_window);
162 void window_change_server(WINDOW_REC *window, void *server)
164 SERVER_REC *active, *connect;
166 if (server != NULL && SERVER(server)->disconnected)
169 if (server == NULL) {
170 active = connect = NULL;
171 } else if (g_slist_find(servers, server) != NULL) {
179 if (window->connect_server != connect) {
180 window->connect_server = connect;
181 signal_emit("window connect changed", 2, window, connect);
184 if (window->active_server != active) {
185 window->active_server = active;
186 signal_emit("window server changed", 2, window, active);
190 void window_set_refnum(WINDOW_REC *window, int refnum)
195 g_return_if_fail(window != NULL);
196 g_return_if_fail(refnum >= 1);
197 if (window->refnum == refnum) return;
199 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
200 WINDOW_REC *rec = tmp->data;
202 if (rec->refnum == refnum) {
203 rec->refnum = window->refnum;
204 signal_emit("window refnum changed", 2, rec, GINT_TO_POINTER(refnum));
209 old_refnum = window->refnum;
210 window->refnum = refnum;
211 signal_emit("window refnum changed", 2, window, GINT_TO_POINTER(old_refnum));
214 void window_set_name(WINDOW_REC *window, const char *name)
216 g_free_not_null(window->name);
217 window->name = name == NULL || *name == '\0' ? NULL : g_strdup(name);
219 signal_emit("window name changed", 1, window);
222 void window_set_history(WINDOW_REC *window, const char *name)
225 oldname = window->history_name;
227 if (name == NULL || *name == '\0')
228 window->history_name = NULL;
230 window->history_name = g_strdup(name);
232 signal_emit("window history changed", 1, window, oldname);
234 g_free_not_null(oldname);
237 void window_set_level(WINDOW_REC *window, int level)
239 g_return_if_fail(window != NULL);
241 window->level = level;
242 signal_emit("window level changed", 1, window);
245 void window_set_immortal(WINDOW_REC *window, int immortal)
247 g_return_if_fail(window != NULL);
249 window->immortal = immortal;
250 signal_emit("window immortal changed", 1, window);
253 /* return active item's name, or if none is active, window's name */
254 const char *window_get_active_name(WINDOW_REC *window)
256 g_return_val_if_fail(window != NULL, NULL);
258 if (window->active != NULL)
259 return window->active->visible_name;
264 #define WINDOW_LEVEL_MATCH(window, server, level) \
265 (((window)->level & level) && \
266 (server == NULL || (window)->active_server == server))
268 WINDOW_REC *window_find_level(void *server, int level)
274 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
275 WINDOW_REC *rec = tmp->data;
277 if (WINDOW_LEVEL_MATCH(rec, server, level)) {
278 /* prefer windows without any items */
279 if (rec->items == NULL)
284 else if (active_win == rec) {
285 /* prefer active window over others */
294 WINDOW_REC *window_find_closest(void *server, const char *name, int level)
296 WINDOW_REC *window,*namewindow=NULL;
301 item = name == NULL ? NULL :
302 window_item_find(server, name);
304 namewindow = window_item_window(item);
305 if (namewindow != NULL &&
306 ((namewindow->level & level) != 0 ||
307 !settings_get_bool("window_check_level_first"))) {
308 /* match, but if multiple windows have the same level
309 we could be choosing a bad one here, eg.
310 name=nick1 would get nick2's query instead of
311 generic msgs window. */
312 if (g_strcasecmp(name, item->visible_name) == 0)
317 /* prefer windows without items */
318 for (i = 0; i < 2; i++) {
320 if (level != MSGLEVEL_HILIGHT)
321 level &= ~(MSGLEVEL_HILIGHT | MSGLEVEL_NOHILIGHT);
322 window = window_find_level(server, level);
323 if (window != NULL && (i == 1 || window->items == NULL))
326 /* match by level - ignore server */
327 window = window_find_level(NULL, level);
328 if (window != NULL && (i == 1 || window->items == NULL))
332 /* still return item's window if we didnt find anything */
333 if (namewindow != NULL) return namewindow;
335 /* fallback to active */
339 WINDOW_REC *window_find_refnum(int refnum)
343 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
344 WINDOW_REC *rec = tmp->data;
346 if (rec->refnum == refnum)
353 WINDOW_REC *window_find_name(const char *name)
357 g_return_val_if_fail(name != NULL, NULL);
359 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
360 WINDOW_REC *rec = tmp->data;
362 if (rec->name != NULL && g_strcasecmp(rec->name, name) == 0)
369 WINDOW_REC *window_find_item(SERVER_REC *server, const char *name)
374 g_return_val_if_fail(name != NULL, NULL);
376 rec = window_find_name(name);
377 if (rec != NULL) return rec;
379 item = server == NULL ? NULL :
380 window_item_find(server, name);
382 /* not found from the active server - any server? */
383 item = window_item_find(NULL, name);
389 return window_item_window(item);
392 int window_refnum_prev(int refnum, int wrap)
398 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
399 WINDOW_REC *rec = tmp->data;
401 if (rec->refnum < refnum && (prev == -1 || rec->refnum > prev))
403 if (wrap && (max == -1 || rec->refnum > max))
407 return prev != -1 ? prev : max;
410 int window_refnum_next(int refnum, int wrap)
416 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
417 WINDOW_REC *rec = tmp->data;
419 if (rec->refnum > refnum && (next == -1 || rec->refnum < next))
421 if (wrap && (min == -1 || rec->refnum < min))
425 return next != -1 ? next : min;
428 int windows_refnum_last(void)
434 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
435 WINDOW_REC *rec = tmp->data;
437 if (rec->refnum > max)
444 int window_refnum_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
446 return w1->refnum < w2->refnum ? -1 : 1;
449 GSList *windows_get_sorted(void)
451 GSList *tmp, *sorted;
454 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
455 WINDOW_REC *rec = tmp->data;
457 sorted = g_slist_insert_sorted(sorted, rec, (GCompareFunc)
464 /* Add a new bind to window - if duplicate is found it's returned */
465 WINDOW_BIND_REC *window_bind_add(WINDOW_REC *window, const char *servertag,
468 WINDOW_BIND_REC *rec;
470 g_return_val_if_fail(window != NULL, NULL);
471 g_return_val_if_fail(servertag != NULL, NULL);
472 g_return_val_if_fail(name != NULL, NULL);
474 rec = window_bind_find(window, servertag, name);
478 rec = g_new0(WINDOW_BIND_REC, 1);
479 rec->name = g_strdup(name);
480 rec->servertag = g_strdup(servertag);
482 window->bound_items = g_slist_append(window->bound_items, rec);
486 void window_bind_destroy(WINDOW_REC *window, WINDOW_BIND_REC *rec)
488 g_return_if_fail(window != NULL);
489 g_return_if_fail(rec != NULL);
491 window->bound_items = g_slist_remove(window->bound_items, rec);
493 g_free(rec->servertag);
498 WINDOW_BIND_REC *window_bind_find(WINDOW_REC *window, const char *servertag,
503 g_return_val_if_fail(window != NULL, NULL);
504 g_return_val_if_fail(servertag != NULL, NULL);
505 g_return_val_if_fail(name != NULL, NULL);
507 for (tmp = window->bound_items; tmp != NULL; tmp = tmp->next) {
508 WINDOW_BIND_REC *rec = tmp->data;
510 if (g_strcasecmp(rec->name, name) == 0 &&
511 g_strcasecmp(rec->servertag, servertag) == 0)
518 void window_bind_remove_unsticky(WINDOW_REC *window)
522 for (tmp = window->bound_items; tmp != NULL; tmp = next) {
523 WINDOW_BIND_REC *rec = tmp->data;
527 window_bind_destroy(window, rec);
531 static void sig_server_connected(SERVER_REC *server)
535 g_return_if_fail(server != NULL);
537 /* Try to keep some server assigned to windows..
538 Also change active window's server if the window is empty */
539 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
540 WINDOW_REC *rec = tmp->data;
542 if ((rec->servertag == NULL ||
543 g_strcasecmp(rec->servertag, server->tag) == 0) &&
544 (rec->active_server == NULL ||
545 (rec == active_win && rec->items == NULL)))
546 window_change_server(rec, server);
550 static void sig_server_disconnected(SERVER_REC *server)
553 SERVER_REC *new_server;
555 g_return_if_fail(server != NULL);
557 new_server = servers == NULL ? NULL : servers->data;
558 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
559 WINDOW_REC *rec = tmp->data;
561 if (rec->active_server == server ||
562 rec->connect_server == server) {
563 window_change_server(rec, rec->servertag != NULL ?
569 static void window_print_daychange(WINDOW_REC *window, struct tm *tm)
573 char *format, str[256];
575 theme = active_win->theme != NULL ? active_win->theme : current_theme;
576 format_create_dest(&dest, NULL, NULL, MSGLEVEL_NEVER, window);
577 format = format_get_text_theme(theme, MODULE_NAME, &dest,
579 if (strftime(str, sizeof(str), format, tm) <= 0)
583 printtext_string_window(window, MSGLEVEL_NEVER, str);
586 static void sig_print_text(void)
594 if (tm->tm_hour != 0 || tm->tm_min != 0)
598 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
600 /* day changed, print notice about it to every window */
601 for (tmp = windows; tmp != NULL; tmp = tmp->next)
602 window_print_daychange(tmp->data, tm);
605 static int sig_check_daychange(void)
613 if (daycheck == 1 && tm->tm_hour == 0 && tm->tm_min == 0) {
618 if (tm->tm_hour != 23 || tm->tm_min != 59) {
626 signal_add("print text", (SIGNAL_FUNC) sig_print_text);
631 static void read_settings(void)
634 g_source_remove(daytag);
638 if (settings_get_bool("timestamps"))
639 daytag = g_timeout_add(30000, (GSourceFunc) sig_check_daychange, NULL);
642 void windows_init(void)
645 daycheck = 0; daytag = -1;
646 settings_add_bool("lookandfeel", "window_auto_change", FALSE);
647 settings_add_bool("lookandfeel", "windows_auto_renumber", TRUE);
648 settings_add_bool("lookandfeel", "window_check_level_first", FALSE);
649 settings_add_str("lookandfeel", "window_default_level", "NONE");
652 signal_add("server looking", (SIGNAL_FUNC) sig_server_connected);
653 signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
654 signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
655 signal_add("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
656 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
659 void windows_deinit(void)
661 if (daytag != -1) g_source_remove(daytag);
662 if (daycheck == 1) signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
664 signal_remove("server looking", (SIGNAL_FUNC) sig_server_connected);
665 signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
666 signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
667 signal_remove("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
668 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);