[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [tellico/frameworks] src: update openlibraryfetcher
From: Robby Stephenson <robby () periapsis ! org>
Date: 2015-06-01 1:06:50
Message-ID: E1YzEBm-0008KJ-0S () scm ! kde ! org
[Download RAW message or body]
Git commit 4eead8ba6fb1b17f28ecaeb17a1e5395047b6ad6 by Robby Stephenson.
Committed on 31/05/2015 at 20:42.
Pushed by rstephenson into branch 'frameworks'.
update openlibraryfetcher
M +25 -64 src/fetch/openlibraryfetcher.cpp
M +0 -1 src/fetch/openlibraryfetcher.h
M +4 -4 src/tests/CMakeLists.txt
M +3 -2 src/tests/openlibraryfetchertest.cpp
http://commits.kde.org/tellico/4eead8ba6fb1b17f28ecaeb17a1e5395047b6ad6
diff --git a/src/fetch/openlibraryfetcher.cpp b/src/fetch/openlibraryfetcher.cpp
index da8caa6..40aa6ee 100644
--- a/src/fetch/openlibraryfetcher.cpp
+++ b/src/fetch/openlibraryfetcher.cpp
@@ -22,7 +22,6 @@
* *
***************************************************************************/
-#include <config.h>
#include "openlibraryfetcher.h"
#include "../collections/bookcollection.h"
#include "../images/imagefactory.h"
@@ -35,18 +34,17 @@
#include <KLocalizedString>
#include <kio/job.h>
-#include <kio/jobuidelegate.h>
+#include <KJobUiDelegate>
+#include <KJobWidgets/KJobWidgets>
#include <QLabel>
#include <QFile>
#include <QTextStream>
#include <QGridLayout>
#include <QTextCodec>
-
-#ifdef HAVE_QJSON
-#include <qjson/parser.h>
-#include <KJobWidgets/KJobWidgets>
-#endif
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
namespace {
static const char* OPENLIBRARY_QUERY_URL = "http://openlibrary.org/query.json";
@@ -68,11 +66,7 @@ QString OpenLibraryFetcher::source() const {
// without QJSON, we can only search on ISBN for covers
bool OpenLibraryFetcher::canSearch(FetchKey k) const {
-#ifndef HAVE_QJSON
- return k == ISBN;
-#else
return k == Title || k == Person || k == ISBN || k == LCCN || k == Keyword;
-#endif
}
bool OpenLibraryFetcher::canFetch(int type) const {
@@ -100,9 +94,6 @@ void OpenLibraryFetcher::search() {
}
void OpenLibraryFetcher::doSearch(const QString& term_) {
-#ifndef HAVE_QJSON
- doCoverOnly(term_);
-#else
QUrl u(QString::fromLatin1(OPENLIBRARY_QUERY_URL));
// books are type/edition
@@ -155,31 +146,6 @@ void OpenLibraryFetcher::doSearch(const QString& term_) {
KJobWidgets::setWindow(job, GUI::Proxy::widget());
connect(job, SIGNAL(result(KJob*)), SLOT(slotComplete(KJob*)));
m_jobs << job;
-#endif
-}
-
-void OpenLibraryFetcher::doCoverOnly(const QString& term_) {
- switch(request().key) {
- case ISBN:
- break;
-
- default:
- myWarning() << "key not recognized: " << request().key;
- return;
- }
-
- int pos = 0;
- ISBNValidator val(this);
- QString isbn = term_;
- if(val.validate(isbn, pos) == QValidator::Acceptable) {
- Data::CollPtr coll(new Data::BookCollection(true));
- Data::EntryPtr entry(new Data::Entry(coll));
- entry->setField(QLatin1String("isbn"), isbn);
-
- FetchResult* r = new FetchResult(Fetcher::Ptr(this), entry);
- m_entries.insert(r->uid, Data::EntryPtr(entry));
- emit signalResultFound(r);
- }
}
void OpenLibraryFetcher::endJob(KIO::StoredTransferJob* job_) {
@@ -243,7 +209,6 @@ Tellico::Fetch::FetchRequest \
OpenLibraryFetcher::updateRequest(Data::EntryPtr en
void OpenLibraryFetcher::slotComplete(KJob* job_) {
KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(job_);
-#ifdef HAVE_QJSON
// myDebug();
if(job->error()) {
@@ -270,9 +235,9 @@ void OpenLibraryFetcher::slotComplete(KJob* job_) {
f.close();
#endif
- QJson::Parser parser;
- QVariantList resultList = parser.parse(data).toList();
- if(resultList.isEmpty()) {
+ QJsonDocument doc = QJsonDocument::fromJson(data);
+ QJsonArray array = doc.array();
+ if(array.isEmpty()) {
// myDebug() << "no results";
endJob(job);
return;
@@ -285,15 +250,14 @@ void OpenLibraryFetcher::slotComplete(KJob* job_) {
coll->addField(field);
}
- QVariantMap resultMap;
- foreach(const QVariant& result, resultList) {
+ for(int i = 0; i < array.count(); i++) {
// be sure to check that the fetcher has not been stopped
// crashes can occur if not
if(!m_started) {
break;
}
// myDebug() << "found result:" << result;
- resultMap = result.toMap();
+ QVariantMap resultMap = array.at(i).toObject().toVariantMap();
// myDebug() << resultMap.value(QLatin1String("isbn_10")).toList().at(0);
@@ -321,7 +285,9 @@ void OpenLibraryFetcher::slotComplete(KJob* job_) {
} else if(binding.toLower().contains(QLatin1String("paperback"))) {
binding = QLatin1String("Paperback");
}
- entry->setField(QLatin1String("binding"), i18n(binding.toUtf8()));
+ if(!binding.isEmpty()) {
+ entry->setField(QLatin1String("binding"), i18n(binding.toUtf8()));
+ }
entry->setField(QLatin1String("publisher"), value(resultMap, "publishers"));
entry->setField(QLatin1String("series"), value(resultMap, "series"));
entry->setField(QLatin1String("pages"), value(resultMap, "number_of_pages"));
@@ -335,14 +301,15 @@ void OpenLibraryFetcher::slotComplete(KJob* job_) {
foreach(const QVariant& authorMap, \
resultMap.value(QLatin1String("authors")).toList()) { const QString key = \
value(authorMap.toMap(), "key"); if(!key.isEmpty()) {
- QUrl authorUrl(OPENLIBRARY_QUERY_URL);
+ QUrl authorUrl(QString::fromLatin1(OPENLIBRARY_QUERY_URL));
authorUrl.addQueryItem(QLatin1String("type"), \
QLatin1String("/type/author")); authorUrl.addQueryItem(QLatin1String("key"), key);
authorUrl.addQueryItem(QLatin1String("name"), QString());
QString output = FileHandler::readTextFile(authorUrl, true /*quiet*/);
- QVariantList authorList = parser.parse(output.toUtf8()).toList();
- QVariantMap authorResult = authorList.isEmpty() ? QVariantMap() : \
authorList.at(0).toMap(); + QJsonDocument doc = \
QJsonDocument::fromJson(output.toUtf8()); + QJsonArray array = doc.array();
+ QVariantMap authorResult = array.isEmpty() ? QVariantMap() : \
array.at(0).toObject().toVariantMap(); const QString name = value(authorResult, \
"name"); if(!name.isEmpty()) {
authors << name;
@@ -357,14 +324,15 @@ void OpenLibraryFetcher::slotComplete(KJob* job_) {
foreach(const QVariant& langMap, \
resultMap.value(QLatin1String("languages")).toList()) { const QString key = \
value(langMap.toMap(), "key"); if(!key.isEmpty()) {
- QUrl langUrl(OPENLIBRARY_QUERY_URL);
+ QUrl langUrl(QString::fromLatin1(OPENLIBRARY_QUERY_URL));
langUrl.addQueryItem(QLatin1String("type"), \
QLatin1String("/type/language")); langUrl.addQueryItem(QLatin1String("key"), key);
langUrl.addQueryItem(QLatin1String("name"), QString());
QString output = FileHandler::readTextFile(langUrl, true /*quiet*/, true \
/*utf8*/);
- QVariantList langList = parser.parse(output.toUtf8()).toList();
- QVariantMap langResult = langList.isEmpty() ? QVariantMap() : \
langList.at(0).toMap(); + QJsonDocument doc = \
QJsonDocument::fromJson(output.toUtf8()); + QJsonArray array = doc.array();
+ QVariantMap langResult = array.isEmpty() ? QVariantMap() : \
array.at(0).toObject().toVariantMap(); const QString name = value(langResult, \
"name"); if(!name.isEmpty()) {
langs << i18n(name.toUtf8());
@@ -383,27 +351,21 @@ void OpenLibraryFetcher::slotComplete(KJob* job_) {
// m_start = m_entries.count();
// m_hasMoreResults = m_start <= m_total;
m_hasMoreResults = false; // for now, no continued searches
-#endif
endJob(job);
}
QString OpenLibraryFetcher::getAuthorKeys(const QString& term_) {
-#ifdef HAVE_QJSON
QUrl u(QString::fromLatin1(OPENLIBRARY_QUERY_URL));
u.addQueryItem(QLatin1String("type"), QLatin1String("/type/author"));
u.addQueryItem(QLatin1String("name"), term_);
QString output = FileHandler::readTextFile(u, true /*quiet*/, true /*utf8*/);
- QJson::Parser parser;
- QVariantList results = parser.parse(output.toUtf8()).toList();
- myDebug() << "found" << results.count() << "authors";
+ QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
+ QJsonArray array = doc.array();
+ myDebug() << "found" << array.count() << "authors";
// right now, only use the first
- return results.isEmpty() ? QString() : value(results.at(0).toMap(), "key");
-#else
- Q_UNUSED(term_);
- return QString();
-#endif
+ return array.isEmpty() ? QString() : value(array.at(0).toObject().toVariantMap(), \
"key"); }
Tellico::Fetch::ConfigWidget* OpenLibraryFetcher::configWidget(QWidget* parent_) \
const { @@ -456,4 +418,3 @@ QString OpenLibraryFetcher::value(const QVariantMap& map, \
const char* name) { return QString();
}
}
-
diff --git a/src/fetch/openlibraryfetcher.h b/src/fetch/openlibraryfetcher.h
index b835267..61dd8d9 100644
--- a/src/fetch/openlibraryfetcher.h
+++ b/src/fetch/openlibraryfetcher.h
@@ -91,7 +91,6 @@ private:
virtual void search();
virtual FetchRequest updateRequest(Data::EntryPtr entry);
void doSearch(const QString& term);
- void doCoverOnly(const QString& term);
QString getAuthorKeys(const QString& term);
void endJob(KIO::StoredTransferJob* job);
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index bb82f89..221f2e0 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -569,13 +569,13 @@ add_test(musicbrainzfetchertest musicbrainzfetchertest)
ecm_mark_as_test(musicbrainzfetchertest)
TARGET_LINK_LIBRARIES(musicbrainzfetchertest fetcherstest ${TELLICO2_TEST_LIBS})
-IF( QJSON_FOUND )
- add_executable(openlibraryfetchertest openlibraryfetchertest.cpp \
abstractfetchertest.cpp) +add_executable(openlibraryfetchertest \
openlibraryfetchertest.cpp abstractfetchertest.cpp + ../fetch/openlibraryfetcher.cpp
+)
ecm_mark_nongui_executable(openlibraryfetchertest)
add_test(openlibraryfetchertest openlibraryfetchertest)
ecm_mark_as_test(openlibraryfetchertest)
- TARGET_LINK_LIBRARIES(openlibraryfetchertest fetch ${TELLICO_TEST_LIBS})
-ENDIF( QJSON_FOUND )
+TARGET_LINK_LIBRARIES(openlibraryfetchertest fetcherstest ${TELLICO2_TEST_LIBS})
add_executable(springerfetchertest springerfetchertest.cpp abstractfetchertest.cpp)
ecm_mark_nongui_executable(springerfetchertest)
diff --git a/src/tests/openlibraryfetchertest.cpp \
b/src/tests/openlibraryfetchertest.cpp index a334d0c..31945b0 100644
--- a/src/tests/openlibraryfetchertest.cpp
+++ b/src/tests/openlibraryfetchertest.cpp
@@ -25,13 +25,14 @@
#undef QT_NO_CAST_FROM_ASCII
#include "openlibraryfetchertest.h"
-#include "qtest_kde.h"
#include "../fetch/openlibraryfetcher.h"
#include "../entry.h"
#include "../images/imagefactory.h"
-QTEST_KDEMAIN( OpenLibraryFetcherTest, GUI )
+#include <QTest>
+
+QTEST_GUILESS_MAIN( OpenLibraryFetcherTest )
OpenLibraryFetcherTest::OpenLibraryFetcherTest() : AbstractFetcherTest() {
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic