mirror of
https://github.com/Dark98/threeSD.git
synced 2026-07-05 08:39:04 +00:00
Rename ImportJob to MultiJob and generalize
This commit is contained in:
@@ -9,8 +9,8 @@ endif()
|
|||||||
file(GLOB_RECURSE THEMES ${PROJECT_SOURCE_DIR}/dist/themes/*)
|
file(GLOB_RECURSE THEMES ${PROJECT_SOURCE_DIR}/dist/themes/*)
|
||||||
|
|
||||||
add_executable(threeSD
|
add_executable(threeSD
|
||||||
helpers/import_job.cpp
|
helpers/multi_job.cpp
|
||||||
helpers/import_job.h
|
helpers/multi_job.h
|
||||||
helpers/simple_job.cpp
|
helpers/simple_job.cpp
|
||||||
helpers/simple_job.h
|
helpers/simple_job.h
|
||||||
import_dialog.cpp
|
import_dialog.cpp
|
||||||
|
|||||||
@@ -3,15 +3,17 @@
|
|||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include "frontend/helpers/import_job.h"
|
#include "frontend/helpers/multi_job.h"
|
||||||
|
|
||||||
ImportJob::ImportJob(QObject* parent, Core::SDMCImporter& importer_,
|
MultiJob::MultiJob(QObject* parent, Core::SDMCImporter& importer_,
|
||||||
std::vector<Core::ContentSpecifier> contents_)
|
std::vector<Core::ContentSpecifier> contents_, ExecuteFunc execute_func_,
|
||||||
: QThread(parent), importer(importer_), contents(std::move(contents_)) {}
|
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;
|
u64 total_size = 0;
|
||||||
for (const auto& content : contents) {
|
for (const auto& content : contents) {
|
||||||
total_size += content.maximum_size;
|
total_size += content.maximum_size;
|
||||||
@@ -38,7 +40,8 @@ void ImportJob::run() {
|
|||||||
UpdateETA(size_imported + current_size);
|
UpdateETA(size_imported + current_size);
|
||||||
emit ProgressUpdated(size_imported + current_size, current_size, eta);
|
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);
|
importer.DeleteContent(content);
|
||||||
if (!cancelled) {
|
if (!cancelled) {
|
||||||
failed_contents.emplace_back(content);
|
failed_contents.emplace_back(content);
|
||||||
@@ -55,11 +58,11 @@ void ImportJob::run() {
|
|||||||
emit Completed();
|
emit Completed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImportJob::Cancel() {
|
void MultiJob::Cancel() {
|
||||||
cancelled.store(true);
|
cancelled.store(true);
|
||||||
importer.AbortImporting();
|
importer.AbortImporting();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Core::ContentSpecifier> ImportJob::GetFailedContents() const {
|
std::vector<Core::ContentSpecifier> MultiJob::GetFailedContents() const {
|
||||||
return failed_contents;
|
return failed_contents;
|
||||||
}
|
}
|
||||||
@@ -5,16 +5,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <functional>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include "core/importer.h"
|
#include "core/importer.h"
|
||||||
|
|
||||||
class ImportJob : public QThread {
|
class MultiJob : public QThread {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ImportJob(QObject* parent, Core::SDMCImporter& importer,
|
using ExecuteFunc = std::function<bool(Core::SDMCImporter&, const Core::ContentSpecifier&,
|
||||||
std::vector<Core::ContentSpecifier> contents);
|
const Core::SDMCImporter::ProgressCallback&)>;
|
||||||
~ImportJob() override;
|
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 run() override;
|
||||||
void Cancel();
|
void Cancel();
|
||||||
@@ -41,6 +47,8 @@ private:
|
|||||||
Core::SDMCImporter& importer;
|
Core::SDMCImporter& importer;
|
||||||
std::vector<Core::ContentSpecifier> contents;
|
std::vector<Core::ContentSpecifier> contents;
|
||||||
std::vector<Core::ContentSpecifier> failed_contents;
|
std::vector<Core::ContentSpecifier> failed_contents;
|
||||||
|
ExecuteFunc execute_func;
|
||||||
|
DeleteFunc delete_func;
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(Core::ContentSpecifier)
|
Q_DECLARE_METATYPE(Core::ContentSpecifier)
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "common/scope_exit.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/helpers/simple_job.h"
|
||||||
#include "frontend/import_dialog.h"
|
#include "frontend/import_dialog.h"
|
||||||
#include "ui_import_dialog.h"
|
#include "ui_import_dialog.h"
|
||||||
@@ -512,9 +512,17 @@ void ImportDialog::StartImporting() {
|
|||||||
dialog->setWindowModality(Qt::WindowModal);
|
dialog->setWindowModality(Qt::WindowModal);
|
||||||
dialog->setMinimumDuration(0);
|
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](
|
[this, bar, dialog, multiplier, total_count](
|
||||||
u64 size_imported, u64 count, Core::ContentSpecifier next_content, int eta) {
|
u64 size_imported, u64 count, Core::ContentSpecifier next_content, int eta) {
|
||||||
bar->setValue(static_cast<int>(size_imported / multiplier));
|
bar->setValue(static_cast<int>(size_imported / multiplier));
|
||||||
@@ -528,7 +536,7 @@ void ImportDialog::StartImporting() {
|
|||||||
current_content = next_content;
|
current_content = next_content;
|
||||||
current_count = count;
|
current_count = count;
|
||||||
});
|
});
|
||||||
connect(job, &ImportJob::ProgressUpdated, this,
|
connect(job, &MultiJob::ProgressUpdated, this,
|
||||||
[this, bar, dialog, multiplier, total_count](u64 total_size_imported,
|
[this, bar, dialog, multiplier, total_count](u64 total_size_imported,
|
||||||
u64 current_size_imported, int eta) {
|
u64 current_size_imported, int eta) {
|
||||||
bar->setValue(static_cast<int>(total_size_imported / multiplier));
|
bar->setValue(static_cast<int>(total_size_imported / multiplier));
|
||||||
@@ -542,7 +550,7 @@ void ImportDialog::StartImporting() {
|
|||||||
.arg(ReadableByteSize(current_content.maximum_size))
|
.arg(ReadableByteSize(current_content.maximum_size))
|
||||||
.arg(FormatETA(eta)));
|
.arg(FormatETA(eta)));
|
||||||
});
|
});
|
||||||
connect(job, &ImportJob::Completed, this, [this, dialog, job] {
|
connect(job, &MultiJob::Completed, this, [this, dialog, job] {
|
||||||
dialog->setValue(dialog->maximum());
|
dialog->setValue(dialog->maximum());
|
||||||
|
|
||||||
const auto failed_contents = job->GetFailedContents();
|
const auto failed_contents = job->GetFailedContents();
|
||||||
@@ -570,7 +578,7 @@ void ImportDialog::StartImporting() {
|
|||||||
cancel_dialog->setCancelButton(nullptr);
|
cancel_dialog->setCancelButton(nullptr);
|
||||||
cancel_dialog->setMinimumDuration(0);
|
cancel_dialog->setMinimumDuration(0);
|
||||||
cancel_dialog->setValue(0);
|
cancel_dialog->setValue(0);
|
||||||
connect(job, &ImportJob::Completed, cancel_dialog, &QProgressDialog::hide);
|
connect(job, &MultiJob::Completed, cancel_dialog, &QProgressDialog::hide);
|
||||||
job->Cancel();
|
job->Cancel();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user