1 /*
2 * This file Copyright (C) 2012-2015 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 */
8
9 #include <libtransmission/transmission.h>
10 #include <libtransmission/utils.h> // tr_formatter
11
12 #include "Formatter.h"
13 #include "Speed.h"
14
15 /***
16 **** Constants
17 ***/
18
19 namespace
20 {
21
22 unsigned int speed_K;
23 unsigned int mem_K;
24 unsigned int size_K;
25
26 } // namespace
27
28 QString Formatter::unitStrings[3][5];
29
initUnits()30 void Formatter::initUnits()
31 {
32 speed_K = 1000;
33 unitStrings[SPEED][B] = tr("B/s");
34 unitStrings[SPEED][KB] = tr("kB/s");
35 unitStrings[SPEED][MB] = tr("MB/s");
36 unitStrings[SPEED][GB] = tr("GB/s");
37 unitStrings[SPEED][TB] = tr("TB/s");
38 tr_formatter_speed_init(speed_K, unitStrings[SPEED][KB].toUtf8().constData(), unitStrings[SPEED][MB].toUtf8().constData(),
39 unitStrings[SPEED][GB].toUtf8().constData(), unitStrings[SPEED][TB].toUtf8().constData());
40
41 size_K = 1000;
42 unitStrings[SIZE][B] = tr("B");
43 unitStrings[SIZE][KB] = tr("kB");
44 unitStrings[SIZE][MB] = tr("MB");
45 unitStrings[SIZE][GB] = tr("GB");
46 unitStrings[SIZE][TB] = tr("TB");
47 tr_formatter_size_init(size_K, unitStrings[SIZE][KB].toUtf8().constData(), unitStrings[SIZE][MB].toUtf8().constData(),
48 unitStrings[SIZE][GB].toUtf8().constData(), unitStrings[SIZE][TB].toUtf8().constData());
49
50 mem_K = 1024;
51 unitStrings[MEM][B] = tr("B");
52 unitStrings[MEM][KB] = tr("KiB");
53 unitStrings[MEM][MB] = tr("MiB");
54 unitStrings[MEM][GB] = tr("GiB");
55 unitStrings[MEM][TB] = tr("TiB");
56 tr_formatter_mem_init(mem_K, unitStrings[MEM][KB].toUtf8().constData(), unitStrings[MEM][MB].toUtf8().constData(),
57 unitStrings[MEM][GB].toUtf8().constData(), unitStrings[MEM][TB].toUtf8().constData());
58 }
59
60 /***
61 ****
62 ***/
63
KBps() const64 double Speed::KBps() const
65 {
66 return _Bps / static_cast<double>(speed_K);
67 }
68
fromKBps(double KBps)69 Speed Speed::fromKBps(double KBps)
70 {
71 return static_cast<int>(KBps * speed_K);
72 }
73
74 /***
75 ****
76 ***/
77
memToString(int64_t bytes)78 QString Formatter::memToString(int64_t bytes)
79 {
80 if (bytes < 0)
81 {
82 return tr("Unknown");
83 }
84
85 if (bytes == 0)
86 {
87 return tr("None");
88 }
89
90 char buf[128];
91 tr_formatter_mem_B(buf, bytes, sizeof(buf));
92 return QString::fromUtf8(buf);
93 }
94
sizeToString(int64_t bytes)95 QString Formatter::sizeToString(int64_t bytes)
96 {
97 if (bytes < 0)
98 {
99 return tr("Unknown");
100 }
101
102 if (bytes == 0)
103 {
104 return tr("None");
105 }
106
107 char buf[128];
108 tr_formatter_size_B(buf, bytes, sizeof(buf));
109 return QString::fromUtf8(buf);
110 }
111
speedToString(Speed const & speed)112 QString Formatter::speedToString(Speed const& speed)
113 {
114 char buf[128];
115 tr_formatter_speed_KBps(buf, speed.KBps(), sizeof(buf));
116 return QString::fromUtf8(buf);
117 }
118
uploadSpeedToString(Speed const & uploadSpeed)119 QString Formatter::uploadSpeedToString(Speed const& uploadSpeed)
120 {
121 static QChar const uploadSymbol(0x25B4);
122
123 return tr("%1 %2").arg(speedToString(uploadSpeed)).arg(uploadSymbol);
124 }
125
downloadSpeedToString(Speed const & downloadSpeed)126 QString Formatter::downloadSpeedToString(Speed const& downloadSpeed)
127 {
128 static QChar const downloadSymbol(0x25BE);
129
130 return tr("%1 %2").arg(speedToString(downloadSpeed)).arg(downloadSymbol);
131 }
132
percentToString(double x)133 QString Formatter::percentToString(double x)
134 {
135 char buf[128];
136 return QString::fromUtf8(tr_strpercent(buf, x, sizeof(buf)));
137 }
138
ratioToString(double ratio)139 QString Formatter::ratioToString(double ratio)
140 {
141 char buf[128];
142 return QString::fromUtf8(tr_strratio(buf, sizeof(buf), ratio, "\xE2\x88\x9E"));
143 }
144
timeToString(int seconds)145 QString Formatter::timeToString(int seconds)
146 {
147 int days;
148 int hours;
149 int minutes;
150 QString d;
151 QString h;
152 QString m;
153 QString s;
154 QString str;
155
156 if (seconds < 0)
157 {
158 seconds = 0;
159 }
160
161 days = seconds / 86400;
162 hours = (seconds % 86400) / 3600;
163 minutes = (seconds % 3600) / 60;
164 seconds %= 60;
165
166 d = tr("%Ln day(s)", nullptr, days);
167 h = tr("%Ln hour(s)", nullptr, hours);
168 m = tr("%Ln minute(s)", nullptr, minutes);
169 s = tr("%Ln second(s)", nullptr, seconds);
170
171 if (days != 0)
172 {
173 if (days >= 4 || hours == 0)
174 {
175 str = d;
176 }
177 else
178 {
179 str = tr("%1, %2").arg(d).arg(h);
180 }
181 }
182 else if (hours != 0)
183 {
184 if (hours >= 4 || minutes == 0)
185 {
186 str = h;
187 }
188 else
189 {
190 str = tr("%1, %2").arg(h).arg(m);
191 }
192 }
193 else if (minutes != 0)
194 {
195 if (minutes >= 4 || seconds == 0)
196 {
197 str = m;
198 }
199 else
200 {
201 str = tr("%1, %2").arg(m).arg(s);
202 }
203 }
204 else
205 {
206 str = s;
207 }
208
209 return str;
210 }
211