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