2010-07-03 00:25:22 +00:00
|
|
|
/*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2010-07-03 00:25:22 +00:00
|
|
|
*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2010-07-03 00:25:22 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h> // tr_formatter
|
|
|
|
|
2010-07-03 01:10:36 +00:00
|
|
|
#include "formatter.h"
|
2010-07-03 00:25:22 +00:00
|
|
|
#include "speed.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** Constants
|
|
|
|
***/
|
|
|
|
|
2010-07-07 00:30:42 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
unsigned int speed_K;
|
|
|
|
unsigned int mem_K;
|
|
|
|
unsigned int size_K;
|
|
|
|
}
|
|
|
|
|
2010-07-24 02:43:06 +00:00
|
|
|
QString Formatter::unitStrings[3][5];
|
2010-07-07 00:30:42 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Formatter :: initUnits( )
|
|
|
|
{
|
2010-07-28 00:31:11 +00:00
|
|
|
speed_K = 1024;
|
|
|
|
unitStrings[SPEED][B] = tr( "B/s" );
|
|
|
|
unitStrings[SPEED][KB] = tr( "KiB/s" );
|
|
|
|
unitStrings[SPEED][MB] = tr( "MiB/s" );
|
|
|
|
unitStrings[SPEED][GB] = tr( "GiB/s" );
|
|
|
|
unitStrings[SPEED][TB] = tr( "TiB/s" );
|
2010-07-07 00:30:42 +00:00
|
|
|
tr_formatter_speed_init( speed_K,
|
|
|
|
qPrintable( unitStrings[SPEED][KB] ),
|
|
|
|
qPrintable( unitStrings[SPEED][MB] ),
|
|
|
|
qPrintable( unitStrings[SPEED][GB] ),
|
|
|
|
qPrintable( unitStrings[SPEED][TB] ) );
|
|
|
|
|
2010-07-28 00:31:11 +00:00
|
|
|
size_K = 1024;
|
|
|
|
unitStrings[SIZE][B] = tr( "B" );
|
|
|
|
unitStrings[SIZE][KB] = tr( "KiB" );
|
|
|
|
unitStrings[SIZE][MB] = tr( "MiB" );
|
|
|
|
unitStrings[SIZE][GB] = tr( "GiB" );
|
|
|
|
unitStrings[SIZE][TB] = tr( "TiB" );
|
2010-07-07 00:30:42 +00:00
|
|
|
tr_formatter_size_init( size_K,
|
|
|
|
qPrintable( unitStrings[SIZE][KB] ),
|
|
|
|
qPrintable( unitStrings[SIZE][MB] ),
|
|
|
|
qPrintable( unitStrings[SIZE][GB] ),
|
|
|
|
qPrintable( unitStrings[SIZE][TB] ) );
|
|
|
|
|
|
|
|
mem_K = 1024;
|
|
|
|
unitStrings[MEM][B] = tr( "B" );
|
|
|
|
unitStrings[MEM][KB] = tr( "KiB" );
|
|
|
|
unitStrings[MEM][MB] = tr( "MiB" );
|
|
|
|
unitStrings[MEM][GB] = tr( "GiB" );
|
|
|
|
unitStrings[MEM][TB] = tr( "TiB" );
|
|
|
|
tr_formatter_mem_init( mem_K,
|
|
|
|
qPrintable( unitStrings[MEM][KB] ),
|
|
|
|
qPrintable( unitStrings[MEM][MB] ),
|
|
|
|
qPrintable( unitStrings[MEM][GB] ),
|
|
|
|
qPrintable( unitStrings[MEM][TB] ) );
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-07-07 00:30:42 +00:00
|
|
|
double
|
|
|
|
Speed :: KBps( ) const
|
|
|
|
{
|
2010-07-31 05:54:46 +00:00
|
|
|
return _Bps / (double)speed_K;
|
2010-07-07 00:30:42 +00:00
|
|
|
}
|
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
Speed
|
|
|
|
Speed :: fromKBps( double KBps )
|
|
|
|
{
|
2010-07-25 20:36:33 +00:00
|
|
|
return int( KBps * speed_K );
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
QString
|
2010-07-25 20:36:33 +00:00
|
|
|
Formatter :: memToString( uint64_t bytes )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
|
|
|
if( !bytes )
|
|
|
|
return tr( "None" );
|
|
|
|
else {
|
|
|
|
char buf[128];
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_formatter_mem_B( buf, bytes, sizeof( buf ) );
|
2010-07-03 00:25:22 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2010-07-25 20:36:33 +00:00
|
|
|
Formatter :: sizeToString( uint64_t bytes )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
|
|
|
if( !bytes )
|
|
|
|
return tr( "None" );
|
|
|
|
else {
|
|
|
|
char buf[128];
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_formatter_size_B( buf, bytes, sizeof( buf ) );
|
2010-07-03 00:25:22 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2010-07-03 01:10:36 +00:00
|
|
|
Formatter :: speedToString( const Speed& speed )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
|
|
|
if( speed.isZero( ) )
|
|
|
|
return tr( "None" );
|
|
|
|
else {
|
|
|
|
char buf[128];
|
2010-07-09 20:53:38 +00:00
|
|
|
tr_formatter_speed_KBps( buf, speed.KBps( ), sizeof( buf ) );
|
2010-07-03 00:25:22 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2010-07-03 01:10:36 +00:00
|
|
|
Formatter :: percentToString( double x )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
return QString( tr_strpercent( buf, x, sizeof(buf) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2010-07-03 01:10:36 +00:00
|
|
|
Formatter :: ratioToString( double ratio )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
return QString::fromUtf8( tr_strratio( buf, sizeof(buf), ratio, "\xE2\x88\x9E" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2010-07-03 01:10:36 +00:00
|
|
|
Formatter :: timeToString( int seconds )
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
|
|
|
int days, hours, minutes;
|
|
|
|
QString d, h, m, s;
|
|
|
|
QString str;
|
|
|
|
|
|
|
|
if( seconds < 0 )
|
|
|
|
seconds = 0;
|
|
|
|
|
|
|
|
days = seconds / 86400;
|
|
|
|
hours = ( seconds % 86400 ) / 3600;
|
|
|
|
minutes = ( seconds % 3600 ) / 60;
|
|
|
|
seconds %= 60;
|
|
|
|
|
|
|
|
d = tr( "%Ln day(s)", 0, days );
|
|
|
|
h = tr( "%Ln hour(s)", 0, hours );
|
|
|
|
m = tr( "%Ln minute(s)", 0, minutes );
|
|
|
|
s = tr( "%Ln second(s)", 0, seconds );
|
|
|
|
|
|
|
|
if( days )
|
|
|
|
{
|
|
|
|
if( days >= 4 || !hours )
|
|
|
|
str = d;
|
|
|
|
else
|
|
|
|
str = tr( "%1, %2" ).arg( d ).arg( h );
|
|
|
|
}
|
|
|
|
else if( hours )
|
|
|
|
{
|
|
|
|
if( hours >= 4 || !minutes )
|
|
|
|
str = h;
|
|
|
|
else
|
|
|
|
str = tr( "%1, %2" ).arg( h ).arg( m );
|
|
|
|
}
|
|
|
|
else if( minutes )
|
|
|
|
{
|
|
|
|
if( minutes >= 4 || !seconds )
|
|
|
|
str = m;
|
|
|
|
else
|
|
|
|
str = tr( "%1, %2" ).arg( m ).arg( s );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|