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