2022-01-14 14:13:42 +05:30
|
|
|
#include "ModrinthPage.h"
|
|
|
|
#include "ui_ModrinthPage.h"
|
|
|
|
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
|
|
|
#include "Application.h"
|
|
|
|
#include "Json.h"
|
|
|
|
#include "ui/dialogs/ModDownloadDialog.h"
|
|
|
|
#include "InstanceImportTask.h"
|
|
|
|
#include "ModrinthModel.h"
|
|
|
|
#include "ModDownloadTask.h"
|
2022-01-15 13:21:47 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
2022-01-15 00:52:15 +05:30
|
|
|
#include "minecraft/PackProfile.h"
|
2022-01-14 14:13:42 +05:30
|
|
|
|
2022-01-15 00:52:15 +05:30
|
|
|
ModrinthPage::ModrinthPage(ModDownloadDialog *dialog, BaseInstance *instance)
|
2022-01-15 13:21:47 +05:30
|
|
|
: QWidget(dialog), m_instance(instance), ui(new Ui::ModrinthPage), dialog(dialog)
|
2022-01-14 14:13:42 +05:30
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
connect(ui->searchButton, &QPushButton::clicked, this, &ModrinthPage::triggerSearch);
|
|
|
|
ui->searchEdit->installEventFilter(this);
|
|
|
|
listModel = new Modrinth::ListModel(this);
|
|
|
|
ui->packView->setModel(listModel);
|
|
|
|
|
|
|
|
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
|
|
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
|
|
|
|
|
|
|
|
// 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"));
|
2022-01-31 21:48:11 +05:30
|
|
|
ui->sortByBox->addItem(tr("Sort by Follows"));
|
2022-01-14 14:13:42 +05:30
|
|
|
ui->sortByBox->addItem(tr("Sort by last updated"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by newest"));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
ModrinthPage::~ModrinthPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModrinthPage::eventFilter(QObject* watched, QEvent* event)
|
|
|
|
{
|
|
|
|
if (watched == ui->searchEdit && event->type() == QEvent::KeyPress) {
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
if (keyEvent->key() == Qt::Key_Return) {
|
|
|
|
triggerSearch();
|
|
|
|
keyEvent->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModrinthPage::shouldDisplay() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::openedImpl()
|
|
|
|
{
|
|
|
|
suggestCurrent();
|
|
|
|
triggerSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::triggerSearch()
|
|
|
|
{
|
|
|
|
listModel->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|
|
|
{
|
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
|
|
|
|
if(!first.isValid())
|
|
|
|
{
|
|
|
|
if(isOpened)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedMod();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = listModel->data(first, Qt::UserRole).value<Modrinth::IndexedPack>();
|
|
|
|
QString text = "";
|
|
|
|
QString name = current.name;
|
|
|
|
|
|
|
|
if (current.websiteUrl.isEmpty())
|
|
|
|
text = name;
|
|
|
|
else
|
|
|
|
text = "<a href=\"" + current.websiteUrl + "\">" + name + "</a>";
|
2022-01-24 11:53:01 +05:30
|
|
|
text += "<br>"+ tr(" by ") + "<a href=\""+current.author.url+"\">"+current.author.name+"</a><br><br>";
|
2022-01-14 14:13:42 +05:30
|
|
|
ui->packDescription->setHtml(text + current.description);
|
|
|
|
|
|
|
|
if (!current.versionsLoaded)
|
|
|
|
{
|
|
|
|
qDebug() << "Loading Modrinth mod versions";
|
2022-01-18 16:58:55 +05:30
|
|
|
auto netJob = new NetJob(QString("Modrinth::ModVersions(%1)").arg(current.name), APPLICATION->network());
|
2022-01-14 14:13:42 +05:30
|
|
|
std::shared_ptr<QByteArray> response = std::make_shared<QByteArray>();
|
|
|
|
QString addonId = current.addonId;
|
2022-01-31 21:48:11 +05:30
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QString("https://api.modrinth.com/v2/project/%1/version").arg(addonId), response.get()));
|
2022-01-14 14:13:42 +05:30
|
|
|
|
2022-01-18 16:58:55 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, netJob]
|
2022-01-14 14:13:42 +05:30
|
|
|
{
|
2022-01-18 16:58:55 +05:30
|
|
|
netJob->deleteLater();
|
2022-01-14 14:13:42 +05:30
|
|
|
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 << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonArray arr = doc.array();
|
|
|
|
try
|
|
|
|
{
|
2022-01-15 14:55:24 +05:30
|
|
|
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
|
2022-01-14 14:13:42 +05:30
|
|
|
}
|
|
|
|
catch(const JSONValidationError &e)
|
|
|
|
{
|
|
|
|
qDebug() << *response;
|
|
|
|
qWarning() << "Error while reading Modrinth mod version: " << e.cause();
|
|
|
|
}
|
2022-01-15 13:21:47 +05:30
|
|
|
auto packProfile = ((MinecraftInstance *)m_instance)->getPackProfile();
|
|
|
|
QString mcVersion = packProfile->getComponentVersion("net.minecraft");
|
|
|
|
QString loaderString = (packProfile->getComponentVersion("net.minecraftforge").isEmpty()) ? "fabric" : "forge";
|
|
|
|
for(const auto& version : current.versions) {
|
|
|
|
if(!version.mcVersion.contains(mcVersion) || !version.loaders.contains(loaderString)){
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-14 14:13:42 +05:30
|
|
|
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
|
|
|
|
}
|
2022-01-15 13:36:48 +05:30
|
|
|
if(ui->versionSelectionBox->count() == 0){
|
2022-01-15 14:55:24 +05:30
|
|
|
ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(""));
|
2022-01-15 13:36:48 +05:30
|
|
|
}
|
2022-01-14 14:13:42 +05:30
|
|
|
|
|
|
|
suggestCurrent();
|
|
|
|
});
|
|
|
|
netJob->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(auto version : current.versions) {
|
|
|
|
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
|
|
|
|
}
|
2022-01-15 13:36:48 +05:30
|
|
|
if(ui->versionSelectionBox->count() == 0){
|
2022-01-15 14:55:24 +05:30
|
|
|
ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(""));
|
2022-01-15 13:36:48 +05:30
|
|
|
}
|
2022-01-14 14:13:42 +05:30
|
|
|
suggestCurrent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::suggestCurrent()
|
|
|
|
{
|
|
|
|
if(!isOpened)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedVersion.isEmpty())
|
|
|
|
{
|
|
|
|
dialog->setSuggestedMod();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:18 +05:30
|
|
|
dialog->setSuggestedMod(current.name, new ModDownloadTask(selectedVersion, current.versions.at(0).fileName ,dialog->mods));
|
2022-01-14 14:13:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::onVersionSelectionChanged(QString data)
|
|
|
|
{
|
|
|
|
if(data.isNull() || data.isEmpty())
|
|
|
|
{
|
|
|
|
selectedVersion = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedVersion = ui->versionSelectionBox->currentData().toString();
|
|
|
|
suggestCurrent();
|
|
|
|
}
|