diff --git a/kerfuffle/propertiesdialog.h b/kerfuffle/propertiesdialog.h --- a/kerfuffle/propertiesdialog.h +++ b/kerfuffle/propertiesdialog.h @@ -32,7 +32,8 @@ #include #include -#include + +class QLabel; namespace Kerfuffle { @@ -43,17 +44,11 @@ public: explicit PropertiesDialog(QWidget *parent, Archive *archive); -private slots: - void slotShowSha1(); - void slotShowSha256(); - private: - QString calcHash(QCryptographicHash::Algorithm algorithm); + QString calcHash(QCryptographicHash::Algorithm algorithm, const QString &path); + void showChecksum(QCryptographicHash::Algorithm algorithm, const QString &fileName, QLabel *label); class PropertiesDialogUI *m_ui; - QByteArray m_byteArray; - QFuture m_futureCalcSha1; - QFuture m_futureCalcSha256; }; } diff --git a/kerfuffle/propertiesdialog.cpp b/kerfuffle/propertiesdialog.cpp --- a/kerfuffle/propertiesdialog.cpp +++ b/kerfuffle/propertiesdialog.cpp @@ -99,53 +99,41 @@ QIcon icon = QIcon::fromTheme(archive->mimeType().iconName()); m_ui->lblIcon->setPixmap(icon.pixmap(IconSize(KIconLoader::Desktop), IconSize(KIconLoader::Desktop))); + m_ui->lblMD5->setText(i18n("Calculating...")); m_ui->lblSHA1->setText(i18n("Calculating...")); m_ui->lblSHA256->setText(i18n("Calculating...")); - QFile file(archive->fileName()); - if (file.open(QIODevice::ReadOnly)) { - m_byteArray = file.readAll(); - file.close(); - - QCryptographicHash hashMD5(QCryptographicHash::Md5); - hashMD5.addData(m_byteArray); - m_ui->lblMD5->setText(QLatin1String(hashMD5.result().toHex())); - - // The two SHA hashes take some time to calculate, so run them in another thread. - QFutureWatcher *watchCalcSha1 = new QFutureWatcher; - connect(watchCalcSha1, SIGNAL(finished()), this, SLOT(slotShowSha1())); - m_futureCalcSha1 = QtConcurrent::run(this, &PropertiesDialog::calcHash, QCryptographicHash::Sha1); - watchCalcSha1->setFuture(m_futureCalcSha1); - - QFutureWatcher *watchCalcSha256 = new QFutureWatcher; - connect(watchCalcSha256, SIGNAL(finished()), this, SLOT(slotShowSha256())); - m_futureCalcSha256 = QtConcurrent::run(this, &PropertiesDialog::calcHash, QCryptographicHash::Sha256); - watchCalcSha256->setFuture(m_futureCalcSha256); - } else { - m_ui->lblMD5->setText(QString()); - m_ui->lblSHA1->setText(QString()); - m_ui->lblSHA256->setText(QString()); - } + showChecksum(QCryptographicHash::Md5, archive->fileName(), m_ui->lblMD5); + showChecksum(QCryptographicHash::Sha1, archive->fileName(), m_ui->lblSHA1); + showChecksum(QCryptographicHash::Sha256, archive->fileName(), m_ui->lblSHA256); connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); } -QString PropertiesDialog::calcHash(QCryptographicHash::Algorithm algorithm) +QString PropertiesDialog::calcHash(QCryptographicHash::Algorithm algorithm, const QString &path) { - QCryptographicHash hash(algorithm); - hash.addData(m_byteArray); + QFile file(path); + if (!file.open(QIODevice::ReadOnly)) { + return QString(); + } - return QLatin1String(hash.result().toHex()); -} + QCryptographicHash hash(algorithm); + hash.addData(&file); -void PropertiesDialog::slotShowSha1() -{ - m_ui->lblSHA1->setText(m_futureCalcSha1.result()); + return QString::fromLatin1(hash.result().toHex()); } -void PropertiesDialog::slotShowSha256() +void PropertiesDialog::showChecksum(QCryptographicHash::Algorithm algorithm, const QString &fileName, QLabel *label) { - m_ui->lblSHA256->setText(m_futureCalcSha256.result()); + // Calculate checksum in another thread. + auto futureWatcher = new QFutureWatcher(this); + connect(futureWatcher, &QFutureWatcher::finished, this, [=]() { + label->setText(futureWatcher->result()); + futureWatcher->deleteLater(); + }); + + auto future = QtConcurrent::run(this, &PropertiesDialog::calcHash, algorithm, fileName); + futureWatcher->setFuture(future); } }