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