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
|
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
unsigned int speed_K;
|
|
|
|
unsigned int mem_K;
|
|
|
|
unsigned int size_K;
|
2010-07-07 00:30:42 +00:00
|
|
|
}
|
|
|
|
|
2010-07-24 02:43:06 +00:00
|
|
|
QString Formatter::unitStrings[3][5];
|
2010-07-07 00:30:42 +00:00
|
|
|
|
|
|
|
void
|
2013-02-09 18:59:05 +00:00
|
|
|
Formatter :: initUnits ()
|
2010-07-07 00:30:42 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
speed_K = 1000;
|
|
|
|
unitStrings[SPEED][B] = tr ( "B/s");
|
|
|
|
unitStrings[SPEED][KB] = tr ("kB/s");
|
|
|
|
unitStrings[SPEED][MB] = tr ("MB/s");
|
|
|
|
unitStrings[SPEED][GB] = tr ("GB/s");
|
|
|
|
unitStrings[SPEED][TB] = tr ("TB/s");
|
|
|
|
tr_formatter_speed_init (speed_K,
|
|
|
|
unitStrings[SPEED][KB].toUtf8 ().constData (),
|
|
|
|
unitStrings[SPEED][MB].toUtf8 ().constData (),
|
|
|
|
unitStrings[SPEED][GB].toUtf8 ().constData (),
|
|
|
|
unitStrings[SPEED][TB].toUtf8 ().constData ());
|
|
|
|
|
|
|
|
size_K = 1000;
|
|
|
|
unitStrings[SIZE][B] = tr ( "B");
|
|
|
|
unitStrings[SIZE][KB] = tr ("kB");
|
|
|
|
unitStrings[SIZE][MB] = tr ("MB");
|
|
|
|
unitStrings[SIZE][GB] = tr ("GB");
|
|
|
|
unitStrings[SIZE][TB] = tr ("TB");
|
|
|
|
tr_formatter_size_init (size_K,
|
|
|
|
unitStrings[SIZE][KB].toUtf8 ().constData (),
|
|
|
|
unitStrings[SIZE][MB].toUtf8 ().constData (),
|
|
|
|
unitStrings[SIZE][GB].toUtf8 ().constData (),
|
|
|
|
unitStrings[SIZE][TB].toUtf8 ().constData ());
|
|
|
|
|
|
|
|
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,
|
|
|
|
unitStrings[MEM][KB].toUtf8 ().constData (),
|
|
|
|
unitStrings[MEM][MB].toUtf8 ().constData (),
|
|
|
|
unitStrings[MEM][GB].toUtf8 ().constData (),
|
|
|
|
unitStrings[MEM][TB].toUtf8 ().constData ());
|
2010-07-07 00:30:42 +00:00
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-07-07 00:30:42 +00:00
|
|
|
double
|
2013-02-09 18:59:05 +00:00
|
|
|
Speed :: KBps () const
|
2010-07-07 00:30:42 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
return _Bps / (double)speed_K;
|
2010-07-07 00:30:42 +00:00
|
|
|
}
|
|
|
|
|
2010-07-04 06:07:21 +00:00
|
|
|
Speed
|
2013-02-09 18:59:05 +00:00
|
|
|
Speed :: fromKBps (double KBps)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
return int (KBps * speed_K);
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
QString
|
2013-02-09 18:59:05 +00:00
|
|
|
Formatter :: memToString (int64_t bytes)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
if (bytes < 0)
|
|
|
|
return tr ("Unknown");
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
return tr ("None");
|
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
tr_formatter_mem_B (buf, bytes, sizeof (buf));
|
|
|
|
return QString::fromUtf8 (buf);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2013-02-09 18:59:05 +00:00
|
|
|
Formatter :: sizeToString (int64_t bytes)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
if (bytes < 0)
|
|
|
|
return tr ("Unknown");
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
return tr ("None");
|
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
tr_formatter_size_B (buf, bytes, sizeof (buf));
|
|
|
|
return QString::fromUtf8 (buf);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2013-02-09 18:59:05 +00:00
|
|
|
Formatter :: speedToString (const Speed& speed)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-01-30 00:22:52 +00:00
|
|
|
char buf[128];
|
2013-02-09 18:59:05 +00:00
|
|
|
tr_formatter_speed_KBps (buf, speed.KBps (), sizeof (buf));
|
|
|
|
return QString::fromUtf8 (buf);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2013-02-09 18:59:05 +00:00
|
|
|
Formatter :: uploadSpeedToString (const Speed& uploadSpeed)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
static const QChar uploadSymbol (0x25B4);
|
|
|
|
|
|
|
|
return tr ("%1 %2").arg (speedToString (uploadSpeed)).arg (uploadSymbol);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2013-02-09 18:59:05 +00:00
|
|
|
Formatter :: downloadSpeedToString (const Speed& downloadSpeed)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
static const QChar downloadSymbol (0x25BE);
|
|
|
|
|
|
|
|
return tr ("%1 %2").arg (speedToString (downloadSpeed)).arg (downloadSymbol);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2013-02-09 18:59:05 +00:00
|
|
|
Formatter :: percentToString (double x)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
char buf[128];
|
|
|
|
return QString (tr_strpercent (buf, x, sizeof (buf)));
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2013-02-09 18:59:05 +00:00
|
|
|
QString
|
|
|
|
Formatter :: ratioToString (double ratio)
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
return QString::fromUtf8 (tr_strratio (buf, sizeof (buf), ratio, "\xE2\x88\x9E"));
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2013-02-09 18:59:05 +00:00
|
|
|
QString
|
|
|
|
Formatter :: timeToString (int seconds)
|
|
|
|
{
|
|
|
|
int days, hours, minutes;
|
|
|
|
QString d, h, m, s;
|
|
|
|
QString str;
|
|
|
|
|
|
|
|
if (seconds < 0)
|
|
|
|
seconds = 0;
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2013-02-09 18:59:05 +00:00
|
|
|
days = seconds / 86400;
|
|
|
|
hours = (seconds % 86400) / 3600;
|
|
|
|
minutes = (seconds % 3600) / 60;
|
|
|
|
seconds %= 60;
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2013-04-10 02:07:14 +00:00
|
|
|
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);
|
2013-02-09 18:59:05 +00:00
|
|
|
|
|
|
|
if (days)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
if (days >= 4 || !hours)
|
|
|
|
str = d;
|
|
|
|
else
|
|
|
|
str = tr ("%1, %2").arg (d).arg (h);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2013-02-09 18:59:05 +00:00
|
|
|
else if (hours)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
if (hours >= 4 || !minutes)
|
|
|
|
str = h;
|
|
|
|
else
|
|
|
|
str = tr ("%1, %2").arg (h).arg (m);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2013-02-09 18:59:05 +00:00
|
|
|
else if (minutes)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
if (minutes >= 4 || !seconds)
|
|
|
|
str = m;
|
|
|
|
else
|
|
|
|
str = tr ("%1, %2").arg (m).arg (s);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2013-02-09 18:59:05 +00:00
|
|
|
else
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2013-02-09 18:59:05 +00:00
|
|
|
str = s;
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 18:59:05 +00:00
|
|
|
return str;
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|