summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Jenssen <tim.jenssen@nokia.com>2012-03-21 14:28:40 +0100
committerTim Jenssen <tim.jenssen@nokia.com>2012-03-27 15:04:31 +0200
commit520d98028f37e550c0cfec34b9c10278c4a44aff (patch)
tree3a64434d02f3b1fa5009de88a7832ad8c56f184f
parent564ef626886496ea5e6685f0fc4f96c283245123 (diff)
introduce general humanReadableSize method
Change-Id: I4731be424cf2207e8cc2320ab9e442d02c29aeda Reviewed-by: Tim Jenssen <tim.jenssen@nokia.com>
-rw-r--r--src/libs/installer/binaryformat.cpp6
-rw-r--r--src/libs/installer/component.cpp26
-rw-r--r--src/libs/installer/component.h1
-rw-r--r--src/libs/installer/fileutils.cpp20
-rw-r--r--src/libs/installer/fileutils.h2
-rw-r--r--src/libs/installer/packagemanagergui.cpp22
-rw-r--r--src/libs/kdtools/kdupdaterfiledownloader.cpp26
-rw-r--r--tools/binarycreator/binarycreator.cpp4
8 files changed, 41 insertions, 66 deletions
diff --git a/src/libs/installer/binaryformat.cpp b/src/libs/installer/binaryformat.cpp
index 133df3690..eb9784315 100644
--- a/src/libs/installer/binaryformat.cpp
+++ b/src/libs/installer/binaryformat.cpp
@@ -228,7 +228,7 @@ qint64 QInstaller::findMagicCookie(QFile *in, quint64 magicCookie)
}
searched += 1;
}
- throw Error(QObject::tr("No marker found, stopped after %1 bytes.").arg(QString::number(MAX_SEARCH)));
+ throw Error(QObject::tr("No marker found, stopped after %1.").arg(humanReadableSize(MAX_SEARCH)));
} catch (const Error& err) {
in->seek(oldPos);
throw err;
@@ -1028,9 +1028,9 @@ void BinaryContent::readBinaryData(BinaryContent &content, const QSharedPointer<
qDebug() << component.name().data() << "loaded...";
QStringList archivesWithSize;
foreach (const QSharedPointer<Archive> &archive, archives) {
- QString archiveWithSize(QLatin1String("%1 - %2 Bytes"));
+ QString archiveWithSize(QLatin1String("%1 - %2"));
archiveWithSize = archiveWithSize.arg(QString::fromLocal8Bit(archive->name()),
- QString::number(archive->size()));
+ humanReadableSize(archive->size()));
archivesWithSize.append(archiveWithSize);
}
if (!archivesWithSize.isEmpty()) {
diff --git a/src/libs/installer/component.cpp b/src/libs/installer/component.cpp
index 4f8345836..8f1a2337e 100644
--- a/src/libs/installer/component.cpp
+++ b/src/libs/installer/component.cpp
@@ -193,33 +193,17 @@ quint64 Component::updateUncompressedSize()
quint64 size = 0;
if (isSelected())
- size = (quint64)value(scUncompressedSize).toDouble();
+ size = value(scUncompressedSize).toLongLong();
foreach (Component* comp, d->m_allChildComponents)
size += comp->updateUncompressedSize();
setValue(scUncompressedSizeSum, QString::number(size));
- setData(uncompressedSize(), UncompressedSize);
+ setData(humanReadableSize(size), UncompressedSize);
return size;
}
-QString Component::uncompressedSize() const
-{
- double size = value(scUncompressedSizeSum).toDouble();
- if (size < 1000.0)
- return tr("%L1 Bytes").arg(size);
- size /= 1024.0;
- if (size < 1000.0)
- return tr("%L1 kBytes").arg(size, 0, 'f', 2);
- size /= 1024.0;
- if (size < 1000.0)
- return tr("%L1 MBytes").arg(size, 0, 'f', 2);
- size /= 1024.0;
-
- return tr("%L1 GBytes").arg(size, 0, 'f', 2);
-}
-
void Component::markAsPerformedInstallation()
{
d->m_newlyInstalled = true;
@@ -1181,8 +1165,10 @@ void Component::updateModelData(const QString &key, const QString &data)
if (key == scDisplayVersion)
setData(data, LocalDisplayVersion);
- if (key == scUncompressedSize)
- setData(uncompressedSize(), UncompressedSize);
+ if (key == scUncompressedSize) {
+ quint64 size = value(scUncompressedSizeSum).toLongLong();
+ setData(humanReadableSize(size), UncompressedSize);
+ }
const QString &updateInfo = value(scUpdateText);
if (!d->m_core->isUpdater() || updateInfo.isEmpty()) {
diff --git a/src/libs/installer/component.h b/src/libs/installer/component.h
index 14d16722c..44f9e795b 100644
--- a/src/libs/installer/component.h
+++ b/src/libs/installer/component.h
@@ -152,7 +152,6 @@ public:
QString name() const;
QString displayName() const;
- QString uncompressedSize() const;
quint64 updateUncompressedSize();
QUrl repositoryUrl() const;
diff --git a/src/libs/installer/fileutils.cpp b/src/libs/installer/fileutils.cpp
index b07b5b08f..ece86aa5d 100644
--- a/src/libs/installer/fileutils.cpp
+++ b/src/libs/installer/fileutils.cpp
@@ -120,6 +120,26 @@ void TempDirDeleter::releaseAndDelete(const QString &path)
}
}
+QString QInstaller::humanReadableSize(const qint64 &size, int precision)
+{
+ double sizeAsDouble = size;
+ static QStringList measures;
+ if (measures.isEmpty())
+ measures << QString::fromLatin1("bytes") << QString::fromLatin1("KiB") << QString::fromLatin1("MiB")
+ << QString::fromLatin1("GiB") << QString::fromLatin1("TiB") << QString::fromLatin1("PiB")
+ << QString::fromLatin1("EiB") << QString::fromLatin1("ZiB") << QString::fromLatin1("YiB");
+
+ QStringListIterator it(measures);
+ QString measure(it.next());
+
+ while (sizeAsDouble >= 1024.0 && it.hasNext()) {
+ measure = it.next();
+ sizeAsDouble /= 1024.0;
+ }
+ return QString::fromLatin1("%1 %2").arg(sizeAsDouble, 0, 'f', precision).arg(measure);
+}
+
+
// -- read, write operations
diff --git a/src/libs/installer/fileutils.h b/src/libs/installer/fileutils.h
index 4e634a210..22b7dc584 100644
--- a/src/libs/installer/fileutils.h
+++ b/src/libs/installer/fileutils.h
@@ -64,6 +64,8 @@ private:
QSet<QString> m_paths;
};
+ QString INSTALLER_EXPORT humanReadableSize(const qint64 &size, int precision = 2);
+
void INSTALLER_EXPORT openForRead(QIODevice *dev, const QString &name);
void INSTALLER_EXPORT openForWrite(QIODevice *dev, const QString &name);
void INSTALLER_EXPORT openForAppend(QIODevice *dev, const QString &name);
diff --git a/src/libs/installer/packagemanagergui.cpp b/src/libs/installer/packagemanagergui.cpp
index 0b96e2d94..6e0909517 100644
--- a/src/libs/installer/packagemanagergui.cpp
+++ b/src/libs/installer/packagemanagergui.cpp
@@ -115,28 +115,6 @@ TRANSLATOR QInstaller::PerformInstallationPage
TRANSLATOR QInstaller::FinishedPage
*/
-
-static QString humanReadableSize(quint64 intSize)
-{
- QString unit;
- double size;
-
- if (intSize < 1024 * 1024) {
- size = 1. + intSize / 1024.;
- unit = QObject::tr("kB");
- } else if (intSize < 1024 * 1024 * 1024) {
- size = 1. + intSize / 1024. / 1024.;
- unit = QObject::tr("MB");
- } else {
- size = 1. + intSize / 1024. / 1024. / 1024.;
- unit = QObject::tr("GB");
- }
-
- size = qRound(size * 10) / 10.0;
- return QString::fromLatin1("%L1 %2").arg(size, 0, 'g', 4).arg(unit);
-}
-
-
class DynamicInstallerPage : public PackageManagerPage
{
public:
diff --git a/src/libs/kdtools/kdupdaterfiledownloader.cpp b/src/libs/kdtools/kdupdaterfiledownloader.cpp
index a938c9cb5..6494d72b2 100644
--- a/src/libs/kdtools/kdupdaterfiledownloader.cpp
+++ b/src/libs/kdtools/kdupdaterfiledownloader.cpp
@@ -23,6 +23,8 @@
#include "kdupdaterfiledownloader_p.h"
#include "kdupdaterfiledownloaderfactory.h"
+#include <fileutils.h>
+
#include <QFile>
#include <QtNetwork/QFtp>
#include <QtNetwork/QNetworkAccessManager>
@@ -39,26 +41,13 @@
#include <QTimerEvent>
using namespace KDUpdater;
+using namespace QInstaller;
static double calcProgress(qint32 done, qint32 total)
{
return total ? (double(done) / double(total)) : 0;
}
-static QString format(double data)
-{
- if (data < 1024.0)
- return KDUpdater::FileDownloader::tr("%L1 B").arg(data);
- data /= 1024.0;
- if (data < 1024.0)
- return KDUpdater::FileDownloader::tr("%L1 KB").arg(data, 0, 'f', 2);
- data /= 1024.0;
- if (data < 1024.0)
- return KDUpdater::FileDownloader::tr("%L1 MB").arg(data, 0, 'f', 2);
- data /= 1024.0;
- return KDUpdater::FileDownloader::tr("%L1 GB").arg(data, 0, 'f', 2);
-}
-
QByteArray KDUpdater::calculateHash(QIODevice* device, QCryptographicHash::Algorithm algo)
{
Q_ASSERT(device);
@@ -417,9 +406,10 @@ void KDUpdater::FileDownloader::emitDownloadStatus()
{
QString status;
if (d->m_bytesToReceive > 0) {
- QString bytesReceived = format(d->m_bytesReceived);
- const QString bytesToReceive = format(d->m_bytesToReceive);
+ QString bytesReceived = humanReadableSize(d->m_bytesReceived);
+ const QString bytesToReceive = humanReadableSize(d->m_bytesToReceive);
+ // remove the unit from the bytesReceived value if bytesToReceive has the same
const QString tmp = bytesToReceive.mid(bytesToReceive.indexOf(QLatin1Char(' ')));
if (bytesReceived.endsWith(tmp))
bytesReceived.chop(tmp.length());
@@ -427,10 +417,10 @@ void KDUpdater::FileDownloader::emitDownloadStatus()
status = bytesReceived + tr(" of ") + bytesToReceive;
} else {
if (d->m_bytesReceived > 0)
- status = format(d->m_bytesReceived) + tr(" downloaded.");
+ status = humanReadableSize(d->m_bytesReceived) + tr(" downloaded.");
}
- status += QLatin1String(" (") + format(d->m_downloadSpeed) + tr("/sec") + QLatin1Char(')');
+ status += QLatin1String(" (") + humanReadableSize(d->m_downloadSpeed) + tr("/sec") + QLatin1Char(')');
if (d->m_bytesToReceive > 0 && d->m_downloadSpeed > 0) {
const qint64 time = (d->m_bytesToReceive - d->m_bytesReceived) / d->m_downloadSpeed;
diff --git a/tools/binarycreator/binarycreator.cpp b/tools/binarycreator/binarycreator.cpp
index c749fb276..33d1eb1b3 100644
--- a/tools/binarycreator/binarycreator.cpp
+++ b/tools/binarycreator/binarycreator.cpp
@@ -682,8 +682,8 @@ int main(int argc, char **argv)
qDebug() << "Creating component info for" << info.name;
foreach (const QString &archive, info.copiedArchives) {
const QSharedPointer<Archive> arch(new Archive(archive));
- qDebug() << QString::fromLatin1("\tAppending %1 (%2 bytes)").arg(archive,
- QString::number(arch->size()));
+ qDebug() << QString::fromLatin1("\tAppending %1 (%2)").arg(archive,
+ humanReadableSize(arch->size()));
comp.appendArchive(arch);
}
input.components.insertComponent(comp);