summaryrefslogtreecommitdiff
path: root/src/fe-text/statusbar-items.c
blob: 3cb496c9ad1a88206550926ea9befda684331ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 statusbar-items.c : irssi

    Copyright (C) 1999-2001 Timo Sirainen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"
#include "signals.h"
#include "settings.h"

#include "statusbar.h"
#include "gui-entry.h"

/* how often to redraw lagging time (seconds) */
#define LAG_REFRESH_TIME 10

/* If we haven't been able to check lag for this long, "(??)" is added after
   the lag */
#define MAX_LAG_UNKNOWN_TIME 30

/* activity */
static GSList *activity_items;
static GList *activity_list;

static GHashTable *input_entries;

static void item_window_active(SBAR_ITEM_REC *item, int get_size_only)
{
	WINDOW_REC *window;

        window = active_win;
	if (item->bar->parent_window != NULL)
		window = item->bar->parent_window->active;

	if (window != NULL && window->active != NULL) {
		statusbar_item_default_handler(item, get_size_only,
					       NULL, "", TRUE);
	} else if (get_size_only) {
                item->min_size = item->max_size = 0;
	}
}

static void item_window_empty(SBAR_ITEM_REC *item, int get_size_only)
{
	WINDOW_REC *window;

        window = active_win;
	if (item->bar->parent_window != NULL)
		window = item->bar->parent_window->active;

	if (window != NULL && window->active == NULL) {
		statusbar_item_default_handler(item, get_size_only,
					       NULL, "", TRUE);
	} else if (get_size_only) {
                item->min_size = item->max_size = 0;
	}
}

static void item_lag(SBAR_ITEM_REC *item, int get_size_only)
{
	SERVER_REC *server;
	GString *str;
	int lag_unknown, lag_min_show;
	time_t now;

	server = active_win == NULL ? NULL : active_win->active_server;
	if (server == NULL || server->lag_last_check == 0) {
                /* No lag information */
		if (get_size_only)
			item->min_size = item->max_size = 0;
		return;
	}

	now = time(NULL);
	str = g_string_new(NULL);

	/* FIXME: ugly ugly.. */
	if (server->lag_sent == 0 || now-server->lag_sent < 5) {
		lag_unknown = now-server->lag_last_check >
			MAX_LAG_UNKNOWN_TIME+settings_get_int("lag_check_time");
                lag_min_show = settings_get_int("lag_min_show");

		if (lag_min_show < 0 || (server->lag < lag_min_show && !lag_unknown)) {
                        /* small lag, don't display */
		} else {
			g_string_sprintfa(str, "%d.%02d", server->lag/1000,
					  (server->lag % 1000)/10);
			if (lag_unknown)
				g_string_append(str, " (?""?)");
		}
	} else {
		/* big lag, still waiting .. */
		g_string_sprintfa(str, "%ld (?""?)",
				  (long) (now-server->lag_sent));
	}

	if (str->len != 0) {
		statusbar_item_default_handler(item, get_size_only,
					       NULL, str->str, TRUE);
	} else {
		if (get_size_only)
			item->min_size = item->max_size = 0;
	}

	g_string_free(str, TRUE);
}

static char *get_activity_list(int normal, int hilight)
{
	GString *str;
	GList *tmp;
        char *ret;
        int is_det;

	str = g_string_new(NULL);

	for (tmp = activity_list; tmp != NULL; tmp = tmp->next) {
		WINDOW_REC *window = tmp->data;

		is_det = window->data_level >= DATA_LEVEL_HILIGHT;
		if ((!is_det && !normal) || (is_det && !hilight))
                        continue;

		g_string_append(str, "%c");
                if (str->len > 2)
			g_string_append_c(str, ',');

		switch (window->data_level) {
		case DATA_LEVEL_NONE:
		case DATA_LEVEL_TEXT:
			break;
		case DATA_LEVEL_MSG:
                        g_string_append(str, "%W");
			break;
		default:
			g_string_append(str, window->hilight_color == NULL ?
					"%M" : window->hilight_color);
			break;
		}
		g_string_sprintfa(str, "%d", window->refnum);

                /* make sure the background is returned to default */
		g_string_append(str, "%n");
	}

	ret = str->len == 0 ? NULL : str->str;
        g_string_free(str, ret == NULL);
        return ret;
}

/* redraw activity, FIXME: if we didn't get enough size, this gets buggy.
   At least "Det:" isn't printed properly. also we should rearrange the
   act list so that the highest priority items comes first. */
static void item_act(SBAR_ITEM_REC *item, int get_size_only)
{
	char *actlist;

	actlist = get_activity_list(TRUE, TRUE);
	if (actlist == NULL) {
		if (get_size_only)
			item->min_size = item->max_size = 0;
		return;
	}

	statusbar_item_default_handler(item, get_size_only,
				       NULL, actlist, FALSE);

	g_free_not_null(actlist);
}

