From f6c9d13b094d94845ed5e456a86fdfd71665b1da Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sat, 3 Feb 2024 09:05:22 -0500 Subject: [PATCH] karaku: Add feature to download definitions from Godbert --- CMakeLists.txt | 4 +- karuku/CMakeLists.txt | 4 +- karuku/include/mainwindow.h | 6 +++ karuku/src/mainwindow.cpp | 76 +++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 08f2565..b7f8f32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,8 +31,8 @@ ecm_setup_version(${PROJECT_VERSION} VERSION_HEADER ${CMAKE_CURRENT_BINARY_DIR}/novus-version.h ) -find_package(Qt6 ${QT_MIN_VERSION} COMPONENTS Core Widgets Concurrent Core5Compat Sql HttpServer CONFIG REQUIRED) -find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS CoreAddons Config XmlGui) +find_package(Qt6 ${QT_MIN_VERSION} COMPONENTS Core Widgets Concurrent Core5Compat Sql HttpServer Network CONFIG REQUIRED) +find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS CoreAddons Config XmlGui Archive) find_package(Vulkan REQUIRED) find_package(glm REQUIRED) if (NOT TARGET glm::glm) diff --git a/karuku/CMakeLists.txt b/karuku/CMakeLists.txt index f2de2b4..4d1ebe0 100644 --- a/karuku/CMakeLists.txt +++ b/karuku/CMakeLists.txt @@ -15,12 +15,14 @@ target_include_directories(novus-karuku include) target_link_libraries(novus-karuku PRIVATE + KF6::Archive Novus::Common Novus::ExdPart Physis::Physis Physis::Logger Qt6::Core - Qt6::Widgets) + Qt6::Widgets + Qt6::Network) install(FILES zone.xiv.karaku.desktop DESTINATION ${KDE_INSTALL_APPDIR}) install(FILES zone.xiv.karaku.svg DESTINATION ${KDE_INSTALL_FULL_ICONDIR}/hicolor/scalable/apps) diff --git a/karuku/include/mainwindow.h b/karuku/include/mainwindow.h index 591f45f..9e215c0 100644 --- a/karuku/include/mainwindow.h +++ b/karuku/include/mainwindow.h @@ -3,6 +3,8 @@ #pragma once +#include + #include "novusmainwindow.h" struct GameData; @@ -14,6 +16,10 @@ class MainWindow : public NovusMainWindow public: explicit MainWindow(GameData *data); +protected: + void setupFileMenu(QMenu *menu) override; + private: GameData *data = nullptr; + QNetworkAccessManager *mgr = nullptr; }; \ No newline at end of file diff --git a/karuku/src/mainwindow.cpp b/karuku/src/mainwindow.cpp index 9838d28..4159491 100644 --- a/karuku/src/mainwindow.cpp +++ b/karuku/src/mainwindow.cpp @@ -3,11 +3,17 @@ #include "mainwindow.h" +#include #include #include +#include #include #include #include +#include +#include +#include +#include #include #include @@ -21,6 +27,8 @@ MainWindow::MainWindow(GameData *data) setMinimumSize(1280, 720); setupMenubar(); + mgr = new QNetworkAccessManager(this); + auto dummyWidget = new QWidget(); setCentralWidget(dummyWidget); @@ -44,4 +52,72 @@ MainWindow::MainWindow(GameData *data) }); } +void MainWindow::setupFileMenu(QMenu *menu) +{ + auto openList = menu->addAction(QStringLiteral("Import Definitions...")); + openList->setIcon(QIcon::fromTheme(QStringLiteral("document-open"))); + connect(openList, &QAction::triggered, [this] { + auto fileName = QFileDialog::getOpenFileName(nullptr, QStringLiteral("Open Path List"), QStringLiteral("~")); + }); + + auto downloadList = menu->addAction(QStringLiteral("Download Definitions...")); + downloadList->setIcon(QIcon::fromTheme(QStringLiteral("download-symbolic"))); + connect(downloadList, &QAction::triggered, [this] { + const int ret = + QMessageBox::information(this, + QStringLiteral("Download Confirmation"), + QStringLiteral("This will download the definitions from the SaintCoinach repository on GitHub.

Continue?"), + QMessageBox::Ok | QMessageBox::Cancel, + QMessageBox::Ok); + + if (ret != QMessageBox::Ok) { + return; + } + + QUrl url; + url.setScheme(QStringLiteral("https")); + url.setHost(QStringLiteral("github.com")); + url.setPath(QStringLiteral("/xivapi/SaintCoinach/releases/latest/download/Godbert.zip")); + + // TODO: Use Qcoro? + auto reply = mgr->get(QNetworkRequest(url)); + connect(reply, &QNetworkReply::finished, this, [this, reply] { + qInfo() << "Finished downloading definitions!"; + + QTemporaryDir tempDir; + + QFile file(tempDir.filePath(QStringLiteral("Godbert.zip"))); + file.open(QIODevice::WriteOnly); + file.write(reply->readAll()); + file.close(); + + KZip archive(tempDir.filePath(QStringLiteral("Godbert.zip"))); + if (!archive.open(QIODevice::ReadOnly)) { + // TODO: these should show as message boxes + qFatal() << "Failed to open Godbert zip!"; + return; + } + + const KArchiveDirectory *root = dynamic_cast(archive.directory()->entry(QStringLiteral("Definitions"))); + + const QDir dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + const QDir definitionsDir = dataDir.absoluteFilePath(QStringLiteral("definitions")); + + // delete old directory + if (definitionsDir.exists()) { + QDir().rmdir(definitionsDir.absolutePath()); + } + + QDir().mkpath(definitionsDir.absolutePath()); + + root->copyTo(definitionsDir.absolutePath(), true); + + archive.close(); + + QMessageBox::information(this, QStringLiteral("Definitions"), QStringLiteral("Successfully updated definitions!")); + }); + }); +} + #include "moc_mainwindow.cpp" \ No newline at end of file