From 4b1b897a140589426c9325f040feee741798c167 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Mon, 1 Jun 2020 17:12:20 +0200 Subject: Use a single extractFiles() method and avoir member variables when not needed. --- src/installationmanager.cpp | 171 ++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 108 deletions(-) (limited to 'src/installationmanager.cpp') diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp index 201868de..00f06af1 100644 --- a/src/installationmanager.cpp +++ b/src/installationmanager.cpp @@ -131,43 +131,77 @@ void InstallationManager::queryPassword(QString *password) tr("Password"), QLineEdit::Password); } - -bool InstallationManager::extractFiles(QDir extractPath) +bool InstallationManager::extractFiles(QString extractPath, QString title, bool showFilenames) { - m_InstallationProgress = new QProgressDialog(m_ParentWidget); - ON_BLOCK_EXIT([this]() { - m_InstallationProgress->cancel(); - m_InstallationProgress->hide(); - m_InstallationProgress->deleteLater(); - m_InstallationProgress = nullptr; - m_Progress = 0; - }); - m_InstallationProgress->setWindowFlags( - m_InstallationProgress->windowFlags() & (~Qt::WindowContextHelpButtonHint)); - m_InstallationProgress->setWindowTitle(tr("Extracting files")); - m_InstallationProgress->setWindowModality(Qt::WindowModal); - m_InstallationProgress->setFixedSize(600, 100); - m_InstallationProgress->setAutoReset(false); - m_InstallationProgress->show(); + QProgressDialog *installationProgress = new QProgressDialog(m_ParentWidget); + ON_BLOCK_EXIT([=]() { + installationProgress->cancel(); + installationProgress->hide(); + installationProgress->deleteLater(); + }); + installationProgress->setWindowFlags( + installationProgress->windowFlags() & (~Qt::WindowContextHelpButtonHint)); + if (!title.isEmpty()) { + installationProgress->setWindowTitle(title); + } + installationProgress->setWindowModality(Qt::WindowModal); + installationProgress->setFixedSize(600, 100); + + // Turn off auto-reset otherwize the progress dialog is reset before the end. This + // is kind of annoying because updateProgress consider percentage of progression + // through the archive (pack), while we are waiting for extracting archive entries, so + // the percentage of in updateProgress is not really related to the percentage of files + // extracted... + installationProgress->setAutoReset(false); + + installationProgress->show(); + + auto start = std::chrono::system_clock::now(); + + // Variable updated by the callbacks: + int progress = 0; + QString progressFile; + QString errorMessage; + + // The callbacks: + ProgressCallback progressCallback = [&progress, installationProgress, this](float p) { + progress = static_cast(p * 100.0); + + if (installationProgress->wasCanceled()) { + m_ArchiveHandler->cancel(); + installationProgress->reset(); + } + }; + FileChangeCallback fileChangeCallback = [&progressFile](std::wstring const& file) { + progressFile = QString::fromStdWString(file); + }; + ErrorCallback errorCallback = [&errorMessage, this](std::wstring const& message) { + errorMessage = QString::fromStdWString(message); + }; // unpack only the files we need for the installer QFuture future = QtConcurrent::run([&]() -> bool { return m_ArchiveHandler->extract( - QDir::tempPath().toStdWString(), - [this](float f) { updateProgress(f); }, - nullptr, - [this](std::wstring const& error) { report7ZipError(QString::fromStdWString(error)); } + extractPath.toStdWString(), + progressCallback, + showFilenames ? fileChangeCallback : nullptr, + errorCallback ); - }); + }); do { - if (m_Progress != m_InstallationProgress->value()) - m_InstallationProgress->setValue(m_Progress); + if (progress != installationProgress->value()) { + installationProgress->setValue(progress); + } + if (showFilenames && progressFile != installationProgress->labelText()) { + installationProgress->setLabelText(progressFile); + } QCoreApplication::processEvents(); } while (!future.isFinished()); + log::debug("Extraction took {:.3f}s.", std::chrono::duration(std::chrono::system_clock::now() - start).count()); if (!future.result()) { if (m_ArchiveHandler->getLastError() == Archive::Error::ERROR_EXTRACT_CANCELLED) { - if (!m_ErrorMessage.isEmpty()) { - throw MyException(tr("Extraction failed: %1").arg(m_ErrorMessage)); + if (!errorMessage.isEmpty()) { + throw MyException(tr("Extraction failed: %1").arg(errorMessage)); } else { return false; @@ -181,14 +215,12 @@ bool InstallationManager::extractFiles(QDir extractPath) return true; } - QString InstallationManager::extractFile(std::shared_ptr entry) { QStringList result = this->extractFiles({ entry }); return result.isEmpty() ? QString() : result[0]; } - QStringList InstallationManager::extractFiles(std::vector> const& entries) { // Remove the directory since mapToArchive would add them: @@ -208,7 +240,7 @@ QStringList InstallationManager::extractFiles(std::vector(percentage * 100.0); - - if (m_InstallationProgress->wasCanceled()) { - m_ArchiveHandler->cancel(); - m_InstallationProgress->reset(); - } - } -} - - -void InstallationManager::updateProgressFile(QString const &fileName) -{ - m_ProgressFile = fileName; -} - - -void InstallationManager::report7ZipError(QString const &errorMessage) -{ - m_ErrorMessage = errorMessage; - m_ArchiveHandler->cancel(); -} - QString InstallationManager::generateBackupName(const QString &directoryName) const { @@ -430,60 +437,8 @@ IPluginInstaller::EInstallResult InstallationManager::doInstall(GuessedValuecancel(); - m_InstallationProgress->hide(); - m_InstallationProgress->deleteLater(); - m_InstallationProgress = nullptr; - m_Progress = 0; - m_ProgressFile = QString(); - }); - - // Turn off auto-reset otherwize the progress dialog is reset before the end. This - // is kind of annoying because updateProgress consider percentage of progression - // through the archive (pack), while we are waiting for extracting archive entries, so - // the percentage of in updateProgress is not really related to the percentage of files - // extracted... - m_InstallationProgress->setAutoReset(false); - - m_InstallationProgress->setWindowFlags( - m_InstallationProgress->windowFlags() & (~Qt::WindowContextHelpButtonHint)); - m_InstallationProgress->setWindowModality(Qt::WindowModal); - m_InstallationProgress->setFixedSize(600, 100); - m_InstallationProgress->show(); - - auto start = std::chrono::system_clock::now(); - - QFuture future = QtConcurrent::run([&]() -> bool { - return m_ArchiveHandler->extract( - targetDirectory.toStdWString(), - [this](float progress) { updateProgress(progress); }, - [this](std::wstring const& filename) { updateProgressFile(QString::fromStdWString(filename)); }, - [this](std::wstring const& error) { report7ZipError(QString::fromStdWString(error)); } - ); - }); - do { - if (m_Progress != m_InstallationProgress->value()) - m_InstallationProgress->setValue(m_Progress); - if (m_ProgressFile != m_InstallationProgress->labelText()) - m_InstallationProgress->setLabelText(m_ProgressFile); - QCoreApplication::processEvents(); - } while (!future.isFinished()); - log::debug("Extraction took {:.3f}s.", std::chrono::duration(std::chrono::system_clock::now() - start).count()); - - if (!future.result()) { - if (m_ArchiveHandler->getLastError() == Archive::Error::ERROR_EXTRACT_CANCELLED) { - if (!m_ErrorMessage.isEmpty()) { - throw MyException(tr("Extraction failed: %1").arg(m_ErrorMessage)); - } else { - return IPluginInstaller::RESULT_CANCELED; - } - } else { - throw MyException(tr("Extraction failed: %1").arg(static_cast(m_ArchiveHandler->getLastError()))); - } + if (!extractFiles(targetDirectory, "", true)) { + return IPluginInstaller::RESULT_CANCELED; } // Copy the created files: -- cgit v1.3.1