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 && windows != NULL) {
113 active_win = NULL; /* it's corrupted */
114 window_set_active(windows->data);
117 while (window->items != NULL)
118 window_item_destroy(window->items->data);
120 if (settings_get_bool("windows_auto_renumber"))
121 windows_pack(window->refnum);
123 signal_emit("window destroyed", 1, window);
125 while (window->bound_items != NULL)
126 window_bind_destroy(window, window->bound_items->data);
128 g_free_not_null(window->hilight_color);
129 g_free_not_null(window->servertag);
130 g_free_not_null(window->theme_name);
131 g_free_not_null(window->name);
135 void window_auto_destroy(WINDOW_REC *window)
137 if (settings_get_bool("autoclose_windows") && windows->next != NULL &&
138 window->items == NULL && window->bound_items == NULL &&
139 window->level == 0 && !window->immortal)
140 window_destroy(window);
143 void window_set_active(WINDOW_REC *window)
145 WINDOW_REC *old_window;
147 if (window == active_win)
150 old_window = active_win;
152 if (active_win != NULL) {
153 windows = g_slist_remove(windows, active_win);
154 windows = g_slist_prepend(windows, active_win);
157 if (active_win != NULL)
158 signal_emit("window changed", 2, active_win, old_window);
161 void window_change_server(WINDOW_REC *window, void *server)
163 window->active_server = server;
164 signal_emit("window server changed", 2, window, server);
167 void window_set_refnum(WINDOW_REC *window, int refnum)
172 g_return_if_fail(window != NULL);
173 g_return_if_fail(refnum >= 1);
174 if (window->refnum == refnum) return;
176 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
177 WINDOW_REC *rec = tmp->data;
179 if (rec->refnum == refnum) {
180 rec->refnum = window->refnum;
181 signal_emit("window refnum changed", 2, rec, GINT_TO_POINTER(refnum));
186 old_refnum = window->refnum;
187 window->refnum = refnum;
188 signal_emit("window refnum changed", 2, window, GINT_TO_POINTER(old_refnum));
191 void window_set_name(WINDOW_REC *window, const char *name)
193 g_free_not_null(window->name);
194 window->name = g_strdup(name);
196 signal_emit("window name changed", 1, window);
199 void window_set_history(WINDOW_REC *window, const char *name)
202 oldname = window->history_name;
204 if (name == NULL || *name == '\0')
205 window->history_name = NULL;
207 window->history_name = g_strdup(name);
209 signal_emit("window history changed", 1, window, oldname);
211 g_free_not_null(oldname);
214 void window_set_level(WINDOW_REC *window, int level)
216 g_return_if_fail(window != NULL);
218 window->level = level;
219 signal_emit("window level changed", 1, window);
222 void window_set_immortal(WINDOW_REC *window, int immortal)
224 g_return_if_fail(window != NULL);
226 window->immortal = immortal;
227 signal_emit("window immortal changed", 1, window);
230 /* return active item's name, or if none is active, window's name */
231 char *window_get_active_name(WINDOW_REC *window)
233 g_return_val_if_fail(window != NULL, NULL);
235 if (window->active != NULL)
236 return window->active->name;
241 WINDOW_REC *window_find_level(void *server, int level)
247 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
248 WINDOW_REC *rec = tmp->data;
250 if ((server == NULL || rec->active_server == server) &&
251 (rec->level & level)) {
252 if (server == NULL || rec->active_server == server)
261 WINDOW_REC *window_find_closest(void *server, const char *name, int level)
263 WINDOW_REC *window,*namewindow=NULL;
267 item = name == NULL ? NULL :
268 window_item_find(server, name);
270 namewindow = window_item_window(item);
271 if (namewindow != NULL && ((namewindow->level & level) != 0 ||
272 !settings_get_bool("window_check_level_first")))
277 if (level != MSGLEVEL_HILIGHT)
278 level &= ~(MSGLEVEL_HILIGHT | MSGLEVEL_NOHILIGHT);
279 window = window_find_level(server, level);
280 if (window != NULL) return window;
282 /* match by level - ignore server */
283 window = window_find_level(NULL, level);
284 if (window != NULL) return window;
286 /* still return item's window if we didnt find anything */
287 if (namewindow != NULL) return namewindow;
289 /* fallback to active */
293 WINDOW_REC *window_find_refnum(int refnum)
297 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
298 WINDOW_REC *rec = tmp->data;
300 if (rec->refnum == refnum)
307 WINDOW_REC *window_find_name(const char *name)
311 g_return_val_if_fail(name != NULL, NULL);
313 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
314 WINDOW_REC *rec = tmp->data;
316 if (rec->name != NULL && g_strcasecmp(rec->name, name) == 0)
323 WINDOW_REC *window_find_item(SERVER_REC *server, const char *name)
328 g_return_val_if_fail(name != NULL, NULL);
330 rec = window_find_name(name);
331 if (rec != NULL) return rec;
333 item = server == NULL ? NULL :
334 window_item_find(server, name);
335 if (item == NULL && server == NULL) {
336 /* not found from the active server - any server? */
337 item = window_item_find(NULL, name);
343 /* still nothing? maybe user just left the # in front of
344 channel, try again with it.. */
345 chan = g_strdup_printf("#%s", name);
346 item = server == NULL ? NULL :
347 window_item_find(server, chan);
348 if (item == NULL) item = window_item_find(NULL, chan);
355 return window_item_window(item);
358 int window_refnum_prev(int refnum, int wrap)
364 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
365 WINDOW_REC *rec = tmp->data;
367 if (rec->refnum < refnum && (prev == -1 || rec->refnum > prev))
369 if (wrap && (max == -1 || rec->refnum > max))
373 return prev != -1 ? prev : max;
376 int window_refnum_next(int refnum, int wrap)
382 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
383 WINDOW_REC *rec = tmp->data;
385 if (rec->refnum > refnum && (next == -1 || rec->refnum < next))
387 if (wrap && (min == -1 || rec->refnum < min))
391 return next != -1 ? next : min;
394 int windows_refnum_last(void)
400 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
401 WINDOW_REC *rec = tmp->data;
403 if (rec->refnum > max)
410 int window_refnum_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
412 return w1->refnum < w2->refnum ? -1 : 1;
415 GSList *windows_get_sorted(void)
417 GSList *tmp, *sorted;
420 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
421 WINDOW_REC *rec = tmp->data;
423 sorted = g_slist_insert_sorted(sorted, rec, (GCompareFunc)
430 /* Add a new bind to window - if duplicate is found it's returned */
431 WINDOW_BIND_REC *window_bind_add(WINDOW_REC *window, const char *servertag,
434 WINDOW_BIND_REC *rec;
436 g_return_val_if_fail(window != NULL, NULL);
437 g_return_val_if_fail(servertag != NULL, NULL);
438 g_return_val_if_fail(name != NULL, NULL);
440 rec = window_bind_find(window, servertag, name);
444 rec = g_new0(WINDOW_BIND_REC, 1);
445 rec->name = g_strdup(name);
446 rec->servertag = g_strdup(servertag);
448 window->bound_items = g_slist_append(window->bound_items, rec);
452 void window_bind_destroy(WINDOW_REC *window, WINDOW_BIND_REC *rec)
454 g_return_if_fail(window != NULL);
455 g_return_if_fail(rec != NULL);
457 window->bound_items = g_slist_remove(window->bound_items, rec);
459 g_free(rec->servertag);
464 WINDOW_BIND_REC *window_bind_find(WINDOW_REC *window, const char *servertag,
469 g_return_val_if_fail(window != NULL, NULL);
470 g_return_val_if_fail(servertag != NULL, NULL);
471 g_return_val_if_fail(name != NULL, NULL);
473 for (tmp = window->bound_items; tmp != NULL; tmp = tmp->next) {
474 WINDOW_BIND_REC *rec = tmp->data;
476 if (g_strcasecmp(rec->name, name) == 0 &&
477 g_strcasecmp(rec->servertag, servertag) == 0)
484 void window_bind_remove_unsticky(WINDOW_REC *window)
488 for (tmp = window->bound_items; tmp != NULL; tmp = next) {
489 WINDOW_BIND_REC *rec = tmp->data;
493 window_bind_destroy(window, rec);
497 static void sig_server_looking(SERVER_REC *server)
501 g_return_if_fail(server != NULL);
503 /* Try to keep some server assigned to windows..
504 Also change active window's server if the window is empty */
505 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
506 WINDOW_REC *rec = tmp->data;
508 if ((rec->servertag == NULL ||
509 g_strcasecmp(rec->servertag, server->tag) == 0) &&
510 (rec->active_server == NULL ||
511 (rec == active_win && rec->items == NULL)))
512 window_change_server(rec, server);
516 static void sig_server_disconnected(SERVER_REC *server)
519 SERVER_REC *new_server;
521 g_return_if_fail(server != NULL);
523 new_server = servers == NULL ? NULL : servers->data;
524 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
525 WINDOW_REC *rec = tmp->data;
527 if (rec->active_server == server) {
528 window_change_server(rec, rec->servertag != NULL ?
534 static void window_print_daychange(WINDOW_REC *window, struct tm *tm)
538 char *format, str[256];
540 theme = active_win->theme != NULL ? active_win->theme : current_theme;
541 format_create_dest(&dest, NULL, NULL, MSGLEVEL_NEVER, window);
542 format = format_get_text_theme(theme, MODULE_NAME, &dest,
544 if (strftime(str, sizeof(str), format, tm) <= 0)
548 printtext_string_window(window, MSGLEVEL_NEVER, str);
551 static void sig_print_text(void)
559 if (tm->tm_hour != 0 || tm->tm_min != 0)
563 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
565 /* day changed, print notice about it to every window */
566 for (tmp = windows; tmp != NULL; tmp = tmp->next)
567 window_print_daychange(tmp->data, tm);
570 static int sig_check_daychange(void)
578 if (daycheck == 1 && tm->tm_hour == 0 && tm->tm_min == 0) {
583 if (tm->tm_hour != 23 || tm->tm_min != 59) {
591 signal_add("print text", (SIGNAL_FUNC) sig_print_text);
596 static void read_settings(void)
599 g_source_remove(daytag);
603 if (settings_get_bool("timestamps"))
604 daytag = g_timeout_add(30000, (GSourceFunc) sig_check_daychange, NULL);
607 void windows_init(void)
610 daycheck = 0; daytag = -1;
611 settings_add_bool("lookandfeel", "window_auto_change", FALSE);
612 settings_add_bool("lookandfeel", "windows_auto_renumber", TRUE);
613 settings_add_bool("lookandfeel", "window_check_level_first", FALSE);
614 settings_add_str("lookandfeel", "window_default_level", "NONE");
617 signal_add("server looking", (SIGNAL_FUNC) sig_server_looking);
618 signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
619 signal_add("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
620 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
623 void windows_deinit(void)
625 if (daytag != -1) g_source_remove(daytag);
626 if (daycheck == 1) signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
628 signal_remove("server looking", (SIGNAL_FUNC) sig_server_looking);
629 signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
630 signal_remove("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
631 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);