File indexing completed on 2024-04-28 05:08:14

0001 /***************************************************************************
0002     Copyright (C) 2011 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 "bibtexkeydialog.h"
0026 #include "collections/bibtexcollection.h"
0027 #include "entry.h"
0028 #include "tellico_debug.h"
0029 
0030 #include <KLocalizedString>
0031 #include <KTitleWidget>
0032 #include <KSharedConfig>
0033 #include <KGuiItem>
0034 #include <kwidgetsaddons_version.h>
0035 
0036 #include <QLabel>
0037 #include <QVBoxLayout>
0038 #include <QTimer>
0039 #include <QDialogButtonBox>
0040 #include <QPushButton>
0041 
0042 using Tellico::BibtexKeyDialog;
0043 
0044 // default button is going to be used as a print button, so it's separated
0045 BibtexKeyDialog::BibtexKeyDialog(Tellico::Data::CollPtr coll_, QWidget* parent_)
0046     : QDialog(parent_), m_coll(coll_) {
0047   Q_ASSERT(m_coll);
0048   setModal(false);
0049   setWindowTitle(i18n("Citation Key Manager"));
0050 
0051   QVBoxLayout* topLayout = new QVBoxLayout;
0052   setLayout(topLayout);
0053 
0054   QWidget* mainWidget = new QWidget(this);
0055   topLayout->addWidget(mainWidget);
0056 
0057   m_dupeLabel = new KTitleWidget(this);
0058   m_dupeLabel->setText(m_coll->title(), KTitleWidget::PlainMessage);
0059   m_dupeLabel->setComment(i18n("Checking for entries with duplicate citation keys..."));
0060 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5,63,0)
0061   m_dupeLabel->setIcon(QIcon::fromTheme(QStringLiteral("tools-wizard")), KTitleWidget::ImageLeft);
0062 #else
0063   m_dupeLabel->setPixmap(QIcon::fromTheme(QStringLiteral("tools-wizard")).pixmap(64, 64), KTitleWidget::ImageLeft);
0064 #endif
0065   topLayout->addWidget(m_dupeLabel);
0066 
0067   QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
0068   buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
0069   connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0070   connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0071 
0072   QPushButton* checkDuplicates = new QPushButton(buttonBox);
0073   KGuiItem::assign(checkDuplicates, KGuiItem(i18n("Check for duplicates"), QStringLiteral("system-search")));
0074   buttonBox->addButton(checkDuplicates, QDialogButtonBox::ActionRole);
0075   m_filterButton = new QPushButton(buttonBox);
0076   KGuiItem::assign(m_filterButton, KGuiItem(i18n("Filter for duplicates"), QStringLiteral("view-filter")));
0077   buttonBox->addButton(m_filterButton, QDialogButtonBox::ActionRole);
0078 
0079   topLayout->addWidget(buttonBox);
0080 
0081   if(m_coll->type() != Data::Collection::Bibtex)  {
0082     // if it's not a bibliography, no need to save a pointer
0083     m_coll = Data::CollPtr();
0084     myWarning() << "not a bibliography";
0085   } else {
0086     // the button is enabled when duplicates are found
0087     m_filterButton->setEnabled(false);
0088     connect(m_filterButton, &QAbstractButton::clicked, this, &BibtexKeyDialog::slotFilterDuplicates);
0089     connect(checkDuplicates, &QAbstractButton::clicked, this, &BibtexKeyDialog::slotCheckDuplicates);
0090     QTimer::singleShot(0, this, &BibtexKeyDialog::slotCheckDuplicatesImpl);
0091   }
0092 }
0093 
0094 BibtexKeyDialog::~BibtexKeyDialog() {
0095 }
0096 
0097 void BibtexKeyDialog::slotCheckDuplicates() {
0098   if(!m_coll) {
0099     return;
0100   }
0101   m_dupeLabel->setComment(i18n("Checking for entries with duplicate citation keys..."));
0102   QTimer::singleShot(0, this, &BibtexKeyDialog::slotCheckDuplicatesImpl);
0103 }
0104 
0105 void BibtexKeyDialog::slotCheckDuplicatesImpl() {
0106   const Data::BibtexCollection* c = static_cast<Data::BibtexCollection*>(m_coll.data());
0107   m_dupes = c->duplicateBibtexKeys();
0108 
0109   m_filterButton->setEnabled(false);
0110   if(m_dupes.isEmpty())  {
0111     m_dupeLabel->setComment(i18n("There are no duplicate citation keys."));
0112     m_filterButton->setEnabled(false);
0113   } else {
0114     m_dupeLabel->setComment(i18np("There is %1 duplicate citation key.", "There are %1 duplicate citation keys.", m_dupes.count()));
0115     m_filterButton->setEnabled(true);
0116   }
0117 }
0118 
0119 void BibtexKeyDialog::slotFilterDuplicates() {
0120   if(!m_coll || m_dupes.isEmpty()) {
0121     return;
0122   }
0123 
0124   FilterPtr filter(new Filter(Filter::MatchAny));
0125 
0126   QSet<QString> keys;
0127   foreach(Data::EntryPtr entry, m_dupes) {
0128     const QString key = entry->field(QStringLiteral("bibtex-key"));
0129     if(!keys.contains(key)) {
0130       filter->append(new FilterRule(QStringLiteral("bibtex-key"), key, FilterRule::FuncEquals));
0131       keys << key;
0132     }
0133   }
0134 
0135   if(!filter->isEmpty()) {
0136     emit signalUpdateFilter(filter);
0137   }
0138 }