Retry only for https protocol
[elinks.git] / src / dialogs / progress.c
blob9f6e2ef27562db3653c70a4357188589d13d7658
1 /* Display of downloads progression stuff. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include "elinks.h"
9 #include "bfu/dialog.h"
10 #include "intl/gettext/libintl.h"
11 #include "network/progress.h"
12 #include "terminal/draw.h"
13 #include "util/conv.h"
14 #include "util/error.h"
15 #include "util/memory.h"
16 #include "util/string.h"
18 static unsigned char *
19 get_progress_msg_2(struct progress *progress, struct terminal *term,
20 int wide, int full, unsigned char *separator, unsigned char *type)
22 struct string msg;
23 int newlines = separator[strlen(separator) - 1] == '\n';
25 if (!init_string(&msg)) return NULL;
27 /* FIXME: The following is a PITA from the l10n standpoint. A *big*
28 * one, _("of")-like pearls are a nightmare. Format strings need to
29 * be introduced to this fuggy corner of code as well. --pasky */
31 add_to_string(&msg, type);
32 add_char_to_string(&msg, ' ');
33 add_xnum_to_string(&msg, progress->pos);
34 if (progress->size >= 0) {
35 add_char_to_string(&msg, ' ');
36 add_to_string(&msg, _("of", term));
37 add_char_to_string(&msg, ' ');
38 add_xnum_to_string(&msg, progress->size);
41 add_to_string(&msg, separator);
43 if (wide) {
44 /* Do the following only if there is room */
46 add_to_string(&msg,
47 _(full ? (newlines ? N_("Average speed")
48 : N_("average speed"))
49 : N_("avg"),
50 term));
51 add_char_to_string(&msg, ' ');
52 add_xnum_to_string(&msg, progress->average_speed);
53 add_to_string(&msg, "/s");
55 add_to_string(&msg, ", ");
56 add_to_string(&msg,
57 _(full ? N_("current speed") : N_("cur"), term));
58 add_char_to_string(&msg, ' '),
59 add_xnum_to_string(&msg, progress->current_speed);
60 add_to_string(&msg, "/s");
62 add_to_string(&msg, separator);
64 add_to_string(&msg, _(full ? (newlines ? N_("Elapsed time")
65 : N_("elapsed time"))
66 : N_("ETT"),
67 term));
68 add_char_to_string(&msg, ' ');
69 add_timeval_to_string(&msg, &progress->elapsed);
71 } else {
72 add_to_string(&msg, _(newlines ? N_("Speed") : N_("speed"),
73 term));
75 add_char_to_string(&msg, ' ');
76 add_xnum_to_string(&msg, progress->average_speed);
77 add_to_string(&msg, "/s");
80 if (progress->size >= 0 && progress->loaded > 0) {
81 add_to_string(&msg, ", ");
82 add_to_string(&msg, _(full ? N_("estimated time")
83 : N_("ETA"),
84 term));
85 add_char_to_string(&msg, ' ');
86 add_timeval_to_string(&msg, &progress->estimated_time);
89 return msg.source;
92 unsigned char *
93 get_upload_progress_msg(struct progress *progress, struct terminal *term,
94 int wide, int full, unsigned char *separator)
96 return get_progress_msg_2(progress, term, wide, full, separator, _("Sent", term));
99 unsigned char *
100 get_progress_msg(struct progress *progress, struct terminal *term,
101 int wide, int full, unsigned char *separator)
103 return get_progress_msg_2(progress, term, wide, full, separator, _("Received", term));
106 void
107 draw_progress_bar(struct progress *progress, struct terminal *term,
108 int x, int y, int width,
109 unsigned char *text, struct color_pair *meter_color)
111 /* Note : values > 100% are theorically possible and were seen. */
112 int percent = 0;
113 struct box barprogress;
115 if (progress->size > 0)
116 percent = (int) ((longlong) 100 * progress->pos / progress->size);
118 /* Draw the progress meter part "[### ]" */
119 if (!text && width > 2) {
120 width -= 2;
121 draw_text(term, x++, y, "[", 1, 0, NULL);
122 draw_text(term, x + width, y, "]", 1, 0, NULL);
125 if (!meter_color) meter_color = get_bfu_color(term, "dialog.meter");
126 set_box(&barprogress,
127 x, y, int_min(width * percent / 100, width), 1);
128 draw_box(term, &barprogress, ' ', 0, meter_color);
130 /* On error, will print '?' only, should not occur. */
131 if (text) {
132 width = int_min(width, strlen(text));
134 } else if (width > 1) {
135 static unsigned char s[] = "????"; /* Reduce or enlarge at will. */
136 unsigned int slen = 0;
137 int max = int_min(sizeof(s), width) - 1;
139 if (ulongcat(s, &slen, percent, max, 0)) {
140 s[0] = '?';
141 slen = 1;
144 s[slen++] = '%';
146 /* Draw the percentage centered in the progress meter */
147 x += (1 + width - slen) / 2;
149 assert(slen <= width);
150 width = slen;
151 text = s;
154 draw_text(term, x, y, text, width, 0, NULL);