1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License version 2 |
---|
6 | * as published by the Free Software Foundation. |
---|
7 | * |
---|
8 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
9 | * |
---|
10 | * $Id: formatter.cc 13919 2013-02-01 18:52:55Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | #include <libtransmission/transmission.h> |
---|
16 | #include <libtransmission/utils.h> // tr_formatter |
---|
17 | |
---|
18 | #include "formatter.h" |
---|
19 | #include "speed.h" |
---|
20 | |
---|
21 | /*** |
---|
22 | **** Constants |
---|
23 | ***/ |
---|
24 | |
---|
25 | namespace |
---|
26 | { |
---|
27 | unsigned int speed_K; |
---|
28 | unsigned int mem_K; |
---|
29 | unsigned int size_K; |
---|
30 | } |
---|
31 | |
---|
32 | QString Formatter::unitStrings[3][5]; |
---|
33 | |
---|
34 | void |
---|
35 | Formatter :: initUnits( ) |
---|
36 | { |
---|
37 | speed_K = 1000; |
---|
38 | unitStrings[SPEED][B] = tr( "B/s" ); |
---|
39 | unitStrings[SPEED][KB] = tr( "kB/s" ); |
---|
40 | unitStrings[SPEED][MB] = tr( "MB/s" ); |
---|
41 | unitStrings[SPEED][GB] = tr( "GB/s" ); |
---|
42 | unitStrings[SPEED][TB] = tr( "TB/s" ); |
---|
43 | tr_formatter_speed_init( speed_K, |
---|
44 | unitStrings[SPEED][KB].toUtf8().constData(), |
---|
45 | unitStrings[SPEED][MB].toUtf8().constData(), |
---|
46 | unitStrings[SPEED][GB].toUtf8().constData(), |
---|
47 | unitStrings[SPEED][TB].toUtf8().constData() ); |
---|
48 | |
---|
49 | size_K = 1000; |
---|
50 | unitStrings[SIZE][B] = tr( "B" ); |
---|
51 | unitStrings[SIZE][KB] = tr( "kB" ); |
---|
52 | unitStrings[SIZE][MB] = tr( "MB" ); |
---|
53 | unitStrings[SIZE][GB] = tr( "GB" ); |
---|
54 | unitStrings[SIZE][TB] = tr( "TB" ); |
---|
55 | tr_formatter_size_init( size_K, |
---|
56 | unitStrings[SIZE][KB].toUtf8().constData(), |
---|
57 | unitStrings[SIZE][MB].toUtf8().constData(), |
---|
58 | unitStrings[SIZE][GB].toUtf8().constData(), |
---|
59 | unitStrings[SIZE][TB].toUtf8().constData() ); |
---|
60 | |
---|
61 | mem_K = 1024; |
---|
62 | unitStrings[MEM][B] = tr( "B" ); |
---|
63 | unitStrings[MEM][KB] = tr( "KiB" ); |
---|
64 | unitStrings[MEM][MB] = tr( "MiB" ); |
---|
65 | unitStrings[MEM][GB] = tr( "GiB" ); |
---|
66 | unitStrings[MEM][TB] = tr( "TiB" ); |
---|
67 | tr_formatter_mem_init( mem_K, |
---|
68 | unitStrings[MEM][KB].toUtf8().constData(), |
---|
69 | unitStrings[MEM][MB].toUtf8().constData(), |
---|
70 | unitStrings[MEM][GB].toUtf8().constData(), |
---|
71 | unitStrings[MEM][TB].toUtf8().constData() ); |
---|
72 | } |
---|
73 | |
---|
74 | /*** |
---|
75 | **** |
---|
76 | ***/ |
---|
77 | |
---|
78 | double |
---|
79 | Speed :: KBps( ) const |
---|
80 | { |
---|
81 | return _Bps / (double)speed_K; |
---|
82 | } |
---|
83 | |
---|
84 | Speed |
---|
85 | Speed :: fromKBps( double KBps ) |
---|
86 | { |
---|
87 | return int( KBps * speed_K ); |
---|
88 | } |
---|
89 | |
---|
90 | /*** |
---|
91 | **** |
---|
92 | ***/ |
---|
93 | |
---|
94 | QString |
---|
95 | Formatter :: memToString( int64_t bytes ) |
---|
96 | { |
---|
97 | if( bytes < 1 ) |
---|
98 | return tr( "Unknown" ); |
---|
99 | else if( !bytes ) |
---|
100 | return tr( "None" ); |
---|
101 | else { |
---|
102 | char buf[128]; |
---|
103 | tr_formatter_mem_B( buf, bytes, sizeof( buf ) ); |
---|
104 | return QString::fromUtf8( buf ); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | QString |
---|
109 | Formatter :: sizeToString( int64_t bytes ) |
---|
110 | { |
---|
111 | if( bytes < 1 ) |
---|
112 | return tr( "Unknown" ); |
---|
113 | else if( !bytes ) |
---|
114 | return tr( "None" ); |
---|
115 | else { |
---|
116 | char buf[128]; |
---|
117 | tr_formatter_size_B( buf, bytes, sizeof( buf ) ); |
---|
118 | return QString::fromUtf8( buf ); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | QString |
---|
123 | Formatter :: speedToString( const Speed& speed ) |
---|
124 | { |
---|
125 | if( speed.isZero( ) ) |
---|
126 | return tr( "None" ); |
---|
127 | else { |
---|
128 | char buf[128]; |
---|
129 | tr_formatter_speed_KBps( buf, speed.KBps( ), sizeof( buf ) ); |
---|
130 | return QString::fromUtf8( buf ); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | QString |
---|
135 | Formatter :: percentToString( double x ) |
---|
136 | { |
---|
137 | char buf[128]; |
---|
138 | return QString( tr_strpercent( buf, x, sizeof(buf) ) ); |
---|
139 | } |
---|
140 | |
---|
141 | QString |
---|
142 | Formatter :: ratioToString( double ratio ) |
---|
143 | { |
---|
144 | char buf[128]; |
---|
145 | return QString::fromUtf8( tr_strratio( buf, sizeof(buf), ratio, "\xE2\x88\x9E" ) ); |
---|
146 | } |
---|
147 | |
---|
148 | QString |
---|
149 | Formatter :: timeToString( int seconds ) |
---|
150 | { |
---|
151 | int days, hours, minutes; |
---|
152 | QString d, h, m, s; |
---|
153 | QString str; |
---|
154 | |
---|
155 | if( seconds < 0 ) |
---|
156 | seconds = 0; |
---|
157 | |
---|
158 | days = seconds / 86400; |
---|
159 | hours = ( seconds % 86400 ) / 3600; |
---|
160 | minutes = ( seconds % 3600 ) / 60; |
---|
161 | seconds %= 60; |
---|
162 | |
---|
163 | d = tr( "%Ln day(s)", 0, days ); |
---|
164 | h = tr( "%Ln hour(s)", 0, hours ); |
---|
165 | m = tr( "%Ln minute(s)", 0, minutes ); |
---|
166 | s = tr( "%Ln second(s)", 0, seconds ); |
---|
167 | |
---|
168 | if( days ) |
---|
169 | { |
---|
170 | if( days >= 4 || !hours ) |
---|
171 | str = d; |
---|
172 | else |
---|
173 | str = tr( "%1, %2" ).arg( d ).arg( h ); |
---|
174 | } |
---|
175 | else if( hours ) |
---|
176 | { |
---|
177 | if( hours >= 4 || !minutes ) |
---|
178 | str = h; |
---|
179 | else |
---|
180 | str = tr( "%1, %2" ).arg( h ).arg( m ); |
---|
181 | } |
---|
182 | else if( minutes ) |
---|
183 | { |
---|
184 | if( minutes >= 4 || !seconds ) |
---|
185 | str = m; |
---|
186 | else |
---|
187 | str = tr( "%1, %2" ).arg( m ).arg( s ); |
---|
188 | } |
---|
189 | else |
---|
190 | { |
---|
191 | str = s; |
---|
192 | } |
---|
193 | |
---|
194 | return str; |
---|
195 | } |
---|