g_type_init: no longer needs to be called
[quvi-tool.git] / src / pbar / lpbar.c
blobd90e1c7a35aa05701d77505cdb14853212a10075
1 /* quvi
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
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 Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <glib/gi18n.h>
25 /* -- */
26 #include "lpbar.h"
28 typedef enum
30 Ki = 1024,
31 Mi = 1048576,
32 Gi = 1073741824,
33 } ieee_1541;
35 #define _s(n) #n
36 static const gchar *_units[] =
38 _s(Ki),
39 _s(Mi),
40 _s(Gi),
41 NULL
43 #undef _s
45 static const gchar *_to_unit(gdouble *n)
47 gint i = 0;
48 if (*n >=Gi)
50 *n /= Gi;
51 i = 2;
53 else if (*n >=Mi)
55 *n /= Mi;
56 i = 1;
58 else
59 *n /= Ki;
60 return (_units[i]);
63 lpbar_t lpbar_new()
65 lpbar_t p = g_new0(struct lpbar_s, 1);
66 p->counters.timer = g_timer_new();
67 p->mode = write;
68 return (p);
71 void lpbar_free(lpbar_t p)
73 if (p == NULL)
74 return;
76 if (p->content_bytes >0
77 && (p->counters.count + p->initial_bytes >p->content_bytes))
79 p->content_bytes = p->initial_bytes + p->counters.count;
82 if (p->flags.failed == FALSE)
84 p->flags.done = TRUE;
85 lpbar_update(p, -1);
88 g_timer_destroy(p->counters.timer);
89 g_free(p->content_type);
90 g_free(p->fname);
91 g_free(p);
93 memset(p, 0, sizeof(struct lpbar_s));
96 static const gchar *frames[] =
98 "=---",
99 "-=--",
100 "--=-",
101 "---=",
102 NULL
105 static const gchar *_next_frame(lpbar_t p)
107 if (frames[p->counters.curr_frame] == NULL)
108 p->counters.curr_frame = 0;
109 return (frames[p->counters.curr_frame++]);
112 static gchar *_eta(const glong s)
114 if (s >=86400) /* 24h */
115 return (g_strdup_printf(_("%ld hours"), (s/3600%60)));
116 return (g_strdup_printf("%02ld:%02ld:%02ld", (s/3600)%60, (s/60)%60, s%60));
119 static const gdouble update_interval = .5;
121 gint lpbar_update(lpbar_t p, gdouble dlnow)
123 gdouble rate, size, elapsed, percent;
124 const gchar *rate_unit, *frame;
125 gboolean inactive;
126 gchar *eta;
128 if (dlnow == 0 || p->flags.failed == TRUE)
129 return (0);
131 elapsed = g_timer_elapsed(p->counters.timer, NULL);
133 if (p->flags.done == TRUE)
134 dlnow = p->content_bytes;
135 else
137 if ((elapsed - p->counters.last_update) < update_interval)
138 return (0);
141 size = dlnow;
142 if (p->flags.done == FALSE)
143 size += p->initial_bytes;
145 inactive = (dlnow == 0) ? TRUE:FALSE;
146 rate = (elapsed >0) ? (dlnow/elapsed):0;
148 if (inactive == FALSE)
150 if (p->flags.done == FALSE)
152 const gdouble left =
153 (p->content_bytes - (dlnow + p->initial_bytes)) / rate;
155 eta = _eta(left+.5);
157 else
159 rate = (p->content_bytes - p->initial_bytes) / elapsed;
160 eta = _eta(elapsed);
162 rate_unit = _to_unit(&rate);
163 frame = _next_frame(p);
165 else
167 frame = frames[p->counters.curr_frame];
168 eta = g_strdup("--:--");
169 rate_unit = "--.-";
172 percent = 0;
173 if (p->content_bytes >0)
175 percent = (100.0 * size / p->content_bytes);
176 if (percent >= 100)
177 percent = 100;
180 g_print(_("copy: %s %3.0f%% %6.1f%s/s %4s%s"),
181 frame, percent, rate, rate_unit, eta,
182 (p->flags.done == TRUE) ? "\n":"\r");
184 p->counters.last_update = elapsed;
185 p->counters.count = dlnow;
186 g_free(eta);
188 return (0);
191 void lpbar_print(const lpbar_t p)
193 const gchar *u;
194 gdouble b;
196 b = p->content_bytes;
197 u = _to_unit(&b);
199 g_print(_("file: %s [media]\n"), p->fname);
200 g_print(_(" content length: %.1f%s"), b, u);
202 if (p->content_type != NULL)
203 g_print(_(" content type: %s"), p->content_type);
205 g_print(C_("To indicate transfer mode (resumed, ...) ", " mode: "));
206 switch (p->mode)
208 case retrieved_already:
209 g_print(C_("Transfer mode with a reason", "skip <retrieved already>"));
210 break;
211 case forced_skip:
212 g_print(C_("Transfer mode with a reason", "skip <forced>"));
213 break;
214 case resume:
215 case write:
216 default:
217 g_print("%s", (p->initial_bytes ==0)
218 ? C_("Transfer mode (begin at offset 0)", "write")
219 : C_("Transfer mode", "resume"));
220 break;
222 g_print("\n");
225 /* vim: set ts=2 sw=2 tw=72 expandtab: */