2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2012-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-07-03 00:25:22 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h> // tr_formatter
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Formatter.h"
|
|
|
|
#include "Speed.h"
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
Formatter& Formatter::get()
|
2010-07-07 00:30:42 +00:00
|
|
|
{
|
2020-09-09 14:24:39 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
2020-08-15 15:42:51 +00:00
|
|
|
static auto& singleton = *new Formatter();
|
|
|
|
return singleton;
|
2010-07-07 00:30:42 +00:00
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
Formatter::Formatter()
|
|
|
|
: UnitStrings{ {
|
|
|
|
{ tr("B/s"), tr("kB/s"), tr("MB/s"), tr("GB/s"), tr("TB/s") }, // SPEED
|
|
|
|
{ tr("B"), tr("kB"), tr("MB"), tr("GB"), tr("TB") }, // SIZE
|
|
|
|
{ tr("B"), tr("KiB"), tr("MiB"), tr("GiB"), tr("TiB") } // MEM
|
|
|
|
} }
|
2010-07-07 00:30:42 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
auto const& speed = UnitStrings[SPEED];
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_formatter_speed_init(
|
|
|
|
SpeedBase,
|
2020-08-15 15:42:51 +00:00
|
|
|
speed[KB].toUtf8().constData(),
|
|
|
|
speed[MB].toUtf8().constData(),
|
|
|
|
speed[GB].toUtf8().constData(),
|
|
|
|
speed[TB].toUtf8().constData());
|
|
|
|
|
|
|
|
auto const& size = UnitStrings[SIZE];
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_formatter_size_init(
|
|
|
|
SizeBase,
|
2020-08-15 15:42:51 +00:00
|
|
|
size[KB].toUtf8().constData(),
|
|
|
|
size[MB].toUtf8().constData(),
|
|
|
|
size[GB].toUtf8().constData(),
|
|
|
|
size[TB].toUtf8().constData());
|
|
|
|
|
|
|
|
auto const& mem = UnitStrings[MEM];
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_formatter_mem_init(
|
|
|
|
MemBase,
|
2020-08-15 15:42:51 +00:00
|
|
|
mem[KB].toUtf8().constData(),
|
|
|
|
mem[MB].toUtf8().constData(),
|
|
|
|
mem[GB].toUtf8().constData(),
|
|
|
|
mem[TB].toUtf8().constData());
|
2010-07-07 00:30:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::unitStr(Type t, Size s) const
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
return UnitStrings[t][s];
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::memToString(int64_t bytes) const
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes < 0)
|
|
|
|
{
|
|
|
|
return tr("Unknown");
|
|
|
|
}
|
2013-02-09 18:59:05 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (bytes == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return tr("None");
|
|
|
|
}
|
2013-02-09 18:59:05 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
return QString::fromStdString(tr_formatter_mem_B(bytes));
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 03:31:02 +00:00
|
|
|
QString Formatter::sizeToString(uint64_t bytes) const
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (bytes == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return tr("None");
|
|
|
|
}
|
2013-02-09 18:59:05 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
return QString::fromStdString(tr_formatter_size_B(bytes));
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 03:31:02 +00:00
|
|
|
QString Formatter::sizeToString(int64_t bytes) const
|
|
|
|
{
|
|
|
|
if (bytes < 0)
|
|
|
|
{
|
|
|
|
return tr("Unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Formatter::sizeToString(static_cast<uint64_t>(bytes));
|
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::speedToString(Speed const& speed) const
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return QString::fromStdString(tr_formatter_speed_KBps(speed.getKBps()));
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::uploadSpeedToString(Speed const& upload_speed) const
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
static QChar constexpr UploadSymbol(0x25B4);
|
2013-02-09 18:59:05 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
return tr("%1 %2").arg(speedToString(upload_speed)).arg(UploadSymbol);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::downloadSpeedToString(Speed const& download_speed) const
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
static QChar constexpr DownloadSymbol(0x25BE);
|
2013-02-09 18:59:05 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
return tr("%1 %2").arg(speedToString(download_speed)).arg(DownloadSymbol);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::percentToString(double x) const
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return QString::fromStdString(tr_strpercent(x));
|
2013-02-09 18:59:05 +00:00
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::ratioToString(double ratio) const
|
2013-02-09 18:59:05 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return QString::fromStdString(tr_strratio(ratio, "\xE2\x88\x9E"));
|
2013-02-09 18:59:05 +00:00
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
QString Formatter::timeToString(int seconds) const
|
2013-02-09 18:59:05 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
seconds = std::max(seconds, 0);
|
|
|
|
auto const days = seconds / 86400;
|
|
|
|
auto const hours = (seconds % 86400) / 3600;
|
|
|
|
auto const minutes = (seconds % 3600) / 60;
|
2017-04-19 12:04:45 +00:00
|
|
|
seconds %= 60;
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
auto const d = tr("%Ln day(s)", nullptr, days);
|
|
|
|
auto const h = tr("%Ln hour(s)", nullptr, hours);
|
|
|
|
auto const m = tr("%Ln minute(s)", nullptr, minutes);
|
|
|
|
auto const s = tr("%Ln second(s)", nullptr, seconds);
|
|
|
|
|
|
|
|
QString str;
|
2013-02-09 18:59:05 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (days != 0)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (days >= 4 || hours == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
str = d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = tr("%1, %2").arg(d).arg(h);
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (hours != 0)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (hours >= 4 || minutes == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
str = h;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = tr("%1, %2").arg(h).arg(m);
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (minutes != 0)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (minutes >= 4 || seconds == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
str = m;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = tr("%1, %2").arg(m).arg(s);
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
str = s;
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return str;
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2020-08-15 15:42:51 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
double Speed::getKBps() const
|
|
|
|
{
|
|
|
|
return getBps() / static_cast<double>(Formatter::SpeedBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
Speed Speed::fromKBps(double KBps)
|
|
|
|
{
|
|
|
|
return Speed{ static_cast<int>(KBps * Formatter::SpeedBase) };
|
|
|
|
}
|