Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display terabyte again #6286

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions src/common/utility.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/common/utility.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/common/utility.cpp

File src/common/utility.cpp does not conform to Custom style guidelines. (lines 61)
* Copyright (C) by Klaas Freitag <[email protected]>
* Copyright (C) by Daniel Molkentin <[email protected]>
*
Expand Down Expand Up @@ -58,6 +58,14 @@
#include "utility_unix.cpp"
#endif

namespace {
constexpr auto bytes = 1024;
constexpr auto kilobytes = bytes;
constexpr auto megabytes = bytes * kilobytes;
constexpr qint64 gigabytes = bytes * megabytes;
constexpr qint64 terabytes = bytes * gigabytes;
}

namespace OCC {

Q_LOGGING_CATEGORY(lcUtility, "nextcloud.sync.utility", QtInfoMsg)
Expand Down Expand Up @@ -114,46 +122,40 @@
removeFavLink_private(folder);
}

QString Utility::octetsToString(qint64 octets)
{
#define THE_FACTOR 1024
static const qint64 kb = THE_FACTOR;
static const qint64 mb = THE_FACTOR * kb;
static const qint64 gb = THE_FACTOR * mb;

QString s;
qreal value = octets;

// Whether we care about decimals: only for GB/MB and only
// if it's less than 10 units.
bool round = true;

// do not display terra byte with the current units, as when
// the MB, GB and KB units were made, there was no TB,
// see the JEDEC standard
// https://en.wikipedia.org/wiki/JEDEC_memory_standards
if (octets >= gb) {
s = QCoreApplication::translate("Utility", "%L1 GB");
value /= gb;
round = false;
} else if (octets >= mb) {
s = QCoreApplication::translate("Utility", "%L1 MB");
value /= mb;
round = false;
} else if (octets >= kb) {
s = QCoreApplication::translate("Utility", "%L1 KB");
value /= kb;
} else {
s = QCoreApplication::translate("Utility", "%L1 B");
QString Utility::octetsToString(const qint64 octets)
{
auto unitName = QCoreApplication::translate("Utility", "%L1 B");
qreal dataSize = octets;

// Display decimals when value < TB and unit < 10
auto showDecimals = true;

if (octets >= terabytes) {
unitName = QCoreApplication::translate("Utility", "%L1 TB");
dataSize /= terabytes;
showDecimals = false;
} else if (octets >= gigabytes) {
unitName = QCoreApplication::translate("Utility", "%L1 GB");
dataSize /= gigabytes;
showDecimals = false;
} else if (octets >= megabytes) {
unitName = QCoreApplication::translate("Utility", "%L1 MB");
dataSize /= megabytes;
showDecimals = false;
} else if (octets >= kilobytes) {
unitName = QCoreApplication::translate("Utility", "%L1 KB");
dataSize /= kilobytes;
}

if (value > 9.95)
round = true;
if (dataSize > 9.95) {
showDecimals = true;
}

if (round)
return s.arg(qRound(value));
if (showDecimals) {
return unitName.arg(qRound(dataSize));
}

return s.arg(value, 0, 'g', 2);
return unitName.arg(dataSize, 0, 'g', 2);
}

// Qtified version of get_platforms() in csync_owncloud.c
Expand Down
2 changes: 1 addition & 1 deletion src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Utility {
OCSYNC_EXPORT void setupFavLink(const QString &folder);
OCSYNC_EXPORT void removeFavLink(const QString &folder);
OCSYNC_EXPORT bool writeRandomFile(const QString &fname, int size = -1);
OCSYNC_EXPORT QString octetsToString(qint64 octets);
OCSYNC_EXPORT QString octetsToString(const qint64 octets);
OCSYNC_EXPORT QByteArray userAgentString();
OCSYNC_EXPORT QByteArray friendlyUserAgentString();
/**
Expand Down
4 changes: 4 additions & 0 deletions test/testutility.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in test/testutility.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on test/testutility.cpp

File test/testutility.cpp does not conform to Custom style guidelines. (lines 40, 45, 59, 60)
This software is in the public domain, furnished "as is", without technical
support, and with no warranty, express or implied, as to its usefulness for
any purpose.
Expand Down Expand Up @@ -37,10 +37,12 @@
QLocale::setDefault(QLocale("en"));
QCOMPARE(octetsToString(999) , QString("999 B"));
QCOMPARE(octetsToString(1024) , QString("1 KB"));
QCOMPARE(octetsToString(1110) , QString("1 KB"));
QCOMPARE(octetsToString(1364) , QString("1 KB"));

QCOMPARE(octetsToString(9110) , QString("9 KB"));
QCOMPARE(octetsToString(9910) , QString("10 KB"));
QCOMPARE(octetsToString(9999) , QString("10 KB"));
QCOMPARE(octetsToString(10240) , QString("10 KB"));

QCOMPARE(octetsToString(123456) , QString("121 KB"));
Expand All @@ -54,6 +56,8 @@
QCOMPARE(octetsToString(1024), QString("1 KB"));
QCOMPARE(octetsToString(1024*1024), QString("1 MB"));
QCOMPARE(octetsToString(1024LL*1024*1024), QString("1 GB"));
QCOMPARE(octetsToString(1024LL*1024*1024*1024), QString("1 TB"));
QCOMPARE(octetsToString(1024LL*1024*1024*1024 * 5), QString("5 TB"));
}

void testLaunchOnStartup()
Expand Down
Loading