static void sig_statusbar_activity_hilight(WINDOW_REC *window, gpointer oldlevel)
{
	GList *tmp;
	int inspos;

	g_return_if_fail(window != NULL);

	if (settings_get_bool("actlist_moves")) {
		/* Move the window to the first in the activity list */
		if (g_list_find(activity_list, window) != NULL)
			activity_list = g_list_remove(activity_list, window);
		if (window->data_level != 0)
			activity_list = g_list_prepend(activity_list, window);
		statusbar_items_redraw(activity_items);
		return;
	}

	if (g_list_find(activity_list, window) != NULL) {
		/* already in activity list */
		if (window->data_level == 0) {
			/* remove from activity list */
			activity_list = g_list_remove(activity_list, window);
			statusbar_items_redraw(activity_items);
		} else if (window->data_level != GPOINTER_TO_INT(oldlevel) ||
			 window->hilight_color != 0) {
			/* different level as last time (or maybe different
			   hilight color?), just redraw it. */
			statusbar_items_redraw(activity_items);
		}
		return;
	}

	if (window->data_level == 0)
		return;

	/* add window to activity list .. */
	inspos = 0;
	for (tmp = activity_list; tmp != NULL; tmp = tmp->next, inspos++) {
		WINDOW_REC *rec = tmp->data;

		if (window->refnum < rec->refnum) {
			activity_list =
				g_list_insert(activity_list, window, inspos);
			break;
		}
	}
	if (tmp == NULL)
		activity_list = g_list_append(activity_list, window);

	statusbar_items_redraw(activity_items);
}

static void sig_statusbar_activity_window_destroyed(WINDOW_REC *window)
{
	g_return_if_fail(window != NULL);

	if (g_list_find(activity_list, window) != NULL)
		activity_list = g_list_remove(activity_list, window);
	statusbar_items_redraw(activity_items);
}

static void sig_statusbar_activity_updated(void)
{
	statusbar_items_redraw(activity_items);
}

static void item_more(SBAR_ITEM_REC *item, int get_size_only)
{
}

static void item_input(SBAR_ITEM_REC *item, int get_size_only)
{
	GUI_ENTRY_REC *rec;

	if (get_size_only) {
		item->min_size = 2+screen_width/10;
                item->max_size = screen_width;
                return;
	}

	rec = g_hash_table_lookup(input_entries, item);
	if (rec == NULL) {
		rec = gui_entry_create(item->xpos, item->bar->real_ypos,
				       item->size);
                if (active_entry == NULL)
			gui_entry_set_active(rec);
		g_hash_table_insert(input_entries, item, rec);
	} else {
		gui_entry_move(rec, item->xpos, item->bar->real_ypos,
			       item->size);
		gui_entry_redraw(rec); /* FIXME: this is only necessary with ^L.. */
	}
}

static void sig_statusbar_item_created(SBAR_ITEM_REC *item)
{
	if (item->func == item_act)
		activity_items = g_slist_prepend(activity_items, item);
}

static void sig_statusbar_item_destroyed(SBAR_ITEM_REC *item)
{
	if (item->func == item_act)
		activity_items = g_slist_remove(activity_items, item);
	else {
		GUI_ENTRY_REC *rec;

		rec = g_hash_table_lookup(input_entries, item);
		if (rec != NULL) {
			gui_entry_destroy(rec);
                        g_hash_table_remove(input_entries, item);
		}
	}
}

void statusbar_items_init(void)
{
	settings_add_int("misc", "lag_min_show", 100);
	settings_add_bool("lookandfeel", "actlist_moves", FALSE);

	statusbar_item_register("window", NULL, item_window_active);
	statusbar_item_register("window_empty", NULL, item_window_empty);
	statusbar_item_register("prompt", NULL, item_window_active);
	statusbar_item_register("prompt_empty", NULL, item_window_empty);
	statusbar_item_register("lag", NULL, item_lag);
	statusbar_item_register("act", NULL, item_act);
	statusbar_item_register("more", NULL, item_more);
	statusbar_item_register("input", NULL, item_input);

	input_entries = g_hash_table_new((GHashFunc) g_direct_hash,
					 (GCompareFunc) g_direct_equal);

	activity_list = NULL;
	signal_add("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
	signal_add("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
	signal_add("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);

	signal_add("statusbar item created", (SIGNAL_FUNC) sig_statusbar_item_created);
	signal_add("statusbar item destroyed", (SIGNAL_FUNC) sig_statusbar_item_destroyed);
}

void statusbar_items_deinit(void)
{
        g_hash_table_destroy(input_entries);

	signal_remove("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
	signal_remove("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
	signal_remove("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);

	g_list_free(activity_list);
        activity_list = NULL;

	signal_remove("statusbar item created", (SIGNAL_FUNC) sig_statusbar_item_created);
	signal_remove("statusbar item destroyed", (SIGNAL_FUNC) sig_statusbar_item_destroyed);
}