Applied fixed from cras to fix crashes in irssi.
[silc.git] / apps / irssi / src / fe-common / core / fe-windows.c
1 /*
2  windows.c : irssi
3
4     Copyright (C) 1999-2000 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 "module-formats.h"
23 #include "modules.h"
24 #include "signals.h"
25 #include "commands.h"
26 #include "servers.h"
27 #include "misc.h"
28 #include "settings.h"
29
30 #include "levels.h"
31
32 #include "printtext.h"
33 #include "fe-windows.h"
34 #include "window-items.h"
35
36 GSList *windows; /* first in the list is the active window,
37                     next is the last active, etc. */
38 WINDOW_REC *active_win;
39
40 static int daytag;
41 static int daycheck; /* 0 = don't check, 1 = time is 00:00, check,
42                         2 = time is 00:00, already checked */
43
44 static int window_get_new_refnum(void)
45 {
46         WINDOW_REC *win;
47         GSList *tmp;
48         int refnum;
49
50         refnum = 1;
51         tmp = windows;
52         while (tmp != NULL) {
53                 win = tmp->data;
54
55                 if (refnum != win->refnum) {
56                         tmp = tmp->next;
57                         continue;
58                 }
59
60                 refnum++;
61                 tmp = windows;
62         }
63
64         return refnum;
65 }
66
67 WINDOW_REC *window_create(WI_ITEM_REC *item, int automatic)
68 {
69         WINDOW_REC *rec;
70
71         rec = g_new0(WINDOW_REC, 1);
72         rec->refnum = window_get_new_refnum();
73         rec->level = level2bits(settings_get_str("window_default_level"));
74
75         windows = g_slist_prepend(windows, rec);
76         signal_emit("window created", 2, rec, GINT_TO_POINTER(automatic));
77
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);
83         }
84         return rec;
85 }
86
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)
91 {
92         WINDOW_REC *window;
93         int refnum;
94
95         for (refnum = removed_refnum+1;; refnum++) {
96                 window = window_find_refnum(refnum);
97                 if (window == NULL || window->sticky_refnum)
98                         break;
99
100                 window_set_refnum(window, refnum-1);
101         }
102 }
103
104 void window_destroy(WINDOW_REC *window)
105 {
106         g_return_if_fail(window != NULL);
107
108         if (window->destroying) return;
109         window->destroying = TRUE;
110         windows = g_slist_remove(windows, window);
111
112         if (active_win == window && windows != NULL) {
113                 active_win = NULL; /* it's corrupted */
114                 window_set_active(windows->data);
115         }
116
117         while (window->items != NULL)
118                 window_item_destroy(window->items->data);
119
120         if (settings_get_bool("windows_auto_renumber"))
121                 windows_pack(window->refnum);
122
123         signal_emit("window destroyed", 1, window);
124
125         while (window->bound_items != NULL)
126                 window_bind_destroy(window, window->bound_items->data);
127
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);
132         g_free(window);
133 }
134
135 void window_auto_destroy(WINDOW_REC *window)
136 {
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);
141 }
142
143 void window_set_active(WINDOW_REC *window)
144 {
145         WINDOW_REC *old_window;
146
147         if (window == active_win)
148                 return;
149
150         old_window = active_win;
151         active_win = window;
152         if (active_win != NULL) {
153                 windows = g_slist_remove(windows, active_win);
154                 windows = g_slist_prepend(windows, active_win);
155         }
156
157         if (active_win != NULL)
158                 signal_emit("window changed", 2, active_win, old_window);
159 }
160
161 void window_change_server(WINDOW_REC *window, void *server)
162 {
163         if (server != NULL && SERVER(server)->disconnected)
164                 return;
165
166         window->active_server = server;
167         signal_emit("window server changed", 2, window, server);
168 }
169
170 void window_set_refnum(WINDOW_REC *window, int refnum)
171 {
172         GSList *tmp;
173         int old_refnum;
174
175         g_return_if_fail(window != NULL);
176         g_return_if_fail(refnum >= 1);
177         if (window->refnum == refnum) return;
178
179         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
180                 WINDOW_REC *rec = tmp->data;
181
182                 if (rec->refnum == refnum) {
183                         rec->refnum = window->refnum;
184                         signal_emit("window refnum changed", 2, rec, GINT_TO_POINTER(refnum));
185                         break;
186                 }
187         }
188
189         old_refnum = window->refnum;
190         window->refnum = refnum;
191         signal_emit("window refnum changed", 2, window, GINT_TO_POINTER(old_refnum));
192 }
193
194 void window_set_name(WINDOW_REC *window, const char *name)
195 {
196         g_free_not_null(window->name);
197         window->name = g_strdup(name);
198
199         signal_emit("window name changed", 1, window);
200 }
201
202 void window_set_history(WINDOW_REC *window, const char *name)
203 {
204         char *oldname;
205         oldname = window->history_name;
206
207         if (name == NULL || *name == '\0')
208                 window->history_name = NULL;
209         else
210                 window->history_name = g_strdup(name);
211
212         signal_emit("window history changed", 1, window, oldname);
213
214         g_free_not_null(oldname);
215 }
216
217 void window_set_level(WINDOW_REC *window, int level)
218 {
219         g_return_if_fail(window != NULL);
220
221         window->level = level;
222         signal_emit("window level changed", 1, window);
223 }
224
225 void window_set_immortal(WINDOW_REC *window, int immortal)
226 {
227         g_return_if_fail(window != NULL);
228
229         window->immortal = immortal;
230         signal_emit("window immortal changed", 1, window);
231 }
232
233 /* return active item's name, or if none is active, window's name */
234 char *window_get_active_name(WINDOW_REC *window)
235 {
236         g_return_val_if_fail(window != NULL, NULL);
237
238         if (window->active != NULL)
239                 return window->active->name;
240
241         return window->name;
242 }
243
244 #define WINDOW_LEVEL_MATCH(window, server, level) \
245         (((window)->level & level) && \
246          (server == NULL || (window)->active_server == server))
247
248 WINDOW_REC *window_find_level(void *server, int level)
249 {
250         GSList *tmp;
251
252         /* prefer active window if possible */
253         if (active_win != NULL &&
254             WINDOW_LEVEL_MATCH(active_win, server, level))
255                 return active_win;
256
257         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
258                 WINDOW_REC *rec = tmp->data;
259
260                 if (WINDOW_LEVEL_MATCH(rec, server, level))
261                         return rec;
262         }
263
264         return NULL;
265 }
266
267 WINDOW_REC *window_find_closest(void *server, const char *name, int level)
268 {
269         WINDOW_REC *window,*namewindow=NULL;
270         WI_ITEM_REC *item;
271
272         /* match by name */
273         item = name == NULL ? NULL :
274                 window_item_find(server, name);
275         if (item != NULL) {
276                 namewindow = window_item_window(item);
277                 if (namewindow != NULL && ((namewindow->level & level) != 0 ||
278                     !settings_get_bool("window_check_level_first")))
279                   return namewindow;
280         }
281
282         /* match by level */
283         if (level != MSGLEVEL_HILIGHT)
284                 level &= ~(MSGLEVEL_HILIGHT | MSGLEVEL_NOHILIGHT);
285         window = window_find_level(server, level);
286         if (window != NULL) return window;
287
288         /* match by level - ignore server */
289         window = window_find_level(NULL, level);
290         if (window != NULL) return window;
291
292         /* still return item's window if we didnt find anything */
293         if (namewindow != NULL) return namewindow;
294
295         /* fallback to active */
296         return active_win;
297 }
298
299 WINDOW_REC *window_find_refnum(int refnum)
300 {
301         GSList *tmp;
302
303         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
304                 WINDOW_REC *rec = tmp->data;
305
306                 if (rec->refnum == refnum)
307                         return rec;
308         }
309
310         return NULL;
311 }
312
313 WINDOW_REC *window_find_name(const char *name)
314 {
315         GSList *tmp;
316
317         g_return_val_if_fail(name != NULL, NULL);
318
319         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
320                 WINDOW_REC *rec = tmp->data;
321
322                 if (rec->name != NULL && g_strcasecmp(rec->name, name) == 0)
323                         return rec;
324         }
325
326         return NULL;
327 }
328
329 WINDOW_REC *window_find_item(SERVER_REC *server, const char *name)
330 {
331         WINDOW_REC *rec;
332         WI_ITEM_REC *item;
333
334         g_return_val_if_fail(name != NULL, NULL);
335
336         rec = window_find_name(name);
337         if (rec != NULL) return rec;
338
339         item = server == NULL ? NULL :
340                 window_item_find(server, name);
341         if (item == NULL && server == NULL) {
342                 /* not found from the active server - any server? */
343                 item = window_item_find(NULL, name);
344         }
345
346         if (item == NULL) {
347                 char *chan;
348
349                 /* still nothing? maybe user just left the # in front of
350                    channel, try again with it.. */
351                 chan = g_strdup_printf("#%s", name);
352                 item = server == NULL ? NULL :
353                         window_item_find(server, chan);
354                 if (item == NULL) item = window_item_find(NULL, chan);
355                 g_free(chan);
356         }
357
358         if (item == NULL)
359                 return 0;
360
361         return window_item_window(item);
362 }
363
364 int window_refnum_prev(int refnum, int wrap)
365 {
366         GSList *tmp;
367         int prev, max;
368
369         max = prev = -1;
370         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
371                 WINDOW_REC *rec = tmp->data;
372
373                 if (rec->refnum < refnum && (prev == -1 || rec->refnum > prev))
374                         prev = rec->refnum;
375                 if (wrap && (max == -1 || rec->refnum > max))
376                         max = rec->refnum;
377         }
378
379         return prev != -1 ? prev : max;
380 }
381
382 int window_refnum_next(int refnum, int wrap)
383 {
384         GSList *tmp;
385         int min, next;
386
387         min = next = -1;
388         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
389                 WINDOW_REC *rec = tmp->data;
390
391                 if (rec->refnum > refnum && (next == -1 || rec->refnum < next))
392                         next = rec->refnum;
393                 if (wrap && (min == -1 || rec->refnum < min))
394                         min = rec->refnum;
395         }
396
397         return next != -1 ? next : min;
398 }
399
400 int windows_refnum_last(void)
401 {
402         GSList *tmp;
403         int max;
404
405         max = -1;
406         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
407                 WINDOW_REC *rec = tmp->data;
408
409                 if (rec->refnum > max)
410                         max = rec->refnum;
411         }
412
413         return max;
414 }
415
416 int window_refnum_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
417 {
418         return w1->refnum < w2->refnum ? -1 : 1;
419 }
420
421 GSList *windows_get_sorted(void)
422 {
423         GSList *tmp, *sorted;
424
425         sorted = NULL;
426         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
427                 WINDOW_REC *rec = tmp->data;
428
429                 sorted = g_slist_insert_sorted(sorted, rec, (GCompareFunc)
430                                                window_refnum_cmp);
431         }
432
433         return sorted;
434 }
435
436 /* Add a new bind to window - if duplicate is found it's returned */
437 WINDOW_BIND_REC *window_bind_add(WINDOW_REC *window, const char *servertag,
438                                  const char *name)
439 {
440         WINDOW_BIND_REC *rec;
441
442         g_return_val_if_fail(window != NULL, NULL);
443         g_return_val_if_fail(servertag != NULL, NULL);
444         g_return_val_if_fail(name != NULL, NULL);
445
446         rec = window_bind_find(window, servertag, name);
447         if (rec != NULL)
448                 return rec;
449
450         rec = g_new0(WINDOW_BIND_REC, 1);
451         rec->name = g_strdup(name);
452         rec->servertag = g_strdup(servertag);
453
454         window->bound_items = g_slist_append(window->bound_items, rec);
455         return rec;
456 }
457
458 void window_bind_destroy(WINDOW_REC *window, WINDOW_BIND_REC *rec)
459 {
460         g_return_if_fail(window != NULL);
461         g_return_if_fail(rec != NULL);
462
463         window->bound_items = g_slist_remove(window->bound_items, rec);
464
465         g_free(rec->servertag);
466         g_free(rec->name);
467         g_free(rec);
468 }
469
470 WINDOW_BIND_REC *window_bind_find(WINDOW_REC *window, const char *servertag,
471                                   const char *name)
472 {
473         GSList *tmp;
474
475         g_return_val_if_fail(window != NULL, NULL);
476         g_return_val_if_fail(servertag != NULL, NULL);
477         g_return_val_if_fail(name != NULL, NULL);
478
479         for (tmp = window->bound_items; tmp != NULL; tmp = tmp->next) {
480                 WINDOW_BIND_REC *rec = tmp->data;
481
482                 if (g_strcasecmp(rec->name, name) == 0 &&
483                     g_strcasecmp(rec->servertag, servertag) == 0)
484                         return rec;
485         }
486
487         return NULL;
488 }
489
490 void window_bind_remove_unsticky(WINDOW_REC *window)
491 {
492         GSList *tmp, *next;
493
494         for (tmp = window->bound_items; tmp != NULL; tmp = next) {
495                 WINDOW_BIND_REC *rec = tmp->data;
496
497                 next = tmp->next;
498                 if (!rec->sticky)
499                         window_bind_destroy(window, rec);
500         }
501 }
502
503 static void sig_server_looking(SERVER_REC *server)
504 {
505         GSList *tmp;
506
507         g_return_if_fail(server != NULL);
508
509         /* Try to keep some server assigned to windows..
510            Also change active window's server if the window is empty */
511         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
512                 WINDOW_REC *rec = tmp->data;
513
514                 if ((rec->servertag == NULL ||
515                      g_strcasecmp(rec->servertag, server->tag) == 0) &&
516                     (rec->active_server == NULL ||
517                      (rec == active_win && rec->items == NULL)))
518                         window_change_server(rec, server);
519         }
520 }
521
522 static void sig_server_disconnected(SERVER_REC *server)
523 {
524         GSList *tmp;
525         SERVER_REC *new_server;
526
527         g_return_if_fail(server != NULL);
528
529         new_server = servers == NULL ? NULL : servers->data;
530         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
531                 WINDOW_REC *rec = tmp->data;
532
533                 if (rec->active_server == server) {
534                         window_change_server(rec, rec->servertag != NULL ?
535                                              NULL : new_server);
536                 }
537         }
538 }
539
540 static void window_print_daychange(WINDOW_REC *window, struct tm *tm)
541 {
542         THEME_REC *theme;
543         TEXT_DEST_REC dest;
544         char *format, str[256];
545
546         theme = active_win->theme != NULL ? active_win->theme : current_theme;
547         format_create_dest(&dest, NULL, NULL, MSGLEVEL_NEVER, window);
548         format = format_get_text_theme(theme, MODULE_NAME, &dest,
549                                        TXT_DAYCHANGE);
550         if (strftime(str, sizeof(str), format, tm) <= 0)
551                 str[0] = '\0';
552         g_free(format);
553
554         printtext_string_window(window, MSGLEVEL_NEVER, str);
555 }
556
557 static void sig_print_text(void)
558 {
559         GSList *tmp;
560         time_t t;
561         struct tm *tm;
562
563         t = time(NULL);
564         tm = localtime(&t);
565         if (tm->tm_hour != 0 || tm->tm_min != 0)
566                 return;
567
568         daycheck = 2;
569         signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
570
571         /* day changed, print notice about it to every window */
572         for (tmp = windows; tmp != NULL; tmp = tmp->next)
573                 window_print_daychange(tmp->data, tm);
574 }
575
576 static int sig_check_daychange(void)
577 {
578         time_t t;
579         struct tm *tm;
580
581         t = time(NULL);
582         tm = localtime(&t);
583
584         if (daycheck == 1 && tm->tm_hour == 0 && tm->tm_min == 0) {
585                 sig_print_text();
586                 return TRUE;
587         }
588
589         if (tm->tm_hour != 23 || tm->tm_min != 59) {
590                 daycheck = 0;
591                 return TRUE;
592         }
593
594         /* time is 23:59 */
595         if (daycheck == 0) {
596                 daycheck = 1;
597                 signal_add("print text", (SIGNAL_FUNC) sig_print_text);
598         }
599         return TRUE;
600 }
601
602 static void read_settings(void)
603 {
604         if (daytag != -1) {
605                 g_source_remove(daytag);
606                 daytag = -1;
607         }
608
609         if (settings_get_bool("timestamps"))
610                 daytag = g_timeout_add(30000, (GSourceFunc) sig_check_daychange, NULL);
611 }
612
613 void windows_init(void)
614 {
615         active_win = NULL;
616         daycheck = 0; daytag = -1;
617         settings_add_bool("lookandfeel", "window_auto_change", FALSE);
618         settings_add_bool("lookandfeel", "windows_auto_renumber", TRUE);
619         settings_add_bool("lookandfeel", "window_check_level_first", FALSE);
620         settings_add_str("lookandfeel", "window_default_level", "NONE");
621
622         read_settings();
623         signal_add("server looking", (SIGNAL_FUNC) sig_server_looking);
624         signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
625         signal_add("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
626         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
627 }
628
629 void windows_deinit(void)
630 {
631         if (daytag != -1) g_source_remove(daytag);
632         if (daycheck == 1) signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
633
634         signal_remove("server looking", (SIGNAL_FUNC) sig_server_looking);
635         signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
636         signal_remove("server connect failed", (SIGNAL_FUNC) sig_server_disconnected);
637         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
638 }