2021-03-23 23:59:43 +00:00
|
|
|
#include "FlamePage.h"
|
|
|
|
#include "ui_FlamePage.h"
|
2020-03-31 02:13:19 +01:00
|
|
|
|
2021-10-13 00:59:25 +01:00
|
|
|
#include "Launcher.h"
|
2021-03-26 22:15:20 +00:00
|
|
|
#include <Json.h>
|
2020-03-31 02:13:19 +01:00
|
|
|
#include "dialogs/NewInstanceDialog.h"
|
|
|
|
#include <InstanceImportTask.h>
|
2021-03-23 23:59:43 +00:00
|
|
|
#include "FlameModel.h"
|
2020-03-31 02:13:19 +01:00
|
|
|
#include <QKeyEvent>
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
FlamePage::FlamePage(NewInstanceDialog* dialog, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::FlamePage), dialog(dialog)
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2021-03-23 23:59:43 +00:00
|
|
|
connect(ui->searchButton, &QPushButton::clicked, this, &FlamePage::triggerSearch);
|
2020-03-31 02:13:19 +01:00
|
|
|
ui->searchEdit->installEventFilter(this);
|
2021-03-26 22:15:20 +00:00
|
|
|
listModel = new Flame::ListModel(this);
|
|
|
|
ui->packView->setModel(listModel);
|
|
|
|
|
2021-06-20 21:55:09 +01:00
|
|
|
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
|
|
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
|
2021-06-18 17:31:14 +01:00
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
// index is used to set the sorting with the curseforge api
|
|
|
|
ui->sortByBox->addItem(tr("Sort by featured"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by popularity"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by last updated"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by name"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by author"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by total downloads"));
|
|
|
|
|
2021-04-08 21:28:55 +01:00
|
|
|
connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch()));
|
2021-03-23 23:59:43 +00:00
|
|
|
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FlamePage::onSelectionChanged);
|
2021-03-26 22:15:20 +00:00
|
|
|
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FlamePage::onVersionSelectionChanged);
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
FlamePage::~FlamePage()
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
bool FlamePage::eventFilter(QObject* watched, QEvent* event)
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
bool FlamePage::shouldDisplay() const
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
void FlamePage::openedImpl()
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
suggestCurrent();
|
2021-03-26 22:15:20 +00:00
|
|
|
triggerSearch();
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
void FlamePage::triggerSearch()
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
2021-03-26 22:15:20 +00:00
|
|
|
listModel->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex());
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
void FlamePage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
2021-03-26 22:15:20 +00:00
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
|
2020-03-31 02:13:19 +01:00
|
|
|
if(!first.isValid())
|
|
|
|
{
|
|
|
|
if(isOpened)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-04-29 20:17:51 +01:00
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
current = listModel->data(first, Qt::UserRole).value<Flame::IndexedPack>();
|
2020-04-29 20:17:51 +01:00
|
|
|
QString text = "";
|
|
|
|
QString name = current.name;
|
|
|
|
|
|
|
|
if (current.websiteUrl.isEmpty())
|
|
|
|
text = name;
|
|
|
|
else
|
|
|
|
text = "<a href=\"" + current.websiteUrl + "\">" + name + "</a>";
|
|
|
|
if (!current.authors.empty()) {
|
2021-03-23 23:59:43 +00:00
|
|
|
auto authorToStr = [](Flame::ModpackAuthor & author) {
|
2020-04-29 20:17:51 +01:00
|
|
|
if(author.url.isEmpty()) {
|
|
|
|
return author.name;
|
|
|
|
}
|
|
|
|
return QString("<a href=\"%1\">%2</a>").arg(author.url, author.name);
|
|
|
|
};
|
|
|
|
QStringList authorStrs;
|
|
|
|
for(auto & author: current.authors) {
|
|
|
|
authorStrs.push_back(authorToStr(author));
|
|
|
|
}
|
2021-03-26 22:15:20 +00:00
|
|
|
text += "<br>" + tr(" by ") + authorStrs.join(", ");
|
2020-04-29 20:17:51 +01:00
|
|
|
}
|
2021-03-26 22:15:20 +00:00
|
|
|
text += "<br><br>";
|
2020-04-29 20:17:51 +01:00
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
ui->packDescription->setHtml(text + current.description);
|
|
|
|
|
|
|
|
if (current.versionsLoaded == false)
|
|
|
|
{
|
|
|
|
qDebug() << "Loading flame modpack versions";
|
|
|
|
NetJob *netJob = new NetJob(QString("Flame::PackVersions(%1)").arg(current.name));
|
|
|
|
std::shared_ptr<QByteArray> response = std::make_shared<QByteArray>();
|
|
|
|
int addonId = current.addonId;
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QString("https://addons-ecs.forgesvc.net/api/v2/addon/%1/files").arg(addonId), response.get()));
|
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response]
|
|
|
|
{
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if(parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from CurseForge at " << parse_error.offset << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonArray arr = doc.array();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Flame::loadIndexedPackVersions(current, arr);
|
|
|
|
}
|
|
|
|
catch(const JSONValidationError &e)
|
|
|
|
{
|
|
|
|
qDebug() << *response;
|
|
|
|
qWarning() << "Error while reading flame modpack version: " << e.cause();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto version : current.versions) {
|
|
|
|
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestCurrent();
|
|
|
|
});
|
|
|
|
netJob->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(auto version : current.versions) {
|
|
|
|
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestCurrent();
|
|
|
|
}
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
void FlamePage::suggestCurrent()
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
if(!isOpened)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-19 15:19:39 +01:00
|
|
|
if (selectedVersion.isEmpty())
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
dialog->setSuggestedPack(current.name, new InstanceImportTask(selectedVersion));
|
2020-03-31 02:13:19 +01:00
|
|
|
QString editedLogoName;
|
2021-03-23 23:59:43 +00:00
|
|
|
editedLogoName = "curseforge_" + current.logoName.section(".", 0, 0);
|
2021-03-26 22:15:20 +00:00
|
|
|
listModel->getLogo(current.logoName, current.logoUrl, [this, editedLogoName](QString logo)
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
dialog->setSuggestedIconFromFile(logo, editedLogoName);
|
|
|
|
});
|
|
|
|
}
|
2021-03-26 22:15:20 +00:00
|
|
|
|
|
|
|
void FlamePage::onVersionSelectionChanged(QString data)
|
|
|
|
{
|
|
|
|
if(data.isNull() || data.isEmpty())
|
|
|
|
{
|
|
|
|
selectedVersion = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedVersion = ui->versionSelectionBox->currentData().toString();
|
|
|
|
suggestCurrent();
|
2021-04-08 21:28:55 +01:00
|
|
|
}
|