2021-01-18 07:28:54 +00:00
|
|
|
/* Copyright 2013-2021 MultiMC Contributors
|
2020-06-07 15:46:12 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TechnicPage.h"
|
|
|
|
#include "ui_TechnicPage.h"
|
|
|
|
|
|
|
|
#include <QKeyEvent>
|
2021-11-22 02:55:16 +00:00
|
|
|
|
|
|
|
#include "ui/dialogs/NewInstanceDialog.h"
|
|
|
|
|
|
|
|
#include "TechnicModel.h"
|
2020-06-07 15:46:12 +00:00
|
|
|
#include "modplatform/technic/SingleZipPackInstallTask.h"
|
|
|
|
#include "modplatform/technic/SolderPackInstallTask.h"
|
|
|
|
#include "Json.h"
|
|
|
|
|
2021-11-22 02:55:16 +00:00
|
|
|
#include "Application.h"
|
|
|
|
|
2020-06-07 15:46:12 +00:00
|
|
|
TechnicPage::TechnicPage(NewInstanceDialog* dialog, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::TechnicPage), dialog(dialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
connect(ui->searchButton, &QPushButton::clicked, this, &TechnicPage::triggerSearch);
|
|
|
|
ui->searchEdit->installEventFilter(this);
|
|
|
|
model = new Technic::ListModel(this);
|
|
|
|
ui->packView->setModel(model);
|
|
|
|
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &TechnicPage::onSelectionChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TechnicPage::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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TechnicPage::~TechnicPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TechnicPage::shouldDisplay() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::openedImpl()
|
|
|
|
{
|
2021-06-19 14:19:39 +00:00
|
|
|
suggestCurrent();
|
|
|
|
triggerSearch();
|
2020-06-07 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::triggerSearch() {
|
|
|
|
model->searchWithTerm(ui->searchEdit->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|
|
|
{
|
|
|
|
if(!first.isValid())
|
|
|
|
{
|
|
|
|
if(isOpened)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
}
|
|
|
|
//ui->frame->clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = model->data(first, Qt::UserRole).value<Technic::Modpack>();
|
|
|
|
suggestCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechnicPage::suggestCurrent()
|
|
|
|
{
|
|
|
|
if (!isOpened)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (current.broken)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-19 14:19:39 +00:00
|
|
|
QString editedLogoName = "technic_" + current.logoName.section(".", 0, 0);
|
2020-06-07 15:46:12 +00:00
|
|
|
model->getLogo(current.logoName, current.logoUrl, [this, editedLogoName](QString logo)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedIconFromFile(logo, editedLogoName);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (current.metadataLoaded)
|
|
|
|
{
|
|
|
|
metadataLoaded();
|
2021-06-19 14:19:39 +00:00
|
|
|
return;
|
2020-06-07 15:46:12 +00:00
|
|
|
}
|
2021-06-19 14:19:39 +00:00
|
|
|
|
|
|
|
NetJob *netJob = new NetJob(QString("Technic::PackMeta(%1)").arg(current.name));
|
|
|
|
std::shared_ptr<QByteArray> response = std::make_shared<QByteArray>();
|
|
|
|
QString slug = current.slug;
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QString("https://api.technicpack.net/modpack/%1?build=multimc").arg(slug), response.get()));
|
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, slug]
|
2020-06-07 15:46:12 +00:00
|
|
|
{
|
2021-06-19 14:19:39 +00:00
|
|
|
if (current.slug != slug)
|
2020-06-07 15:46:12 +00:00
|
|
|
{
|
2021-06-19 14:19:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
QJsonObject obj = doc.object();
|
|
|
|
if(parse_error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
qWarning() << "Error while parsing JSON response from Technic at " << parse_error.offset << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!obj.contains("url"))
|
|
|
|
{
|
|
|
|
qWarning() << "Json doesn't contain an url key";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonValueRef url = obj["url"];
|
|
|
|
if (url.isString())
|
|
|
|
{
|
|
|
|
current.url = url.toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!obj.contains("solder"))
|
2020-06-07 15:46:12 +00:00
|
|
|
{
|
2021-06-19 14:19:39 +00:00
|
|
|
qWarning() << "Json doesn't contain a valid url or solder key";
|
2020-06-07 15:46:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-19 14:19:39 +00:00
|
|
|
QJsonValueRef solderUrl = obj["solder"];
|
|
|
|
if (solderUrl.isString())
|
2020-06-07 15:46:12 +00:00
|
|
|
{
|
2021-06-19 14:19:39 +00:00
|
|
|
current.url = solderUrl.toString();
|
|
|
|
current.isSolder = true;
|
2020-06-07 15:46:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-19 14:19:39 +00:00
|
|
|
qWarning() << "Json doesn't contain a valid url or solder key";
|
|
|
|
return;
|
2020-06-07 15:46:12 +00:00
|
|
|
}
|
2021-06-19 14:19:39 +00:00
|
|
|
}
|
2020-06-07 15:46:12 +00:00
|
|
|
|
2021-06-19 14:19:39 +00:00
|
|
|
current.minecraftVersion = Json::ensureString(obj, "minecraft", QString(), "__placeholder__");
|
|
|
|
current.websiteUrl = Json::ensureString(obj, "platformUrl", QString(), "__placeholder__");
|
|
|
|
current.author = Json::ensureString(obj, "user", QString(), "__placeholder__");
|
|
|
|
current.description = Json::ensureString(obj, "description", QString(), "__placeholder__");
|
|
|
|
current.metadataLoaded = true;
|
|
|
|
metadataLoaded();
|
|
|
|
});
|
2021-11-21 22:21:12 +00:00
|
|
|
netJob->start(APPLICATION->network());
|
2020-06-07 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// expects current.metadataLoaded to be true
|
|
|
|
void TechnicPage::metadataLoaded()
|
|
|
|
{
|
2020-11-02 21:28:07 +00:00
|
|
|
QString text = "";
|
2020-06-07 15:46:12 +00:00
|
|
|
QString name = current.name;
|
|
|
|
|
|
|
|
if (current.websiteUrl.isEmpty())
|
2020-11-02 21:28:07 +00:00
|
|
|
// This allows injecting HTML here.
|
2020-06-07 15:46:12 +00:00
|
|
|
text = name;
|
|
|
|
else
|
2020-11-02 21:28:07 +00:00
|
|
|
// URL not properly escaped for inclusion in HTML. The name allows for injecting HTML.
|
2020-06-07 15:46:12 +00:00
|
|
|
text = "<a href=\"" + current.websiteUrl + "\">" + name + "</a>";
|
2020-11-02 21:28:07 +00:00
|
|
|
if (!current.author.isEmpty()) {
|
|
|
|
// This allows injecting HTML here
|
|
|
|
text += tr(" by ") + current.author;
|
2020-06-07 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ui->frame->setModText(text);
|
2020-11-02 21:28:07 +00:00
|
|
|
ui->frame->setModDescription(current.description);
|
2020-06-07 15:46:12 +00:00
|
|
|
if (!current.isSolder)
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack(current.name, new Technic::SingleZipPackInstallTask(current.url, current.minecraftVersion));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (current.url.endsWith('/')) current.url.chop(1);
|
2021-11-21 22:21:12 +00:00
|
|
|
dialog->setSuggestedPack(current.name, new Technic::SolderPackInstallTask(APPLICATION->network(), current.url + "/modpack/" + current.slug, current.minecraftVersion));
|
2020-06-07 15:46:12 +00:00
|
|
|
}
|
|
|
|
}
|