2022-10-25 01:20:35 +05:30
|
|
|
// SPDX-FileCopyrightText: 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
2022-10-23 05:15:32 +05:30
|
|
|
#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);
|
|
|
|
|
2022-11-10 23:34:42 +05:30
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
if (!m_copyFuture.isValid() || !m_copyFuture.result()) {
|
|
|
|
#else
|
2022-10-23 05:15:32 +05:30
|
|
|
if (!m_copyFuture.result()) {
|
2022-11-10 23:34:42 +05:30
|
|
|
#endif
|
|
|
|
emitFailed(tr("Failed to scan source path."));
|
2022-10-23 05:15:32 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Copy
|
|
|
|
// Actually copy all files now.
|
|
|
|
m_toCopy = m_copy.totalCopied();
|
2022-10-23 17:54:11 +05:30
|
|
|
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));
|
|
|
|
});
|
2022-10-23 05:15:32 +05:30
|
|
|
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);
|
|
|
|
|
2022-11-10 23:34:42 +05:30
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
if (!m_copyFuture.isValid() || !m_copyFuture.result()) {
|
|
|
|
#else
|
2022-10-23 05:15:32 +05:30
|
|
|
if (!m_copyFuture.result()) {
|
2022-11-10 23:34:42 +05:30
|
|
|
#endif
|
|
|
|
emitFailed(tr("Some paths could not be copied!"));
|
2022-10-23 05:15:32 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataMigrationTask::copyAborted()
|
|
|
|
{
|
|
|
|
emitFailed(tr("Aborted"));
|
|
|
|
}
|