2022-03-08 01:16:08 +05:30
|
|
|
#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 19:42:35 +05:30
|
|
|
auto netJob = new NetJob(QString("%1::Search").arg(caller->debugName()), APPLICATION->network());
|
2022-03-08 01:16:08 +05:30
|
|
|
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);
|
2022-10-24 17:37:41 +05:30
|
|
|
QObject::connect(netJob, &NetJob::aborted, caller, &CallerType::searchRequestAborted);
|
2022-03-08 01:16:08 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, caller, [caller, response] {
|
2022-03-08 19:42:35 +05:30
|
|
|
QJsonParseError parse_error{};
|
2022-03-08 01:16:08 +05:30
|
|
|
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-08 04:25:20 +05:30
|
|
|
}
|
2022-03-08 01:16:08 +05:30
|
|
|
|
2022-07-19 03:45:02 +05:30
|
|
|
void NetworkModAPI::getModInfo(ModPlatform::IndexedPack& pack, std::function<void(QJsonDocument&, ModPlatform::IndexedPack&)> callback)
|
2022-05-24 18:08:48 +05:30
|
|
|
{
|
|
|
|
auto response = new QByteArray();
|
2022-06-19 22:59:21 +05:30
|
|
|
auto job = getProject(pack.addonId.toString(), response);
|
2022-05-24 18:08:48 +05:30
|
|
|
|
2022-07-19 03:45:02 +05:30
|
|
|
QObject::connect(job, &NetJob::succeeded, [callback, &pack, response] {
|
2022-05-24 18:08:48 +05:30
|
|
|
QJsonParseError parse_error{};
|
2022-06-19 22:59:21 +05:30
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
2022-05-24 18:08:48 +05:30
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
2022-07-19 03:45:02 +05:30
|
|
|
qWarning() << "Error while parsing JSON response for mod info at " << parse_error.offset
|
2022-05-24 18:08:48 +05:30
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-19 03:45:02 +05:30
|
|
|
callback(doc, pack);
|
2022-05-24 18:08:48 +05:30
|
|
|
});
|
|
|
|
|
2022-06-19 22:59:21 +05:30
|
|
|
job->start();
|
2022-05-24 18:08:48 +05:30
|
|
|
}
|
|
|
|
|
2022-07-19 03:52:31 +05:30
|
|
|
void NetworkModAPI::getVersions(VersionSearchArgs&& args, std::function<void(QJsonDocument&, QString)> callback) const
|
2022-03-08 01:16:08 +05:30
|
|
|
{
|
2022-07-19 03:52:31 +05:30
|
|
|
auto netJob = new NetJob(QString("ModVersions(%2)").arg(args.addonId), APPLICATION->network());
|
2022-03-08 01:16:08 +05:30
|
|
|
auto response = new QByteArray();
|
|
|
|
|
2022-03-25 03:09:53 +05:30
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(getVersionsURL(args), response));
|
2022-03-08 01:16:08 +05:30
|
|
|
|
2022-07-19 03:52:31 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, [response, callback, args] {
|
2022-03-08 19:42:35 +05:30
|
|
|
QJsonParseError parse_error{};
|
2022-03-08 01:16:08 +05:30
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
2022-07-19 03:52:31 +05:30
|
|
|
qWarning() << "Error while parsing JSON response for getting versions at " << parse_error.offset
|
2022-03-08 01:16:08 +05:30
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-19 03:52:31 +05:30
|
|
|
callback(doc, args.addonId);
|
2022-03-08 01:16:08 +05:30
|
|
|
});
|
|
|
|
|
2022-07-19 03:52:31 +05:30
|
|
|
QObject::connect(netJob, &NetJob::finished, [response, netJob] {
|
2022-03-08 01:16:08 +05:30
|
|
|
netJob->deleteLater();
|
|
|
|
delete response;
|
|
|
|
});
|
|
|
|
|
|
|
|
netJob->start();
|
2022-03-08 04:25:20 +05:30
|
|
|
}
|
2022-06-19 22:59:21 +05:30
|
|
|
|
2022-06-28 15:39:58 +05:30
|
|
|
auto NetworkModAPI::getProject(QString addonId, QByteArray* response) const -> NetJob*
|
2022-06-19 22:59:21 +05:30
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|