2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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
|
|
|
|
2023-11-14 23:23:51 +00:00
|
|
|
#include <libtransmission/values.h>
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Formatter.h"
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2023-11-13 17:13:17 +00:00
|
|
|
using namespace std::literals;
|
2023-11-14 23:23:51 +00:00
|
|
|
using namespace libtransmission::Values;
|
|
|
|
|
2023-11-16 04:15:40 +00:00
|
|
|
QString Formatter::memory_to_string(int64_t const bytes)
|
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
|
|
|
|
2023-11-14 23:23:51 +00:00
|
|
|
return QString::fromStdString(Memory{ bytes, Memory::Units::Bytes }.to_string());
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 04:15:40 +00:00
|
|
|
QString Formatter::storage_to_string(uint64_t const bytes)
|
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
|
|
|
|
2023-11-14 23:23:51 +00:00
|
|
|
return QString::fromStdString(Storage{ bytes, Storage::Units::Bytes }.to_string());
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 04:15:40 +00:00
|
|
|
QString Formatter::storage_to_string(int64_t const bytes)
|
2020-11-09 03:31:02 +00:00
|
|
|
{
|
|
|
|
if (bytes < 0)
|
|
|
|
{
|
|
|
|
return tr("Unknown");
|
|
|
|
}
|
|
|
|
|
2023-11-16 04:15:40 +00:00
|
|
|
return storage_to_string(static_cast<uint64_t>(bytes));
|
2020-11-09 03:31:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 04:15:40 +00:00
|
|
|
QString Formatter::time_to_string(int seconds)
|
2013-02-09 18:59:05 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
seconds = std::max(seconds, 0);
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2022-09-09 01:17:29 +00:00
|
|
|
if (seconds < 60)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2022-09-09 01:17:29 +00:00
|
|
|
return tr("%Ln second(s)", nullptr, seconds);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2022-09-09 01:17:29 +00:00
|
|
|
|
|
|
|
auto const minutes = seconds / 60;
|
|
|
|
|
|
|
|
if (minutes < 60)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2022-09-09 01:17:29 +00:00
|
|
|
return tr("%Ln minute(s)", nullptr, minutes);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
2022-09-09 01:17:29 +00:00
|
|
|
|
|
|
|
auto const hours = minutes / 60;
|
|
|
|
|
|
|
|
if (hours < 24)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2022-09-09 01:17:29 +00:00
|
|
|
return tr("%Ln hour(s)", nullptr, hours);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 01:17:29 +00:00
|
|
|
auto const days = hours / 24;
|
|
|
|
|
|
|
|
return tr("%Ln day(s)", nullptr, days);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|