File indexing completed on 2024-10-06 04:35:40

0001 /*
0002     This class provides the network search toolbar.
0003 
0004     SPDX-FileCopyrightText: 2018-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4knetworksearchtoolbar.h"
0010 #include "core/smb4kshare.h"
0011 
0012 // Qt includes
0013 #include <QDebug>
0014 #include <QtGlobal>
0015 
0016 // KDE includes
0017 
0018 #include <KIconLoader>
0019 #include <KLineEdit>
0020 #include <KLocalizedString>
0021 
0022 Smb4KNetworkSearchToolBar::Smb4KNetworkSearchToolBar(QWidget *parent)
0023     : QToolBar(parent)
0024     , m_iterator(QStringList())
0025 {
0026     // Set up tool bar:
0027     // Use the settings suggested by the note provided in the 'Detailed Description'
0028     // section of KToolBar (https://api.kde.org/frameworks/kxmlgui/html/classKToolBar.html)
0029     setToolButtonStyle(Qt::ToolButtonFollowStyle);
0030     setProperty("otherToolbar", true);
0031 
0032     QAction *closeAction = new QAction(this);
0033     closeAction->setIcon(KDE::icon(QStringLiteral("window-close")));
0034     closeAction->setText(i18n("Close"));
0035 
0036     connect(closeAction, &QAction::triggered, this, &Smb4KNetworkSearchToolBar::slotCloseButtonPressed);
0037 
0038     addAction(closeAction);
0039 
0040     m_searchComboBox = new KComboBox(true, this);
0041     m_searchComboBox->lineEdit()->setPlaceholderText(i18n("Search string"));
0042     m_searchComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0043     m_searchComboBox->setCompletionMode(KCompletion::CompletionPopupAuto);
0044 
0045     connect(m_searchComboBox, QOverload<const QString &>::of(&KComboBox::returnPressed), this, &Smb4KNetworkSearchToolBar::slotReturnKeyPressed);
0046     // FIXME: Add a connection to the clearSearch() slot
0047 
0048     addWidget(m_searchComboBox);
0049 
0050     m_searchAction = new KDualAction(this);
0051     m_searchAction->setInactiveIcon(KDE::icon(QStringLiteral("search")));
0052     m_searchAction->setInactiveText(i18n("Search"));
0053     m_searchAction->setActiveIcon(KDE::icon(QStringLiteral("process-stop")));
0054     m_searchAction->setActiveText(i18n("Stop"));
0055     m_searchAction->setAutoToggle(false);
0056 
0057     connect(m_searchAction, &KDualAction::triggered, this, &Smb4KNetworkSearchToolBar::slotSearchActionTriggered);
0058 
0059     addAction(m_searchAction);
0060 
0061     m_downAction = new QAction(this);
0062     m_downAction->setIcon(KDE::icon(QStringLiteral("go-down-search")));
0063     m_downAction->setText(i18n("Item Down"));
0064     m_downAction->setEnabled(false);
0065 
0066     connect(m_downAction, &QAction::triggered, this, &Smb4KNetworkSearchToolBar::slotDownActionTriggered);
0067 
0068     addAction(m_downAction);
0069 
0070     m_upAction = new QAction(this);
0071     m_upAction->setIcon(KDE::icon(QStringLiteral("go-up-search")));
0072     m_upAction->setText(i18n("Item Up"));
0073     m_upAction->setEnabled(false);
0074 
0075     connect(m_upAction, &QAction::triggered, this, &Smb4KNetworkSearchToolBar::slotUpActionTriggered);
0076 
0077     addAction(m_upAction);
0078 
0079     m_clearAction = new QAction(this);
0080     m_clearAction->setIcon(KDE::icon(QStringLiteral("edit-clear-all")));
0081     m_clearAction->setText(i18n("Clear"));
0082     m_clearAction->setEnabled(false);
0083 
0084     connect(m_clearAction, &QAction::triggered, this, &Smb4KNetworkSearchToolBar::slotClearSearch);
0085 
0086     addAction(m_clearAction);
0087 }
0088 
0089 Smb4KNetworkSearchToolBar::~Smb4KNetworkSearchToolBar()
0090 {
0091 }
0092 
0093 void Smb4KNetworkSearchToolBar::prepareInput()
0094 {
0095     m_searchComboBox->lineEdit()->setFocus();
0096 }
0097 
0098 void Smb4KNetworkSearchToolBar::setActiveState(bool active)
0099 {
0100     m_searchAction->setActive(active);
0101     m_searchComboBox->setEnabled(!active);
0102 }
0103 
0104 void Smb4KNetworkSearchToolBar::clearSearch()
0105 {
0106     m_searchResults.clear();
0107 
0108     m_searchComboBox->clear();
0109     m_searchComboBox->clearEditText();
0110 
0111     m_downAction->setEnabled(!m_searchResults.isEmpty());
0112     m_upAction->setEnabled(!m_searchResults.isEmpty());
0113     m_clearAction->setEnabled(!m_searchResults.isEmpty());
0114 
0115     Q_EMIT clearSearchResults();
0116 }
0117 
0118 void Smb4KNetworkSearchToolBar::setSearchResults(const QList<SharePtr> &list)
0119 {
0120     for (const SharePtr &share : list) {
0121         m_searchResults << share->url().toString();
0122     }
0123 
0124     m_searchResults.sort();
0125 
0126     m_iterator = m_searchResults;
0127 
0128     m_downAction->setEnabled(!m_searchResults.isEmpty());
0129     m_upAction->setEnabled(!m_searchResults.isEmpty());
0130     m_clearAction->setEnabled(!m_searchResults.isEmpty());
0131 }
0132 
0133 void Smb4KNetworkSearchToolBar::setCompletionItems(const QStringList &items)
0134 {
0135     m_searchComboBox->completionObject()->setItems(items);
0136 }
0137 
0138 QStringList Smb4KNetworkSearchToolBar::completionItems() const
0139 {
0140     return m_searchComboBox->completionObject()->items();
0141 }
0142 
0143 void Smb4KNetworkSearchToolBar::slotReturnKeyPressed(const QString &text)
0144 {
0145     if (!text.isEmpty()) {
0146         m_searchComboBox->completionObject()->addItem(text);
0147         Q_EMIT search(text);
0148     }
0149 }
0150 
0151 void Smb4KNetworkSearchToolBar::slotSearchActionTriggered()
0152 {
0153     if (!m_searchAction->isActive()) {
0154         if (!m_searchComboBox->currentText().isEmpty()) {
0155             m_searchComboBox->completionObject()->addItem(m_searchComboBox->currentText());
0156             Q_EMIT search(m_searchComboBox->currentText());
0157         }
0158     } else {
0159         Q_EMIT abort();
0160     }
0161 }
0162 
0163 void Smb4KNetworkSearchToolBar::slotCloseButtonPressed()
0164 {
0165     clearSearch();
0166 
0167     Q_EMIT abort();
0168     Q_EMIT closeSearchBar();
0169 }
0170 
0171 void Smb4KNetworkSearchToolBar::slotDownActionTriggered()
0172 {
0173     if (m_iterator.hasNext()) {
0174         QString url = m_iterator.next();
0175         Q_EMIT jumpToResult(url);
0176     }
0177 }
0178 
0179 void Smb4KNetworkSearchToolBar::slotUpActionTriggered()
0180 {
0181     if (m_iterator.hasPrevious()) {
0182         QString url = m_iterator.previous();
0183         Q_EMIT jumpToResult(url);
0184     }
0185 }
0186 
0187 void Smb4KNetworkSearchToolBar::slotClearSearch()
0188 {
0189     clearSearch();
0190 }