~hendrik-grewe/transmission/private-patch

« back to all changes in this revision

Viewing changes to qt/utils.cc

  • Committer: charles
  • Date: 2009-04-09 17:55:47 UTC
  • Revision ID: svn-v4:f4695dd4-2c0a-0410-b89c-da849a56a58e:trunk:8188
(trunk) add the Qt beta into svn 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
 
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:$
 
11
 */
 
12
 
 
13
#include <iostream>
 
14
 
 
15
#include <QApplication>
 
16
#include <QDataStream>
 
17
#include <QFile>
 
18
#include <QFileInfo>
 
19
#include <QObject>
 
20
#include <QSet>
 
21
#include <QStyle>
 
22
 
 
23
#include <libtransmission/transmission.h>
 
24
#include <libtransmission/utils.h>
 
25
 
 
26
#include "qticonloader.h"
 
27
#include "utils.h"
 
28
 
 
29
#define KILOBYTE_FACTOR 1024.0
 
30
#define MEGABYTE_FACTOR ( 1024.0 * 1024.0 )
 
31
#define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 )
 
32
 
 
33
QString
 
34
Utils :: sizeToString( double size )
 
35
{
 
36
    QString str;
 
37
 
 
38
    if( !size )
 
39
    {
 
40
        str = tr( "None" );
 
41
    }
 
42
    else if( size < 1024.0 )
 
43
    {
 
44
        const int i = (int)size;
 
45
        str = tr( "%Ln byte(s)", 0, i );
 
46
    }
 
47
    else
 
48
    {
 
49
        double displayed_size;
 
50
 
 
51
        if( size < (int64_t)MEGABYTE_FACTOR )
 
52
        {
 
53
            displayed_size = (double)size / KILOBYTE_FACTOR;
 
54
            str = tr( "%L1 KB" ).arg( displayed_size,  0, 'f', 1 );
 
55
        }
 
56
        else if( size < (int64_t)GIGABYTE_FACTOR )
 
57
        {
 
58
            displayed_size = (double)size / MEGABYTE_FACTOR;
 
59
            str = tr( "%L1 MB" ).arg( displayed_size,  0, 'f', 1 );
 
60
        }
 
61
        else
 
62
        {
 
63
            displayed_size = (double) size / GIGABYTE_FACTOR;
 
64
            str = tr( "%L1 GB" ).arg( displayed_size,  0, 'f', 1 );
 
65
        }
 
66
    }
 
67
 
 
68
    return str;
 
69
}
 
70
 
 
71
QString
 
72
Utils :: ratioToString( double ratio )
 
73
{
 
74
    QString buf;
 
75
 
 
76
    if( (int)ratio == TR_RATIO_NA )
 
77
        buf = tr( "None" );
 
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 );
 
84
    else
 
85
        buf.sprintf( "%'.0f", ratio );
 
86
 
 
87
    return buf;
 
88
 
 
89
}
 
90
 
 
91
QString
 
92
Utils :: timeToString( int seconds )
 
93
{
 
94
    int days, hours, minutes;
 
95
    QString d, h, m, s;
 
96
    QString str;
 
97
 
 
98
    if( seconds < 0 )
 
99
        seconds = 0;
 
100
 
 
101
    days = seconds / 86400;
 
102
    hours = ( seconds % 86400 ) / 3600;
 
103
    minutes = ( seconds % 3600 ) / 60;
 
104
    seconds %= 60;
 
105
 
 
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 );
 
110
 
 
111
    if( days )
 
112
    {
 
113
        if( days >= 4 || !hours )
 
114
            str = d;
 
115
        else
 
116
            str = tr( "%1, %2" ).arg( d ).arg( h );
 
117
    }
 
118
    else if( hours )
 
119
    {
 
120
        if( hours >= 4 || !minutes )
 
121
            str = h;
 
122
        else
 
123
            str = tr( "%1, %2" ).arg( h ).arg( m );
 
124
    }
 
125
    else if( minutes )
 
126
    {
 
127
        if( minutes >= 4 || !seconds )
 
128
            str = m;
 
129
        else
 
130
            str = tr( "%1, %2" ).arg( m ).arg( s );
 
131
    }
 
132
    else
 
133
    {
 
134
        str = s;
 
135
    }
 
136
 
 
137
    return str;
 
138
}
 
139
 
 
140
QString
 
141
Utils :: speedToString( const Speed& speed )
 
142
{
 
143
    const double kbps( speed.kbps( ) );
 
144
    QString str;
 
145
 
 
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 );
 
152
 
 
153
    return str;
 
154
}
 
155
 
 
156
void
 
157
Utils :: toStderr( const QString& str )
 
158
{
 
159
    std::cerr << str.toLatin1().constData() << std::endl;
 
160
}
 
161
 
 
162
const QIcon&
 
163
Utils :: guessMimeIcon( const QString& filename )
 
164
{
 
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];
 
169
 
 
170
    if( fileIcons[0].isNull( ) )
 
171
    {
 
172
        fallback = QApplication::style()->standardIcon( QStyle :: SP_FileIcon );
 
173
 
 
174
        fileIcons[DISK]= QtIconLoader :: icon( "media-optical", fallback );
 
175
        suffixes[DISK] << "iso";
 
176
 
 
177
        fileIcons[DOCUMENT] = QtIconLoader :: icon( "text-x-generic", fallback );
 
178
        suffixes[DOCUMENT] << "txt" << "doc" << "pdf" << "rtf" << "htm" << "html";
 
179
 
 
180
        fileIcons[PICTURE]  = QtIconLoader :: icon( "image-x-generic", fallback );
 
181
        suffixes[PICTURE] << "jpg" << "jpeg" << "png" << "gif" << "tiff" << "pcx";
 
182
 
 
183
        fileIcons[VIDEO] = QtIconLoader :: icon( "video-x-generic", fallback );
 
184
        suffixes[VIDEO] << "avi" << "mpeg" << "mp4" << "mkv" << "mov";
 
185
 
 
186
        fileIcons[ARCHIVE]  = QtIconLoader :: icon( "package-x-generic", fallback );
 
187
        suffixes[ARCHIVE] << "rar" << "zip" << "sft" << "tar" << "7z" << "cbz";
 
188
 
 
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";
 
192
 
 
193
        fileIcons[APP] = QtIconLoader :: icon( "application-x-executable", fallback );
 
194
        suffixes[APP] << "exe";
 
195
    }
 
196
 
 
197
    QString suffix( QFileInfo( filename ).suffix( ).toLower( ) );
 
198
 
 
199
    for( int i=0; i<TYPE_COUNT; ++i )
 
200
        if( suffixes[i].contains( suffix ) )
 
201
            return fileIcons[i];
 
202
 
 
203
    return fallback;
 
204
}