Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / fe-text / statusbar-items.c
1 /*
2  statusbar-items.c : irssi
3
4     Copyright (C) 1999-2001 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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "settings.h"
24 #include "servers.h"
25
26 #include "themes.h"
27 #include "statusbar.h"
28 #include "gui-entry.h"
29 #include "gui-windows.h"
30
31 /* how often to redraw lagging time (seconds) */
32 #define LAG_REFRESH_TIME 10
33
34 static GList *activity_list;
35 static guint8 actlist_sort;
36 static GSList *more_visible; /* list of MAIN_WINDOW_RECs which have --more-- */
37 static GHashTable *input_entries;
38 static int last_lag, last_lag_unknown, lag_timeout_tag;
39
40 static void item_window_active(SBAR_ITEM_REC *item, int get_size_only)
41 {
42         WINDOW_REC *window;
43
44         window = active_win;
45         if (item->bar->parent_window != NULL)
46                 window = item->bar->parent_window->active;
47
48         if (window != NULL && window->active != NULL) {
49                 statusbar_item_default_handler(item, get_size_only,
50                                                NULL, "", TRUE);
51         } else if (get_size_only) {
52                 item->min_size = item->max_size = 0;
53         }
54 }
55
56 static void item_window_empty(SBAR_ITEM_REC *item, int get_size_only)
57 {
58         WINDOW_REC *window;
59
60         window = active_win;
61         if (item->bar->parent_window != NULL)
62                 window = item->bar->parent_window->active;
63
64         if (window != NULL && window->active == NULL) {
65                 statusbar_item_default_handler(item, get_size_only,
66                                                NULL, "", TRUE);
67         } else if (get_size_only) {
68                 item->min_size = item->max_size = 0;
69         }
70 }
71
72 static char *get_activity_list(MAIN_WINDOW_REC *window, int normal, int hilight)
73 {
74         THEME_REC *theme;
75         GString *str;
76         GString *format;
77         GList *tmp;
78         char *ret, *name, *value;
79         int is_det;
80         int add_name = settings_get_bool("actlist_names");
81
82         str = g_string_new(NULL);
83         format = g_string_new(NULL);
84
85         theme = window != NULL && window->active != NULL &&
86                 window->active->theme != NULL ?
87                 window->active->theme : current_theme;
88
89         for (tmp = activity_list; tmp != NULL; tmp = tmp->next) {
90                 WINDOW_REC *window = tmp->data;
91
92                 is_det = window->data_level >= DATA_LEVEL_HILIGHT;
93                 if ((!is_det && !normal) || (is_det && !hilight))
94                         continue;
95
96                 /* comma separator */
97                 if (str->len > 0) {
98                         value = theme_format_expand(theme, "{sb_act_sep ,}");
99                         g_string_append(str, value);
100                         g_free(value);
101                 }
102
103                 switch (window->data_level) {
104                 case DATA_LEVEL_NONE:
105                 case DATA_LEVEL_TEXT:
106                         name = "{sb_act_text %d";
107                         break;
108                 case DATA_LEVEL_MSG:
109                         name = "{sb_act_msg %d";
110                         break;
111                 default:
112                         if (window->hilight_color == NULL)
113                                 name = "{sb_act_hilight %d";
114                         else
115                                 name = NULL;
116                         break;
117                 }
118
119                 if (name != NULL)
120                         g_string_printf(format, name, window->refnum);
121                 else
122                         g_string_printf(format, "{sb_act_hilight_color %s %d",
123                                                  window->hilight_color,
124                                                  window->refnum);
125                 if (add_name && window->active != NULL)
126                         g_string_append_printf(format, ":%s", window->active->visible_name);
127                 g_string_append_c(format, '}');
128
129                 value = theme_format_expand(theme, format->str);
130                 g_string_append(str, value);
131                 g_free(value);
132         }
133
134         ret = str->len == 0 ? NULL : str->str;
135         g_string_free(str, ret == NULL);
136         g_string_free(format, TRUE);
137         return ret;
138 }
139
140 /* redraw activity, FIXME: if we didn't get enough size, this gets buggy.
141    At least "Det:" isn't printed properly. also we should rearrange the
142    act list so that the highest priority items comes first. */
143 static void item_act(SBAR_ITEM_REC *item, int get_size_only)
144 {
145         char *actlist;
146
147         actlist = get_activity_list(item->bar->parent_window, TRUE, TRUE);
148         if (actlist == NULL) {
149                 if (get_size_only)
150                         item->min_size = item->max_size = 0;
151                 return;
152         }
153
154         statusbar_item_default_handler(item, get_size_only,
155                                        NULL, actlist, FALSE);
156
157         g_free_not_null(actlist);
158 }
159
160 static int window_level_recent_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
161 {
162         if (w1->data_level >= w2->data_level)
163                 return -1;
164         else
165                 return 1;
166 }
167
168 static int window_level_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
169 {
170         if (w1->data_level > w2->data_level ||
171             (w1->data_level == w2->data_level && w1->refnum < w2->refnum))
172                 return -1;
173         else
174                 return 1;
175 }
176
177 static void sig_statusbar_activity_hilight(WINDOW_REC *window, gpointer oldlevel)
178 {
179         GList *node;
180
181         g_return_if_fail(window != NULL);
182
183         node = g_list_find(activity_list, window);
184
185         if (actlist_sort == 1) {
186                 /* Move the window to the first in the activity list */
187                 if (node != NULL)
188                         activity_list = g_list_delete_link(activity_list, node);
189                 if (window->data_level != 0)
190                         activity_list = g_list_prepend(activity_list, window);
191                 statusbar_items_redraw("act");
192                 return;
193         }
194
195         if (actlist_sort == 2) {
196                 if (node != NULL) {
197                         if (window->data_level == GPOINTER_TO_INT(oldlevel)) {
198                                 if (window->hilight_color != 0)
199                                         statusbar_items_redraw("act");
200                                 return;
201                         }
202                         activity_list = g_list_delete_link(activity_list, node);
203                 }
204                 if (window->data_level != 0)
205                         activity_list = g_list_insert_sorted(activity_list, window, (GCompareFunc)
206                                                              window_level_cmp);
207                 statusbar_items_redraw("act");
208                 return;
209         }
210
211         if (actlist_sort == 3) {
212                 if (node != NULL)
213                         activity_list = g_list_delete_link(activity_list, node);
214                 if (window->data_level != 0)
215                         activity_list = g_list_insert_sorted(activity_list, window, (GCompareFunc)
216                                                              window_level_recent_cmp);
217                 statusbar_items_redraw("act");
218                 return;
219         }
220
221         if (node != NULL) {
222                 /* already in activity list */
223                 if (window->data_level == 0) {
224                         /* remove from activity list */
225                         activity_list = g_list_delete_link(activity_list, node);
226                         statusbar_items_redraw("act");
227                 } else if (window->data_level != GPOINTER_TO_INT(oldlevel) ||
228                          window->hilight_color != 0) {
229                         /* different level as last time (or maybe different
230                            hilight color?), just redraw it. */
231                         statusbar_items_redraw("act");
232                 }
233                 return;
234         }
235
236         if (window->data_level == 0)
237                 return;
238
239         /* add window to activity list .. */
240         activity_list = g_list_insert_sorted(activity_list, window, (GCompareFunc)
241                                              window_refnum_cmp);
242
243         statusbar_items_redraw("act");
244 }
245
246 static void sig_statusbar_activity_window_destroyed(WINDOW_REC *window)
247 {
248         GList *node;
249
250         g_return_if_fail(window != NULL);
251
252         node = g_list_find(activity_list, window);
253         if (node != NULL)
254                 activity_list = g_list_delete_link(activity_list, node);
255         statusbar_items_redraw("act");
256 }
257
258 static void sig_statusbar_activity_updated(void)
259 {
260         statusbar_items_redraw("act");
261 }
262
263 static void item_more(SBAR_ITEM_REC *item, int get_size_only)
264 {
265         MAIN_WINDOW_REC *mainwin;
266         int visible;
267
268         if (active_win == NULL) {
269                 mainwin = NULL;
270                 visible = FALSE;
271         } else {
272                 mainwin = WINDOW_MAIN(active_win);
273                 visible = WINDOW_GUI(active_win)->view->more_text;
274         }
275
276         if (!visible) {
277                 if (mainwin != NULL)
278                         more_visible = g_slist_remove(more_visible, mainwin);
279                 if (get_size_only)
280                         item->min_size = item->max_size = 0;
281                 return;
282         }
283
284         more_visible = g_slist_prepend(more_visible, mainwin);
285         statusbar_item_default_handler(item, get_size_only, NULL, "", FALSE);
286 }
287
288 static void sig_statusbar_more_updated(void)
289 {
290         int visible;
291
292         visible = g_slist_find(more_visible, WINDOW_MAIN(active_win)) != NULL;
293         if (WINDOW_GUI(active_win)->view->more_text != visible)
294                 statusbar_items_redraw("more");
295 }
296
297 /* Returns the lag in milliseconds. If we haven't been able to ask the lag
298    for a while, unknown is set to TRUE. */
299 static int get_lag(SERVER_REC *server, int *unknown)
300 {
301         long lag;
302
303         *unknown = FALSE;
304
305         if (server == NULL || server->lag_last_check == 0) {
306                 /* lag has not been asked even once yet */
307                 return 0;
308         }
309
310         if (server->lag_sent.tv_sec == 0) {
311                 /* no lag queries going on currently */
312                 return server->lag;
313         }
314
315         /* we're not sure about our current lag.. */
316         *unknown = TRUE;
317
318         lag = (long) (time(NULL)-server->lag_sent.tv_sec);
319         if (server->lag/1000 > lag) {
320                 /* we've been waiting the lag reply less time than
321                    what last known lag was -> use the last known lag */
322                 return server->lag;
323         }
324
325         /* return how long we have been waiting for lag reply */
326         return lag*1000;
327 }
328
329 static void item_lag(SBAR_ITEM_REC *item, int get_size_only)
330 {
331         SERVER_REC *server;
332         char str[MAX_INT_STRLEN+10];
333         int lag, lag_unknown;
334
335         server = active_win == NULL ? NULL : active_win->active_server;
336         lag = get_lag(server, &lag_unknown);
337
338         if (lag <= 0 || lag < settings_get_time("lag_min_show")) {
339                 /* don't print the lag item */
340                 if (get_size_only)
341                         item->min_size = item->max_size = 0;
342                 return;
343         }
344
345         lag /= 10;
346         last_lag = lag;
347         last_lag_unknown = lag_unknown;
348
349         if (lag_unknown) {
350                 g_snprintf(str, sizeof(str), "%d (?""?)", lag/100);
351         } else {
352                 g_snprintf(str, sizeof(str),
353                            lag%100 == 0 ? "%d" : "%d.%02d", lag/100, lag%100);
354         }
355
356         statusbar_item_default_handler(item, get_size_only,
357                                        NULL, str, TRUE);
358 }
359
360 static void lag_check_update(void)
361 {
362         SERVER_REC *server;
363         int lag, lag_unknown;
364
365         server = active_win == NULL ? NULL : active_win->active_server;
366         lag = get_lag(server, &lag_unknown)/10;
367
368         if (lag < settings_get_time("lag_min_show"))
369                 lag = 0;
370         else
371                 lag /= 10;
372
373         if (lag != last_lag || (lag > 0 && lag_unknown != last_lag_unknown))
374                 statusbar_items_redraw("lag");
375 }
376
377 static void sig_server_lag_updated(SERVER_REC *server)
378 {
379         if (active_win != NULL && active_win->active_server == server)
380                 lag_check_update();
381 }
382
383 static int sig_lag_timeout(void)
384 {
385         lag_check_update();
386         return 1;
387 }
388
389 static void item_input(SBAR_ITEM_REC *item, int get_size_only)
390 {
391         GUI_ENTRY_REC *rec;
392
393         rec = g_hash_table_lookup(input_entries, item->bar->config->name);
394         if (rec == NULL) {
395                 rec = gui_entry_create(item->xpos, item->bar->real_ypos,
396                                        item->size, term_type == TERM_TYPE_UTF8);
397                 gui_entry_set_active(rec);
398                 g_hash_table_insert(input_entries,
399                                     g_strdup(item->bar->config->name), rec);
400         }
401
402         if (get_size_only) {
403                 item->min_size = 2+term_width/10;
404                 item->max_size = term_width;
405                 return;
406         }
407
408         gui_entry_move(rec, item->xpos, item->bar->real_ypos,
409                        item->size);
410         gui_entry_redraw(rec); /* FIXME: this is only necessary with ^L.. */
411 }
412
413 static void read_settings(void)
414 {
415         const char *str;
416
417         if (active_entry != NULL)
418                 gui_entry_set_utf8(active_entry, term_type == TERM_TYPE_UTF8);
419
420         str = settings_get_str("actlist_sort");
421         if (g_ascii_strcasecmp(str, "recent") == 0)
422                 actlist_sort = 1;
423         else if (g_ascii_strcasecmp(str, "level") == 0)
424                 actlist_sort = 2;
425         else if (g_ascii_strcasecmp(str, "level,recent") == 0)
426                 actlist_sort = 3;
427         else {
428                 settings_set_str("actlist_sort", "refnum");
429                 actlist_sort = 0;
430         }
431 }
432
433 void statusbar_items_init(void)
434 {
435         settings_add_time("misc", "lag_min_show", "1sec");
436         settings_add_str("lookandfeel", "actlist_sort", "refnum");
437         settings_add_bool("lookandfeel", "actlist_names", FALSE);
438
439         statusbar_item_register("window", NULL, item_window_active);
440         statusbar_item_register("window_empty", NULL, item_window_empty);
441         statusbar_item_register("prompt", NULL, item_window_active);
442         statusbar_item_register("prompt_empty", NULL, item_window_empty);
443         statusbar_item_register("topic", NULL, item_window_active);
444         statusbar_item_register("topic_empty", NULL, item_window_empty);
445         statusbar_item_register("lag", NULL, item_lag);
446         statusbar_item_register("act", NULL, item_act);
447         statusbar_item_register("more", NULL, item_more);
448         statusbar_item_register("input", NULL, item_input);
449
450         /* activity */
451         activity_list = NULL;
452         signal_add("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
453         signal_add("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
454         signal_add("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);
455
456         /* more */
457         more_visible = NULL;
458         signal_add("gui page scrolled", (SIGNAL_FUNC) sig_statusbar_more_updated);
459         signal_add("window changed", (SIGNAL_FUNC) sig_statusbar_more_updated);
460         signal_add_last("gui print text finished", (SIGNAL_FUNC) sig_statusbar_more_updated);
461         signal_add_last("command clear", (SIGNAL_FUNC) sig_statusbar_more_updated);
462         signal_add_last("command scrollback", (SIGNAL_FUNC) sig_statusbar_more_updated);
463
464         /* lag */
465         last_lag = 0; last_lag_unknown = FALSE;
466         signal_add("server lag", (SIGNAL_FUNC) sig_server_lag_updated);
467         signal_add("window changed", (SIGNAL_FUNC) lag_check_update);
468         signal_add("window server changed", (SIGNAL_FUNC) lag_check_update);
469         lag_timeout_tag = g_timeout_add(5000, (GSourceFunc) sig_lag_timeout, NULL);
470
471         /* input */
472         input_entries = g_hash_table_new((GHashFunc) g_str_hash,
473                                          (GCompareFunc) g_str_equal);
474
475         read_settings();
476         signal_add_last("setup changed", (SIGNAL_FUNC) read_settings);
477 }
478
479 void statusbar_items_deinit(void)
480 {
481         /* activity */
482         signal_remove("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
483         signal_remove("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
484         signal_remove("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);
485         g_list_free(activity_list);
486         activity_list = NULL;
487
488         /* more */
489         g_slist_free(more_visible);
490         signal_remove("gui page scrolled", (SIGNAL_FUNC) sig_statusbar_more_updated);
491         signal_remove("window changed", (SIGNAL_FUNC) sig_statusbar_more_updated);
492         signal_remove("gui print text finished", (SIGNAL_FUNC) sig_statusbar_more_updated);
493         signal_remove("command clear", (SIGNAL_FUNC) sig_statusbar_more_updated);
494         signal_remove("command scrollback", (SIGNAL_FUNC) sig_statusbar_more_updated);
495
496         /* lag */
497         signal_remove("server lag", (SIGNAL_FUNC) sig_server_lag_updated);
498         signal_remove("window changed", (SIGNAL_FUNC) lag_check_update);
499         signal_remove("window server changed", (SIGNAL_FUNC) lag_check_update);
500         g_source_remove(lag_timeout_tag);
501
502         /* input */
503         g_hash_table_foreach(input_entries, (GHFunc) g_free, NULL);
504         g_hash_table_destroy(input_entries);
505
506         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
507 }