From d60cdef63745f5d4c0152e9301380db681deab93 Mon Sep 17 00:00:00 2001 From: Digital Artifex <7929434+DigitalArtifex@users.noreply.github.com> Date: Thu, 20 Aug 2026 05:09:44 -0400 Subject: [PATCH] Started to add pack download --- KomplexHubContent/pages/FeaturedPacksPage.qml | 1 + KomplexHubContent/views/PackView.qml | 2 + KomplexHubPlugin/downloadmanager.cpp | 63 ++++++--- KomplexHubPlugin/downloadmanager.h | 3 +- KomplexHubPlugin/packcompiler.cpp | 123 ++++++++++++++---- KomplexHubPlugin/packcompiler.h | 4 +- 6 files changed, 151 insertions(+), 45 deletions(-) diff --git a/KomplexHubContent/pages/FeaturedPacksPage.qml b/KomplexHubContent/pages/FeaturedPacksPage.qml index f7c3839..3fd63d7 100644 --- a/KomplexHubContent/pages/FeaturedPacksPage.qml +++ b/KomplexHubContent/pages/FeaturedPacksPage.qml @@ -136,6 +136,7 @@ Rectangle viewMoreWallpaperPopup.authorId = packsModel.data(packsModel.index(index,0), NewestPacksModel.AuthorIdRole) viewMoreWallpaperPopup.description = packsModel.data(packsModel.index(index,0), NewestPacksModel.DescriptionRole) viewMoreWallpaperPopup.thumbnail = packsModel.data(packsModel.index(index,0), NewestPacksModel.ThumbnailRole) + viewMoreWallpaperPopup.uuid = packsModel.data(packsModel.index(index,0), NewestPacksModel.UuidRole) viewMoreWallpaperPopup.opacity = 1 popupContainer.opacity = 1 resultsLayout.opacity = 0 diff --git a/KomplexHubContent/views/PackView.qml b/KomplexHubContent/views/PackView.qml index 8b4fe36..7248ad4 100644 --- a/KomplexHubContent/views/PackView.qml +++ b/KomplexHubContent/views/PackView.qml @@ -304,4 +304,6 @@ Item { downloadManager.downloadPack(uuid) } + + Component.onCompleted: () => downloadManager.reset() } diff --git a/KomplexHubPlugin/downloadmanager.cpp b/KomplexHubPlugin/downloadmanager.cpp index 1bf383c..343383e 100644 --- a/KomplexHubPlugin/downloadmanager.cpp +++ b/KomplexHubPlugin/downloadmanager.cpp @@ -350,8 +350,9 @@ auto DownloadManager::downloadPack(const QString &id) -> void QNetworkRequest request(downloadUrl); request.setRawHeader(QByteArray("uuid"), id.toUtf8()); + QString tempFile(QString("%1.tar.gz").arg(id)); - QFuture downloadUri = download(request, id, Post); + QFuture downloadUri = download(request, id, Post, tempFile); downloadUri .then @@ -378,31 +379,35 @@ auto DownloadManager::downloadPack(const QString &id) -> void ) .onCanceled ( - [this]() + [this, tempFile]() { + QFile::remove(QStringLiteral("/tmp/") + tempFile); reset(); } ) .onFailed ( - [this] (const NetworkException &e) + [this, tempFile] (const NetworkException &e) { + QFile::remove(QStringLiteral("/tmp/") + tempFile); reset(); setError(QStringLiteral("Network Exception %1").arg(QString::number(e.errorCode)), e.message); } ) .onFailed ( - [this] (const FileException &e) + [this, tempFile] (const FileException &e) { + QFile::remove(QStringLiteral("/tmp/") + tempFile); reset(); setError(QStringLiteral("File Exception %1").arg(QString::number(e.errorCode)), e.message); } ) .onFailed ( - [this] () + [this, tempFile] () { + QFile::remove(QStringLiteral("/tmp/") + tempFile); reset(); setError(QStringLiteral("Unknown Exception"), QString()); } @@ -503,7 +508,7 @@ auto DownloadManager::install(const QUrl &uri) noexcept(false) -> QUrl QDir tempDirectory(uri.toLocalFile()); - QUrl installLocation = QStringLiteral("%1/.local/share/komplex/packs/%2").arg + QUrl installLocation = QStringLiteral("file://%1/.local/share/komplex/packs/%2").arg ( QStandardPaths::writableLocation(QStandardPaths::HomeLocation), tempDirectory.dirName() @@ -537,24 +542,28 @@ auto DownloadManager::install(const QUrl &uri) noexcept(false) -> QUrl return installLocation; } -auto DownloadManager::download(const QNetworkRequest &request, const QString &id, RequestType type) -> QFuture +auto DownloadManager::download(const QNetworkRequest &request, const QString &id, RequestType type, const QString &filename) -> QFuture { return QtConcurrent::run ( - [this, request, id, type]() -> QUrl + [this, request, id, type, _filename = filename]() -> QUrl { QEventLoop loop; - QString filename = id; + QString filename = _filename; - if(filename.isEmpty()) + if(filename.isEmpty() && !request.url().fileName().isEmpty()) { - filename = QUuid::createUuidV7().toString(); + filename = request.url().fileName(); + } + else if(filename.isEmpty()) + { + filename = id; } QUrl downloadUri = QStringLiteral("file://%1/%2").arg ( QStandardPaths::writableLocation(QStandardPaths::TempLocation), - request.url().fileName() + filename ); auto manager = CoreServices::networkAccessManager().toStrongRef(); @@ -578,13 +587,13 @@ auto DownloadManager::download(const QNetworkRequest &request, const QString &id switch(type) { - case Post: - reply = manager->post(request, nullptr); - break; - default: - case Get: - reply = manager->get(request, nullptr); - break; + case Post: + reply = manager->post(request, nullptr); + break; + default: + case Get: + reply = manager->get(request, nullptr); + break; } QObject::connect @@ -624,8 +633,9 @@ auto DownloadManager::download(const QNetworkRequest &request, const QString &id reply, &QNetworkReply::readyRead, this, - [this, downloadUri, reply]() + [this, downloadUri/*, reply, &downloadFile*/]() { + QMutexLocker locker(&m_fileMutex); QFile downloadFile(downloadUri.toLocalFile()); if(!downloadFile.open(QFile::ReadWrite | QFile::Append)) @@ -633,8 +643,19 @@ auto DownloadManager::download(const QNetworkRequest &request, const QString &id throw FileException(QStringLiteral("Could not open temp file location")); } + QNetworkReply *reply = qobject_cast(sender()); + + if(reply == nullptr) + { + throw NetworkException + { + QStringLiteral("Reply is not a qobject") + }; + } + quint64 bytes = reply->bytesAvailable(); - quint64 bytesWritten = downloadFile.write(reply->readAll()/*bytes)*/); + QByteArray data = reply->readAll(); + quint64 bytesWritten = downloadFile.write(data/*bytes)*/); downloadFile.close(); } diff --git a/KomplexHubPlugin/downloadmanager.h b/KomplexHubPlugin/downloadmanager.h index 600b9f6..03dc16a 100644 --- a/KomplexHubPlugin/downloadmanager.h +++ b/KomplexHubPlugin/downloadmanager.h @@ -143,7 +143,7 @@ signals: private: auto compile(const QUrl &uri) noexcept(false) -> QUrl; auto install(const QUrl &uri) noexcept(false) -> QUrl; - auto download(const QNetworkRequest &request, const QString &id, RequestType type = Get) -> QFuture; + auto download(const QNetworkRequest &request, const QString &id, RequestType type = Get, const QString &filename = QString()) -> QFuture; auto readShaderToyEntry(const QUrl &uri) noexcept(false) -> ShaderToyEntry; QString m_lastInstalledFile; @@ -159,6 +159,7 @@ private: State m_state; QMutex m_downloadMutex; + QMutex m_fileMutex; PackCompiler *m_compiler = nullptr; diff --git a/KomplexHubPlugin/packcompiler.cpp b/KomplexHubPlugin/packcompiler.cpp index fd2b004..63cd8b4 100644 --- a/KomplexHubPlugin/packcompiler.cpp +++ b/KomplexHubPlugin/packcompiler.cpp @@ -60,29 +60,22 @@ auto PackCompiler::process(const QUrl &uri) -> QFuture setStatus(QStringLiteral("Compiling %1").arg(uri.fileName())); setState(Compiling); - if(!validateDirectory(uri)) - { - throw FileException - ( - QStringLiteral("Source directory is invalid") - ); - } + // this was a leftover from ThumbnailGenerator + // if(!validateDirectory(uri)) + // { + // throw FileException + // ( + // QStringLiteral("Source directory is invalid") + // ); + // } - QUrl buildUri - ( - QStringLiteral("file://%1/komplex/build/%2").arg - ( - QStandardPaths::writableLocation - ( - QStandardPaths::TempLocation - ), - uri.fileName() - ) - ); + QFileInfo info(uri.toLocalFile()); + + QUrl buildUri; try { - extract(uri, buildUri); + buildUri = extract(uri); prepareShaders(buildUri); compile(buildUri); } @@ -98,6 +91,10 @@ auto PackCompiler::process(const QUrl &uri) -> QFuture setError(QStringLiteral("Compiler Error"), e.message); throw e; } + catch (...) + { + removeDirectory(buildUri); + } return buildUri; } @@ -457,18 +454,89 @@ auto PackCompiler::validateDirectory(const QUrl &uri) -> bool ); } -auto PackCompiler::extract(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void +auto PackCompiler::createDirectory(const QUrl &uri) -> void +{ + if(!uri.isLocalFile() || !uri.isValid()) + { + throw FileException(QStringLiteral("Uri needs to be a local file"), 0); + } + + QStringList arguments = + { + QStringLiteral("-p"), + uri.toLocalFile() + }; + + QProcess *process = new QProcess(this); + + QObject::connect + ( + process, + &QProcess::readyReadStandardOutput, + this, + [this, process]() + { + QByteArray processData = process->readAllStandardOutput(); + setCompilerOutput(m_compilerOutput + processData); + } + ); + + QObject::connect + ( + process, + &QProcess::readyReadStandardError, + this, + [this, process]() + { + QByteArray processData = process->readAllStandardError(); + + if(!processData.isValidUtf8()) + { + qWarning() << QStringLiteral("Process output not valid UTF8 data"); + return; + } + + setCompilerOutput(m_compilerOutput + processData); + } + ); + + process->start(QStringLiteral("mkdir"), arguments); + + if(!process->waitForStarted(3000)) + { + process->deleteLater(); + throw FileException(QStringLiteral("Could not start preprocessor")); + } + + if(!process->waitForFinished()) + { + process->deleteLater(); + throw FileException(QStringLiteral("Preprocessor timeout")); + } + + if(process->exitCode() != 0) + { + process->deleteLater(); + throw ShaderCompilerException(m_compilerOutput); + } + + process->deleteLater(); +} + +auto PackCompiler::extract(const QUrl &sourceUri) noexcept(false) -> QUrl { if(!sourceUri.isLocalFile() || !sourceUri.isValid()) { throw FileException(QStringLiteral("Uri needs to be a local file"), 0); } + QFileInfo info(sourceUri.toLocalFile()); + QUrl outputUri ( - QStringLiteral("file://%1_proc").arg + QStringLiteral("file:///tmp/%1").arg ( - sourceUri.toLocalFile() + info.baseName() ) ); @@ -480,6 +548,13 @@ auto PackCompiler::extract(const QUrl &sourceUri, const QUrl &destinationUri) no outputUri.toLocalFile() }; + QDir outputDir(outputUri.toLocalFile()); + + if(!outputDir.exists()) + { + createDirectory(outputUri); + } + QProcess *process = new QProcess(this); QObject::connect @@ -534,6 +609,10 @@ auto PackCompiler::extract(const QUrl &sourceUri, const QUrl &destinationUri) no } process->deleteLater(); + + QFile::remove(sourceUri.toLocalFile()); + + return outputUri; } auto PackCompiler::copyFile(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void diff --git a/KomplexHubPlugin/packcompiler.h b/KomplexHubPlugin/packcompiler.h index cdb4f7c..e312dc2 100644 --- a/KomplexHubPlugin/packcompiler.h +++ b/KomplexHubPlugin/packcompiler.h @@ -112,7 +112,9 @@ protected: */ auto validateDirectory(const QUrl &uri) -> bool; - auto extract(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void; + auto createDirectory(const QUrl &uri) -> void; + + auto extract(const QUrl &sourceUri) noexcept(false) -> QUrl; auto copyFile(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void; auto compile(const QUrl &uri) noexcept(false) -> void; auto preprocess(const QUrl &uri) noexcept(false) -> void;