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

0001 /***************************************************************************
0002     Copyright (C) 2012 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 "dblpfetcher.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* DBLP_API_URL = "http://www.dblp.org/search/api/";
0037   static const int DBLP_MAX_RETURNS_TOTAL = 20;
0038 }
0039 
0040 using namespace Tellico;
0041 using Tellico::Fetch::DBLPFetcher;
0042 
0043 DBLPFetcher::DBLPFetcher(QObject* parent_) : XMLFetcher(parent_) {
0044   setLimit(DBLP_MAX_RETURNS_TOTAL);
0045   setXSLTFilename(QStringLiteral("dblp2tellico.xsl"));
0046 }
0047 
0048 DBLPFetcher::~DBLPFetcher() {
0049 }
0050 
0051 QString DBLPFetcher::source() const {
0052   return m_name.isEmpty() ? defaultName() : m_name;
0053 }
0054 
0055 bool DBLPFetcher::canFetch(int type) const {
0056   return type == Data::Collection::Bibtex;
0057 }
0058 
0059 void DBLPFetcher::readConfigHook(const KConfigGroup&) {
0060 }
0061 
0062 QUrl DBLPFetcher::searchUrl() {
0063   QUrl u(QString::fromLatin1(DBLP_API_URL));
0064 
0065   QUrlQuery q;
0066   switch(request().key()) {
0067     case Keyword:
0068       q.addQueryItem(QStringLiteral("q"), request().value());
0069       q.addQueryItem(QStringLiteral("h"), QString::number(DBLP_MAX_RETURNS_TOTAL));
0070       q.addQueryItem(QStringLiteral("c"), QString::number(0));
0071       break;
0072 
0073     default:
0074       myWarning() << source() << "- key not recognized:" << request().key();
0075       return QUrl();
0076   }
0077   // has to be after query
0078   q.addQueryItem(QStringLiteral("format"), QStringLiteral("xml"));
0079   u.setQuery(q);
0080 
0081 //  myDebug() << "url:" << u.url();
0082   return u;
0083 }
0084 
0085 void DBLPFetcher::resetSearch() {
0086 }
0087 
0088 void DBLPFetcher::parseData(QByteArray& data_) {
0089   // weird XML
0090   // remove the CDATA and the dblp:: namespace
0091   data_.replace("<![CDATA[", ""); // krazy:exclude=doublequote_chars
0092   data_.replace("]]", ""); // krazy:exclude=doublequote_chars
0093   data_.replace("dblp:", ""); // krazy:exclude=doublequote_chars
0094 }
0095 
0096 Tellico::Data::EntryPtr DBLPFetcher::fetchEntryHookData(Data::EntryPtr entry_) {
0097   Q_ASSERT(entry_);
0098   return entry_;
0099 }
0100 
0101 Tellico::Fetch::FetchRequest DBLPFetcher::updateRequest(Data::EntryPtr entry_) {
0102   QString title = entry_->field(QStringLiteral("title"));
0103   if(!title.isEmpty()) {
0104     return FetchRequest(Keyword, title);
0105   }
0106   return FetchRequest();
0107 }
0108 
0109 Tellico::Fetch::ConfigWidget* DBLPFetcher::configWidget(QWidget* parent_) const {
0110   return new DBLPFetcher::ConfigWidget(parent_, this);
0111 }
0112 
0113 QString DBLPFetcher::defaultName() {
0114   return QStringLiteral("DBLP");
0115 }
0116 
0117 QString DBLPFetcher::defaultIcon() {
0118   // don't know why FavIcon job fails for this
0119 //  return favIcon("https://dblp.org");
0120   return favIcon(QUrl(QLatin1String("https://dblp.org")),
0121                  QUrl(QLatin1String("https://tellico-project.org/img/dblp-favicon.ico")));
0122 }
0123 
0124 Tellico::StringHash DBLPFetcher::allOptionalFields() {
0125   StringHash hash;
0126   return hash;
0127 }
0128 
0129 DBLPFetcher::ConfigWidget::ConfigWidget(QWidget* parent_, const DBLPFetcher* fetcher_)
0130     : Fetch::ConfigWidget(parent_) {
0131   QVBoxLayout* l = new QVBoxLayout(optionsWidget());
0132   l->addWidget(new QLabel(i18n("This source has no options."), optionsWidget()));
0133   l->addStretch();
0134 
0135   // now add additional fields widget
0136   addFieldsWidget(DBLPFetcher::allOptionalFields(), fetcher_ ? fetcher_->optionalFields() : QStringList());
0137 }
0138 
0139 void DBLPFetcher::ConfigWidget::saveConfigHook(KConfigGroup&) {
0140 }
0141 
0142 QString DBLPFetcher::ConfigWidget::preferredName() const {
0143   return DBLPFetcher::defaultName();
0144 }