Merge branch 'prism_export' of github.com:Trial97/PrismLauncher into prism_export2

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-17 17:58:42 +03:00
commit 5162203c7d
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
2 changed files with 46 additions and 30 deletions

View File

@ -423,37 +423,36 @@ bool collectFileListRecursively(const QString& rootDir, const QString& subDir, Q
} }
void ExportToZipTask::executeTask() void ExportToZipTask::executeTask()
{
(void)QtConcurrent::run(QThreadPool::globalInstance(), [this]() { exportZip(); });
}
void ExportToZipTask::exportZip()
{ {
setStatus("Adding files..."); setStatus("Adding files...");
setProgress(0, m_files.length()); setProgress(0, m_files.length());
m_build_zip_future = QtConcurrent::run(QThreadPool::globalInstance(), [this]() { return exportZip(); });
connect(&m_build_zip_watcher, &QFutureWatcher<ZipResult>::finished, this, &ExportToZipTask::finish);
m_build_zip_watcher.setFuture(m_build_zip_future);
}
auto ExportToZipTask::exportZip() -> ZipResult
{
if (!m_dir.exists()) { if (!m_dir.exists()) {
emitFailed(tr("Folder doesn't exist")); return ZipResult(tr("Folder doesn't exist"));
return;
} }
if (!m_output.isOpen() && !m_output.open(QuaZip::mdCreate)) { if (!m_output.isOpen() && !m_output.open(QuaZip::mdCreate)) {
emitFailed(tr("Could not create file")); return ZipResult(tr("Could not create file"));
return;
} }
for (auto fileName : m_extra_files.keys()) { for (auto fileName : m_extra_files.keys()) {
if (!isRunning()) if (m_build_zip_future.isCanceled())
return; return ZipResult();
QuaZipFile indexFile(&m_output); QuaZipFile indexFile(&m_output);
if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileName))) { if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileName))) {
emitFailed(tr("Could not create:") + fileName); return ZipResult(tr("Could not create:") + fileName);
return;
} }
indexFile.write(m_extra_files[fileName]); indexFile.write(m_extra_files[fileName]);
} }
for (const QFileInfo& file : m_files) { for (const QFileInfo& file : m_files) {
if (!isRunning()) if (m_build_zip_future.isCanceled())
return; return ZipResult();
auto absolute = file.absoluteFilePath(); auto absolute = file.absoluteFilePath();
auto relative = m_dir.relativeFilePath(absolute); auto relative = m_dir.relativeFilePath(absolute);
@ -467,28 +466,39 @@ void ExportToZipTask::exportZip()
} }
if (!m_exclude_files.contains(relative) && !JlCompress::compressFile(&m_output, absolute, m_destination_prefix + relative)) { if (!m_exclude_files.contains(relative) && !JlCompress::compressFile(&m_output, absolute, m_destination_prefix + relative)) {
emitFailed(tr("Could not read and compress %1").arg(relative)); return ZipResult(tr("Could not read and compress %1").arg(relative));
return;
} }
} }
m_output.close(); m_output.close();
if (m_output.getZipError() != 0) { if (m_output.getZipError() != 0) {
emitFailed(tr("A zip error occurred")); return ZipResult(tr("A zip error occurred"));
return;
} }
emitSucceeded(); return ZipResult();
} }
void ExportToZipTask::emitAborted() void ExportToZipTask::finish()
{ {
QFile::remove(m_output_path); if (m_build_zip_future.isCanceled()) {
Task::emitAborted(); QFile::remove(m_output_path);
emitAborted();
} else if (auto result = m_build_zip_future.result(); result.has_value()) {
QFile::remove(m_output_path);
emitFailed(result.value());
} else {
emitSucceeded();
}
} }
void ExportToZipTask::emitFailed(QString reason)
bool ExportToZipTask::abort()
{ {
QFile::remove(m_output_path); if (m_build_zip_future.isRunning()) {
Task::emitFailed(reason); m_build_zip_future.cancel();
// NOTE: Here we don't do `emitAborted()` because it will be done when `m_build_zip_future` actually cancels, which may not occur
// immediately.
return true;
}
return false;
} }
} // namespace MMCZip } // namespace MMCZip

View File

@ -40,6 +40,8 @@
#include <quazip/JlCompress.h> #include <quazip/JlCompress.h>
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QFuture>
#include <QFutureWatcher>
#include <QHash> #include <QHash>
#include <QSet> #include <QSet>
#include <QString> #include <QString>
@ -165,13 +167,14 @@ class ExportToZipTask : public Task {
void setExcludeFiles(QStringList excludeFiles) { m_exclude_files = excludeFiles; } void setExcludeFiles(QStringList excludeFiles) { m_exclude_files = excludeFiles; }
void addExtraFile(QString fileName, QByteArray data) { m_extra_files.insert(fileName, data); } void addExtraFile(QString fileName, QByteArray data) { m_extra_files.insert(fileName, data); }
typedef std::optional<QString> ZipResult;
protected: protected:
virtual void executeTask() override; virtual void executeTask() override;
void exportZip(); bool abort() override;
protected slots: ZipResult exportZip();
virtual void emitAborted() override; void finish();
virtual void emitFailed(QString reason = "") override;
private: private:
QString m_output_path; QString m_output_path;
@ -182,5 +185,8 @@ class ExportToZipTask : public Task {
bool m_follow_symlinks; bool m_follow_symlinks;
QStringList m_exclude_files; QStringList m_exclude_files;
QHash<QString, QByteArray> m_extra_files; QHash<QString, QByteArray> m_extra_files;
QFuture<ZipResult> m_build_zip_future;
QFutureWatcher<ZipResult> m_build_zip_watcher;
}; };
} // namespace MMCZip } // namespace MMCZip