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