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

0001 /***************************************************************************
0002     Copyright (C) 2007-2009 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 "bibsonomyfetcher.h"
0026 #include "../translators/bibteximporter.h"
0027 #include "../utils/guiproxy.h"
0028 #include "../utils/string_utils.h"
0029 #include "../collection.h"
0030 #include "../entry.h"
0031 #include "../core/netaccess.h"
0032 #include "../core/filehandler.h"
0033 #include "../tellico_debug.h"
0034 
0035 #include <KLocalizedString>
0036 #include <KIO/Job>
0037 #include <KIO/JobUiDelegate>
0038 #include <KJobWidgets/KJobWidgets>
0039 
0040 #include <QLabel>
0041 #include <QVBoxLayout>
0042 #include <QUrlQuery>
0043 
0044 namespace {
0045   // always bibtex
0046   static const char* BIBSONOMY_BASE_URL = "http://bibsonomy.org/";
0047   static const int BIBSONOMY_MAX_RESULTS = 20;
0048 }
0049 
0050 using namespace Tellico;
0051 using Tellico::Fetch::BibsonomyFetcher;
0052 
0053 BibsonomyFetcher::BibsonomyFetcher(QObject* parent_)
0054     : Fetcher(parent_), m_job(nullptr), m_started(false) {
0055 }
0056 
0057 BibsonomyFetcher::~BibsonomyFetcher() {
0058 }
0059 
0060 QString BibsonomyFetcher::source() const {
0061   return m_name.isEmpty() ? defaultName() : m_name;
0062 }
0063 
0064 bool BibsonomyFetcher::canSearch(Fetch::FetchKey k) const {
0065   return k == Person || k == Keyword;
0066 }
0067 
0068 bool BibsonomyFetcher::canFetch(int type) const {
0069   return type == Data::Collection::Bibtex;
0070 }
0071 
0072 void BibsonomyFetcher::readConfigHook(const KConfigGroup&) {
0073 }
0074 
0075 void BibsonomyFetcher::search() {
0076   m_started = true;
0077 
0078 //  myDebug() << "value = " << value_;
0079 
0080   QUrl u(QString::fromLatin1(BIBSONOMY_BASE_URL));
0081   u.setPath(QStringLiteral("/bib/"));
0082 
0083   switch(request().key()) {
0084     case Person:
0085       u.setPath(u.path() + QStringLiteral("author/%1").arg(request().value()));
0086       break;
0087 
0088     case Keyword:
0089       u.setPath(u.path() + QStringLiteral("search/%1").arg(request().value()));
0090       break;
0091 
0092     default:
0093       myWarning() << source() << "- key not recognized:" << request().key();
0094       stop();
0095       return;
0096   }
0097   QUrlQuery q;
0098   q.addQueryItem(QStringLiteral("items"), QString::number(BIBSONOMY_MAX_RESULTS));
0099   u.setQuery(q);
0100 
0101   m_job = KIO::storedGet(u, KIO::NoReload, KIO::HideProgressInfo);
0102   KJobWidgets::setWindow(m_job, GUI::Proxy::widget());
0103   connect(m_job.data(), &KJob::result,
0104           this, &BibsonomyFetcher::slotComplete);
0105 }
0106 
0107 void BibsonomyFetcher::stop() {
0108   if(!m_started) {
0109     return;
0110   }
0111 //  myDebug();
0112   if(m_job) {
0113     m_job->kill();
0114     m_job = nullptr;
0115   }
0116   m_started = false;
0117   emit signalDone(this);
0118 }
0119 
0120 void BibsonomyFetcher::slotComplete(KJob*) {
0121 //  myDebug();
0122 
0123   if(m_job->error()) {
0124     m_job->uiDelegate()->showErrorMessage();
0125     stop();
0126     return;
0127   }
0128 
0129   QByteArray data = m_job->data();
0130   if(data.isEmpty()) {
0131     myDebug() << "no data";
0132     stop();
0133     return;
0134   }
0135 
0136   // since the fetch is done, don't worry about holding the job pointer
0137   m_job = nullptr;
0138 
0139   Import::BibtexImporter imp(QString::fromUtf8(data.constData(), data.size()));
0140   Data::CollPtr coll = imp.collection();
0141 
0142   if(!coll) {
0143     myDebug() << "no valid result";
0144     stop();
0145     return;
0146   }
0147 
0148   Data::EntryList entries = coll->entries();
0149   foreach(Data::EntryPtr entry, entries) {
0150     if(!m_started) {
0151       // might get aborted
0152       break;
0153     }
0154 
0155     FetchResult* r = new FetchResult(this, entry);
0156     m_entries.insert(r->uid, Data::EntryPtr(entry));
0157     emit signalResultFound(r);
0158   }
0159 
0160   stop(); // required
0161 }
0162 
0163 Tellico::Data::EntryPtr BibsonomyFetcher::fetchEntryHook(uint uid_) {
0164   return m_entries[uid_];
0165 }
0166 
0167 Tellico::Fetch::FetchRequest BibsonomyFetcher::updateRequest(Data::EntryPtr entry_) {
0168   QString title = entry_->field(QStringLiteral("title"));
0169   if(!title.isEmpty()) {
0170     return FetchRequest(Fetch::Keyword, title);
0171   }
0172   return FetchRequest();
0173 }
0174 
0175 Tellico::Fetch::ConfigWidget* BibsonomyFetcher::configWidget(QWidget* parent_) const {
0176   return new BibsonomyFetcher::ConfigWidget(parent_, this);
0177 }
0178 
0179 QString BibsonomyFetcher::defaultName() {
0180   return QStringLiteral("Bibsonomy");
0181 }
0182 
0183 QString BibsonomyFetcher::defaultIcon() {
0184   return favIcon("https://www.bibsonomy.org");
0185 }
0186 
0187 BibsonomyFetcher::ConfigWidget::ConfigWidget(QWidget* parent_, const BibsonomyFetcher*)
0188     : Fetch::ConfigWidget(parent_) {
0189   QVBoxLayout* l = new QVBoxLayout(optionsWidget());
0190   l->addWidget(new QLabel(i18n("This source has no options."), optionsWidget()));
0191   l->addStretch();
0192 }
0193 
0194 void BibsonomyFetcher::ConfigWidget::saveConfigHook(KConfigGroup&) {
0195 }
0196 
0197 QString BibsonomyFetcher::ConfigWidget::preferredName() const {
0198   return BibsonomyFetcher::defaultName();
0199 }