File indexing completed on 2024-05-12 05:09:29

0001 /***************************************************************************
0002     Copyright (C) 2017 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "dbcfetcher.h"
0026 #include "../tellico_debug.h"
0027 
0028 #include <KLocalizedString>
0029 #include <KConfigGroup>
0030 
0031 #include <QLabel>
0032 #include <QVBoxLayout>
0033 #include <QUrlQuery>
0034 
0035 namespace {
0036   static const char* DBC_API_URL = "https://opensearch.addi.dk/test_5.2/";
0037   static const int DBC_MAX_RETURNS_TOTAL = 20;
0038 }
0039 
0040 using namespace Tellico;
0041 using Tellico::Fetch::DBCFetcher;
0042 
0043 DBCFetcher::DBCFetcher(QObject* parent_) : XMLFetcher(parent_) {
0044   setLimit(DBC_MAX_RETURNS_TOTAL);
0045   setXSLTFilename(QStringLiteral("dbc2tellico.xsl"));
0046 }
0047 
0048 DBCFetcher::~DBCFetcher() {
0049 }
0050 
0051 QString DBCFetcher::source() const {
0052   return m_name.isEmpty() ? defaultName() : m_name;
0053 }
0054 
0055 bool DBCFetcher::canSearch(Fetch::FetchKey k) const {
0056   return k == Title || k == Person || k == Keyword || k == ISBN;
0057 }
0058 
0059 bool DBCFetcher::canFetch(int type) const {
0060   return type == Data::Collection::Book || type == Data::Collection::Bibtex;
0061 }
0062 
0063 void DBCFetcher::readConfigHook(const KConfigGroup&) {
0064 }
0065 
0066 QUrl DBCFetcher::searchUrl() {
0067   QUrl u(QString::fromLatin1(DBC_API_URL));
0068 
0069   QUrlQuery query;
0070   switch(request().key()) {
0071     case Title:
0072       query.addQueryItem(QStringLiteral("query"), QLatin1String("dkcclterm.ti=\"") + request().value() + QLatin1Char('"'));
0073       break;
0074 
0075     case Keyword:
0076       query.addQueryItem(QStringLiteral("query"), QLatin1String("cql.keywords=\"") + request().value() + QLatin1Char('"'));
0077       break;
0078 
0079     case Person:
0080       query.addQueryItem(QStringLiteral("query"), QLatin1String("term.mainCreator=\"") + request().value() + QLatin1Char('"'));
0081       break;
0082 
0083     case ISBN:
0084       {
0085         QString s = request().value();
0086         s.remove(QLatin1Char('-'));
0087         QStringList isbnList = FieldFormat::splitValue(s);
0088         // only search for first ISBN for now
0089         QString q = isbnList.isEmpty() ? QString() : QLatin1String("dkcclterm.ib=") + isbnList.at(0);
0090         query.addQueryItem(QStringLiteral("query"), q);
0091       }
0092       break;
0093 
0094     default:
0095       myWarning() << source() << "- key not recognized:" << request().key();
0096       return QUrl();
0097   }
0098   query.addQueryItem(QStringLiteral("action"), QStringLiteral("search"));
0099   // see https://github.com/DBCDK/OpenSearch-webservice/wiki/OpenSearch-Web-Service
0100   // agency and profile determine the search collections
0101 //  query.addQueryItem(QLatin1String("agency"), QLatin1String("100200"));
0102 //  query.addQueryItem(QLatin1String("profile"), QLatin1String("test"));
0103   query.addQueryItem(QStringLiteral("agency"), QStringLiteral("761500"));
0104   query.addQueryItem(QStringLiteral("profile"), QStringLiteral("opac"));
0105   query.addQueryItem(QStringLiteral("term.type"), QStringLiteral("bog"));
0106   query.addQueryItem(QStringLiteral("start"), QStringLiteral("1"));
0107   query.addQueryItem(QStringLiteral("stepValue"), QStringLiteral("5"));
0108   query.addQueryItem(QStringLiteral("outputType"), QStringLiteral("xml"));
0109   u.setQuery(query);
0110 
0111 //  myDebug() << "url:" << u.url();
0112   return u;
0113 }
0114 
0115 void DBCFetcher::resetSearch() {
0116 }
0117 
0118 void DBCFetcher::parseData(QByteArray& data_) {
0119   Q_UNUSED(data_);
0120 }
0121 
0122 Tellico::Data::EntryPtr DBCFetcher::fetchEntryHookData(Data::EntryPtr entry_) {
0123   Q_ASSERT(entry_);
0124   return entry_;
0125 }
0126 
0127 Tellico::Fetch::FetchRequest DBCFetcher::updateRequest(Data::EntryPtr entry_) {
0128   const QString isbn = entry_->field(QStringLiteral("isbn"));
0129   if(!isbn.isEmpty()) {
0130     return FetchRequest(ISBN, isbn);
0131   }
0132 
0133   const QString title = entry_->field(QStringLiteral("title"));
0134   if(!title.isEmpty()) {
0135     return FetchRequest(Keyword, title);
0136   }
0137   return FetchRequest();
0138 }
0139 
0140 Tellico::Fetch::ConfigWidget* DBCFetcher::configWidget(QWidget* parent_) const {
0141   return new DBCFetcher::ConfigWidget(parent_, this);
0142 }
0143 
0144 QString DBCFetcher::defaultName() {
0145   return i18n("Danish Bibliographic Center (DBC.dk)");
0146 }
0147 
0148 QString DBCFetcher::defaultIcon() {
0149   return favIcon("https://www.dbc.dk/++theme++dbc.theme/favicon.ico");
0150 }
0151 
0152 Tellico::StringHash DBCFetcher::allOptionalFields() {
0153   StringHash hash;
0154   return hash;
0155 }
0156 
0157 DBCFetcher::ConfigWidget::ConfigWidget(QWidget* parent_, const DBCFetcher* fetcher_)
0158     : Fetch::ConfigWidget(parent_) {
0159   QVBoxLayout* l = new QVBoxLayout(optionsWidget());
0160   l->addWidget(new QLabel(i18n("This source has no options."), optionsWidget()));
0161   l->addStretch();
0162 
0163   // now add additional fields widget
0164   addFieldsWidget(DBCFetcher::allOptionalFields(), fetcher_ ? fetcher_->optionalFields() : QStringList());
0165 }
0166 
0167 void DBCFetcher::ConfigWidget::saveConfigHook(KConfigGroup&) {
0168 }
0169 
0170 QString DBCFetcher::ConfigWidget::preferredName() const {
0171   return DBCFetcher::defaultName();
0172 }