2023-08-06 08:48:11 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-03-16 00:31:24 -04:00
|
|
|
#include "mainwindow.h"
|
|
|
|
|
2024-02-04 15:16:18 -05:00
|
|
|
#include <KLocalizedString>
|
2024-02-03 09:05:22 -05:00
|
|
|
#include <KZip>
|
2023-08-06 08:48:11 -04:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopServices>
|
2024-02-03 09:05:22 -05:00
|
|
|
#include <QFileDialog>
|
2022-04-11 10:22:41 -04:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QListWidget>
|
2023-07-08 10:33:12 -04:00
|
|
|
#include <QMenuBar>
|
2024-02-03 09:05:22 -05:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QNetworkReply>
|
2024-02-04 14:29:57 -05:00
|
|
|
#include <QSplitter>
|
2024-02-03 09:05:22 -05:00
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QTemporaryDir>
|
2023-07-08 10:33:12 -04:00
|
|
|
#include <QUrl>
|
2023-08-06 08:48:11 -04:00
|
|
|
#include <physis.hpp>
|
2022-03-16 00:31:24 -04:00
|
|
|
|
2023-04-09 15:28:32 -04:00
|
|
|
#include "exdpart.h"
|
2024-02-02 14:28:49 -05:00
|
|
|
#include "sheetlistwidget.h"
|
2022-03-16 00:31:24 -04:00
|
|
|
|
2023-10-10 18:02:13 -04:00
|
|
|
MainWindow::MainWindow(GameData *data)
|
2023-10-10 18:11:40 -04:00
|
|
|
: NovusMainWindow()
|
|
|
|
, data(data)
|
2023-10-10 18:02:13 -04:00
|
|
|
{
|
2023-09-23 14:09:09 -04:00
|
|
|
setMinimumSize(1280, 720);
|
2023-10-10 18:21:06 -04:00
|
|
|
setupMenubar();
|
2022-03-16 00:31:24 -04:00
|
|
|
|
2024-02-03 09:05:22 -05:00
|
|
|
mgr = new QNetworkAccessManager(this);
|
|
|
|
|
2024-02-04 14:29:57 -05:00
|
|
|
auto dummyWidget = new QSplitter();
|
2024-02-04 14:58:21 -05:00
|
|
|
dummyWidget->setChildrenCollapsible(false);
|
2022-03-16 00:31:24 -04:00
|
|
|
setCentralWidget(dummyWidget);
|
|
|
|
|
2024-02-02 14:28:49 -05:00
|
|
|
auto listWidget = new SheetListWidget(data);
|
2022-04-16 16:19:22 -04:00
|
|
|
listWidget->setMaximumWidth(200);
|
2024-02-04 14:29:57 -05:00
|
|
|
dummyWidget->addWidget(listWidget);
|
2022-04-16 16:19:22 -04:00
|
|
|
|
2023-04-09 15:28:32 -04:00
|
|
|
auto exdPart = new EXDPart(data);
|
2024-02-04 14:29:57 -05:00
|
|
|
dummyWidget->addWidget(exdPart);
|
2022-03-16 00:31:24 -04:00
|
|
|
|
2024-02-02 14:28:49 -05:00
|
|
|
connect(listWidget, &SheetListWidget::sheetSelected, this, [data, exdPart](const QString &name) {
|
2024-02-03 09:18:10 -05:00
|
|
|
QString definitionPath;
|
|
|
|
|
|
|
|
const QDir dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
const QDir definitionsDir = dataDir.absoluteFilePath(QStringLiteral("definitions"));
|
|
|
|
|
2024-02-02 14:28:49 -05:00
|
|
|
auto path = QStringLiteral("exd/%1.exh").arg(name.toLower());
|
2023-10-12 21:33:09 -04:00
|
|
|
auto pathStd = path.toStdString();
|
|
|
|
|
|
|
|
auto file = physis_gamedata_extract_file(data, pathStd.c_str());
|
|
|
|
|
2024-02-03 09:18:10 -05:00
|
|
|
exdPart->loadSheet(name, file, definitionsDir.absoluteFilePath(QStringLiteral("%1.json").arg(name)));
|
2022-04-11 10:22:41 -04:00
|
|
|
});
|
2023-10-12 23:44:59 -04:00
|
|
|
}
|
|
|
|
|
2024-02-03 09:31:57 -05:00
|
|
|
static bool copyDirectory(const QString &srcFilePath, const QString &tgtFilePath)
|
|
|
|
{
|
|
|
|
QFileInfo srcFileInfo(srcFilePath);
|
|
|
|
if (srcFileInfo.isDir()) {
|
|
|
|
const QDir targetDir(tgtFilePath);
|
|
|
|
const QDir sourceDir(srcFilePath);
|
|
|
|
|
|
|
|
const QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
|
|
|
|
for (const QString &fileName : fileNames) {
|
|
|
|
const QString newSrcFilePath = srcFilePath + QLatin1Char('/') + fileName;
|
|
|
|
const QString newTgtFilePath = tgtFilePath + QLatin1Char('/') + fileName;
|
|
|
|
|
|
|
|
if (!QFile::copy(newSrcFilePath, newTgtFilePath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-03 09:05:22 -05:00
|
|
|
void MainWindow::setupFileMenu(QMenu *menu)
|
|
|
|
{
|
2024-02-04 15:16:18 -05:00
|
|
|
auto openList = menu->addAction(i18nc("@action:inmenu", "Import Definitions..."));
|
2024-02-03 09:05:22 -05:00
|
|
|
openList->setIcon(QIcon::fromTheme(QStringLiteral("document-open")));
|
|
|
|
connect(openList, &QAction::triggered, [this] {
|
2024-02-04 15:16:18 -05:00
|
|
|
auto fileName = QFileDialog::getExistingDirectory(nullptr, i18nc("@title:window", "Open Defintions Directory"), QStringLiteral("~"));
|
2024-02-03 09:31:57 -05:00
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
copyDirectory(fileName, definitionsDir.absolutePath());
|
|
|
|
|
2024-02-04 15:16:18 -05:00
|
|
|
QMessageBox::information(this, i18nc("@title:window", "Definitions"), i18n("Successfully imported definitions!"));
|
2024-02-03 09:05:22 -05:00
|
|
|
});
|
|
|
|
|
2024-02-04 15:16:18 -05:00
|
|
|
auto downloadList = menu->addAction(i18nc("@action:inmenu", "Download Definitions..."));
|
2024-02-03 09:05:22 -05:00
|
|
|
downloadList->setIcon(QIcon::fromTheme(QStringLiteral("download-symbolic")));
|
|
|
|
connect(downloadList, &QAction::triggered, [this] {
|
|
|
|
const int ret =
|
|
|
|
QMessageBox::information(this,
|
2024-02-04 15:16:18 -05:00
|
|
|
i18nc("@title:window", "Download Confirmation"),
|
|
|
|
i18n("This will download the definitions from the <a "
|
|
|
|
"href=\"https://github.com/xivapi/SaintCoinach\">SaintCoinach repository on GitHub</a>.<br><br>Continue?"),
|
2024-02-03 09:05:22 -05:00
|
|
|
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<const KArchiveDirectory *>(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();
|
|
|
|
|
2024-02-04 15:16:18 -05:00
|
|
|
QMessageBox::information(this, i18nc("@title:window", "Definitions"), i18n("Successfully downloaded and imported definitions!"));
|
2024-02-03 09:05:22 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:44:59 -04:00
|
|
|
#include "moc_mainwindow.cpp"
|