2021-12-18 00:07:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020-2021 Jamie Mansfield <jmansfield@cadixdev.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-04-05 21:19:05 +01:00
|
|
|
#include "AtlListModel.h"
|
2020-08-24 23:13:43 +01:00
|
|
|
|
|
|
|
#include <BuildConfig.h>
|
2021-11-20 15:22:22 +00:00
|
|
|
#include <Application.h>
|
2021-04-05 21:19:05 +01:00
|
|
|
#include <Json.h>
|
2020-08-24 23:13:43 +01:00
|
|
|
|
2023-06-02 00:39:04 +01:00
|
|
|
#include "net/ApiDownload.h"
|
|
|
|
|
2020-08-24 23:13:43 +01:00
|
|
|
namespace Atl {
|
|
|
|
|
|
|
|
ListModel::ListModel(QObject *parent) : QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ListModel::~ListModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int ListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2022-11-13 14:35:55 +00:00
|
|
|
return parent.isValid() ? 0 : modpacks.size();
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ListModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2022-11-13 14:35:55 +00:00
|
|
|
return parent.isValid() ? 0 : 1;
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
int pos = index.row();
|
|
|
|
if(pos >= modpacks.size() || pos < 0 || !index.isValid())
|
|
|
|
{
|
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATLauncher::IndexedPack pack = modpacks.at(pos);
|
|
|
|
if(role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
return pack.name;
|
|
|
|
}
|
|
|
|
else if (role == Qt::ToolTipRole)
|
|
|
|
{
|
2021-02-10 16:11:59 +00:00
|
|
|
return pack.name;
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
|
|
|
else if(role == Qt::DecorationRole)
|
|
|
|
{
|
|
|
|
if(m_logoMap.contains(pack.safeName))
|
|
|
|
{
|
|
|
|
return (m_logoMap.value(pack.safeName));
|
|
|
|
}
|
2021-11-20 15:22:22 +00:00
|
|
|
auto icon = APPLICATION->getThemedIcon("atlauncher-placeholder");
|
2020-08-24 23:13:43 +01:00
|
|
|
|
|
|
|
auto url = QString(BuildConfig.ATL_DOWNLOAD_SERVER_URL + "launcher/images/%1.png").arg(pack.safeName.toLower());
|
|
|
|
((ListModel *)this)->requestLogo(pack.safeName, url);
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
else if(role == Qt::UserRole)
|
|
|
|
{
|
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::request()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>("Atl::Request", APPLICATION->network());
|
2020-08-24 23:13:43 +01:00
|
|
|
auto url = QString(BuildConfig.ATL_DOWNLOAD_SERVER_URL + "launcher/json/packsnew.json");
|
2023-06-02 00:39:04 +01:00
|
|
|
netJob->addNetAction(Net::ApiDownload::makeByteArray(QUrl(url), &response));
|
2020-08-24 23:13:43 +01:00
|
|
|
jobPtr = netJob;
|
2021-12-31 04:27:59 +00:00
|
|
|
jobPtr->start();
|
2020-08-24 23:13:43 +01:00
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::succeeded, this, &ListModel::requestFinished);
|
|
|
|
QObject::connect(netJob.get(), &NetJob::failed, this, &ListModel::requestFailed);
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::requestFinished()
|
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
|
|
|
|
if(parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from ATL at " << parse_error.offset << " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<ATLauncher::IndexedPack> newList;
|
|
|
|
|
|
|
|
auto packs = doc.array();
|
|
|
|
for(auto packRaw : packs) {
|
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
|
|
|
ATLauncher::IndexedPack pack;
|
2021-04-05 21:19:05 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
ATLauncher::loadIndexedPack(pack, packObj);
|
|
|
|
}
|
|
|
|
catch (const JSONValidationError &e) {
|
|
|
|
qDebug() << QString::fromUtf8(response);
|
|
|
|
qWarning() << "Error while reading pack manifest from ATLauncher: " << e.cause();
|
|
|
|
return;
|
|
|
|
}
|
2020-08-24 23:13:43 +01:00
|
|
|
|
|
|
|
// ignore packs without a published version
|
|
|
|
if(pack.versions.length() == 0) continue;
|
|
|
|
// only display public packs (for now)
|
|
|
|
if(pack.type != ATLauncher::PackType::Public) continue;
|
|
|
|
// ignore "system" packs (Vanilla, Vanilla with Forge, etc)
|
|
|
|
if(pack.system) continue;
|
|
|
|
|
|
|
|
newList.append(pack);
|
|
|
|
}
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::requestFailed(QString reason)
|
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback)
|
|
|
|
{
|
|
|
|
if(m_logoMap.contains(logo))
|
|
|
|
{
|
2021-11-21 22:21:12 +00:00
|
|
|
callback(APPLICATION->metacache()->resolveEntry("ATLauncherPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
requestLogo(logo, logoUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::logoFailed(QString logo)
|
|
|
|
{
|
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::logoLoaded(QString logo, QIcon out)
|
|
|
|
{
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
m_logoMap.insert(logo, out);
|
|
|
|
|
|
|
|
for(int i = 0; i < modpacks.size(); i++) {
|
|
|
|
if(modpacks[i].safeName == logo) {
|
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 0), {Qt::DecorationRole});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::requestLogo(QString file, QString url)
|
|
|
|
{
|
|
|
|
if(m_loadingLogos.contains(file) || m_failedLogos.contains(file))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-21 22:21:12 +00:00
|
|
|
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("ATLauncherPacks", QString("logos/%1").arg(file.section(".", 0, 0)));
|
2021-12-31 04:27:59 +00:00
|
|
|
NetJob *job = new NetJob(QString("ATLauncher Icon Download %1").arg(file), APPLICATION->network());
|
2023-06-02 00:39:04 +01:00
|
|
|
job->addNetAction(Net::ApiDownload::makeCached(QUrl(url), entry));
|
2020-08-24 23:13:43 +01:00
|
|
|
|
|
|
|
auto fullPath = entry->getFullPath();
|
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, file, fullPath]
|
|
|
|
{
|
|
|
|
emit logoLoaded(file, QIcon(fullPath));
|
|
|
|
if(waitingCallbacks.contains(file))
|
|
|
|
{
|
|
|
|
waitingCallbacks.value(file)(fullPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, file]
|
|
|
|
{
|
|
|
|
emit logoFailed(file);
|
|
|
|
});
|
|
|
|
|
2021-12-31 04:27:59 +00:00
|
|
|
job->start();
|
2020-08-24 23:13:43 +01:00
|
|
|
|
|
|
|
m_loadingLogos.append(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|