Implement contents check

This commit is contained in:
Pengfei
2021-08-09 00:42:45 +08:00
parent ea24716a38
commit d0da439731
12 changed files with 450 additions and 302 deletions
+2
View File
@@ -9,6 +9,8 @@ endif()
file(GLOB_RECURSE THEMES ${PROJECT_SOURCE_DIR}/dist/themes/*)
add_executable(threeSD
helpers/frontend_common.cpp
helpers/frontend_common.h
helpers/multi_job.cpp
helpers/multi_job.h
helpers/simple_job.cpp
+21
View File
@@ -0,0 +1,21 @@
// Copyright 2021 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <array>
#include <cmath>
#include <QObject>
#include "frontend/helpers/frontend_common.h"
QString ReadableByteSize(qulonglong size) {
static const std::array<const char*, 6> units = {QT_TR_NOOP("B"), QT_TR_NOOP("KiB"),
QT_TR_NOOP("MiB"), QT_TR_NOOP("GiB"),
QT_TR_NOOP("TiB"), QT_TR_NOOP("PiB")};
if (size == 0)
return QStringLiteral("0");
int digit_groups = std::min<int>(static_cast<int>(std::log10(size) / std::log10(1024)),
static_cast<int>(units.size()));
return QStringLiteral("%L1 %2")
.arg(size / std::pow(1024, digit_groups), 0, 'f', 1)
.arg(QObject::tr(units[digit_groups], "FrontendCommon"));
}
+9
View File
@@ -0,0 +1,9 @@
// Copyright 2021 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <QString>
QString ReadableByteSize(qulonglong size);
+63 -26
View File
@@ -1,26 +1,63 @@
// Copyright 2020 Pengfei Zhu
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "frontend/helpers/simple_job.h"
SimpleJob::SimpleJob(QObject* parent, ExecuteFunc execute_, AbortFunc abort_)
: QThread(parent), execute(std::move(execute_)), abort(std::move(abort_)) {}
SimpleJob::~SimpleJob() = default;
void SimpleJob::run() {
const bool ret =
execute([this](u64 current, u64 total) { emit ProgressUpdated(current, total); });
if (ret || canceled) {
emit Completed();
} else {
emit ErrorOccured();
}
}
void SimpleJob::Cancel() {
canceled = true;
abort();
}
// Copyright 2020 Pengfei Zhu
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <QMessageBox>
#include <QProgressBar>
#include <QProgressDialog>
#include "frontend/helpers/frontend_common.h"
#include "frontend/helpers/simple_job.h"
SimpleJob::SimpleJob(QObject* parent, ExecuteFunc execute_, AbortFunc abort_)
: QThread(parent), execute(std::move(execute_)), abort(std::move(abort_)) {}
SimpleJob::~SimpleJob() = default;
void SimpleJob::run() {
const bool ret =
execute([this](u64 current, u64 total) { emit ProgressUpdated(current, total); });
if (ret || canceled) {
emit Completed(canceled);
} else {
emit ErrorOccured();
}
}
void SimpleJob::Cancel() {
canceled = true;
abort();
}
void SimpleJob::StartWithProgressDialog(QWidget* widget) {
// We need to create the bar ourselves to circumvent an issue caused by modal ProgressDialog's
// event handling.
auto* bar = new QProgressBar(widget);
bar->setRange(0, 100);
bar->setValue(0);
auto* dialog = new QProgressDialog(tr("Initializing..."), tr("Cancel"), 0, 0, widget);
dialog->setBar(bar);
dialog->setWindowModality(Qt::WindowModal);
dialog->setMinimumDuration(0);
connect(this, &SimpleJob::ProgressUpdated, this, [bar, dialog](u64 current, u64 total) {
// Try to map total to int range
// This is equal to ceil(total / INT_MAX)
const u64 multiplier =
(total + std::numeric_limits<int>::max() - 1) / std::numeric_limits<int>::max();
bar->setMaximum(static_cast<int>(total / multiplier));
bar->setValue(static_cast<int>(current / multiplier));
dialog->setLabelText(
tr("%1 / %2").arg(ReadableByteSize(current)).arg(ReadableByteSize(total)));
});
connect(this, &SimpleJob::ErrorOccured, this, [widget, dialog] {
QMessageBox::critical(widget, tr("threeSD"),
tr("Operation failed. Please refer to the log."));
dialog->hide();
});
connect(this, &SimpleJob::Completed, this, [dialog] { dialog->setValue(dialog->maximum()); });
connect(dialog, &QProgressDialog::canceled, this, &SimpleJob::Cancel);
start();
}
+39 -37
View File
@@ -1,37 +1,39 @@
// Copyright 2019 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <functional>
#include <QThread>
#include "common/common_types.h"
#include "common/progress_callback.h"
/**
* Lightweight wrapper around QThread, for easy use with progressive jobs.
*/
class SimpleJob : public QThread {
Q_OBJECT
public:
using ExecuteFunc = std::function<bool(const Common::ProgressCallback&)>;
using AbortFunc = std::function<void()>;
explicit SimpleJob(QObject* parent, ExecuteFunc execute, AbortFunc abort);
~SimpleJob() override;
void run() override;
void Cancel();
signals:
void ProgressUpdated(u64 current, u64 total);
void Completed();
void ErrorOccured();
private:
ExecuteFunc execute;
AbortFunc abort;
bool canceled{};
};
// Copyright 2019 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <functional>
#include <QThread>
#include "common/common_types.h"
#include "common/progress_callback.h"
/**
* Lightweight wrapper around QThread, for easy use with progressive jobs.
*/
class SimpleJob : public QThread {
Q_OBJECT
public:
using ExecuteFunc = std::function<bool(const Common::ProgressCallback&)>;
using AbortFunc = std::function<void()>;
explicit SimpleJob(QObject* parent, ExecuteFunc execute, AbortFunc abort);
~SimpleJob() override;
void run() override;
void Cancel();
void StartWithProgressDialog(QWidget* widget);
signals:
void ProgressUpdated(u64 current, u64 total);
void Completed(bool canceled);
void ErrorOccured();
private:
ExecuteFunc execute;
AbortFunc abort;
bool canceled{};
};
+3 -49
View File
@@ -24,25 +24,13 @@
#include "common/progress_callback.h"
#include "common/scope_exit.h"
#include "frontend/cia_build_dialog.h"
#include "frontend/helpers/frontend_common.h"
#include "frontend/helpers/multi_job.h"
#include "frontend/helpers/simple_job.h"
#include "frontend/import_dialog.h"
#include "frontend/title_info_dialog.h"
#include "ui_import_dialog.h"
static QString ReadableByteSize(qulonglong size) {
static const std::array<const char*, 6> units = {QT_TR_NOOP("B"), QT_TR_NOOP("KiB"),
QT_TR_NOOP("MiB"), QT_TR_NOOP("GiB"),
QT_TR_NOOP("TiB"), QT_TR_NOOP("PiB")};
if (size == 0)
return QStringLiteral("0");
int digit_groups = std::min<int>(static_cast<int>(std::log10(size) / std::log10(1024)),
static_cast<int>(units.size()));
return QStringLiteral("%L1 %2")
.arg(size / std::pow(1024, digit_groups), 0, 'f', 1)
.arg(QObject::tr(units[digit_groups], "ImportDialog"));
}
// content type, singular name, plural name, icon name
// clang-format off
static constexpr std::array<std::tuple<Core::ContentType, const char*, const char*, const char*>, 9>
@@ -686,40 +674,6 @@ void ImportDialog::RunMultiJob(MultiJob* job, std::size_t total_count, u64 total
job->start();
}
// Runs the job, opening a dialog to report its progress.
void ImportDialog::RunSimpleJob(SimpleJob* job) {
// We need to create the bar ourselves to circumvent an issue caused by modal ProgressDialog's
// event handling.
auto* bar = new QProgressBar(this);
bar->setRange(0, 100);
bar->setValue(0);
auto* dialog = new QProgressDialog(tr("Initializing..."), tr("Cancel"), 0, 0, this);
dialog->setBar(bar);
dialog->setWindowModality(Qt::WindowModal);
dialog->setMinimumDuration(0);
connect(job, &SimpleJob::ProgressUpdated, this, [bar, dialog](u64 current, u64 total) {
// Try to map total to int range
// This is equal to ceil(total / INT_MAX)
const u64 multiplier =
(total + std::numeric_limits<int>::max() - 1) / std::numeric_limits<int>::max();
bar->setMaximum(static_cast<int>(total / multiplier));
bar->setValue(static_cast<int>(current / multiplier));
dialog->setLabelText(
tr("%1 / %2").arg(ReadableByteSize(current)).arg(ReadableByteSize(total)));
});
connect(job, &SimpleJob::ErrorOccured, this, [this, dialog] {
QMessageBox::critical(this, tr("threeSD"),
tr("Operation failed. Please refer to the log."));
dialog->hide();
});
connect(job, &SimpleJob::Completed, this, [dialog] { dialog->setValue(dialog->maximum()); });
connect(dialog, &QProgressDialog::canceled, this, [job] { job->Cancel(); });
job->start();
}
void ImportDialog::StartImporting() {
UpdateSizeDisplay();
if (!ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->isEnabled()) {
@@ -755,7 +709,7 @@ void ImportDialog::StartDumpingCXISingle(const Core::ContentSpecifier& specifier
return importer->DumpCXI(specifier, path.toStdString(), callback);
},
[this] { importer->AbortDumpCXI(); });
RunSimpleJob(job);
job->StartWithProgressDialog(this);
}
void ImportDialog::StartBatchDumpingCXI() {
@@ -830,7 +784,7 @@ void ImportDialog::StartBuildingCIASingle(const Core::ContentSpecifier& specifie
return importer->BuildCIA(type, specifier, path.toStdString(), callback);
},
[this] { importer->AbortBuildCIA(); });
RunSimpleJob(job);
job->StartWithProgressDialog(this);
}
void ImportDialog::StartBatchBuildingCIA() {
+87 -88
View File
@@ -1,88 +1,87 @@
// Copyright 2019 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <QDialog>
#include <QPixmap>
#include "core/file_sys/ncch_container.h"
#include "core/importer.h"
class AdvancedMenu;
class MultiJob;
class SimpleJob;
class QTreeWidgetItem;
namespace Ui {
class ImportDialog;
}
class ImportDialog : public QDialog {
Q_OBJECT
public:
explicit ImportDialog(QWidget* parent, const Core::Config& config);
~ImportDialog() override;
private:
void RelistContent();
void RepopulateContent();
void UpdateSizeDisplay();
void UpdateItemCheckState(QTreeWidgetItem* item);
std::vector<Core::ContentSpecifier> GetSelectedContentList();
void InsertTopLevelItem(const QString& text, QPixmap icon = {});
// When replace_name and replace_icon are present they are used instead of those in `content`.
void InsertSecondLevelItem(std::size_t row, const Core::ContentSpecifier& content,
std::size_t id, QString replace_name = {},
QPixmap replace_icon = {});
Core::ContentSpecifier SpecifierFromItem(QTreeWidgetItem* item) const;
void OnContextMenu(const QPoint& point);
void ShowAdvancedMenu();
void RunMultiJob(MultiJob* job, std::size_t total_count, u64 total_size);
void RunSimpleJob(SimpleJob* job);
void StartImporting();
void StartDumpingCXISingle(const Core::ContentSpecifier& content);
QString last_dump_cxi_path; // Used for recording last path in StartDumpingCXISingle
void StartBatchDumpingCXI();
QString last_batch_dump_cxi_path; // Used for recording last path in StartBatchDumpingCXI
void StartBuildingCIASingle(const Core::ContentSpecifier& content);
QString last_build_cia_path; // Used for recording last path in StartBuildingCIASingle
void StartBatchBuildingCIA();
QString last_batch_build_cia_path; // Used for recording last path in StartBatchBuildingCIA
std::unique_ptr<Ui::ImportDialog> ui;
std::unique_ptr<Core::SDMCImporter> importer;
const Core::Config config;
std::vector<Core::ContentSpecifier> contents;
u64 total_selected_size = 0;
// HACK: To tell whether the checkbox state change is a programmatic trigger
// TODO: Is there a more elegant way of doing the same?
bool program_trigger = false;
// HACK: Block advanced menu trigger once.
bool block_advanced_menu = false;
friend class AdvancedMenu;
// Whether the System Archive / System Data warning has been shown
bool system_warning_shown = false;
// Whether the Applets warning has been shown
bool applet_warning_shown = false;
// TODO: Why this won't work as locals?
Core::ContentSpecifier current_content = {};
std::size_t current_count = 0;
};
// Copyright 2019 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <QDialog>
#include <QPixmap>
#include "core/file_sys/ncch_container.h"
#include "core/importer.h"
class AdvancedMenu;
class MultiJob;
class SimpleJob;
class QTreeWidgetItem;
namespace Ui {
class ImportDialog;
}
class ImportDialog : public QDialog {
Q_OBJECT
public:
explicit ImportDialog(QWidget* parent, const Core::Config& config);
~ImportDialog() override;
private:
void RelistContent();
void RepopulateContent();
void UpdateSizeDisplay();
void UpdateItemCheckState(QTreeWidgetItem* item);
std::vector<Core::ContentSpecifier> GetSelectedContentList();
void InsertTopLevelItem(const QString& text, QPixmap icon = {});
// When replace_name and replace_icon are present they are used instead of those in `content`.
void InsertSecondLevelItem(std::size_t row, const Core::ContentSpecifier& content,
std::size_t id, QString replace_name = {},
QPixmap replace_icon = {});
Core::ContentSpecifier SpecifierFromItem(QTreeWidgetItem* item) const;
void OnContextMenu(const QPoint& point);
void ShowAdvancedMenu();
void RunMultiJob(MultiJob* job, std::size_t total_count, u64 total_size);
void StartImporting();
void StartDumpingCXISingle(const Core::ContentSpecifier& content);
QString last_dump_cxi_path; // Used for recording last path in StartDumpingCXISingle
void StartBatchDumpingCXI();
QString last_batch_dump_cxi_path; // Used for recording last path in StartBatchDumpingCXI
void StartBuildingCIASingle(const Core::ContentSpecifier& content);
QString last_build_cia_path; // Used for recording last path in StartBuildingCIASingle
void StartBatchBuildingCIA();
QString last_batch_build_cia_path; // Used for recording last path in StartBatchBuildingCIA
std::unique_ptr<Ui::ImportDialog> ui;
std::unique_ptr<Core::SDMCImporter> importer;
const Core::Config config;
std::vector<Core::ContentSpecifier> contents;
u64 total_selected_size = 0;
// HACK: To tell whether the checkbox state change is a programmatic trigger
// TODO: Is there a more elegant way of doing the same?
bool program_trigger = false;
// HACK: Block advanced menu trigger once.
bool block_advanced_menu = false;
friend class AdvancedMenu;
// Whether the System Archive / System Data warning has been shown
bool system_warning_shown = false;
// Whether the Applets warning has been shown
bool applet_warning_shown = false;
// TODO: Why this won't work as locals?
Core::ContentSpecifier current_content = {};
std::size_t current_count = 0;
};
+32 -6
View File
@@ -11,28 +11,28 @@
#include "core/file_sys/ncch_container.h"
#include "core/file_sys/title_metadata.h"
#include "core/importer.h"
#include "frontend/helpers/simple_job.h"
#include "frontend/title_info_dialog.h"
#include "ui_title_info_dialog.h"
TitleInfoDialog::TitleInfoDialog(QWidget* parent, const Core::Config& config,
Core::SDMCImporter& importer,
const Core::ContentSpecifier& specifier)
: QDialog(parent), ui(std::make_unique<Ui::TitleInfoDialog>()) {
Core::SDMCImporter& importer_, Core::ContentSpecifier specifier_)
: QDialog(parent), ui(std::make_unique<Ui::TitleInfoDialog>()), importer(importer_),
specifier(std::move(specifier_)) {
ui->setupUi(this);
const double scale = qApp->desktop()->logicalDpiX() / 96.0;
resize(static_cast<int>(width() * scale), static_cast<int>(height() * scale));
LoadInfo(config, importer, specifier);
LoadInfo(config);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &TitleInfoDialog::accept);
}
TitleInfoDialog::~TitleInfoDialog() = default;
void TitleInfoDialog::LoadInfo(const Core::Config& config, Core::SDMCImporter& importer,
const Core::ContentSpecifier& specifier) {
void TitleInfoDialog::LoadInfo(const Core::Config& config) {
Core::TitleMetadata tmd;
if (!importer.LoadTMD(specifier, tmd)) {
QMessageBox::warning(this, tr("threeSD"), tr("Could not load title information."));
@@ -100,6 +100,8 @@ void TitleInfoDialog::LoadInfo(const Core::Config& config, Core::SDMCImporter& i
} else {
ui->ticketCheckLabel->setText(tr("Missing"));
}
connect(ui->contentsCheckButton, &QPushButton::clicked, this,
&TitleInfoDialog::ExecuteContentsCheck);
// Load SMDH
std::vector<u8> smdh_buffer;
@@ -170,3 +172,27 @@ void TitleInfoDialog::UpdateNames() {
ui->publisherLineEdit->setText(
QString::fromStdString(Common::UTF16BufferToUTF8(title.publisher)));
}
void TitleInfoDialog::ExecuteContentsCheck() {
auto* job = new SimpleJob(
this,
[this](const Common::ProgressCallback& callback) {
contents_check_result = importer.CheckTitleContents(specifier, callback);
return true;
},
[this] { importer.AbortImporting(); });
connect(job, &SimpleJob::Completed, this, [this](bool canceled) {
if (canceled) {
return;
}
ui->contentsCheckButton->setVisible(false);
ui->contentsCheckLabel->setVisible(true);
if (contents_check_result) {
ui->contentsCheckLabel->setText(tr("OK"));
} else {
ui->contentsCheckLabel->setText(tr("Failed"));
}
});
job->StartWithProgressDialog(this);
}
+7 -3
View File
@@ -22,15 +22,19 @@ class TitleInfoDialog : public QDialog {
public:
explicit TitleInfoDialog(QWidget* parent, const Core::Config& config,
Core::SDMCImporter& importer, const Core::ContentSpecifier& specifier);
Core::SDMCImporter& importer, Core::ContentSpecifier specifier);
~TitleInfoDialog();
private:
void LoadInfo(const Core::Config& config, Core::SDMCImporter& importer,
const Core::ContentSpecifier& specifier);
void LoadInfo(const Core::Config& config);
void InitializeLanguageComboBox();
void UpdateNames();
void ExecuteContentsCheck();
std::unique_ptr<Ui::TitleInfoDialog> ui;
Core::SDMCImporter& importer;
const Core::ContentSpecifier specifier;
Core::SMDH smdh{};
bool contents_check_result = false;
};