diff options
author | Sefa Eyeoglu <contact@scrumplex.net> | 2022-11-21 12:14:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-21 12:14:49 +0100 |
commit | c74f852364d512dd850111f92cc989c0c2ce7392 (patch) | |
tree | 7ae2a740b0195b0adcdbcd3460b5e75a68bc7b5d /launcher/DataMigrationTask.cpp | |
parent | 30607c34a153fd32085712e18827983772d77f7b (diff) | |
parent | fe94c3609ef875166b71b9f6c540c45eff97a5ab (diff) | |
download | PrismLauncher-c74f852364d512dd850111f92cc989c0c2ce7392.tar.gz PrismLauncher-c74f852364d512dd850111f92cc989c0c2ce7392.tar.bz2 PrismLauncher-c74f852364d512dd850111f92cc989c0c2ce7392.zip |
Merge pull request #243 from Scrumplex/migration-dialog
Closes https://github.com/PrismLauncher/PrismLauncher/issues/46
Closes https://github.com/PrismLauncher/PrismLauncher/issues/204
Diffstat (limited to 'launcher/DataMigrationTask.cpp')
-rw-r--r-- | launcher/DataMigrationTask.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/launcher/DataMigrationTask.cpp b/launcher/DataMigrationTask.cpp new file mode 100644 index 00000000..27ce5f01 --- /dev/null +++ b/launcher/DataMigrationTask.cpp @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2022 Sefa Eyeoglu <contact@scrumplex.net> +// +// SPDX-License-Identifier: GPL-3.0-only + +#include "DataMigrationTask.h" + +#include "FileSystem.h" + +#include <QDirIterator> +#include <QFileInfo> +#include <QMap> + +#include <QtConcurrent> + +DataMigrationTask::DataMigrationTask(QObject* parent, + const QString& sourcePath, + const QString& targetPath, + const IPathMatcher::Ptr pathMatcher) + : Task(parent), m_sourcePath(sourcePath), m_targetPath(targetPath), m_pathMatcher(pathMatcher), m_copy(sourcePath, targetPath) +{ + m_copy.matcher(m_pathMatcher.get()).whitelist(true); +} + +void DataMigrationTask::executeTask() +{ + setStatus(tr("Scanning files...")); + + // 1. Scan + // Check how many files we gotta copy + m_copyFuture = QtConcurrent::run(QThreadPool::globalInstance(), [&] { + return m_copy(true); // dry run to collect amount of files + }); + connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::finished, this, &DataMigrationTask::dryRunFinished); + connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::canceled, this, &DataMigrationTask::dryRunAborted); + m_copyFutureWatcher.setFuture(m_copyFuture); +} + +void DataMigrationTask::dryRunFinished() +{ + disconnect(&m_copyFutureWatcher, &QFutureWatcher<bool>::finished, this, &DataMigrationTask::dryRunFinished); + disconnect(&m_copyFutureWatcher, &QFutureWatcher<bool>::canceled, this, &DataMigrationTask::dryRunAborted); + +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) + if (!m_copyFuture.isValid() || !m_copyFuture.result()) { +#else + if (!m_copyFuture.result()) { +#endif + emitFailed(tr("Failed to scan source path.")); + return; + } + + // 2. Copy + // Actually copy all files now. + m_toCopy = m_copy.totalCopied(); + connect(&m_copy, &FS::copy::fileCopied, [&, this](const QString& relativeName) { + QString shortenedName = relativeName; + // shorten the filename to hopefully fit into one line + if (shortenedName.length() > 50) + shortenedName = relativeName.left(20) + "…" + relativeName.right(29); + setProgress(m_copy.totalCopied(), m_toCopy); + setStatus(tr("Copying %1…").arg(shortenedName)); + }); + m_copyFuture = QtConcurrent::run(QThreadPool::globalInstance(), [&] { + return m_copy(false); // actually copy now + }); + connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::finished, this, &DataMigrationTask::copyFinished); + connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::canceled, this, &DataMigrationTask::copyAborted); + m_copyFutureWatcher.setFuture(m_copyFuture); +} + +void DataMigrationTask::dryRunAborted() +{ + emitFailed(tr("Aborted")); +} + +void DataMigrationTask::copyFinished() +{ + disconnect(&m_copyFutureWatcher, &QFutureWatcher<bool>::finished, this, &DataMigrationTask::copyFinished); + disconnect(&m_copyFutureWatcher, &QFutureWatcher<bool>::canceled, this, &DataMigrationTask::copyAborted); + +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) + if (!m_copyFuture.isValid() || !m_copyFuture.result()) { +#else + if (!m_copyFuture.result()) { +#endif + emitFailed(tr("Some paths could not be copied!")); + return; + } + + emitSucceeded(); +} + +void DataMigrationTask::copyAborted() +{ + emitFailed(tr("Aborted")); +} |