1 /* Sessions status management */
11 #include "bfu/dialog.h"
12 #include "cache/cache.h"
13 #include "config/options.h"
14 #include "dialogs/progress.h"
15 #include "dialogs/status.h"
16 #include "document/document.h"
17 #include "document/renderer.h"
18 #include "document/view.h"
19 #include "intl/gettext/libintl.h"
20 #include "network/connection.h"
21 #include "network/progress.h"
22 #include "network/state.h"
23 #include "protocol/bittorrent/dialogs.h"
24 #include "protocol/protocol.h"
25 #include "protocol/uri.h"
26 #include "session/download.h"
27 #include "session/session.h"
28 #include "terminal/draw.h"
29 #include "terminal/screen.h"
30 #include "terminal/tab.h"
31 #include "terminal/terminal.h"
32 #include "terminal/window.h"
33 #include "util/color.h"
34 #include "util/conv.h"
35 #include "util/error.h"
36 #include "util/memory.h"
37 #include "util/snprintf.h"
38 #include "util/string.h"
39 #include "viewer/text/form.h"
40 #include "viewer/text/link.h"
41 #include "viewer/text/view.h"
45 get_download_msg(struct download
*download
, struct terminal
*term
,
46 int wide
, int full
, unsigned char *separator
)
48 if (!download_is_progressing(download
)) {
49 /* DBG("%d -> %s", download->state, _(get_err_msg(download->state), term)); */
50 return stracpy(get_state_message(download
->state
, term
));
53 #ifdef CONFIG_BITTORRENT
55 && download
->conn
->uri
->protocol
== PROTOCOL_BITTORRENT
)
56 return get_bittorrent_message(download
, term
, wide
, full
, separator
);
59 return get_progress_msg(download
->progress
, term
, wide
, full
, separator
);
62 #define show_tabs(option, tabs) (((option) > 0) && !((option) == 1 && (tabs) < 2))
67 int show_title_bar
= get_opt_bool("ui.show_title_bar");
68 int show_status_bar
= get_opt_bool("ui.show_status_bar");
69 int show_tabs_bar
= get_opt_int("ui.tabs.show_bar");
70 int show_tabs_bar_at_top
= get_opt_bool("ui.tabs.top");
72 int show_leds
= get_opt_bool("ui.leds.enable");
74 int set_window_title
= get_opt_bool("ui.window_title");
75 int insert_mode
= get_opt_bool("document.browse.forms.insert_mode");
78 struct terminal
*term
= NULL
;
80 foreach (ses
, sessions
) {
81 struct session_status
*status
= &ses
->status
;
84 /* Try to descrease the number of tab calculation using that
85 * tab sessions share the same term. */
86 if (ses
->tab
->term
!= term
) {
87 term
= ses
->tab
->term
;
88 tabs_count
= number_of_tabs(term
);
91 if (status
->force_show_title_bar
>= 0)
92 show_title_bar
= status
->force_show_title_bar
;
93 if (status
->show_title_bar
!= show_title_bar
) {
94 status
->show_title_bar
= show_title_bar
;
98 if (status
->force_show_status_bar
>= 0)
99 show_status_bar
= status
->force_show_status_bar
;
100 if (status
->show_status_bar
!= show_status_bar
) {
101 status
->show_status_bar
= show_status_bar
;
105 if (show_tabs(show_tabs_bar
, tabs_count
) != status
->show_tabs_bar
) {
106 status
->show_tabs_bar
= show_tabs(show_tabs_bar
, tabs_count
);
110 if (status
->show_tabs_bar
) {
111 if (status
->show_tabs_bar_at_top
!= show_tabs_bar_at_top
) {
112 status
->show_tabs_bar_at_top
= show_tabs_bar_at_top
;
117 if (status
->show_leds
!= show_leds
) {
118 status
->show_leds
= show_leds
;
123 status
->set_window_title
= set_window_title
;
125 /* This more belongs to the current browsing state but ... */
127 ses
->insert_mode
= INSERT_MODE_LESS
;
128 else if (ses
->insert_mode
== INSERT_MODE_LESS
)
129 ses
->insert_mode
= INSERT_MODE_OFF
;
131 if (!dirty
) continue;
133 /* Force the current document to be rerendered so the
134 * document view and document height is updated to fit
135 * into the new dimensions. Related to bug 87. */
136 render_document_frames(ses
, 1);
138 set_screen_dirty(term
->screen
, 0, term
->height
);
142 static unsigned char *
143 get_current_link_info_and_title(struct session
*ses
,
144 struct document_view
*doc_view
)
146 unsigned char *link_info
, *link_title
, *ret
= NULL
;
148 link_info
= get_current_link_info(ses
, doc_view
);
149 if (!link_info
) return NULL
;
151 link_title
= get_current_link_title(doc_view
);
155 ret
= straconcat(link_info
, " - ", link_title
, NULL
);
157 mem_free(link_title
);
160 if (!ret
) ret
= link_info
;
166 display_status_bar(struct session
*ses
, struct terminal
*term
, int tabs_count
)
168 unsigned char *msg
= NULL
;
169 unsigned int tab_info_len
= 0;
170 struct download
*download
= get_current_download(ses
);
171 struct session_status
*status
= &ses
->status
;
172 struct color_pair
*text_color
= NULL
;
177 if (ses
->kbdprefix
.mark
!= KP_MARK_NOTHING
) {
178 switch (ses
->kbdprefix
.mark
) {
179 case KP_MARK_NOTHING
:
184 msg
= msg_text(term
, N_("Enter a mark to set"));
188 msg
= msg_text(term
, N_("Enter a mark"
189 " to which to jump"));
194 if (ses
->kbdprefix
.repeat_count
) {
195 msg
= msg_text(term
, N_("Keyboard prefix: %d"),
196 ses
->kbdprefix
.repeat_count
);
198 #ifdef CONFIG_ECMASCRIPT
199 else if (ses
->status
.window_status
) {
200 msg
= stracpy(ses
->status
.window_status
);
204 struct document_view
*doc_view
= current_frame(ses
);
206 /* Show S_INTERRUPTED message *once* but then show links
208 /* doc_view->vs may be NULL here in the short interval between
209 * ses_forward() with @loading_in_frame set, disconnecting the
210 * doc_view from vs, and render_document_frames(), detaching
212 if (doc_view
&& doc_view
->vs
) {
213 static int last_current_link
;
214 int ncl
= doc_view
->vs
->current_link
;
216 if (download
->state
== S_INTERRUPTED
217 && ncl
!= last_current_link
)
218 download
->state
= S_OK
;
219 last_current_link
= ncl
;
221 if (download
->state
== S_OK
) {
222 if (get_current_link(doc_view
)) {
223 msg
= get_current_link_info_and_title(ses
, doc_view
);
224 } else if (ses
->navigate_mode
== NAVIGATE_CURSOR_ROUTING
) {
225 msg
= msg_text(term
, N_("Cursor position: %dx%d"),
226 ses
->tab
->x
+ 1, ses
->tab
->y
+ 1);
232 int full
= term
->width
> 130;
233 int wide
= term
->width
> 80;
235 msg
= get_download_msg(download
, term
, wide
, full
, ", ");
239 set_box(&box
, 0, term
->height
- 1, term
->width
, 1);
240 draw_box(term
, &box
, ' ', 0, get_bfu_color(term
, "status.status-bar"));
242 if (!status
->show_tabs_bar
&& tabs_count
> 1) {
243 unsigned char tab_info
[8];
245 tab_info
[tab_info_len
++] = '[';
246 ulongcat(tab_info
, &tab_info_len
, term
->current_tab
+ 1, 4, 0);
247 tab_info
[tab_info_len
++] = ']';
248 tab_info
[tab_info_len
++] = ' ';
249 tab_info
[tab_info_len
] = '\0';
251 text_color
= get_bfu_color(term
, "status.status-text");
252 draw_text(term
, 0, term
->height
- 1, tab_info
, tab_info_len
,
259 text_color
= get_bfu_color(term
, "status.status-text");
261 msglen
= strlen(msg
);
262 draw_text(term
, 0 + tab_info_len
, term
->height
- 1,
263 msg
, msglen
, 0, text_color
);
266 if (download_is_progressing(download
) && download
->progress
->size
> 0) {
267 int xend
= term
->width
- 1;
271 if (ses
->status
.show_leds
)
272 xend
-= term
->leds_length
;
275 width
= int_max(0, xend
- msglen
- tab_info_len
- 1);
276 if (width
< 6) return;
277 int_upper_bound(&width
, 20);
278 draw_progress_bar(download
->progress
, term
,
279 xend
- width
, term
->height
- 1, width
,
285 display_tab_bar(struct session
*ses
, struct terminal
*term
, int tabs_count
)
287 struct color_pair
*normal_color
= get_bfu_color(term
, "tabs.normal");
288 struct color_pair
*selected_color
= get_bfu_color(term
, "tabs.selected");
289 struct color_pair
*loading_color
= get_bfu_color(term
, "tabs.loading");
290 struct color_pair
*fresh_color
= get_bfu_color(term
, "tabs.unvisited");
291 struct color_pair
*tabsep_color
= get_bfu_color(term
, "tabs.separator");
292 struct session_status
*status
= &ses
->status
;
293 int tab_width
= int_max(1, term
->width
/ tabs_count
);
294 int tab_total_width
= tab_width
* tabs_count
;
295 int tab_remain_width
= int_max(0, term
->width
- tab_total_width
);
296 int tab_add
= int_max(1, (tab_remain_width
/ tabs_count
));
300 if (status
->show_tabs_bar_at_top
) set_box(&box
, 0, status
->show_title_bar
, term
->width
, 1);
301 else set_box(&box
, 0, term
->height
- (status
->show_status_bar
? 2 : 1), 0, 1);
303 for (tab_num
= 0; tab_num
< tabs_count
; tab_num
++) {
304 struct download
*download
= NULL
;
305 struct color_pair
*color
= normal_color
;
306 struct window
*tab
= get_tab_by_number(term
, tab_num
);
307 struct document_view
*doc_view
;
308 struct session
*tab_ses
= tab
->data
;
309 int actual_tab_width
= tab_width
- 1;
312 /* Adjust tab size to use full term width. */
313 if (tab_remain_width
) {
314 actual_tab_width
+= tab_add
;
315 tab_remain_width
-= tab_add
;
318 doc_view
= tab_ses
? current_frame(tab_ses
) : NULL
;
321 if (doc_view
->document
->title
322 && *(doc_view
->document
->title
))
323 msg
= doc_view
->document
->title
;
325 msg
= _("Untitled", term
);
327 msg
= _("No document", term
);
331 draw_char(term
, box
.x
, box
.y
, BORDER_SVLINE
,
332 SCREEN_ATTR_FRAME
, tabsep_color
);
336 if (tab_num
== term
->current_tab
) {
337 color
= selected_color
;
340 download
= get_current_download(tab_ses
);
342 if (download
&& download
->state
!= S_OK
) {
343 color
= loading_color
;
344 } else if (!tab_ses
|| !tab_ses
->status
.visited
) {
348 if (!download_is_progressing(download
)
349 || download
->progress
->size
<= 0)
353 box
.width
= actual_tab_width
+ 1;
354 draw_box(term
, &box
, ' ', 0, color
);
357 draw_progress_bar(download
->progress
, term
,
358 box
.x
, box
.y
, actual_tab_width
,
361 int msglen
= int_min(strlen(msg
), actual_tab_width
);
363 draw_text(term
, box
.x
, box
.y
, msg
, msglen
, 0, color
);
367 tab
->width
= actual_tab_width
;
368 if (tab_num
== tabs_count
- 1) {
369 /* This is the last tab, and is therefore followed
370 * by a space, not a separator; increment tab->width
371 * to count that space as part of the tab.
376 box
.x
+= actual_tab_width
;
380 /* Print page's title and numbering at window top. */
382 display_title_bar(struct session
*ses
, struct terminal
*term
)
384 struct document_view
*doc_view
;
385 struct document
*document
;
387 unsigned char buf
[40];
391 /* Clear the old title */
392 if (!get_opt_bool("ui.show_menu_bar_always")) {
395 set_box(&box
, 0, 0, term
->width
, 1);
396 draw_box(term
, &box
, ' ', 0, get_bfu_color(term
, "title.title-bar"));
399 doc_view
= current_frame(ses
);
400 if (!doc_view
|| !doc_view
->document
) return;
402 if (!init_string(&title
)) return;
404 document
= doc_view
->document
;
406 /* Set up the document page info string: '(' %page '/' %pages ')' */
407 height
= doc_view
->box
.height
;
408 if (height
< document
->height
&& doc_view
->vs
) {
409 int pos
= doc_view
->vs
->y
+ height
;
411 int pages
= height
? (document
->height
+ height
- 1) / height
: 1;
413 /* Check if at the end else calculate the page. */
414 if (pos
>= document
->height
) {
417 page
= int_min((pos
- height
/ 2) / height
+ 1, pages
);
420 buflen
= snprintf(buf
, sizeof(buf
), " (%d/%d)", page
, pages
);
421 if (buflen
< 0) buflen
= 0;
424 if (document
->title
) {
425 int maxlen
= int_max(term
->width
- 4 - buflen
, 0);
426 int titlelen
, titlewidth
;
430 titlewidth
= utf8_ptr2cells(document
->title
, NULL
);
431 titlewidth
= int_min(titlewidth
, maxlen
);
433 titlelen
= utf8_cells2bytes(document
->title
,
436 #endif /* CONFIG_UTF8 */
438 titlewidth
= int_min(strlen(document
->title
), maxlen
);
439 titlelen
= titlewidth
;
442 add_bytes_to_string(&title
, document
->title
, titlelen
);
444 if (titlewidth
== maxlen
)
445 add_bytes_to_string(&title
, "...", 3);
449 add_bytes_to_string(&title
, buf
, buflen
);
455 x
= int_max(term
->width
- 1
456 - utf8_ptr2cells(title
.source
,
460 #endif /* CONFIG_UTF8 */
461 x
= int_max(term
->width
- 1 - title
.length
, 0);
463 draw_text(term
, x
, 0, title
.source
, title
.length
, 0,
464 get_bfu_color(term
, "title.title-text"));
471 display_window_title(struct session
*ses
, struct terminal
*term
)
473 static struct session
*last_ses
;
474 struct session_status
*status
= &ses
->status
;
475 unsigned char *doc_title
= NULL
;
476 unsigned char *title
;
480 && ses
->doc_view
->document
481 && ses
->doc_view
->document
->title
482 && ses
->doc_view
->document
->title
[0])
483 doc_title
= ses
->doc_view
->document
->title
;
485 title
= doc_title
? straconcat(doc_title
, " - ELinks", NULL
)
489 titlelen
= strlen(title
);
491 || !status
->last_title
492 || strlen(status
->last_title
) != titlelen
493 || memcmp(status
->last_title
, title
, titlelen
)) {
494 mem_free_set(&status
->last_title
, title
);
495 set_terminal_title(term
, title
);
504 display_leds(struct session
*ses
, struct session_status
*status
)
506 if (ses
->doc_view
&& ses
->doc_view
->document
) {
507 struct cache_entry
*cached
= ses
->doc_view
->document
->cached
;
510 if (cached
->ssl_info
)
511 set_led_value(status
->ssl_led
, 'S');
513 unset_led_value(status
->ssl_led
);
515 /* FIXME: We should do this thing better. */
516 set_led_value(status
->ssl_led
, '?');
520 if (ses
->insert_mode
== INSERT_MODE_LESS
) {
521 set_led_value(status
->insert_mode_led
, 'i');
523 if (ses
->insert_mode
== INSERT_MODE_ON
)
524 set_led_value(status
->insert_mode_led
, 'I');
526 unset_led_value(status
->insert_mode_led
);
531 #endif /* CONFIG_LEDS */
533 /* Print statusbar and titlebar, set terminal title. */
535 print_screen_status(struct session
*ses
)
537 struct terminal
*term
= ses
->tab
->term
;
538 struct session_status
*status
= &ses
->status
;
539 int tabs_count
= number_of_tabs(term
);
540 int ses_tab_is_current
= (ses
->tab
== get_current_tab(term
));
542 if (ses_tab_is_current
) {
543 if (status
->set_window_title
)
544 display_window_title(ses
, term
);
546 if (status
->show_title_bar
)
547 display_title_bar(ses
, term
);
549 if (status
->show_status_bar
)
550 display_status_bar(ses
, term
, tabs_count
);
552 if (status
->show_leds
)
553 display_leds(ses
, status
);
556 if (!ses
->status
.visited
)
557 ses
->status
.visited
= 1;
560 if (status
->show_tabs_bar
) {
561 display_tab_bar(ses
, term
, tabs_count
);
564 redraw_from_window(ses
->tab
);