1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: utils.cc 10816 2010-06-22 00:12:52Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | #include <QApplication> |
---|
16 | #include <QDataStream> |
---|
17 | #include <QFile> |
---|
18 | #include <QFileDialog> |
---|
19 | #include <QFileInfo> |
---|
20 | #include <QInputDialog> |
---|
21 | #include <QObject> |
---|
22 | #include <QSet> |
---|
23 | #include <QStyle> |
---|
24 | |
---|
25 | #include <libtransmission/transmission.h> |
---|
26 | #include <libtransmission/utils.h> |
---|
27 | |
---|
28 | #include "qticonloader.h" |
---|
29 | #include "utils.h" |
---|
30 | |
---|
31 | QString |
---|
32 | Utils :: remoteFileChooser( QWidget * parent, const QString& title, const QString& myPath, bool dir, bool local ) |
---|
33 | { |
---|
34 | QString path; |
---|
35 | |
---|
36 | if( local ) |
---|
37 | { |
---|
38 | if( dir ) |
---|
39 | path = QFileDialog::getExistingDirectory( parent, title, myPath ); |
---|
40 | else |
---|
41 | path = QFileDialog::getOpenFileName( parent, title, myPath ); |
---|
42 | } |
---|
43 | else |
---|
44 | path = QInputDialog::getText( parent, title, tr( "Enter a location:" ), QLineEdit::Normal, myPath, NULL ); |
---|
45 | |
---|
46 | return path; |
---|
47 | } |
---|
48 | |
---|
49 | #define KILOBYTE_FACTOR 1024.0 |
---|
50 | #define MEGABYTE_FACTOR ( 1024.0 * 1024.0 ) |
---|
51 | #define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 ) |
---|
52 | |
---|
53 | QString |
---|
54 | Utils :: sizeToString( double size ) |
---|
55 | { |
---|
56 | QString str; |
---|
57 | |
---|
58 | if( !size ) |
---|
59 | { |
---|
60 | str = tr( "None" ); |
---|
61 | } |
---|
62 | else if( size < KILOBYTE_FACTOR ) |
---|
63 | { |
---|
64 | const int i = (int)size; |
---|
65 | str = tr( "%Ln byte(s)", 0, i ); |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | double displayed_size; |
---|
70 | |
---|
71 | if( size < (int64_t)MEGABYTE_FACTOR ) |
---|
72 | { |
---|
73 | displayed_size = (double)size / KILOBYTE_FACTOR; |
---|
74 | str = tr( "%L1 KiB" ).arg( displayed_size, 0, 'f', 1 ); |
---|
75 | } |
---|
76 | else if( size < (int64_t)GIGABYTE_FACTOR ) |
---|
77 | { |
---|
78 | displayed_size = (double)size / MEGABYTE_FACTOR; |
---|
79 | str = tr( "%L1 MiB" ).arg( displayed_size, 0, 'f', 2 ); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | displayed_size = (double) size / GIGABYTE_FACTOR; |
---|
84 | str = tr( "%L1 GiB" ).arg( displayed_size, 0, 'f', 3 ); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | return str; |
---|
89 | } |
---|
90 | |
---|
91 | QString |
---|
92 | Utils :: ratioToString( double ratio ) |
---|
93 | { |
---|
94 | QString buf; |
---|
95 | |
---|
96 | if( (int)ratio == TR_RATIO_NA ) |
---|
97 | buf = tr( "None" ); |
---|
98 | else if( (int)ratio == TR_RATIO_INF ) |
---|
99 | buf = QString::fromUtf8( "\xE2\x88\x9E" ); |
---|
100 | else |
---|
101 | { |
---|
102 | QStringList temp; |
---|
103 | |
---|
104 | temp = QString().sprintf( "%f", ratio ).split( "." ); |
---|
105 | if( ratio < 100.0 ) |
---|
106 | { |
---|
107 | if( ratio < 10.0 ) |
---|
108 | temp[1].truncate( 2 ); |
---|
109 | else |
---|
110 | temp[1].truncate( 1 ); |
---|
111 | buf = temp.join( "." ); |
---|
112 | } |
---|
113 | else |
---|
114 | buf = QString( temp[0] ); |
---|
115 | } |
---|
116 | |
---|
117 | return buf; |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | QString |
---|
122 | Utils :: timeToString( int seconds ) |
---|
123 | { |
---|
124 | int days, hours, minutes; |
---|
125 | QString d, h, m, s; |
---|
126 | QString str; |
---|
127 | |
---|
128 | if( seconds < 0 ) |
---|
129 | seconds = 0; |
---|
130 | |
---|
131 | days = seconds / 86400; |
---|
132 | hours = ( seconds % 86400 ) / 3600; |
---|
133 | minutes = ( seconds % 3600 ) / 60; |
---|
134 | seconds %= 60; |
---|
135 | |
---|
136 | d = tr( "%Ln day(s)", 0, days ); |
---|
137 | h = tr( "%Ln hour(s)", 0, hours ); |
---|
138 | m = tr( "%Ln minute(s)", 0, minutes ); |
---|
139 | s = tr( "%Ln second(s)", 0, seconds ); |
---|
140 | |
---|
141 | if( days ) |
---|
142 | { |
---|
143 | if( days >= 4 || !hours ) |
---|
144 | str = d; |
---|
145 | else |
---|
146 | str = tr( "%1, %2" ).arg( d ).arg( h ); |
---|
147 | } |
---|
148 | else if( hours ) |
---|
149 | { |
---|
150 | if( hours >= 4 || !minutes ) |
---|
151 | str = h; |
---|
152 | else |
---|
153 | str = tr( "%1, %2" ).arg( h ).arg( m ); |
---|
154 | } |
---|
155 | else if( minutes ) |
---|
156 | { |
---|
157 | if( minutes >= 4 || !seconds ) |
---|
158 | str = m; |
---|
159 | else |
---|
160 | str = tr( "%1, %2" ).arg( m ).arg( s ); |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | str = s; |
---|
165 | } |
---|
166 | |
---|
167 | return str; |
---|
168 | } |
---|
169 | |
---|
170 | QString |
---|
171 | Utils :: speedToString( const Speed& speed ) |
---|
172 | { |
---|
173 | const double kbps( speed.kbps( ) ); |
---|
174 | QString str; |
---|
175 | |
---|
176 | if( kbps < 1000.0 ) /* 0.0 KiB to 999.9 KiB */ |
---|
177 | str = tr( "%L1 KiB/s" ).arg( kbps, 0, 'f', 1 ); |
---|
178 | else if( kbps < 102400.0 ) /* 0.98 MiB to 99.99 MiB */ |
---|
179 | str = tr( "%L1 MiB/s" ).arg( kbps / KILOBYTE_FACTOR, 0, 'f', 2 ); |
---|
180 | else // insane speeds |
---|
181 | str = tr( "%L1 GiB/s" ).arg( kbps / MEGABYTE_FACTOR, 0, 'f', 1 ); |
---|
182 | |
---|
183 | return str; |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | Utils :: toStderr( const QString& str ) |
---|
188 | { |
---|
189 | std::cerr << qPrintable(str) << std::endl; |
---|
190 | } |
---|
191 | |
---|
192 | const QIcon& |
---|
193 | Utils :: guessMimeIcon( const QString& filename ) |
---|
194 | { |
---|
195 | enum { DISK, DOCUMENT, PICTURE, VIDEO, ARCHIVE, AUDIO, APP, TYPE_COUNT }; |
---|
196 | static QIcon fallback; |
---|
197 | static QIcon fileIcons[TYPE_COUNT]; |
---|
198 | static QSet<QString> suffixes[TYPE_COUNT]; |
---|
199 | |
---|
200 | if( fileIcons[0].isNull( ) ) |
---|
201 | { |
---|
202 | fallback = QApplication::style()->standardIcon( QStyle :: SP_FileIcon ); |
---|
203 | |
---|
204 | fileIcons[DISK]= QtIconLoader :: icon( "media-optical", fallback ); |
---|
205 | suffixes[DISK] << "iso"; |
---|
206 | |
---|
207 | fileIcons[DOCUMENT] = QtIconLoader :: icon( "text-x-generic", fallback ); |
---|
208 | suffixes[DOCUMENT] << "abw" << "csv" << "doc" << "dvi" << "htm" << "html" << "ini" << "log" |
---|
209 | << "odp" << "ods" << "odt" << "pdf" << "ppt" << "ps" << "rtf" << "tex" |
---|
210 | << "txt" << "xml"; |
---|
211 | |
---|
212 | fileIcons[PICTURE] = QtIconLoader :: icon( "image-x-generic", fallback ); |
---|
213 | suffixes[PICTURE] << "bmp" << "gif" << "jpg" << "jpeg" << "pcx" << "png" << "psd" << "raw" |
---|
214 | << "tga" << "tiff"; |
---|
215 | |
---|
216 | fileIcons[VIDEO] = QtIconLoader :: icon( "video-x-generic", fallback ); |
---|
217 | suffixes[VIDEO] << "3gp" << "asf" << "avi" << "mov" << "mpeg" << "mpg" << "mp4" << "mkv" |
---|
218 | << "mov" << "ogm" << "ogv" << "qt" << "rm" << "wmv"; |
---|
219 | |
---|
220 | fileIcons[ARCHIVE] = QtIconLoader :: icon( "package-x-generic", fallback ); |
---|
221 | suffixes[ARCHIVE] << "7z" << "ace" << "bz2" << "cbz" << "gz" << "gzip" << "lzma" << "rar" |
---|
222 | << "sft" << "tar" << "zip"; |
---|
223 | |
---|
224 | fileIcons[AUDIO] = QtIconLoader :: icon( "audio-x-generic", fallback ); |
---|
225 | suffixes[AUDIO] << "aac" << "ac3" << "aiff" << "ape" << "au" << "flac" << "m3u" << "m4a" |
---|
226 | << "mid" << "midi" << "mp2" << "mp3" << "mpc" << "nsf" << "oga" << "ogg" |
---|
227 | << "ra" << "ram" << "shn" << "voc" << "wav" << "wma"; |
---|
228 | |
---|
229 | fileIcons[APP] = QtIconLoader :: icon( "application-x-executable", fallback ); |
---|
230 | suffixes[APP] << "bat" << "cmd" << "com" << "exe"; |
---|
231 | } |
---|
232 | |
---|
233 | QString suffix( QFileInfo( filename ).suffix( ).toLower( ) ); |
---|
234 | |
---|
235 | for( int i=0; i<TYPE_COUNT; ++i ) |
---|
236 | if( suffixes[i].contains( suffix ) ) |
---|
237 | return fileIcons[i]; |
---|
238 | |
---|
239 | return fallback; |
---|
240 | } |
---|