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