2
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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.
15
#include <QApplication>
16
#include <QDataStream>
23
#include <libtransmission/transmission.h>
24
#include <libtransmission/utils.h>
26
#include "qticonloader.h"
29
#define KILOBYTE_FACTOR 1024.0
30
#define MEGABYTE_FACTOR ( 1024.0 * 1024.0 )
31
#define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 )
34
Utils :: sizeToString( double size )
42
else if( size < 1024.0 )
44
const int i = (int)size;
45
str = tr( "%Ln byte(s)", 0, i );
49
double displayed_size;
51
if( size < (int64_t)MEGABYTE_FACTOR )
53
displayed_size = (double)size / KILOBYTE_FACTOR;
54
str = tr( "%L1 KB" ).arg( displayed_size, 0, 'f', 1 );
56
else if( size < (int64_t)GIGABYTE_FACTOR )
58
displayed_size = (double)size / MEGABYTE_FACTOR;
59
str = tr( "%L1 MB" ).arg( displayed_size, 0, 'f', 1 );
63
displayed_size = (double) size / GIGABYTE_FACTOR;
64
str = tr( "%L1 GB" ).arg( displayed_size, 0, 'f', 1 );
72
Utils :: ratioToString( double ratio )
76
if( (int)ratio == TR_RATIO_NA )
78
else if( (int)ratio == TR_RATIO_INF )
79
buf = QString::fromUtf8( "\xE2\x88\x9E" );
80
else if( ratio < 10.0 )
81
buf.sprintf( "%'.2f", ratio );
82
else if( ratio < 100.0 )
83
buf.sprintf( "%'.1f", ratio );
85
buf.sprintf( "%'.0f", ratio );
92
Utils :: timeToString( int seconds )
94
int days, hours, minutes;
101
days = seconds / 86400;
102
hours = ( seconds % 86400 ) / 3600;
103
minutes = ( seconds % 3600 ) / 60;
106
d = tr( "%Ln day(s)", 0, days );
107
h = tr( "%Ln hour(s)", 0, hours );
108
m = tr( "%Ln minute(s)", 0, minutes );
109
s = tr( "%Ln seconds(s)", 0, seconds );
113
if( days >= 4 || !hours )
116
str = tr( "%1, %2" ).arg( d ).arg( h );
120
if( hours >= 4 || !minutes )
123
str = tr( "%1, %2" ).arg( h ).arg( m );
127
if( minutes >= 4 || !seconds )
130
str = tr( "%1, %2" ).arg( m ).arg( s );
141
Utils :: speedToString( const Speed& speed )
143
const double kbps( speed.kbps( ) );
146
if( kbps < 1000.0 ) /* 0.0 KB to 999.9 KB */
147
str = tr( "%L1 KB/s" ).arg( kbps, 0, 'f', 1 );
148
else if( kbps < 102400.0 ) /* 0.98 MB to 99.99 MB */
149
str = tr( "%L1 MB/s" ).arg( kbps/1024.0, 0, 'f', 2 );
150
else // insane speeds
151
str = tr( "%L1 GB/s" ).arg( kbps/(1024.0*1024.0), 0, 'f', 1 );
157
Utils :: toStderr( const QString& str )
159
std::cerr << str.toLatin1().constData() << std::endl;
163
Utils :: guessMimeIcon( const QString& filename )
165
enum { DISK, DOCUMENT, PICTURE, VIDEO, ARCHIVE, AUDIO, APP, TYPE_COUNT };
166
static QIcon fallback;
167
static QIcon fileIcons[TYPE_COUNT];
168
static QSet<QString> suffixes[TYPE_COUNT];
170
if( fileIcons[0].isNull( ) )
172
fallback = QApplication::style()->standardIcon( QStyle :: SP_FileIcon );
174
fileIcons[DISK]= QtIconLoader :: icon( "media-optical", fallback );
175
suffixes[DISK] << "iso";
177
fileIcons[DOCUMENT] = QtIconLoader :: icon( "text-x-generic", fallback );
178
suffixes[DOCUMENT] << "txt" << "doc" << "pdf" << "rtf" << "htm" << "html";
180
fileIcons[PICTURE] = QtIconLoader :: icon( "image-x-generic", fallback );
181
suffixes[PICTURE] << "jpg" << "jpeg" << "png" << "gif" << "tiff" << "pcx";
183
fileIcons[VIDEO] = QtIconLoader :: icon( "video-x-generic", fallback );
184
suffixes[VIDEO] << "avi" << "mpeg" << "mp4" << "mkv" << "mov";
186
fileIcons[ARCHIVE] = QtIconLoader :: icon( "package-x-generic", fallback );
187
suffixes[ARCHIVE] << "rar" << "zip" << "sft" << "tar" << "7z" << "cbz";
189
fileIcons[AUDIO] = QtIconLoader :: icon( "audio-x-generic", fallback );
190
suffixes[AUDIO] << "aiff" << "au" << "m3u" << "mp2" << "wav" << "mp3" << "ape" << "mid"
191
<< "aac" << "ogg" << "ra" << "ram" << "flac" << "mpc" << "shn";
193
fileIcons[APP] = QtIconLoader :: icon( "application-x-executable", fallback );
194
suffixes[APP] << "exe";
197
QString suffix( QFileInfo( filename ).suffix( ).toLower( ) );
199
for( int i=0; i<TYPE_COUNT; ++i )
200
if( suffixes[i].contains( suffix ) )