mirror of
https://github.com/Dark98/threeSD.git
synced 2026-07-05 00:38:49 +00:00
Add ETA support
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include "frontend/helpers/import_job.h"
|
#include "frontend/helpers/import_job.h"
|
||||||
|
|
||||||
ImportJob::ImportJob(QObject* parent, Core::SDMCImporter& importer_,
|
ImportJob::ImportJob(QObject* parent, Core::SDMCImporter& importer_,
|
||||||
@@ -11,12 +12,31 @@ ImportJob::ImportJob(QObject* parent, Core::SDMCImporter& importer_,
|
|||||||
ImportJob::~ImportJob() = default;
|
ImportJob::~ImportJob() = default;
|
||||||
|
|
||||||
void ImportJob::run() {
|
void ImportJob::run() {
|
||||||
u64 size_imported = 0, count = 0;
|
u64 total_size = 0;
|
||||||
for (const auto& content : contents) {
|
for (const auto& content : contents) {
|
||||||
emit NextContent(size_imported, count + 1, content);
|
total_size += content.maximum_size;
|
||||||
const auto callback = [this, size_imported](std::size_t current_size,
|
}
|
||||||
std::size_t /*total_size*/) {
|
|
||||||
emit ProgressUpdated(size_imported + current_size, current_size);
|
u64 size_imported = 0, count = 0;
|
||||||
|
int eta = -1;
|
||||||
|
|
||||||
|
const auto initial_time = std::chrono::steady_clock::now();
|
||||||
|
const auto UpdateETA = [total_size, &eta, initial_time](u64 size_imported) {
|
||||||
|
if (size_imported >= 10 * 1024 * 1024) { // 10M Threshold
|
||||||
|
using namespace std::chrono;
|
||||||
|
const u64 time_elapsed =
|
||||||
|
duration_cast<milliseconds>(steady_clock::now() - initial_time).count();
|
||||||
|
eta = static_cast<int>(time_elapsed * (total_size - size_imported) / (size_imported) /
|
||||||
|
1000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& content : contents) {
|
||||||
|
emit NextContent(size_imported, count + 1, content, eta);
|
||||||
|
const auto callback = [this, total_size, size_imported, &eta,
|
||||||
|
&UpdateETA](std::size_t current_size, std::size_t /*total_size*/) {
|
||||||
|
UpdateETA(size_imported + current_size);
|
||||||
|
emit ProgressUpdated(size_imported + current_size, current_size, eta);
|
||||||
};
|
};
|
||||||
if (!importer.ImportContent(content, callback)) {
|
if (!importer.ImportContent(content, callback)) {
|
||||||
importer.DeleteContent(content);
|
importer.DeleteContent(content);
|
||||||
@@ -26,6 +46,7 @@ void ImportJob::run() {
|
|||||||
}
|
}
|
||||||
count++;
|
count++;
|
||||||
size_imported += content.maximum_size;
|
size_imported += content.maximum_size;
|
||||||
|
UpdateETA(size_imported);
|
||||||
|
|
||||||
if (cancelled) {
|
if (cancelled) {
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -27,11 +27,12 @@ signals:
|
|||||||
* @param total_size_imported Total imported size taking all previous contents into
|
* @param total_size_imported Total imported size taking all previous contents into
|
||||||
* consideration.
|
* consideration.
|
||||||
* @param current_size_imported Imported size of the current content.
|
* @param current_size_imported Imported size of the current content.
|
||||||
|
* @param eta ETA in seconds, 0 when not determined.
|
||||||
*/
|
*/
|
||||||
void ProgressUpdated(u64 total_size_imported, u64 current_size_imported);
|
void ProgressUpdated(u64 total_size_imported, u64 current_size_imported, int eta);
|
||||||
|
|
||||||
/// Dumping of a content has been finished, go on to the next. Called at start as well.
|
/// Dumping of a content has been finished, go on to the next. Called at start as well.
|
||||||
void NextContent(u64 size_imported, u64 count, Core::ContentSpecifier next_content);
|
void NextContent(u64 size_imported, u64 count, Core::ContentSpecifier next_content, int eta);
|
||||||
|
|
||||||
void Completed();
|
void Completed();
|
||||||
|
|
||||||
|
|||||||
@@ -446,6 +446,15 @@ std::vector<Core::ContentSpecifier> ImportDialog::GetSelectedContentList() {
|
|||||||
return to_import;
|
return to_import;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static QString FormatETA(int eta) {
|
||||||
|
if (eta < 0) {
|
||||||
|
return QStringLiteral(" ");
|
||||||
|
}
|
||||||
|
return QCoreApplication::translate("ImportDialog", "ETA %1m%2s")
|
||||||
|
.arg(eta / 60, 2, 10, QLatin1Char('0'))
|
||||||
|
.arg(eta % 60, 2, 10, QLatin1Char('0'));
|
||||||
|
}
|
||||||
|
|
||||||
void ImportDialog::StartImporting() {
|
void ImportDialog::StartImporting() {
|
||||||
UpdateSizeDisplay();
|
UpdateSizeDisplay();
|
||||||
if (!ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->isEnabled()) {
|
if (!ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->isEnabled()) {
|
||||||
@@ -478,29 +487,31 @@ void ImportDialog::StartImporting() {
|
|||||||
|
|
||||||
connect(job, &ImportJob::NextContent, this,
|
connect(job, &ImportJob::NextContent, this,
|
||||||
[this, dialog, multiplier, total_count](u64 size_imported, u64 count,
|
[this, dialog, multiplier, total_count](u64 size_imported, u64 count,
|
||||||
Core::ContentSpecifier next_content) {
|
Core::ContentSpecifier next_content, int eta) {
|
||||||
dialog->setValue(static_cast<int>(size_imported / multiplier));
|
dialog->setValue(static_cast<int>(size_imported / multiplier));
|
||||||
dialog->setLabelText(
|
dialog->setLabelText(
|
||||||
tr("<p align=\"left\">(%1/%2) Importing %3 (%4)...</p><p> </p>")
|
tr("<p>(%1/%2) Importing %3 (%4)...</p><p> </p><p align=\"right\">%5</p>")
|
||||||
.arg(count)
|
.arg(count)
|
||||||
.arg(total_count)
|
.arg(total_count)
|
||||||
.arg(GetContentName(next_content))
|
.arg(GetContentName(next_content))
|
||||||
.arg(GetContentTypeName(next_content.type)));
|
.arg(GetContentTypeName(next_content.type))
|
||||||
|
.arg(FormatETA(eta)));
|
||||||
current_content = next_content;
|
current_content = next_content;
|
||||||
current_count = count;
|
current_count = count;
|
||||||
});
|
});
|
||||||
connect(job, &ImportJob::ProgressUpdated, this,
|
connect(job, &ImportJob::ProgressUpdated, this,
|
||||||
[this, dialog, multiplier, total_count](u64 total_size_imported,
|
[this, dialog, multiplier, total_count](u64 total_size_imported,
|
||||||
u64 current_size_imported) {
|
u64 current_size_imported, int eta) {
|
||||||
dialog->setValue(static_cast<int>(total_size_imported / multiplier));
|
dialog->setValue(static_cast<int>(total_size_imported / multiplier));
|
||||||
dialog->setLabelText(tr("<p align=\"left\">(%1/%2) Importing %3 (%4)...</p><p "
|
dialog->setLabelText(tr("<p>(%1/%2) Importing %3 (%4)...</p><p align=\"center\">%5 "
|
||||||
"align=\"left\">%5 / %6</p>")
|
"/ %6</p><p align=\"right\">%7</p>")
|
||||||
.arg(current_count)
|
.arg(current_count)
|
||||||
.arg(total_count)
|
.arg(total_count)
|
||||||
.arg(GetContentName(current_content))
|
.arg(GetContentName(current_content))
|
||||||
.arg(GetContentTypeName(current_content.type))
|
.arg(GetContentTypeName(current_content.type))
|
||||||
.arg(ReadableByteSize(current_size_imported))
|
.arg(ReadableByteSize(current_size_imported))
|
||||||
.arg(ReadableByteSize(current_content.maximum_size)));
|
.arg(ReadableByteSize(current_content.maximum_size))
|
||||||
|
.arg(FormatETA(eta)));
|
||||||
});
|
});
|
||||||
connect(job, &ImportJob::Completed, this, [this, dialog, job] {
|
connect(job, &ImportJob::Completed, this, [this, dialog, job] {
|
||||||
dialog->setValue(dialog->maximum());
|
dialog->setValue(dialog->maximum());
|
||||||
|
|||||||
Reference in New Issue
Block a user