mirror of
https://github.com/YACReader/yacreader
synced 2025-07-18 21:14:33 -04:00
Replace QtScript with QJson*
QtScript is deprecated and this was needed to start supporting Qt6
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
#include "response_parser.h"
|
||||
|
||||
#include <QtScript>
|
||||
#include <QDebug>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
|
||||
ResponseParser::ResponseParser(QObject *parent)
|
||||
: QObject(parent), error(false), errorTxt("None"), numResults(-1), currentPage(-1), totalPages(-1)
|
||||
@ -46,32 +46,43 @@ bool ResponseParser::isError(qint32 error)
|
||||
|
||||
void ResponseParser::loadJSONResponse(const QString &response)
|
||||
{
|
||||
QScriptEngine engine;
|
||||
QScriptValue sc;
|
||||
sc = engine.evaluate("(" + response + ")");
|
||||
QJsonParseError Err;
|
||||
QVariantMap sc = QJsonDocument::fromJson(response.toUtf8(), &Err).toVariant().toMap();
|
||||
|
||||
errorTxt = "None";
|
||||
|
||||
if (!sc.property("status_code").isValid() || isError(sc.property("status_code").toInt32())) {
|
||||
if (Err.error != QJsonParseError::NoError) {
|
||||
errorTxt = "Json syntax error";
|
||||
error = true;
|
||||
if (sc.property("error").isValid())
|
||||
errorTxt = sc.property("error").toString();
|
||||
else
|
||||
errorTxt = "Unknown error";
|
||||
} else {
|
||||
error = false;
|
||||
if (sc.property("number_of_total_results").isValid())
|
||||
numResults = sc.property("number_of_total_results").toString().toInt(); // sc.property("number_of_total_results").toInt32();
|
||||
else
|
||||
qDebug() << sc.property("oops").toString();
|
||||
return;
|
||||
}
|
||||
|
||||
int limit = sc.property("limit").toInt32();
|
||||
int offset = sc.property("offset").toInt32();
|
||||
int total = sc.property("number_of_total_results").toInt32();
|
||||
if (limit > 0) {
|
||||
totalPages = (total / limit) + (total % limit > 0 ? 1 : 0);
|
||||
currentPage = (offset / limit) + 1;
|
||||
} else
|
||||
totalPages = currentPage = 1;
|
||||
if (!sc.value("status_code").isValid() || isError(sc.value("status_code").toInt())) {
|
||||
error = true;
|
||||
if (sc.value("error").isValid()) {
|
||||
errorTxt = sc.value("error").toString();
|
||||
|
||||
} else {
|
||||
errorTxt = "Unknown error";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
error = false;
|
||||
if (sc.value("number_of_total_results").isValid()) {
|
||||
numResults = sc.value("number_of_total_results").toInt(); // sc.property("number_of_total_results").toInt32();
|
||||
|
||||
} else {
|
||||
qDebug() << sc.value("oops").toString();
|
||||
}
|
||||
|
||||
auto limit = sc.value("limit").toInt();
|
||||
auto offset = sc.value("offset").toInt();
|
||||
auto total = sc.value("number_of_total_results").toInt();
|
||||
if (limit > 0) {
|
||||
totalPages = (total / limit) + (total % limit > 0 ? 1 : 0);
|
||||
currentPage = (offset / limit) + 1;
|
||||
} else {
|
||||
totalPages = currentPage = 1;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
#include "volume_comics_model.h"
|
||||
#include "qnaturalsorting.h"
|
||||
|
||||
#include <QtScript>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
|
||||
bool lessThan(const QList<QString> &left, const QList<QString> &right)
|
||||
{
|
||||
@ -18,33 +19,29 @@ VolumeComicsModel::VolumeComicsModel(QObject *parent)
|
||||
|
||||
void VolumeComicsModel::load(const QString &json)
|
||||
{
|
||||
QScriptEngine engine;
|
||||
QScriptValue sc;
|
||||
sc = engine.evaluate("(" + json + ")");
|
||||
QJsonParseError Err;
|
||||
QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap();
|
||||
|
||||
if (!sc.property("error").isValid() && sc.property("error").toString() != "OK") {
|
||||
if (Err.error != QJsonParseError::NoError) {
|
||||
qDebug("Error detected");
|
||||
} else {
|
||||
QScriptValueIterator it(sc.property("results"));
|
||||
//bool test;
|
||||
QScriptValue resultsValue;
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (it.flags() & QScriptValue::SkipInEnumeration)
|
||||
continue;
|
||||
resultsValue = it.value();
|
||||
QString issueNumber = resultsValue.property("issue_number").toString();
|
||||
QScriptValue propertyName = resultsValue.property("name");
|
||||
QString name = propertyName.isNull() ? "-" : propertyName.toString();
|
||||
QString coverURL = resultsValue.property("image").property("medium_url").toString();
|
||||
QString id = resultsValue.property("id").toString();
|
||||
QStringList l;
|
||||
l << issueNumber << name << coverURL << id;
|
||||
_data.push_back(l);
|
||||
}
|
||||
|
||||
qSort(_data.begin(), _data.end(), lessThan);
|
||||
return;
|
||||
}
|
||||
|
||||
QListIterator<QVariant> it(sc.value("results").toList());
|
||||
QVariantMap resultsValue;
|
||||
while (it.hasNext()) {
|
||||
resultsValue = it.next().toMap();
|
||||
QString issueNumber = resultsValue.value("issue_number").toString();
|
||||
QVariant propertyName = resultsValue.value("name");
|
||||
QString name = propertyName.isNull() ? "-" : propertyName.toString();
|
||||
QString coverURL = resultsValue.value("image").toMap().value("medium_url").toString();
|
||||
QString id = resultsValue.value("id").toString();
|
||||
QStringList l;
|
||||
l << issueNumber << name << coverURL << id;
|
||||
_data.push_back(l);
|
||||
}
|
||||
|
||||
qSort(_data.begin(), _data.end(), lessThan);
|
||||
}
|
||||
|
||||
/*void VolumeComicsModel::load(const QStringList &jsonList)
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "volumes_model.h"
|
||||
|
||||
#include <QtScript>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
|
||||
VolumesModel::VolumesModel(QObject *parent)
|
||||
: JSONModel(parent)
|
||||
@ -14,34 +15,33 @@ VolumesModel::~VolumesModel()
|
||||
|
||||
void VolumesModel::load(const QString &json)
|
||||
{
|
||||
QScriptEngine engine;
|
||||
QScriptValue sc;
|
||||
sc = engine.evaluate("(" + json + ")");
|
||||
QJsonParseError Err;
|
||||
QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap();
|
||||
|
||||
if (!sc.property("error").isValid() && sc.property("error").toString() != "OK") {
|
||||
if (Err.error != QJsonParseError::NoError) {
|
||||
qDebug("Error detected");
|
||||
} else {
|
||||
int numResults = sc.property("number_of_total_results").toString().toInt(); //fix to weird behaviour using hasNext
|
||||
QScriptValueIterator it(sc.property("results"));
|
||||
bool test;
|
||||
QScriptValue resultsValue;
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
resultsValue = it.value();
|
||||
QString numIssues = resultsValue.property("count_of_issues").toString();
|
||||
QString year = resultsValue.property("start_year").toString();
|
||||
QString name = resultsValue.property("name").toString();
|
||||
QString publisher = resultsValue.property("publisher").property("name").toString();
|
||||
QString url = resultsValue.property("image").property("medium_url").toString();
|
||||
QString deck = resultsValue.property("deck").toString();
|
||||
QString id = resultsValue.property("id").toString();
|
||||
QStringList l;
|
||||
l << name << year << numIssues << publisher << url << deck << id;
|
||||
test = name.isEmpty() && year.isEmpty() && numIssues.isEmpty() && url.isEmpty();
|
||||
if (numResults > 0 && !test)
|
||||
_data.push_back(l);
|
||||
numResults--;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int numResults = sc.value("number_of_total_results").toInt(); //fix to weird behaviour using hasNext
|
||||
QListIterator<QVariant> it(sc.value("results").toList());
|
||||
bool test;
|
||||
QVariantMap resultsValue;
|
||||
while (it.hasNext()) {
|
||||
resultsValue = it.next().toMap();
|
||||
QString numIssues = resultsValue.value("count_of_issues").toString();
|
||||
QString year = resultsValue.value("start_year").toString();
|
||||
QString name = resultsValue.value("name").toString();
|
||||
QString publisher = resultsValue.value("publisher").toMap().value("name").toString();
|
||||
QString url = resultsValue.value("image").toMap().value("medium_url").toString();
|
||||
QString deck = resultsValue.value("deck").toString();
|
||||
QString id = resultsValue.value("id").toString();
|
||||
QStringList l;
|
||||
l << name << year << numIssues << publisher << url << deck << id;
|
||||
test = name.isEmpty() && year.isEmpty() && numIssues.isEmpty() && url.isEmpty();
|
||||
if (numResults > 0 && !test)
|
||||
_data.push_back(l);
|
||||
numResults--;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user