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