sneedmc/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp

69 lines
3.0 KiB
C++
Raw Normal View History

2022-01-14 08:43:42 +00:00
#include "ModrinthPage.h"
#include "ui_ModPage.h"
2022-01-14 08:43:42 +00:00
#include <QKeyEvent>
#include "Application.h"
#include "InstanceImportTask.h"
2022-02-25 16:43:27 +00:00
#include "Json.h"
2022-01-14 08:43:42 +00:00
#include "ModDownloadTask.h"
2022-02-25 16:43:27 +00:00
#include "ModrinthModel.h"
2022-01-15 07:51:47 +00:00
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
2022-02-25 16:43:27 +00:00
#include "ui/dialogs/ModDownloadDialog.h"
2022-01-14 08:43:42 +00:00
ModrinthPage::ModrinthPage(ModDownloadDialog* dialog, BaseInstance* instance)
: ModPage(dialog, instance)
{
listModel = new Modrinth::ListModel(this);
ui->packView->setModel(listModel);
// index is used to set the sorting with the modrinth api
ui->sortByBox->addItem(tr("Sort by Relevence"));
ui->sortByBox->addItem(tr("Sort by Downloads"));
ui->sortByBox->addItem(tr("Sort by Follows"));
ui->sortByBox->addItem(tr("Sort by last updated"));
ui->sortByBox->addItem(tr("Sort by newest"));
// sometimes Qt just ignores virtual slots and doesn't work as intended it seems,
// so it's best not to connect them in the parent's contructor...
connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch()));
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ModrinthPage::onSelectionChanged);
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &ModrinthPage::onVersionSelectionChanged);
connect(ui->modSelectionButton, &QPushButton::clicked, this, &ModrinthPage::onModSelected);
2022-01-14 08:43:42 +00:00
}
2022-02-25 16:43:27 +00:00
bool ModrinthPage::shouldDisplay() const { return true; }
2022-01-14 08:43:42 +00:00
void ModrinthPage::onModVersionSucceed(ModPage* instance, QByteArray* response, QString addonId)
{
if (addonId != current.addonId) { return; }
QJsonParseError parse_error;
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
2022-02-25 16:43:27 +00:00
<< " reason: " << parse_error.errorString();
qWarning() << *response;
2022-01-14 08:43:42 +00:00
return;
}
QJsonArray arr = doc.array();
try {
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) {
2022-02-25 16:43:27 +00:00
qDebug() << *response;
qWarning() << "Error while reading Modrinth mod version: " << e.cause();
}
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();
QString mcVersion = packProfile->getComponentVersion("net.minecraft");
QString loaderString = (packProfile->getComponentVersion("net.minecraftforge").isEmpty()) ? "fabric" : "forge";
for (int i = 0; i < current.versions.size(); i++) {
2022-02-25 16:43:27 +00:00
auto version = current.versions[i];
if (!version.mcVersion.contains(mcVersion) || !version.loaders.contains(loaderString)) { continue; }
2022-02-25 16:43:27 +00:00
ui->versionSelectionBox->addItem(version.version, QVariant(i));
2022-01-14 08:43:42 +00:00
}
if (ui->versionSelectionBox->count() == 0) { ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(-1)); }
2022-01-14 08:43:42 +00:00
ui->modSelectionButton->setText(tr("Cannot select invalid version :("));
2022-02-25 16:43:27 +00:00
updateSelectionButton();
2022-01-14 08:43:42 +00:00
}