source: trunk/qt/formatter.cc @ 10933

Last change on this file since 10933 was 10933, checked in by charles, 12 years ago

(trunk) rename the Qt client's "Units" class as "Formatter"

File size: 3.1 KB
Line 
1/*
2 * This file Copyright (C) 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$
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
25const int     Formatter :: speed_K     = 1000;
26const QString Formatter :: speed_B_str = "B/s";
27const QString Formatter :: speed_K_str = "kB/s";
28const QString Formatter :: speed_M_str = "MB/s";
29const QString Formatter :: speed_G_str = "GB/s";
30
31const int     Formatter :: size_K     = 1000;
32const QString Formatter :: size_B_str = "B";
33const QString Formatter :: size_K_str = "kB";
34const QString Formatter :: size_M_str = "MB";
35const QString Formatter :: size_G_str = "GB";
36
37const int     Formatter :: mem_K     = 1024;
38const QString Formatter :: mem_B_str = "B";
39const QString Formatter :: mem_K_str = "KiB";
40const QString Formatter :: mem_M_str = "MiB";
41const QString Formatter :: mem_G_str = "GiB";
42
43/***
44****
45***/
46
47QString
48Formatter :: memToString( double bytes )
49{
50    if( !bytes )
51        return tr( "None" );
52    else {
53        char buf[128];
54        tr_formatter_mem( buf, bytes, sizeof( buf ) );
55        return buf;
56    }
57}
58
59QString
60Formatter :: sizeToString( double bytes )
61{
62    if( !bytes )
63        return tr( "None" );
64    else {
65        char buf[128];
66        tr_formatter_size( buf, bytes, sizeof( buf ) );
67        return buf;
68    }
69}
70
71QString
72Formatter :: speedToString( const Speed& speed )
73{
74    if( speed.isZero( ) )
75        return tr( "None" );
76    else {
77        char buf[128];
78        tr_formatter_speed( buf, speed.Bps( ), sizeof( buf ) );
79        return buf;
80    }
81}
82
83QString
84Formatter :: percentToString( double x )
85{
86    char buf[128];
87    return QString( tr_strpercent( buf, x, sizeof(buf) ) );
88}
89
90QString
91Formatter :: ratioToString( double ratio )
92{
93    char buf[128];
94    return QString::fromUtf8( tr_strratio( buf, sizeof(buf), ratio, "\xE2\x88\x9E" ) );
95}
96
97QString
98Formatter :: timeToString( int seconds )
99{
100    int days, hours, minutes;
101    QString d, h, m, s;
102    QString str;
103
104    if( seconds < 0 )
105        seconds = 0;
106
107    days = seconds / 86400;
108    hours = ( seconds % 86400 ) / 3600;
109    minutes = ( seconds % 3600 ) / 60;
110    seconds %= 60;
111
112    d = tr( "%Ln day(s)", 0, days );
113    h = tr( "%Ln hour(s)", 0, hours );
114    m = tr( "%Ln minute(s)", 0, minutes );
115    s = tr( "%Ln second(s)", 0, seconds );
116
117    if( days )
118    {
119        if( days >= 4 || !hours )
120            str = d;
121        else
122            str = tr( "%1, %2" ).arg( d ).arg( h );
123    }
124    else if( hours )
125    {
126        if( hours >= 4 || !minutes )
127            str = h;
128        else
129            str = tr( "%1, %2" ).arg( h ).arg( m );
130    }
131    else if( minutes )
132    {
133        if( minutes >= 4 || !seconds )
134            str = m;
135        else
136            str = tr( "%1, %2" ).arg( m ).arg( s );
137    }
138    else
139    {
140        str = s;
141    }
142
143    return str;
144}
Note: See TracBrowser for help on using the repository browser.