From kde-commits Fri Jan 31 21:40:47 2014 From: Thomas Fischer Date: Fri, 31 Jan 2014 21:40:47 +0000 To: kde-commits Subject: [kbibtex] /: Various minor fixes and updates for ISBNdb search Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=139120445715656 Git commit ad35283b292b728f7d24a3d026e9fea78d20b441 by Thomas Fischer. Committed on 31/01/2014 at 21:40. Pushed by thomasfischer into branch 'master'. Various minor fixes and updates for ISBNdb search M +27 -25 src/networking/onlinesearch/onlinesearchisbndb.cpp M +33 -2 xslt/isbndb2bibtex.xsl http://commits.kde.org/kbibtex/ad35283b292b728f7d24a3d026e9fea78d20b441 diff --git a/src/networking/onlinesearch/onlinesearchisbndb.cpp b/src/netwo= rking/onlinesearch/onlinesearchisbndb.cpp index dc304c9..383ab22 100644 --- a/src/networking/onlinesearch/onlinesearchisbndb.cpp +++ b/src/networking/onlinesearch/onlinesearchisbndb.cpp @@ -34,13 +34,13 @@ private: static const QString accessKey; static const QString booksUrl, authorsUrl; = - QString cachedPersonId; - public: XSLTransform *xslt; + KUrl queryUrl; + int currentPage, maxPage; = OnlineSearchIsbnDBPrivate(OnlineSearchIsbnDB *parent) - : p(parent), xslt() { + : p(parent), xslt(), currentPage(0), maxPage(0) { xslt =3D XSLTransform::createXSLTransform(KStandardDirs::locate("d= ata", "kbibtex/isbndb2bibtex.xsl")); } = @@ -49,25 +49,22 @@ public: } = KUrl buildBooksUrl(const QMap &query, int numResults= ) { - Q_UNUSED(numResults) + currentPage =3D 1; + maxPage =3D (numResults + 9) / 10; = - KUrl queryUrl(booksUrl); + queryUrl =3D KUrl(booksUrl); queryUrl.addQueryItem(QLatin1String("access_key"), accessKey); - queryUrl.addQueryItem(QLatin1String("results"), QLatin1String("det= ails,texts")); + queryUrl.addQueryItem(QLatin1String("results"), QLatin1String("tex= ts,authors")); = QString index1, value1; if (query[queryKeyFreeText].isEmpty() && query[queryKeyAuthor].isE= mpty() && !query[queryKeyTitle].isEmpty()) { /// only searching for title index1 =3D QLatin1String("title"); value1 =3D query[queryKeyTitle]; - } else if (!cachedPersonId.isEmpty() && query[queryKeyFreeText].is= Empty() && !query[queryKeyAuthor].isEmpty() && query[queryKeyTitle].isEmpty= ()) { - /// only searching for author - index1 =3D QLatin1String("person_id"); - value1 =3D cachedPersonId; } else { /// multiple different values given index1 =3D QLatin1String("full"); - value1 =3D query[queryKeyFreeText] + QChar(' ') + query[queryK= eyAuthor] + QChar(' ') + query[queryKeyTitle]; + value1 =3D query[queryKeyFreeText] + QLatin1Char(' ') + query[= queryKeyAuthor] + QLatin1Char(' ') + query[queryKeyTitle]; } queryUrl.addQueryItem(QLatin1String("index1"), index1); queryUrl.addQueryItem(QLatin1String("value1"), value1); @@ -95,12 +92,12 @@ void OnlineSearchIsbnDB::startSearch(const QMap &query, int nu { m_hasBeenCanceled =3D false; = + emit progress(0, d->maxPage); + QNetworkRequest request(d->buildBooksUrl(query, numResults)); QNetworkReply *reply =3D InternalNetworkAccessManager::self()->get(req= uest); InternalNetworkAccessManager::self()->setNetworkReplyTimeout(reply); connect(reply, SIGNAL(finished()), this, SLOT(downloadDone())); - - emit progress(0, 2); } = void OnlineSearchIsbnDB::startSearch() @@ -137,7 +134,7 @@ void OnlineSearchIsbnDB::cancel() = void OnlineSearchIsbnDB::downloadDone() { - emit progress(1, 2); + emit progress(d->currentPage, d->maxPage); = QNetworkReply *reply =3D static_cast(sender()); = @@ -145,12 +142,8 @@ void OnlineSearchIsbnDB::downloadDone() /// ensure proper treatment of UTF-8 characters const QString xmlCode =3D QString::fromUtf8(reply->readAll().data(= )); = - dumpToFile("xml.xml", xmlCode); - /// use XSL transformation to get BibTeX document from XML result - QString bibtexCode =3D d->xslt->transform(xmlCode).replace(QLatin1= String(""), QString()); - - dumpToFile("bib.bib", bibtexCode); + const QString bibtexCode =3D d->xslt->transform(xmlCode).remove(QL= atin1String("")).replace(QLat= in1String("&"), QLatin1String("&")); = FileImporterBibTeX importer; File *bibtexFile =3D importer.fromString(bibtexCode); @@ -161,18 +154,27 @@ void OnlineSearchIsbnDB::downloadDone() QSharedPointer entry =3D (*it).dynamicCast(); hasEntries |=3D publishEntry(entry); } + delete bibtexFile; = - if (!hasEntries) + if (!hasEntries) { kDebug() << "No hits found in" << reply->url().toString(); - emit stoppedSearch(resultNoError); - - delete bibtexFile; + emit stoppedSearch(resultNoError); + } else if (d->currentPage >=3D d->maxPage) + emit stoppedSearch(resultNoError); + else { + ++d->currentPage; + KUrl nextUrl =3D d->queryUrl; + nextUrl.addQueryItem(QLatin1String("page_number"), QString= ::number(d->currentPage)); + QNetworkRequest request(nextUrl); + QNetworkReply *nextReply =3D InternalNetworkAccessManager:= :self()->get(request); + InternalNetworkAccessManager::self()->setNetworkReplyTimeo= ut(nextReply); + connect(nextReply, SIGNAL(finished()), this, SLOT(download= Done())); + return; + } } else { kWarning() << "No valid BibTeX file results returned on reques= t on" << reply->url().toString(); emit stoppedSearch(resultUnspecifiedError); } } else kDebug() << "url was" << reply->url().toString(); - - emit progress(2, 2); } diff --git a/xslt/isbndb2bibtex.xsl b/xslt/isbndb2bibtex.xsl index 90e474e..fb0b451 100644 --- a/xslt/isbndb2bibtex.xsl +++ b/xslt/isbndb2bibtex.xsl @@ -59,13 +59,44 @@ } = - -2"> + + +2"> , author =3D {{ }} + + +2"> +, + author =3D { + + + and + +} + + + + + + +2"> +, + isbn =3D { + +} + + +2"> +, + isbn =3D { + +} + + =