diff --git a/YACReaderLibrary/YACReaderLibrary.pro b/YACReaderLibrary/YACReaderLibrary.pro index 31bc2c29..227ac50f 100644 --- a/YACReaderLibrary/YACReaderLibrary.pro +++ b/YACReaderLibrary/YACReaderLibrary.pro @@ -69,7 +69,7 @@ macx { #CONFIG += release CONFIG -= flat -QT += sql network widgets script svg +QT += sql network widgets svg !CONFIG(no_opengl) { QT += opengl } @@ -231,8 +231,8 @@ SOURCES += comic_flow.cpp \ } macx { - HEADERS += trayhandler.h - OBJECTIVE_SOURCES += trayhandler.mm + HEADERS += trayhandler.h + OBJECTIVE_SOURCES += trayhandler.mm } include(./server/server.pri) diff --git a/YACReaderLibrary/comic_vine/comic_vine_all_volume_comics_retriever.cpp b/YACReaderLibrary/comic_vine/comic_vine_all_volume_comics_retriever.cpp index 9b468afe..10b864fc 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_all_volume_comics_retriever.cpp +++ b/YACReaderLibrary/comic_vine/comic_vine_all_volume_comics_retriever.cpp @@ -3,7 +3,9 @@ #include "http_worker.h" #include "response_parser.h" -#include +#include +#include +#include ComicVineAllVolumeComicsRetriever::ComicVineAllVolumeComicsRetriever(const QString &volumeURLString, QObject *parent) : QObject(parent), volumeURLString(volumeURLString) diff --git a/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp b/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp index 8047b4f3..2bbcd3bf 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp +++ b/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp @@ -1,21 +1,22 @@ #include "comic_vine_dialog.h" -#include -#include #include #include -#include -#include -#include #include +#include +#include +#include #include +#include +#include #if QT_VERSION >= 0x050000 #include #else #include #endif -#include -#include #include "data_base_management.h" +#include +#include +#include #include "yacreader_busy_widget.h" #include "comic_vine_client.h" @@ -488,138 +489,114 @@ void ComicVineDialog::getComicInfo(const QString &comicId, int count, const QStr ComicDB ComicVineDialog::parseComicInfo(ComicDB &comic, const QString &json, int count, const QString &publisher) { - QScriptEngine engine; - QScriptValue sc; - sc = engine.evaluate("(" + json + ")"); + QJsonParseError Err; - if (!sc.property("error").isValid() && sc.property("error").toString() != "OK") { + QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap(); + 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 + return comic; + } - if (numResults > 0) { - QScriptValue result = sc.property("results"); + int numResults = sc.value("number_of_total_results").toInt(); //fix to weird behaviour using hasNext - if (!result.property("name").isNull()) { - QString title = result.property("name").toString(); + if (numResults > 0) { + QVariantMap result = sc.value("results").toMap(); + comic.info.title = result.value("name"); + comic.info.number = result.value("issue_number"); + comic.info.volume = result.value("volume").toMap().value("name"); - comic.info.title = title; - } + if (result.contains("person_credits") && !result.value("person_credits").isNull()) { + QMap authors = getAuthors(result.value("person_credits")); - if (!result.property("issue_number").isNull()) { - QString number = result.property("issue_number").toString(); + QString writer = QStringList(authors.values("writer")).join("\n"); + QString penciller = QStringList(authors.values("penciller")).join("\n"); + QString inker = QStringList(authors.values("inker")).join("\n"); + QString colorist = QStringList(authors.values("colorist")).join("\n"); + QString letterer = QStringList(authors.values("letterer")).join("\n"); + QString coverArtist = QStringList(authors.values("cover")).join("\n"); - comic.info.number = number; - } - - if (!result.property("volume").property("name").isNull()) { - QString volume = result.property("volume").property("name").toString(); - - comic.info.volume = volume; - } - - if (!result.property("person_credits").isNull()) { - QMap authors = getAuthors(result.property("person_credits")); - - QString writer = QStringList(authors.values("writer")).join("\n"); - QString penciller = QStringList(authors.values("penciller")).join("\n"); - QString inker = QStringList(authors.values("inker")).join("\n"); - QString colorist = QStringList(authors.values("colorist")).join("\n"); - QString letterer = QStringList(authors.values("letterer")).join("\n"); - QString coverArtist = QStringList(authors.values("cover")).join("\n"); - - comic.info.writer = writer; - comic.info.penciller = penciller; - comic.info.inker = inker; - comic.info.colorist = colorist; - comic.info.letterer = letterer; - comic.info.coverArtist = coverArtist; - } - - if (!result.property("cover_date").isNull()) { - QString date = result.property("cover_date").toString(); - - QStringList tempList = date.split("-"); - - if (tempList.length() == 3) { - std::reverse(tempList.begin(), tempList.end()); - comic.info.date = tempList.join("/"); - } - } - - if (!result.property("description").isNull()) { - QString synopsis = result.property("description").toString().remove(QRegExp("<[^>]*>")); //description - comic.info.synopsis = synopsis; - } - - if (!result.property("character_credits").isNull()) { - QString characters = getCharacters(result.property("character_credits")); - - comic.info.characters = characters; - } - - if (!result.property("story_arc_credits").isNull()) { - QPair storyArcIdAndName = getFirstStoryArcIdAndName(result.property("story_arc_credits")); - QString storyArcId = storyArcIdAndName.first; - QString storyArcName = storyArcIdAndName.second; - if (!storyArcId.isNull()) { - - QString comicId = result.property("id").toString(); - - QPair arcNumberAndArcCount = getArcNumberAndArcCount(storyArcId, comicId); - if (!arcNumberAndArcCount.first.isNull()) { - QString arcNumber = arcNumberAndArcCount.first; - QString arcCount = arcNumberAndArcCount.second; - - comic.info.storyArc = storyArcName; - comic.info.arcNumber = arcNumber; - comic.info.arcCount = arcCount; - } - } - } - - comic.info.count = count; - - comic.info.publisher = publisher; + comic.info.writer = writer; + comic.info.penciller = penciller; + comic.info.inker = inker; + comic.info.colorist = colorist; + comic.info.letterer = letterer; + comic.info.coverArtist = coverArtist; } + + if (result.contains("cover_date") && !result.value("conver_date").isNull()) { + QString date = result.value("cover_date").toString(); + + QStringList tempList = date.split("-"); + + if (tempList.length() == 3) { + std::reverse(tempList.begin(), tempList.end()); + comic.info.date = tempList.join("/"); + } + } + + if (result.contains("description") && !result.value("description").isNull()) { + comic.info.synopsis = result.value("description").toString().remove(QRegExp("<[^>]*>")); //description + } + + if (result.contains("character_credits") && !result.value("character_credits").isNull()) { + comic.info.characters = getCharacters(result.value("character_credits")); + } + + if (result.contains("story_arc_credits") && !result.value("story_arc_credits").isNull()) { + QPair storyArcIdAndName = getFirstStoryArcIdAndName(result.value("story_arc_credits")); + QString storyArcId = storyArcIdAndName.first; + QString storyArcName = storyArcIdAndName.second; + if (!storyArcId.isNull()) { + + QString comicId = result.value("id").toString(); + + QPair arcNumberAndArcCount = getArcNumberAndArcCount(storyArcId, comicId); + if (!arcNumberAndArcCount.first.isNull()) { + QString arcNumber = arcNumberAndArcCount.first; + QString arcCount = arcNumberAndArcCount.second; + + comic.info.storyArc = storyArcName; + comic.info.arcNumber = arcNumber; + comic.info.arcCount = arcCount; + } + } + } + + comic.info.count = count; + + comic.info.publisher = publisher; } return comic; } -QString ComicVineDialog::getCharacters(const QScriptValue &json_characters) +QString ComicVineDialog::getCharacters(const QVariant &json_characters) { - QString characters; + QStringList characters; - QScriptValueIterator it(json_characters); - QScriptValue resultsValue; + QListIterator it(json_characters.toList()); + QVariantMap resultsValue; while (it.hasNext()) { - it.next(); - if (it.flags() & QScriptValue::SkipInEnumeration) - continue; - resultsValue = it.value(); + resultsValue = it.next().toMap(); - characters += resultsValue.property("name").toString() + "\n"; + characters << resultsValue.value("name").toString(); } - return characters; + return (characters.isEmpty()) ? "" : (characters.join("\n") + "\n"); } -QMap ComicVineDialog::getAuthors(const QScriptValue &json_authors) +QMap ComicVineDialog::getAuthors(const QVariant &json_authors) { QMap authors; - QScriptValueIterator it(json_authors); - QScriptValue resultsValue; + QListIterator it(json_authors.toList()); + QVariantMap resultsValue; while (it.hasNext()) { - it.next(); - if (it.flags() & QScriptValue::SkipInEnumeration) - continue; - resultsValue = it.value(); + resultsValue = it.next().toMap(); - QString authorName = resultsValue.property("name").toString(); + QString authorName = resultsValue.value("name").toString(); - QStringList roles = resultsValue.property("role").toString().split(","); + QStringList roles = resultsValue.value("role").toString().split(","); foreach (QString role, roles) { if (role.trimmed() == "writer") authors.insertMulti("writer", authorName); @@ -639,20 +616,17 @@ QMap ComicVineDialog::getAuthors(const QScriptValue &json_auth return authors; } -QPair ComicVineDialog::getFirstStoryArcIdAndName(const QScriptValue &json_story_arcs) +QPair ComicVineDialog::getFirstStoryArcIdAndName(const QVariant &json_story_arcs) { QString story_arc_id = QString(); QString story_arc_name = QString(); - QScriptValueIterator it(json_story_arcs); - QScriptValue resultsValue; + QListIterator it(json_story_arcs.toList()); + QVariantMap resultsValue; while (it.hasNext()) { - it.next(); - if (it.flags() & QScriptValue::SkipInEnumeration) - continue; - resultsValue = it.value(); - story_arc_id = resultsValue.property("id").toString(); - story_arc_name = resultsValue.property("name").toString(); + resultsValue = it.next().toMap(); + story_arc_id = resultsValue.value("id").toString(); + story_arc_name = resultsValue.value("name").toString(); break; } return qMakePair(story_arc_id, story_arc_name); @@ -668,40 +642,33 @@ QPair ComicVineDialog::getArcNumberAndArcCount(const QString & return qMakePair(QString(), QString()); QString json = result; - 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"); return qMakePair(QString(), QString()); - } else { - int numResults = sc.property("number_of_total_results").toString().toInt(); //fix to weird behaviour using hasNext + } - if (numResults > 0) { - QScriptValue result = sc.property("results"); + int numResults = sc.value("number_of_total_results").toInt(); //fix to weird behaviour using hasNext - if (!result.property("issues").isNull()) { - QScriptValue issues = result.property("issues"); + if (numResults > 0) { + QVariantMap result = sc.value("results").toMap(); - int arcNumber = 0; - int arcCount = 0; + if (result.contains("issues")) { + QListIterator it(result.value("issues").toList()); + int arcNumber = 0; + int arcCount = 0; - QScriptValueIterator it(issues); - QScriptValue resultsValue; - while (it.hasNext()) { - it.next(); - if (it.flags() & QScriptValue::SkipInEnumeration) - continue; - resultsValue = it.value(); - if (comicId == resultsValue.property("id").toString()) { - arcNumber = arcCount + 1; - } - arcCount++; + QVariantMap resultsValue; + while (it.hasNext()) { + resultsValue = it.next().toMap(); + if (comicId == resultsValue.value("id").toString()) { + arcNumber = arcCount + 1; } - return qMakePair(QString::number(arcNumber), QString::number(arcCount)); + arcCount++; } - return qMakePair(QString(), QString()); + return qMakePair(QString::number(arcNumber), QString::number(arcCount)); } return qMakePair(QString(), QString()); } diff --git a/YACReaderLibrary/comic_vine/comic_vine_dialog.h b/YACReaderLibrary/comic_vine/comic_vine_dialog.h index 77b8427b..7e4291cc 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_dialog.h +++ b/YACReaderLibrary/comic_vine/comic_vine_dialog.h @@ -18,7 +18,6 @@ class SearchVolume; class SelectComic; class SelectVolume; class SortVolumeComics; -class QScriptValue; //TODO this should use a QStateMachine //---------------------------------------- @@ -64,9 +63,9 @@ protected slots: void goToNextComic(); private: - QString getCharacters(const QScriptValue &json_characters); - QMap getAuthors(const QScriptValue &json_authors); - QPair getFirstStoryArcIdAndName(const QScriptValue &json_story_arcs); + QString getCharacters(const QVariant &json_characters); + QMap getAuthors(const QVariant &json_authors); + QPair getFirstStoryArcIdAndName(const QVariant &json_story_arcs); QPair getArcNumberAndArcCount(const QString &storyArcId, const QString &comicId); void toggleSkipButton(); diff --git a/YACReaderLibrary/comic_vine/model/response_parser.cpp b/YACReaderLibrary/comic_vine/model/response_parser.cpp index b521d4fc..033fd607 100644 --- a/YACReaderLibrary/comic_vine/model/response_parser.cpp +++ b/YACReaderLibrary/comic_vine/model/response_parser.cpp @@ -1,7 +1,7 @@ #include "response_parser.h" - -#include #include +#include +#include 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; } } diff --git a/YACReaderLibrary/comic_vine/model/volume_comics_model.cpp b/YACReaderLibrary/comic_vine/model/volume_comics_model.cpp index e08080f9..01357d8e 100644 --- a/YACReaderLibrary/comic_vine/model/volume_comics_model.cpp +++ b/YACReaderLibrary/comic_vine/model/volume_comics_model.cpp @@ -1,7 +1,8 @@ #include "volume_comics_model.h" #include "qnaturalsorting.h" -#include +#include +#include bool lessThan(const QList &left, const QList &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 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) diff --git a/YACReaderLibrary/comic_vine/model/volumes_model.cpp b/YACReaderLibrary/comic_vine/model/volumes_model.cpp index a7d56a51..9161a0e4 100644 --- a/YACReaderLibrary/comic_vine/model/volumes_model.cpp +++ b/YACReaderLibrary/comic_vine/model/volumes_model.cpp @@ -1,6 +1,7 @@ #include "volumes_model.h" -#include +#include +#include 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 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--; } } diff --git a/YACReaderLibrary/comic_vine/scraper_results_paginator.cpp b/YACReaderLibrary/comic_vine/scraper_results_paginator.cpp index 0876ad71..aa511c9c 100644 --- a/YACReaderLibrary/comic_vine/scraper_results_paginator.cpp +++ b/YACReaderLibrary/comic_vine/scraper_results_paginator.cpp @@ -4,7 +4,6 @@ #include #include #include -#include ScraperResultsPaginator::ScraperResultsPaginator(QWidget *parent) : QWidget(parent), customLabel("items") diff --git a/YACReaderLibrary/comic_vine/select_comic.cpp b/YACReaderLibrary/comic_vine/select_comic.cpp index 681e1d20..103b424e 100644 --- a/YACReaderLibrary/comic_vine/select_comic.cpp +++ b/YACReaderLibrary/comic_vine/select_comic.cpp @@ -5,9 +5,10 @@ #include "scraper_tableview.h" #include "volume_comics_model.h" +#include +#include #include #include -#include SelectComic::SelectComic(QWidget *parent) : ScraperSelector(parent), model(0) @@ -121,18 +122,17 @@ void SelectComic::setCover(const QByteArray &data) void SelectComic::setDescription(const QString &jsonDetail) { - QScriptEngine engine; - QScriptValue sc; - sc = engine.evaluate("(" + jsonDetail + ")"); + QJsonParseError Err; + QVariantMap sc = QJsonDocument::fromJson(jsonDetail.toUtf8(), &Err).toVariant().toMap(); - if (!sc.property("error").isValid() && sc.property("error").toString() != "OK") { + if (Err.error != QJsonParseError::NoError) { qDebug("Error detected"); - } else { - - QScriptValue descriptionValues = sc.property("results").property("description"); - bool valid = !descriptionValues.isNull() && descriptionValues.isValid(); - detailLabel->setText(valid ? descriptionValues.toString().replace("setText(valid ? descriptionValues.toString().replace(" -#include +#include #include -#include +#include +#include +#include +#include #include #include -#include -#include -#include +#include #include +#include +#include #include "scraper_tableview.h" -#include - #include "volumes_model.h" #include "comic_vine_client.h" #include "scraper_scroll_label.h" @@ -149,18 +149,17 @@ void SelectVolume::setCover(const QByteArray &data) void SelectVolume::setDescription(const QString &jsonDetail) { - QScriptEngine engine; - QScriptValue sc; - sc = engine.evaluate("(" + jsonDetail + ")"); + QJsonParseError Err; + QVariantMap sc = QJsonDocument::fromJson(jsonDetail.toUtf8(), &Err).toVariant().toMap(); - if (!sc.property("error").isValid() && sc.property("error").toString() != "OK") { + if (Err.error != QJsonParseError::NoError) { qDebug("Error detected"); - } else { - - QScriptValue descriptionValues = sc.property("results").property("description"); - bool valid = !descriptionValues.isNull() && descriptionValues.isValid(); - detailLabel->setText(valid ? descriptionValues.toString().replace("setText(valid ? descriptionValues.toString().replace("