Commit 7e94c57d authored by Lisa's avatar Lisa
Browse files

Added helper function which replaces KIO::convertSize

parent ea03f678
......@@ -65,6 +65,46 @@ DatabasePrivate* QueryHelper::databasePrivateFromDatabase(Database* db)
namespace Helpers
{
QString formatByteSize(double size)
{
// Per IEC 60027-2
// Binary prefixes
//Tebi-byte TiB 2^40 1,099,511,627,776 bytes
//Gibi-byte GiB 2^30 1,073,741,824 bytes
//Mebi-byte MiB 2^20 1,048,576 bytes
//Kibi-byte KiB 2^10 1,024 bytes
QString s;
// Gibi-byte
if ( size >= 1073741824.0 ) {
size /= 1073741824.0;
if ( size > 1024 ) // Tebi-byte
s = QObject::tr("%1 TiB").arg(QLocale().toString(size / 1024.0, 'f', 1));
else
s = QObject::tr("%1 GiB").arg(QLocale().toString(size, 'f', 1));
}
// Mebi-byte
else if ( size >= 1048576.0 ) {
size /= 1048576.0;
s = QObject::tr("%1 MiB").arg(QLocale().toString(size, 'f', 1));
}
// Kibi-byte
else if ( size >= 1024.0 ) {
size /= 1024.0;
s = QObject::tr("%1 KiB").arg(QLocale().toString(size, 'f', 1));
}
// Just byte
else if ( size > 0 ) {
s = QObject::tr("%1 B").arg(QLocale().toString(size, 'f', 1));
}
// Nothing
else {
s = QObject::tr("0 B");
}
return s;
}
/*
* NOTE: a rule is applied which doesn't seem obvious:
* if we have 2.3-1 and 2.3, 0 is returned (so they're equal); this aims to maintain
......
......@@ -54,6 +54,13 @@ bool removeFile(const QString &file);
Permissions permissionsOfPath(const QString &path);
bool checkAuthorizationSync(const QString &action);
/*
* Converts a size in floating point to a string representation.
* It is designed to replace the similar function in KIO, since we avoid KDE dependencies in akabei.
* It was taken from Solid.
*/
QString formatByteSize(double size);
namespace QueryPerformer
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment