2022-03-07 19:46:08 +00:00
|
|
|
#include "NetworkModAPI.h"
|
|
|
|
|
|
|
|
#include "ui/pages/modplatform/ModModel.h"
|
|
|
|
|
|
|
|
#include "Application.h"
|
|
|
|
#include "net/NetJob.h"
|
|
|
|
|
|
|
|
void NetworkModAPI::searchMods(CallerType* caller, SearchArgs&& args) const
|
|
|
|
{
|
2022-03-08 14:12:35 +00:00
|
|
|
auto netJob = new NetJob(QString("%1::Search").arg(caller->debugName()), APPLICATION->network());
|
2022-03-07 19:46:08 +00:00
|
|
|
auto searchUrl = getModSearchURL(args);
|
|
|
|
|
|
|
|
auto response = new QByteArray();
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
|
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::started, caller, [caller, netJob] { caller->setActiveJob(netJob); });
|
|
|
|
QObject::connect(netJob, &NetJob::failed, caller, &CallerType::searchRequestFailed);
|
|
|
|
QObject::connect(netJob, &NetJob::succeeded, caller, [caller, response] {
|
2022-03-08 14:12:35 +00:00
|
|
|
QJsonParseError parse_error{};
|
2022-03-07 19:46:08 +00:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from " << caller->debugName() << " at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
caller->searchRequestFinished(doc);
|
|
|
|
});
|
|
|
|
|
|
|
|
netJob->start();
|
2022-03-07 22:55:20 +00:00
|
|
|
}
|
2022-03-07 19:46:08 +00:00
|
|
|
|
2022-05-24 13:38:48 +01:00
|
|
|
void NetworkModAPI::getModInfo(CallerType* caller, ModPlatform::IndexedPack& pack)
|
|
|
|
{
|
|
|
|
auto response = new QByteArray();
|
2022-06-19 18:29:21 +01:00
|
|
|
auto job = getProject(pack.addonId.toString(), response);
|
2022-05-24 13:38:48 +01:00
|
|
|
|
2022-06-28 11:09:58 +01:00
|
|
|
QObject::connect(job, &NetJob::succeeded, caller, [caller, &pack, response] {
|
2022-05-24 13:38:48 +01:00
|
|
|
QJsonParseError parse_error{};
|
2022-06-19 18:29:21 +01:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
2022-05-24 13:38:48 +01:00
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
2022-06-19 18:29:21 +01:00
|
|
|
qWarning() << "Error while parsing JSON response from " << caller->debugName() << " at " << parse_error.offset
|
2022-05-24 13:38:48 +01:00
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
caller->infoRequestFinished(doc, pack);
|
|
|
|
});
|
|
|
|
|
2022-06-19 18:29:21 +01:00
|
|
|
job->start();
|
2022-05-24 13:38:48 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 21:39:53 +00:00
|
|
|
void NetworkModAPI::getVersions(CallerType* caller, VersionSearchArgs&& args) const
|
2022-03-07 19:46:08 +00:00
|
|
|
{
|
2022-03-24 21:39:53 +00:00
|
|
|
auto netJob = new NetJob(QString("%1::ModVersions(%2)").arg(caller->debugName()).arg(args.addonId), APPLICATION->network());
|
2022-03-07 19:46:08 +00:00
|
|
|
auto response = new QByteArray();
|
|
|
|
|
2022-03-24 21:39:53 +00:00
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(getVersionsURL(args), response));
|
2022-03-07 19:46:08 +00:00
|
|
|
|
2022-03-24 21:39:53 +00:00
|
|
|
QObject::connect(netJob, &NetJob::succeeded, caller, [response, caller, args] {
|
2022-03-08 14:12:35 +00:00
|
|
|
QJsonParseError parse_error{};
|
2022-03-07 19:46:08 +00:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from " << caller->debugName() << " at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-24 21:39:53 +00:00
|
|
|
caller->versionRequestSucceeded(doc, args.addonId);
|
2022-03-07 19:46:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::finished, caller, [response, netJob] {
|
|
|
|
netJob->deleteLater();
|
|
|
|
delete response;
|
|
|
|
});
|
|
|
|
|
|
|
|
netJob->start();
|
2022-03-07 22:55:20 +00:00
|
|
|
}
|
2022-06-19 18:29:21 +01:00
|
|
|
|
2022-06-28 11:09:58 +01:00
|
|
|
auto NetworkModAPI::getProject(QString addonId, QByteArray* response) const -> NetJob*
|
2022-06-19 18:29:21 +01:00
|
|
|
{
|
|
|
|
auto netJob = new NetJob(QString("%1::GetProject").arg(addonId), APPLICATION->network());
|
|
|
|
auto searchUrl = getModInfoURL(addonId);
|
|
|
|
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
|
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::finished, [response, netJob] {
|
|
|
|
netJob->deleteLater();
|
|
|
|
delete response;
|
|
|
|
});
|
|
|
|
|
|
|
|
return netJob;
|
|
|
|
}
|