From 520d98028f37e550c0cfec34b9c10278c4a44aff Mon Sep 17 00:00:00 2001 From: Tim Jenssen Date: Wed, 21 Mar 2012 14:28:40 +0100 Subject: introduce general humanReadableSize method Change-Id: I4731be424cf2207e8cc2320ab9e442d02c29aeda Reviewed-by: Tim Jenssen --- src/libs/installer/binaryformat.cpp | 6 +++--- src/libs/installer/component.cpp | 26 ++++++-------------------- src/libs/installer/component.h | 1 - src/libs/installer/fileutils.cpp | 20 ++++++++++++++++++++ src/libs/installer/fileutils.h | 2 ++ src/libs/installer/packagemanagergui.cpp | 22 ---------------------- 6 files changed, 31 insertions(+), 46 deletions(-) (limited to 'src/libs/installer') 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, 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 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: -- cgit v1.2.3