File indexing completed on 2024-05-19 05:05:51

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2023 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 "mainwindow.h"
0021 
0022 #include <QDockWidget>
0023 #include <QDragEnterEvent>
0024 #include <QDropEvent>
0025 #include <QLabel>
0026 #include <QMimeData>
0027 #include <QPointer>
0028 #include <QMenu>
0029 #include <QTimer>
0030 #include <QApplication>
0031 #include <QFileDialog>
0032 #include <QAction>
0033 
0034 #include <kwidgetsaddons_version.h>
0035 #include <KActionMenu>
0036 #include <KActionCollection>
0037 #include <KPluginFactory>
0038 #include <KLocalizedString>
0039 #include <KMessageBox>
0040 
0041 #include <KBibTeX>
0042 #include <preferences/KBibTeXPreferencesDialog>
0043 #include <file/FileView>
0044 #include <file/Clipboard>
0045 #include <BibliographyService>
0046 #include <BibUtils>
0047 #include "docklets/referencepreview.h"
0048 #include "docklets/documentpreview.h"
0049 #include "docklets/searchform.h"
0050 #include "docklets/searchresults.h"
0051 #include "docklets/elementform.h"
0052 #include "docklets/statistics.h"
0053 #include "docklets/filesettings.h"
0054 #include "docklets/valuelist.h"
0055 #include "docklets/zoterobrowser.h"
0056 #include "documentlist.h"
0057 #include "mdiwidget.h"
0058 
0059 class KBibTeXMainWindow::KBibTeXMainWindowPrivate
0060 {
0061 private:
0062     KBibTeXMainWindow *p;
0063 
0064 public:
0065     QAction *actionClose;
0066     QDockWidget *dockDocumentList;
0067     QDockWidget *dockReferencePreview;
0068     QDockWidget *dockDocumentPreview;
0069     QDockWidget *dockValueList;
0070     QDockWidget *dockZotero;
0071     QDockWidget *dockStatistics;
0072     QDockWidget *dockSearchForm;
0073     QDockWidget *dockSearchResults;
0074     QDockWidget *dockElementForm;
0075     QDockWidget *dockFileSettings;
0076     DocumentList *listDocumentList;
0077     MDIWidget *mdiWidget;
0078     ReferencePreview *referencePreview;
0079     DocumentPreview *documentPreview;
0080     FileSettings *fileSettings;
0081     ValueList *valueList;
0082     ZoteroBrowser *zotero;
0083     Statistics *statistics;
0084     SearchForm *searchForm;
0085     SearchResults *searchResults;
0086     ElementForm *elementForm;
0087     QMenu *actionMenuRecentFilesMenu;
0088 
0089     KBibTeXMainWindowPrivate(KBibTeXMainWindow *parent)
0090             : p(parent)
0091     {
0092         mdiWidget = new MDIWidget(p);
0093 
0094         KActionMenu *showPanelsAction = new KActionMenu(i18n("Show Panels"), p);
0095         p->actionCollection()->addAction(QStringLiteral("settings_shown_panels"), showPanelsAction);
0096         QMenu *showPanelsMenu = new QMenu(showPanelsAction->text(), p->widget());
0097         showPanelsAction->setMenu(showPanelsMenu);
0098 
0099         KActionMenu *actionMenuRecentFiles = new KActionMenu(QIcon::fromTheme(QStringLiteral("document-open-recent")), i18n("Recently used files"), p);
0100         p->actionCollection()->addAction(QStringLiteral("file_open_recent"), actionMenuRecentFiles);
0101         actionMenuRecentFilesMenu = new QMenu(actionMenuRecentFiles->text(), p->widget());
0102         actionMenuRecentFiles->setMenu(actionMenuRecentFilesMenu);
0103 
0104         /**
0105          * Docklets (a.k.a. panels) will be added by default to the following
0106          * positions unless otherwise configured by the user.
0107          * - "List of Values" on the left
0108          * - "Statistics" on the left
0109          * - "List of Documents" on the left in the same tab
0110          * - "Online Search" on the left in a new tab
0111          * - "Reference Preview" on the left in the same tab
0112          * - "Search Results" on the bottom
0113          * - "Document Preview" is hidden
0114          * - "Element Editor" is hidden
0115          */
0116         dockDocumentList = new QDockWidget(i18n("List of Documents"), p);
0117         dockDocumentList->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0118         p->addDockWidget(Qt::LeftDockWidgetArea, dockDocumentList);
0119         listDocumentList = new DocumentList(dockDocumentList);
0120         dockDocumentList->setWidget(listDocumentList);
0121         dockDocumentList->setObjectName(QStringLiteral("dockDocumentList"));
0122         dockDocumentList->setFeatures(QDockWidget::DockWidgetClosable    | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0123         connect(listDocumentList, &DocumentList::openFile, p, &KBibTeXMainWindow::openDocument);
0124         showPanelsMenu->addAction(dockDocumentList->toggleViewAction());
0125 
0126         dockValueList = new QDockWidget(i18n("List of Values"), p);
0127         dockValueList->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0128         p->addDockWidget(Qt::LeftDockWidgetArea, dockValueList);
0129         valueList = new ValueList(dockValueList);
0130         dockValueList->setWidget(valueList);
0131         dockValueList->setObjectName(QStringLiteral("dockValueList"));
0132         dockValueList->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0133         showPanelsMenu->addAction(dockValueList->toggleViewAction());
0134 
0135         dockStatistics = new QDockWidget(i18n("Statistics"), p);
0136         dockStatistics->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0137         p->addDockWidget(Qt::LeftDockWidgetArea, dockStatistics);
0138         statistics = new Statistics(dockStatistics);
0139         dockStatistics->setWidget(statistics);
0140         dockStatistics->setObjectName(QStringLiteral("dockStatistics"));
0141         dockStatistics->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0142         showPanelsMenu->addAction(dockStatistics->toggleViewAction());
0143 
0144         dockSearchResults = new QDockWidget(i18n("Search Results"), p);
0145         dockSearchResults->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0146         p->addDockWidget(Qt::BottomDockWidgetArea, dockSearchResults);
0147         dockSearchResults->hide();
0148         searchResults = new SearchResults(mdiWidget, dockSearchResults);
0149         dockSearchResults->setWidget(searchResults);
0150         dockSearchResults->setObjectName(QStringLiteral("dockResultsFrom"));
0151         dockSearchResults->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0152         showPanelsMenu->addAction(dockSearchResults->toggleViewAction());
0153         connect(mdiWidget, &MDIWidget::documentSwitched, searchResults, &SearchResults::documentSwitched);
0154 
0155         dockSearchForm = new QDockWidget(i18n("Online Search"), p);
0156         dockSearchForm->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0157         p->addDockWidget(Qt::LeftDockWidgetArea, dockSearchForm);
0158         searchForm = new SearchForm(searchResults, dockSearchForm);
0159         connect(searchForm, &SearchForm::doneSearching, p, &KBibTeXMainWindow::showSearchResults);
0160         dockSearchForm->setWidget(searchForm);
0161         dockSearchForm->setObjectName(QStringLiteral("dockSearchFrom"));
0162         dockSearchForm->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0163         showPanelsMenu->addAction(dockSearchForm->toggleViewAction());
0164 
0165         dockZotero = new QDockWidget(i18n("Zotero"), p);
0166         dockZotero->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0167         p->addDockWidget(Qt::LeftDockWidgetArea, dockZotero);
0168         zotero = new ZoteroBrowser(searchResults, dockZotero);
0169         connect(dockZotero, &QDockWidget::visibilityChanged, zotero, &ZoteroBrowser::visibiltyChanged);
0170         connect(zotero, &ZoteroBrowser::itemToShow, p, &KBibTeXMainWindow::showSearchResults);
0171         dockZotero->setWidget(zotero);
0172         dockZotero->setObjectName(QStringLiteral("dockZotero"));
0173         dockZotero->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0174         showPanelsMenu->addAction(dockZotero->toggleViewAction());
0175 
0176         dockReferencePreview = new QDockWidget(i18n("Reference Preview"), p);
0177         dockReferencePreview->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0178         p->addDockWidget(Qt::LeftDockWidgetArea, dockReferencePreview);
0179         referencePreview = new ReferencePreview(dockReferencePreview);
0180         dockReferencePreview->setWidget(referencePreview);
0181         dockReferencePreview->setObjectName(QStringLiteral("dockReferencePreview"));
0182         dockReferencePreview->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0183         showPanelsMenu->addAction(dockReferencePreview->toggleViewAction());
0184 
0185         dockDocumentPreview = new QDockWidget(i18n("Document Preview"), p);
0186         dockDocumentPreview->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0187         p->addDockWidget(Qt::RightDockWidgetArea, dockDocumentPreview);
0188         dockDocumentPreview->hide();
0189         documentPreview = new DocumentPreview(dockDocumentPreview);
0190         dockDocumentPreview->setWidget(documentPreview);
0191         dockDocumentPreview->setObjectName(QStringLiteral("dockDocumentPreview"));
0192         dockDocumentPreview->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0193         showPanelsMenu->addAction(dockDocumentPreview->toggleViewAction());
0194         p->actionCollection()->setDefaultShortcut(dockDocumentPreview->toggleViewAction(), Qt::CTRL | Qt::SHIFT | Qt::Key_D);
0195 
0196         dockElementForm = new QDockWidget(i18n("Element Editor"), p);
0197         dockElementForm->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0198         p->addDockWidget(Qt::BottomDockWidgetArea, dockElementForm);
0199         dockElementForm->hide();
0200         elementForm = new ElementForm(mdiWidget, dockElementForm);
0201         dockElementForm->setWidget(elementForm);
0202         dockElementForm->setObjectName(QStringLiteral("dockElementFrom"));
0203         dockElementForm->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0204         showPanelsMenu->addAction(dockElementForm->toggleViewAction());
0205 
0206         dockFileSettings = new QDockWidget(i18n("File Settings"), p);
0207         dockFileSettings->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
0208         p->addDockWidget(Qt::LeftDockWidgetArea, dockFileSettings);
0209         fileSettings = new FileSettings(dockFileSettings);
0210         dockFileSettings->setWidget(fileSettings);
0211         dockFileSettings->setObjectName(QStringLiteral("dockFileSettings"));
0212         dockFileSettings->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0213         showPanelsMenu->addAction(dockFileSettings->toggleViewAction());
0214 
0215         p->tabifyDockWidget(dockFileSettings, dockSearchForm);
0216         p->tabifyDockWidget(dockZotero, dockSearchForm);
0217         p->tabifyDockWidget(dockValueList, dockStatistics);
0218         p->tabifyDockWidget(dockStatistics, dockFileSettings);
0219         p->tabifyDockWidget(dockSearchForm, dockReferencePreview);
0220         p->tabifyDockWidget(dockFileSettings, dockDocumentList);
0221 
0222         QAction *action = p->actionCollection()->addAction(KStandardAction::New);
0223         connect(action, &QAction::triggered, p, &KBibTeXMainWindow::newDocument);
0224         action = p->actionCollection()->addAction(KStandardAction::Open);
0225         connect(action, &QAction::triggered, p, &KBibTeXMainWindow::openDocumentDialog);
0226         actionClose = p->actionCollection()->addAction(KStandardAction::Close);
0227         connect(actionClose, &QAction::triggered, p, []() {
0228             OpenFileInfoManager::instance().close(OpenFileInfoManager::instance().currentFile());
0229         });
0230         actionClose->setEnabled(false);
0231         action = p->actionCollection()->addAction(KStandardAction::Quit);
0232         connect(action, &QAction::triggered, p, &KBibTeXMainWindow::queryCloseAll);
0233         action = p->actionCollection()->addAction(KStandardAction::Preferences);
0234         connect(action, &QAction::triggered, p, &KBibTeXMainWindow::showPreferences);
0235     }
0236 };
0237 
0238 KBibTeXMainWindow::KBibTeXMainWindow(QWidget *parent)
0239         : KParts::MainWindow(parent), d(new KBibTeXMainWindowPrivate(this))
0240 {
0241     setObjectName(QStringLiteral("KBibTeXShell"));
0242 
0243     setXMLFile(QStringLiteral("kbibtexui.rc"));
0244 
0245     setCentralWidget(d->mdiWidget);
0246 
0247     connect(d->mdiWidget, &MDIWidget::documentSwitched, this, &KBibTeXMainWindow::documentSwitched);
0248     connect(d->mdiWidget, &MDIWidget::activePartChanged, this, &KBibTeXMainWindow::createGUI); ///< actually: KParts::MainWindow::createGUI
0249     connect(d->mdiWidget, &MDIWidget::documentNew, this, &KBibTeXMainWindow::newDocument);
0250     connect(d->mdiWidget, &MDIWidget::documentOpen, this, &KBibTeXMainWindow::openDocumentDialog);
0251     connect(d->mdiWidget, &MDIWidget::documentOpenURL, this, &KBibTeXMainWindow::openDocument);
0252     connect(&OpenFileInfoManager::instance(), &OpenFileInfoManager::currentChanged, d->mdiWidget, &MDIWidget::setFile);
0253     connect(&OpenFileInfoManager::instance(), &OpenFileInfoManager::flagsChanged, this, &KBibTeXMainWindow::documentListsChanged);
0254     connect(d->mdiWidget, &MDIWidget::setCaption, this, static_cast<void(KMainWindow::*)(const QString &)>(&KMainWindow::setCaption)); ///< actually: KMainWindow::setCaption
0255 
0256     documentListsChanged(OpenFileInfo::StatusFlag::RecentlyUsed); /// force initialization of menu of recently used files
0257 
0258     setupControllers();
0259     setupGUI(KXmlGuiWindow::Create | KXmlGuiWindow::Save | KXmlGuiWindow::Keys | KXmlGuiWindow::ToolBar);
0260 
0261     setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
0262     setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
0263     setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
0264     setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
0265 
0266     setAcceptDrops(true);
0267 
0268     QTimer::singleShot(500, this, &KBibTeXMainWindow::delayed);
0269 }
0270 
0271 KBibTeXMainWindow::~KBibTeXMainWindow()
0272 {
0273     delete d;
0274 }
0275 
0276 void KBibTeXMainWindow::setupControllers()
0277 {
0278     // TODO
0279 }
0280 
0281 void KBibTeXMainWindow::dragEnterEvent(QDragEnterEvent *event)
0282 {
0283     if (!Clipboard::urlsToOpen(event->mimeData()).isEmpty())
0284         event->acceptProposedAction();
0285 }
0286 
0287 void KBibTeXMainWindow::dropEvent(QDropEvent *event)
0288 {
0289     const QSet<QUrl> urls = Clipboard::urlsToOpen(event->mimeData());
0290     for (const QUrl &url : urls)
0291         openDocument(url);
0292 }
0293 
0294 void KBibTeXMainWindow::newDocument()
0295 {
0296     const QString mimeType = FileInfo::mimetypeBibTeX;
0297     OpenFileInfo *openFileInfo = OpenFileInfoManager::instance().createNew(mimeType);
0298     if (openFileInfo)
0299         OpenFileInfoManager::instance().setCurrentFile(openFileInfo);
0300     else
0301         KMessageBox::error(this, i18n("Creating a new document of mime type '%1' failed as no editor component could be instantiated.", mimeType), i18n("Creating document failed"));
0302 }
0303 
0304 void KBibTeXMainWindow::openDocumentDialog()
0305 {
0306     OpenFileInfo *currFile = OpenFileInfoManager::instance().currentFile();
0307     QUrl currFileUrl = currFile == nullptr ? QUrl() : currFile->url();
0308     QString startDir = currFileUrl.isValid() ? QUrl(currFileUrl.url()).path() : QString();
0309     OpenFileInfo *ofi = OpenFileInfoManager::instance().currentFile();
0310     if (ofi != nullptr) {
0311         QUrl url = ofi->url();
0312         if (url.isValid()) startDir = url.path();
0313     }
0314 
0315     /// Assemble list of supported mimetypes
0316     QStringList supportedMimeTypes {QStringLiteral("text/x-bibtex"), QStringLiteral("application/x-research-info-systems"), QStringLiteral("application/xml")};
0317     if (BibUtils::available()) {
0318         supportedMimeTypes.append(QStringLiteral("application/x-isi-export-format"));
0319         supportedMimeTypes.append(QStringLiteral("application/x-endnote-refer"));
0320     }
0321     supportedMimeTypes.append(QStringLiteral("application/pdf"));
0322     supportedMimeTypes.append(QStringLiteral("all/all"));
0323     QPointer<QFileDialog> dlg = new QFileDialog(this, i18n("Open file") /* TODO better text */, startDir);
0324     dlg->setMimeTypeFilters(supportedMimeTypes);
0325     dlg->setFileMode(QFileDialog::ExistingFile);
0326 
0327     const bool dialogAccepted = dlg->exec() != 0;
0328     const QUrl url = (dialogAccepted && !dlg->selectedUrls().isEmpty()) ? dlg->selectedUrls().first() : QUrl();
0329 
0330     delete dlg;
0331 
0332     if (url.isValid())
0333         openDocument(url);
0334 }
0335 
0336 void KBibTeXMainWindow::openDocument(const QUrl &url)
0337 {
0338     OpenFileInfo *openFileInfo = OpenFileInfoManager::instance().open(url);
0339     OpenFileInfoManager::instance().setCurrentFile(openFileInfo);
0340 }
0341 
0342 void KBibTeXMainWindow::closeEvent(QCloseEvent *event)
0343 {
0344     KMainWindow::closeEvent(event);
0345 
0346     if (OpenFileInfoManager::instance().queryCloseAll())
0347         event->accept();
0348     else
0349         event->ignore();
0350 }
0351 
0352 void KBibTeXMainWindow::showPreferences()
0353 {
0354     QPointer<KBibTeXPreferencesDialog> dlg = new KBibTeXPreferencesDialog(this);
0355     dlg->exec();
0356     delete dlg;
0357 }
0358 
0359 void KBibTeXMainWindow::documentSwitched(FileView *oldFileView, FileView *newFileView)
0360 {
0361     OpenFileInfo *openFileInfo = d->mdiWidget->currentFile();
0362     bool validFile = openFileInfo != nullptr;
0363     d->actionClose->setEnabled(validFile);
0364 
0365     setCaption(validFile ? i18n("%1 - KBibTeX", openFileInfo->shortCaption()) : i18n("KBibTeX"));
0366 
0367     d->fileSettings->setEnabled(newFileView != nullptr);
0368     d->referencePreview->setEnabled(newFileView != nullptr);
0369     d->elementForm->setEnabled(newFileView != nullptr);
0370     d->documentPreview->setEnabled(newFileView != nullptr);
0371     if (oldFileView != nullptr) {
0372         disconnect(newFileView, &FileView::currentElementChanged, d->referencePreview, &ReferencePreview::setElement);
0373         disconnect(newFileView, &FileView::currentElementChanged, d->elementForm, &ElementForm::setElement);
0374         disconnect(newFileView, &FileView::currentElementChanged, d->documentPreview, &DocumentPreview::setElement);
0375         disconnect(newFileView, &FileView::currentElementChanged, d->searchForm, &SearchForm::setElement);
0376         disconnect(newFileView, &FileView::modified, d->valueList, &ValueList::update);
0377         disconnect(newFileView, &FileView::modified, d->statistics, &Statistics::update);
0378         // FIXME disconnect(oldEditor, SIGNAL(modified()), d->elementForm, SLOT(refreshElement()));
0379         disconnect(d->elementForm, &ElementForm::elementModified, newFileView, &FileView::externalModification);
0380     }
0381     if (newFileView != nullptr) {
0382         connect(newFileView, &FileView::currentElementChanged, d->referencePreview, &ReferencePreview::setElement);
0383         connect(newFileView, &FileView::currentElementChanged, d->elementForm, &ElementForm::setElement);
0384         connect(newFileView, &FileView::currentElementChanged, d->documentPreview, &DocumentPreview::setElement);
0385         connect(newFileView, &FileView::currentElementChanged, d->searchForm, &SearchForm::setElement);
0386         connect(newFileView, &FileView::modified, d->valueList, &ValueList::update);
0387         connect(newFileView, &FileView::modified, d->statistics, &Statistics::update);
0388         // FIXME connect(newEditor, SIGNAL(modified()), d->elementForm, SLOT(refreshElement()));
0389         connect(d->elementForm, &ElementForm::elementModified, newFileView, &FileView::externalModification);
0390         connect(d->elementForm, &ElementForm::elementModified, newFileView, &FileView::externalModification);
0391     }
0392 
0393     d->documentPreview->setBibTeXUrl(validFile ? openFileInfo->url() : QUrl());
0394     d->referencePreview->setElement(QSharedPointer<const Element>(), nullptr);
0395     d->elementForm->setElement(QSharedPointer<Element>(), nullptr);
0396     d->documentPreview->setElement(QSharedPointer<Element>(), nullptr);
0397     d->valueList->setFileView(newFileView);
0398     d->fileSettings->setFileView(newFileView);
0399     d->statistics->setFileView(newFileView);
0400     d->referencePreview->setFileView(newFileView);
0401 }
0402 
0403 void KBibTeXMainWindow::showSearchResults()
0404 {
0405     d->dockSearchResults->show();
0406 }
0407 
0408 void KBibTeXMainWindow::documentListsChanged(OpenFileInfo::StatusFlags statusFlags)
0409 {
0410     if (statusFlags.testFlag(OpenFileInfo::StatusFlag::RecentlyUsed)) {
0411         const OpenFileInfoManager::OpenFileInfoList list = OpenFileInfoManager::instance().filteredItems(OpenFileInfo::StatusFlag::RecentlyUsed);
0412         d->actionMenuRecentFilesMenu->clear();
0413         for (OpenFileInfo *cur : list) {
0414             /// Fixing bug 19511: too long filenames make menu too large,
0415             /// therefore squeeze text if it is longer than squeezeLen.
0416             const int squeezeLen = 64;
0417             const QString squeezedShortCap = squeeze_text(cur->shortCaption(), squeezeLen);
0418             const QString squeezedFullCap = squeeze_text(cur->fullCaption(), squeezeLen);
0419             QAction *action = new QAction(QString(QStringLiteral("%1 [%2]")).arg(squeezedShortCap, squeezedFullCap), this);
0420             action->setData(cur->url());
0421             action->setIcon(QIcon::fromTheme(cur->mimeType().replace(QLatin1Char('/'), QLatin1Char('-'))));
0422             d->actionMenuRecentFilesMenu->addAction(action);
0423             connect(action, &QAction::triggered, this, &KBibTeXMainWindow::openRecentFile);
0424         }
0425     }
0426 }
0427 
0428 void KBibTeXMainWindow::openRecentFile()
0429 {
0430     QAction *action = static_cast<QAction *>(sender());
0431     QUrl url = action->data().toUrl();
0432     openDocument(url);
0433 }
0434 
0435 void KBibTeXMainWindow::queryCloseAll()
0436 {
0437     if (OpenFileInfoManager::instance().queryCloseAll())
0438         qApp->quit();
0439 }
0440 
0441 void KBibTeXMainWindow::delayed() {
0442     /// Static variable, memorizes the dynamically created
0443     /// BibliographyService instance and allows to tell if
0444     /// this slot was called for the first or second time.
0445     static BibliographyService *bs = nullptr;
0446 
0447     if (bs == nullptr) {
0448         /// First call to this slot
0449         bs = new BibliographyService(this);
0450         if (!bs->isKBibTeXdefault() &&
0451 #if KWIDGETSADDONS_VERSION < QT_VERSION_CHECK(5, 100, 0)
0452                 KMessageBox::questionYesNo(this, i18n("KBibTeX is not the default editor for its bibliography formats like BibTeX or RIS."), i18n("Default Bibliography Editor"), KGuiItem(i18n("Set as Default Editor")), KGuiItem(i18n("Keep settings unchanged")), QStringLiteral("DontAskAgain_SetKBibTeXAsDefaultBibliographyEditor")) == KMessageBox::Yes
0453 #else // >= 5.100.0
0454                 KMessageBox::questionTwoActions(this, i18n("KBibTeX is not the default editor for its bibliography formats like BibTeX or RIS."), i18n("Default Bibliography Editor"), KGuiItem(i18n("Set as Default Editor")), KGuiItem(i18n("Keep settings unchanged")), QStringLiteral("DontAskAgain_SetKBibTeXAsDefaultBibliographyEditor")) == KMessageBox::PrimaryAction
0455 #endif // KWIDGETSADDONS_VERSION < QT_VERSION_CHECK(5, 100, 0)
0456            ) {
0457             bs->setKBibTeXasDefault();
0458             /// QTimer calls this slot again, but as 'bs' will not be NULL,
0459             /// the 'if' construct's 'else' path will be followed.
0460             QTimer::singleShot(5000, this, &KBibTeXMainWindow::delayed);
0461         } else {
0462             /// KBibTeX is default application or user doesn't care,
0463             /// therefore clean up memory
0464             delete bs;
0465             bs = nullptr;
0466         }
0467     } else {
0468         /// Second call to this slot. This time, clean up memory.
0469         bs->deleteLater();
0470         bs = nullptr;
0471     }
0472 }