File indexing completed on 2024-06-02 05:09:30

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2020 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #include "onlinesearchgeneral.h"
0021 
0022 #ifdef HAVE_QTWIDGETS
0023 #include <QFormLayout>
0024 #include <QLabel>
0025 #include <QSpinBox>
0026 #include <QLineEdit>
0027 
0028 #include <KLocalizedString>
0029 #include <KConfigGroup>
0030 
0031 #include <KBibTeX>
0032 #include <Entry>
0033 #include "onlinesearchabstract_p.h"
0034 #include "logging_networking.h"
0035 
0036 namespace OnlineSearchGeneral {
0037 
0038 #ifdef HAVE_QTWIDGETS
0039 class Form::Private : public OnlineSearchAbstract::Form::Private
0040 {
0041 public:
0042     explicit Private(Form *parent)
0043             : OnlineSearchAbstract::Form::Private(),
0044           configGroupName(QStringLiteral("Search Engine General"))
0045     {
0046         QFormLayout *layout = new QFormLayout(parent);
0047         layout->setContentsMargins(0, 0, 0, 0);
0048 
0049         QLabel *label = new QLabel(i18n("Free text or DOI:"), parent);
0050         QLineEdit *lineEdit = new QLineEdit(parent);
0051         layout->addRow(label, lineEdit);
0052         lineEdit->setClearButtonEnabled(true);
0053         lineEdit->setFocus(Qt::TabFocusReason);
0054         queryFields.insert(OnlineSearchAbstract::QueryKey::FreeText, lineEdit);
0055         label->setBuddy(lineEdit);
0056         connect(lineEdit, &QLineEdit::returnPressed, parent, &OnlineSearchAbstract::Form::returnPressed);
0057 
0058         label = new QLabel(i18n("Title:"), parent);
0059         lineEdit = new QLineEdit(parent);
0060         layout->addRow(label, lineEdit);
0061         lineEdit->setClearButtonEnabled(true);
0062         queryFields.insert(OnlineSearchAbstract::QueryKey::Title, lineEdit);
0063         label->setBuddy(lineEdit);
0064         connect(lineEdit, &QLineEdit::returnPressed, parent, &OnlineSearchAbstract::Form::returnPressed);
0065 
0066         label = new QLabel(i18n("Author:"), parent);
0067         lineEdit = new QLineEdit(parent);
0068         layout->addRow(label, lineEdit);
0069         lineEdit->setClearButtonEnabled(true);
0070         queryFields.insert(OnlineSearchAbstract::QueryKey::Author, lineEdit);
0071         label->setBuddy(lineEdit);
0072         connect(lineEdit, &QLineEdit::returnPressed, parent, &OnlineSearchAbstract::Form::returnPressed);
0073 
0074         label = new QLabel(i18n("Year:"), parent);
0075         lineEdit = new QLineEdit(parent);
0076         layout->addRow(label, lineEdit);
0077         lineEdit->setClearButtonEnabled(true);
0078         queryFields.insert(OnlineSearchAbstract::QueryKey::Year, lineEdit);
0079         label->setBuddy(lineEdit);
0080         connect(lineEdit, &QLineEdit::returnPressed, parent, &OnlineSearchAbstract::Form::returnPressed);
0081 
0082         label = new QLabel(i18n("Number of Results:"), parent);
0083         numResultsField = new QSpinBox(parent);
0084         layout->addRow(label, numResultsField);
0085         numResultsField->setMinimum(3);
0086         numResultsField->setMaximum(100);
0087         numResultsField->setValue(20);
0088         label->setBuddy(numResultsField);
0089 
0090         loadState();
0091     }
0092 
0093     virtual ~Private() {
0094         /// nothing
0095     }
0096 
0097     QMap<OnlineSearchAbstract::QueryKey, QLineEdit *> queryFields;
0098     QSpinBox *numResultsField;
0099     const QString configGroupName;
0100 
0101     static QString queryKeyToString(const OnlineSearchAbstract::QueryKey &queryKey)
0102     {
0103         switch (queryKey) {
0104         case OnlineSearchAbstract::QueryKey::FreeText: return QStringLiteral("free");
0105         case OnlineSearchAbstract::QueryKey::Title: return QStringLiteral("title");
0106         case OnlineSearchAbstract::QueryKey::Author: return QStringLiteral("author");
0107         case OnlineSearchAbstract::QueryKey::Year: return QStringLiteral("year");
0108         default: {
0109             qCWarning(LOG_KBIBTEX_NETWORKING) << "Encountered an unsupported queryKey:" << static_cast<int>(queryKey);
0110             return QStringLiteral("free");
0111         }
0112         }
0113     }
0114 
0115     void loadState()
0116     {
0117         KConfigGroup configGroup(config, configGroupName);
0118         for (QMap<OnlineSearchAbstract::QueryKey, QLineEdit *>::ConstIterator it = queryFields.constBegin(); it != queryFields.constEnd(); ++it) {
0119             it.value()->setText(configGroup.readEntry(queryKeyToString(it.key()), QString()));
0120         }
0121         numResultsField->setValue(configGroup.readEntry(QStringLiteral("numResults"), 10));
0122     }
0123 
0124     void saveState()
0125     {
0126         KConfigGroup configGroup(config, configGroupName);
0127         for (QMap<OnlineSearchAbstract::QueryKey, QLineEdit *>::ConstIterator it = queryFields.constBegin(); it != queryFields.constEnd(); ++it) {
0128             configGroup.writeEntry(queryKeyToString(it.key()), it.value()->text());
0129         }
0130         configGroup.writeEntry(QStringLiteral("numResults"), numResultsField->value());
0131         config->sync();
0132     }
0133 };
0134 
0135 Form::Form(QWidget *parent)
0136         : OnlineSearchAbstract::Form(parent), dg(new OnlineSearchGeneral::Form::Private(this))
0137 {
0138     /// nothing
0139 }
0140 
0141 Form::~Form()
0142 {
0143     delete dg;
0144 }
0145 
0146 #endif // HAVE_QTWIDGETS
0147 
0148 
0149 bool Form::readyToStart() const
0150 {
0151     for (QMap<OnlineSearchAbstract::QueryKey, QLineEdit *>::ConstIterator it = dg->queryFields.constBegin(); it != dg->queryFields.constEnd(); ++it)
0152         if (!it.value()->text().isEmpty())
0153             return true;
0154 
0155     return false;
0156 }
0157 
0158 void Form::copyFromEntry(const Entry &entry)
0159 {
0160     dg->queryFields[OnlineSearchAbstract::QueryKey::FreeText]->setText(d->guessFreeText(entry));
0161     dg->queryFields[OnlineSearchAbstract::QueryKey::Title]->setText(PlainTextValue::text(entry[Entry::ftTitle]));
0162     dg->queryFields[OnlineSearchAbstract::QueryKey::Author]->setText(d->authorLastNames(entry).join(QStringLiteral(" ")));
0163     dg->queryFields[OnlineSearchAbstract::QueryKey::Year]->setText(PlainTextValue::text(entry[Entry::ftYear]));
0164 }
0165 
0166 QMap<OnlineSearchAbstract::QueryKey, QString> Form::getQueryTerms()
0167 {
0168     QMap<OnlineSearchAbstract::QueryKey, QString> result;
0169 
0170     for (QMap<OnlineSearchAbstract::QueryKey, QLineEdit *>::ConstIterator it = dg->queryFields.constBegin(); it != dg->queryFields.constEnd(); ++it) {
0171         if (!it.value()->text().isEmpty())
0172             result.insert(it.key(), it.value()->text());
0173     }
0174 
0175     dg->saveState();
0176     return result;
0177 }
0178 
0179 int Form::getNumResults()
0180 {
0181     return dg->numResultsField->value();
0182 }
0183 
0184 } /// namespace OnlineSearchGeneral
0185 
0186 // #include "onlinesearchgeneral.moc"
0187 
0188 #endif // HAVE_QTWIDGETS