From f7c9b8c3336da02d3360b810247c00494df114fd Mon Sep 17 00:00:00 2001 From: Silarn Date: Wed, 2 May 2018 17:04:28 -0500 Subject: Thread extraction process and properly offload function calls from threads --- src/installationmanager.cpp | 80 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 18 deletions(-) (limited to 'src/installationmanager.cpp') diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp index 668be84e..8ee2220e 100644 --- a/src/installationmanager.cpp +++ b/src/installationmanager.cpp @@ -49,6 +49,7 @@ along with Mod Organizer. If not, see . #include #include #include +#include #include @@ -94,6 +95,9 @@ InstallationManager::InstallationManager() if (!m_ArchiveHandler->isValid()) { throw MyException(getErrorString(m_ArchiveHandler->getLastError())); } + + connect(this, SIGNAL(progressUpdate(float)), this, (SLOT(doProgressUpdate(float)))); + connect(this, SIGNAL(progressUpdate(const QString)), this, (SLOT(doProgressFileUpdate(const QString)))); } InstallationManager::~InstallationManager() @@ -190,10 +194,19 @@ bool InstallationManager::unpackSingleFile(const QString &fileName) m_InstallationProgress->setFixedSize(600, 100); m_InstallationProgress->show(); - bool res = m_ArchiveHandler->extract(QDir::tempPath(), - new MethodCallback(this, &InstallationManager::updateProgress), - nullptr, - new MethodCallback(this, &InstallationManager::report7ZipError)); + QFuture future = QtConcurrent::run([&]() -> bool { + return m_ArchiveHandler->extract( + QDir::tempPath(), + new MethodCallback(this, &InstallationManager::updateProgress), + nullptr, + new MethodCallback(this, &InstallationManager::report7ZipError) + ); + }); + while (!future.isFinished()) { + QCoreApplication::processEvents(); + ::Sleep(50); + } + bool res = future.result(); return res; } @@ -279,10 +292,19 @@ QStringList InstallationManager::extractFiles(const QStringList &filesOrig, bool m_InstallationProgress->show(); // unpack only the files we need for the installer - if (!m_ArchiveHandler->extract(QDir::tempPath(), - new MethodCallback(this, &InstallationManager::updateProgress), - nullptr, - new MethodCallback(this, &InstallationManager::report7ZipError))) { + QFuture future = QtConcurrent::run([&]() -> bool { + return m_ArchiveHandler->extract( + QDir::tempPath(), + new MethodCallback(this, &InstallationManager::updateProgress), + nullptr, + new MethodCallback(this, &InstallationManager::report7ZipError) + ); + }); + while (!future.isFinished()) { + QCoreApplication::processEvents(); + ::Sleep(50); + } + if (!future.result()) { throw MyException(QString("extracting failed (%1)").arg(m_ArchiveHandler->getLastError())); } @@ -408,9 +430,23 @@ DirectoryTree::Node *InstallationManager::getSimpleArchiveBase(DirectoryTree *da void InstallationManager::updateProgress(float percentage) +{ + emit progressUpdate(percentage); +} + + +void InstallationManager::updateProgressFile(QString const &fileName) +{ + emit progressUpdate(fileName); +} + + +void InstallationManager::doProgressUpdate(float percentage) { if (m_InstallationProgress != nullptr) { - m_InstallationProgress->setValue(static_cast(percentage * 100.0)); + QMetaObject::invokeMethod(m_InstallationProgress, "setValue", Qt::QueuedConnection, Q_ARG(int, static_cast(percentage * 100.0))); + //m_InstallationProgress->setValue(percent); + if (m_InstallationProgress->wasCanceled()) { m_ArchiveHandler->cancel(); m_InstallationProgress->reset(); @@ -419,12 +455,11 @@ void InstallationManager::updateProgress(float percentage) } -void InstallationManager::updateProgressFile(QString const &fileName) +void InstallationManager::doProgressFileUpdate(QString const fileName) { - if (m_InstallationProgress != nullptr) { - m_InstallationProgress->setLabelText(fileName); - QCoreApplication::processEvents(); - } + if (m_InstallationProgress != nullptr) { + m_InstallationProgress->setLabelText(fileName); + } } @@ -568,10 +603,19 @@ bool InstallationManager::doInstall(GuessedValue &modName, QString game m_InstallationProgress->setWindowModality(Qt::WindowModal); m_InstallationProgress->setFixedSize(600, 100); m_InstallationProgress->show(); - if (!m_ArchiveHandler->extract(targetDirectory, - new MethodCallback(this, &InstallationManager::updateProgress), - new MethodCallback(this, &InstallationManager::updateProgressFile), - new MethodCallback(this, &InstallationManager::report7ZipError))) { + QFuture future = QtConcurrent::run([&]() -> bool { + return m_ArchiveHandler->extract( + targetDirectory, + new MethodCallback(this, &InstallationManager::updateProgress), + new MethodCallback(this, &InstallationManager::updateProgressFile), + new MethodCallback(this, &InstallationManager::report7ZipError) + ); + }); + while (!future.isFinished()) { + QCoreApplication::processEvents(); + ::Sleep(50); + } + if (!future.result()) { if (m_ArchiveHandler->getLastError() == Archive::ERROR_EXTRACT_CANCELLED) { return false; } else { -- cgit v1.3.1