Rename ImportJob to MultiJob and generalize

This commit is contained in:
Pengfei
2021-06-28 00:25:03 +08:00
parent 817c3ea1a8
commit dac4b1cb41
4 changed files with 40 additions and 21 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ endif()
file(GLOB_RECURSE THEMES ${PROJECT_SOURCE_DIR}/dist/themes/*)
add_executable(threeSD
helpers/import_job.cpp
helpers/import_job.h
helpers/multi_job.cpp
helpers/multi_job.h
helpers/simple_job.cpp
helpers/simple_job.h
import_dialog.cpp
@@ -3,15 +3,17 @@
// Refer to the license.txt file included.
#include <chrono>
#include "frontend/helpers/import_job.h"
#include "frontend/helpers/multi_job.h"
ImportJob::ImportJob(QObject* parent, Core::SDMCImporter& importer_,
std::vector<Core::ContentSpecifier> contents_)
: QThread(parent), importer(importer_), contents(std::move(contents_)) {}
MultiJob::MultiJob(QObject* parent, Core::SDMCImporter& importer_,
std::vector<Core::ContentSpecifier> contents_, ExecuteFunc execute_func_,
DeleteFunc delete_func_)
: QThread(parent), importer(importer_), contents(std::move(contents_)),
execute_func(std::move(execute_func_)), delete_func(std::move(delete_func_)) {}
ImportJob::~ImportJob() = default;
MultiJob::~MultiJob() = default;
void ImportJob::run() {
void MultiJob::run() {
u64 total_size = 0;
for (const auto& content : contents) {
total_size += content.maximum_size;
@@ -38,7 +40,8 @@ void ImportJob::run() {
UpdateETA(size_imported + current_size);
emit ProgressUpdated(size_imported + current_size, current_size, eta);
};
if (!importer.ImportContent(content, callback)) {
if (!execute_func(importer, content, callback)) {
delete_func(importer, content);
importer.DeleteContent(content);
if (!cancelled) {
failed_contents.emplace_back(content);
@@ -55,11 +58,11 @@ void ImportJob::run() {
emit Completed();
}
void ImportJob::Cancel() {
void MultiJob::Cancel() {
cancelled.store(true);
importer.AbortImporting();
}
std::vector<Core::ContentSpecifier> ImportJob::GetFailedContents() const {
std::vector<Core::ContentSpecifier> MultiJob::GetFailedContents() const {
return failed_contents;
}
@@ -5,16 +5,22 @@
#pragma once
#include <atomic>
#include <functional>
#include <QThread>
#include "core/importer.h"
class ImportJob : public QThread {
class MultiJob : public QThread {
Q_OBJECT
public:
explicit ImportJob(QObject* parent, Core::SDMCImporter& importer,
std::vector<Core::ContentSpecifier> contents);
~ImportJob() override;
using ExecuteFunc = std::function<bool(Core::SDMCImporter&, const Core::ContentSpecifier&,
const Core::SDMCImporter::ProgressCallback&)>;
using DeleteFunc = std::function<void(Core::SDMCImporter&, const Core::ContentSpecifier&)>;
explicit MultiJob(QObject* parent, Core::SDMCImporter& importer,
std::vector<Core::ContentSpecifier> contents, ExecuteFunc execute_func,
DeleteFunc delete_func);
~MultiJob() override;
void run() override;
void Cancel();
@@ -41,6 +47,8 @@ private:
Core::SDMCImporter& importer;
std::vector<Core::ContentSpecifier> contents;
std::vector<Core::ContentSpecifier> failed_contents;
ExecuteFunc execute_func;
DeleteFunc delete_func;
};
Q_DECLARE_METATYPE(Core::ContentSpecifier)
+14 -6
View File
@@ -19,7 +19,7 @@
#include "common/assert.h"
#include "common/logging/log.h"
#include "common/scope_exit.h"
#include "frontend/helpers/import_job.h"
#include "frontend/helpers/multi_job.h"
#include "frontend/helpers/simple_job.h"
#include "frontend/import_dialog.h"
#include "ui_import_dialog.h"
@@ -512,9 +512,17 @@ void ImportDialog::StartImporting() {
dialog->setWindowModality(Qt::WindowModal);
dialog->setMinimumDuration(0);
auto* job = new ImportJob(this, importer, std::move(to_import));
auto* job = new MultiJob(
this, importer, std::move(to_import),
[](Core::SDMCImporter& importer, const Core::ContentSpecifier& content,
const Core::SDMCImporter::ProgressCallback& callback) {
return importer.ImportContent(content, callback);
},
[](Core::SDMCImporter& importer, const Core::ContentSpecifier& content) {
return importer.DeleteContent(content);
});
connect(job, &ImportJob::NextContent, this,
connect(job, &MultiJob::NextContent, this,
[this, bar, dialog, multiplier, total_count](
u64 size_imported, u64 count, Core::ContentSpecifier next_content, int eta) {
bar->setValue(static_cast<int>(size_imported / multiplier));
@@ -528,7 +536,7 @@ void ImportDialog::StartImporting() {
current_content = next_content;
current_count = count;
});
connect(job, &ImportJob::ProgressUpdated, this,
connect(job, &MultiJob::ProgressUpdated, this,
[this, bar, dialog, multiplier, total_count](u64 total_size_imported,
u64 current_size_imported, int eta) {
bar->setValue(static_cast<int>(total_size_imported / multiplier));
@@ -542,7 +550,7 @@ void ImportDialog::StartImporting() {
.arg(ReadableByteSize(current_content.maximum_size))
.arg(FormatETA(eta)));
});
connect(job, &ImportJob::Completed, this, [this, dialog, job] {
connect(job, &MultiJob::Completed, this, [this, dialog, job] {
dialog->setValue(dialog->maximum());
const auto failed_contents = job->GetFailedContents();
@@ -570,7 +578,7 @@ void ImportDialog::StartImporting() {
cancel_dialog->setCancelButton(nullptr);
cancel_dialog->setMinimumDuration(0);
cancel_dialog->setValue(0);
connect(job, &ImportJob::Completed, cancel_dialog, &QProgressDialog::hide);
connect(job, &MultiJob::Completed, cancel_dialog, &QProgressDialog::hide);
job->Cancel();
});