File indexing completed on 2024-04-21 15:42:57

0001 /***************************************************************************
0002     This class provides the network search toolbar.
0003                              -------------------
0004     begin                : Su Dec 23 2018
0005     copyright            : (C) 2018-2019 by Alexander Reinholdt
0006     email                : alexander.reinholdt@kdemail.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful, but   *
0016  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
0018  *   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, write to the                         *
0022  *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
0023  *   MA 02110-1335, USA                                                    *
0024  ***************************************************************************/
0025 
0026 #ifdef HAVE_CONFIG_H
0027 #include <config.h>
0028 #endif
0029 
0030 // application specific includes
0031 #include "smb4knetworksearchtoolbar.h"
0032 #include "core/smb4kshare.h"
0033 
0034 // Qt includes
0035 #include <QDebug>
0036 
0037 // KDE includes
0038 #include <KCompletion/KComboBox>
0039 #include <KCompletion/KLineEdit>
0040 #include <KI18n/KLocalizedString>
0041 #include <KWidgetsAddons/KDualAction>
0042 #include <KIconThemes/KIconLoader>
0043 
0044 
0045 Smb4KNetworkSearchToolBar::Smb4KNetworkSearchToolBar(QWidget* parent)
0046 : QToolBar(parent), m_iterator(QStringList())
0047 {
0048   //
0049   // Set up tool bar
0050   //
0051   // Use the settings suggested by the note provided in the 'Detailed Description'
0052   // section of KToolBar (https://api.kde.org/frameworks/kxmlgui/html/classKToolBar.html)
0053   // 
0054   setToolButtonStyle(Qt::ToolButtonFollowStyle);
0055   setProperty("otherToolbar", true);
0056   
0057   //
0058   // The Close action
0059   // 
0060   QAction *closeAction = new QAction(this);
0061   closeAction->setObjectName("CloseAction");
0062   closeAction->setIcon(KDE::icon("window-close"));
0063   closeAction->setText(i18n("Close"));
0064   
0065   connect(closeAction, SIGNAL(triggered(bool)), this, SLOT(slotCloseButtonPressed()));
0066   
0067   addAction(closeAction);
0068   
0069   //
0070   // The search combo box
0071   // 
0072   KComboBox *comboBox = new KComboBox(true, this);
0073   comboBox->setObjectName("SearchCombo");
0074   comboBox->lineEdit()->setPlaceholderText(i18n("Search string"));
0075   comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0076   comboBox->setCompletionMode(KCompletion::CompletionPopupAuto);
0077   
0078   connect(comboBox, SIGNAL(returnPressed()), this, SLOT(slotReturnKeyPressed()));
0079   // FIXME: Add a connection to the clearSearch() slot
0080   
0081   addWidget(comboBox);
0082   
0083   //
0084   // The search dual action
0085   //
0086   KDualAction *searchAction = new KDualAction(this);
0087   searchAction->setObjectName("SearchAction");
0088   searchAction->setInactiveIcon(KDE::icon("search"));
0089   searchAction->setInactiveText(i18n("Search"));
0090   searchAction->setActiveIcon(KDE::icon("process-stop"));
0091   searchAction->setActiveText(i18n("Stop"));
0092   searchAction->setAutoToggle(false);
0093   
0094   connect(searchAction, SIGNAL(triggered(bool)), this, SLOT(slotSearchActionTriggered()));
0095   
0096   addAction(searchAction);
0097   
0098   //
0099   // Go one item down action
0100   //
0101   QAction *downAction = new QAction(this);
0102   downAction->setObjectName("DownAction");
0103   downAction->setIcon(KDE::icon("go-down-search"));
0104   downAction->setText(i18n("Item Down"));
0105   downAction->setEnabled(false);
0106   
0107   connect(downAction, SIGNAL(triggered(bool)), this, SLOT(slotDownActionTriggered()));
0108   
0109   addAction(downAction);
0110   
0111   //
0112   // Go one item up action
0113   // 
0114   QAction *upAction = new QAction(this);
0115   upAction->setObjectName("UpAction");
0116   upAction->setIcon(KDE::icon("go-up-search"));
0117   upAction->setText(i18n("Item Up"));
0118   upAction->setEnabled(false);
0119   
0120   connect(upAction, SIGNAL(triggered(bool)), this, SLOT(slotUpActionTriggered()));
0121   
0122   addAction(upAction);
0123   
0124   /**
0125    * Clear the search
0126    */
0127   QAction *clearAction = new QAction(this);
0128   clearAction->setObjectName("ClearAction");
0129   clearAction->setIcon(KDE::icon("edit-clear-all"));
0130   clearAction->setText(i18n("Clear"));
0131   clearAction->setEnabled(false);
0132   
0133   connect(clearAction, SIGNAL(triggered(bool)), this, SLOT(slotClearSearch()));
0134   
0135   addAction(clearAction);
0136 }
0137 
0138 
0139 Smb4KNetworkSearchToolBar::~Smb4KNetworkSearchToolBar()
0140 {
0141 }
0142 
0143 
0144 void Smb4KNetworkSearchToolBar::prepareInput()
0145 {
0146   //
0147   // Get the search combo box
0148   // 
0149   KComboBox *comboBox = findChild<KComboBox *>("SearchCombo");
0150   
0151   //
0152   // Set the keyboard focus to the lineedit
0153   //
0154   comboBox->lineEdit()->setFocus();
0155 }
0156 
0157 
0158 void Smb4KNetworkSearchToolBar::setActiveState(bool active)
0159 {
0160   //
0161   // Get the search dual action and set the active state
0162   // 
0163   KDualAction *searchAction = findChild<KDualAction *>("SearchAction");
0164   searchAction->setActive(active);
0165   
0166   //
0167   // Get the search combo box and disable/enable it
0168   // 
0169   KComboBox *comboBox = findChild<KComboBox *>("SearchCombo");
0170   comboBox->setEnabled(!active);
0171 }
0172 
0173 
0174 void Smb4KNetworkSearchToolBar::clearSearch()
0175 {
0176   //
0177   // Clear the list of search results
0178   // 
0179   m_searchResults.clear();
0180   
0181   //
0182   // Clear the combo box
0183   // 
0184   KComboBox *comboBox = findChild<KComboBox *>("SearchCombo");
0185   comboBox->clear();
0186   comboBox->clearEditText();
0187   
0188   //
0189   // Get the down action and disable it
0190   // 
0191   QAction *downAction = findChild<QAction *>("DownAction");
0192   downAction->setEnabled(!m_searchResults.isEmpty());
0193   
0194   //
0195   // Get the up action and disable it
0196   // 
0197   QAction *upAction = findChild<QAction *>("UpAction");
0198   upAction->setEnabled(!m_searchResults.isEmpty());
0199   
0200   //
0201   // Get the clear action and disable it
0202   // 
0203   QAction *clearAction = findChild<QAction *>("ClearAction");
0204   clearAction->setEnabled(!m_searchResults.isEmpty());
0205   
0206   //
0207   // Emit the clearSearchResults() signal
0208   // 
0209   emit clearSearchResults();
0210 }
0211 
0212 
0213 void Smb4KNetworkSearchToolBar::setSearchResults(const QList<SharePtr>& list)
0214 {
0215   //
0216   // Set the new list
0217   // 
0218   for (const SharePtr &share : list)
0219   {
0220     m_searchResults << share->url().toString();
0221   }
0222   
0223   //
0224   // Sort the search results
0225   // 
0226   m_searchResults.sort();
0227   
0228   //
0229   // Set the iterator
0230   // 
0231   m_iterator = m_searchResults;
0232   
0233   //
0234   // Get the down action and enable it
0235   // 
0236   QAction *downAction = findChild<QAction *>("DownAction");
0237   downAction->setEnabled(!m_searchResults.isEmpty());
0238   
0239   //
0240   // Get the up action and enable it
0241   // 
0242   QAction *upAction = findChild<QAction *>("UpAction");
0243   upAction->setEnabled(!m_searchResults.isEmpty());
0244   
0245   //
0246   // Get the clear action and enable it
0247   // 
0248   QAction *clearAction = findChild<QAction *>("ClearAction");
0249   clearAction->setEnabled(!m_searchResults.isEmpty());
0250 }
0251 
0252 
0253 void Smb4KNetworkSearchToolBar::setCompletionStrings(const QStringList& strings)
0254 {
0255   //
0256   // Get the input combo box 
0257   //
0258   KComboBox *comboBox = findChild<KComboBox *>("SearchCombo");
0259   
0260   //
0261   // Set the completion strings
0262   // 
0263   comboBox->completionObject()->setItems(strings);
0264 }
0265 
0266 
0267 QStringList Smb4KNetworkSearchToolBar::completionStrings() const
0268 {
0269   //
0270   // Get the input combo box 
0271   //
0272   KComboBox *comboBox = findChild<KComboBox *>("SearchCombo");
0273   
0274   //
0275   // Return the completion strings
0276   // 
0277   return comboBox->completionObject()->items();
0278 }
0279 
0280 
0281 void Smb4KNetworkSearchToolBar::slotReturnKeyPressed()
0282 {
0283   //
0284   // Get the input combo box 
0285   //
0286   KComboBox *comboBox = findChild<KComboBox *>("SearchCombo");
0287   
0288   //
0289   // Initialize a search if the line edit is not empty
0290   // 
0291   if (!comboBox->currentText().isEmpty())
0292   {
0293     //
0294     // Add the search item to the completion object
0295     // 
0296     comboBox->completionObject()->addItem(comboBox->currentText());
0297     
0298     //
0299     // Emit the search signal
0300     // 
0301     emit search(comboBox->currentText());
0302   }
0303 }
0304 
0305 
0306 void Smb4KNetworkSearchToolBar::slotSearchActionTriggered()
0307 {
0308   //
0309   // Get the search dual action
0310   // 
0311   KDualAction *searchAction = findChild<KDualAction *>("SearchAction");
0312   
0313   //
0314   // Initialize a search if the action is in active state and abort 
0315   // the search if it is in inactive state
0316   // 
0317   if (!searchAction->isActive())
0318   {
0319     KComboBox *comboBox = findChild<KComboBox *>("SearchCombo");
0320     
0321     if (!comboBox->currentText().isEmpty())
0322     {
0323       //
0324       // Add the search item to the completion object
0325       // 
0326       comboBox->completionObject()->addItem(comboBox->currentText());
0327     
0328       //
0329       // Emit the search signal
0330       // 
0331       emit search(comboBox->currentText());
0332     }
0333   }
0334   else
0335   {
0336     //
0337     // Emit the abort signal
0338     // 
0339     emit abort();
0340   }
0341 }
0342 
0343 
0344 void Smb4KNetworkSearchToolBar::slotCloseButtonPressed()
0345 {
0346   //
0347   // Clear the search toolbar
0348   // 
0349   clearSearch();
0350   
0351   //
0352   // Emit the abort signal
0353   // 
0354   emit abort();
0355   
0356   //
0357   // Emit the close signal
0358   // 
0359   emit close();
0360 }
0361 
0362 
0363 void Smb4KNetworkSearchToolBar::slotDownActionTriggered()
0364 {
0365   if (m_iterator.hasNext())
0366   {
0367     //
0368     // Get the URL of the item
0369     // 
0370     QString url = m_iterator.next();
0371     
0372     //
0373     // Emit the jumpToResult() signal
0374     //
0375     emit jumpToResult(url);
0376   }
0377 }
0378 
0379 
0380 void Smb4KNetworkSearchToolBar::slotUpActionTriggered()
0381 {
0382   if (m_iterator.hasPrevious())
0383   {
0384     //
0385     // Get the URL of the item
0386     // 
0387     QString url = m_iterator.previous();
0388     
0389     //
0390     // Emit the jumpToResult() signal
0391     //
0392     emit jumpToResult(url);
0393   }
0394 }
0395 
0396 
0397 void Smb4KNetworkSearchToolBar::slotClearSearch()
0398 {
0399   clearSearch();
0400 }
0401 
0402 
0403 
0404 
0405