]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/search/dolphinsearchbox.cpp
Merge branch 'Applications/15.12'
[dolphin.git] / src / search / dolphinsearchbox.cpp
index 65d35539cb7a84528072cbb18bcdf9c7ed98eafc..a925a97c9c663c198b7c129f9dac965b31d354ac 100644 (file)
@@ -23,7 +23,7 @@
 #include "dolphinfacetswidget.h"
 
 #include <QIcon>
-#include <KLineEdit>
+#include <QLineEdit>
 #include <KLocalizedString>
 #include <KSeparator>
 
 #include <QTimer>
 #include <QToolButton>
 #include <QVBoxLayout>
+#include <QUrlQuery>
 
+#include <config-baloo.h>
 #ifdef HAVE_BALOO
-    #include <Baloo/NaturalFileQueryParser>
-    #include <Baloo/QueryBuilder>
     #include <Baloo/Query>
-    #include <Baloo/Term>
     #include <Baloo/IndexerConfig>
 #endif
 #include <QFontDatabase>
@@ -131,9 +130,11 @@ QUrl DolphinSearchBox::urlForSearching() const
         url = balooUrlForSearching();
     } else {
         url.setScheme("filenamesearch");
-        url.addQueryItem("search", m_searchInput->text());
+
+        QUrlQuery query;
+        query.addQueryItem("search", m_searchInput->text());
         if (m_contentButton->isChecked()) {
-            url.addQueryItem("checkContent", "yes");
+            query.addQueryItem("checkContent", "yes");
         }
 
         QString encodedUrl;
@@ -145,7 +146,9 @@ QUrl DolphinSearchBox::urlForSearching() const
         } else {
             encodedUrl = m_searchPath.url();
         }
-        url.addQueryItem("url", encodedUrl);
+        query.addQueryItem("url", encodedUrl);
+
+        url.setQuery(query);
     }
 
     return url;
@@ -156,9 +159,10 @@ void DolphinSearchBox::fromSearchUrl(const QUrl& url)
     if (url.scheme() == "baloosearch") {
         fromBalooSearchUrl(url);
     } else if (url.scheme() == "filenamesearch") {
-        setText(url.queryItemValue("search"));
-        setSearchPath(url.queryItemValue("url"));
-        m_contentButton->setChecked(url.queryItemValue("checkContent") == "yes");
+        const QUrlQuery query(url);
+        setText(query.queryItemValue("search"));
+        setSearchPath(QUrl::fromUserInput(query.queryItemValue("url"), QString(), QUrl::AssumeLocalFile));
+        m_contentButton->setChecked(query.queryItemValue("checkContent") == "yes");
     } else {
         setText(QString());
         setSearchPath(url);
@@ -251,9 +255,8 @@ void DolphinSearchBox::slotConfigurationChanged()
     }
 }
 
-void DolphinSearchBox::slotSearchTextChanged()
+void DolphinSearchBox::slotSearchTextChanged(const QString& text)
 {
-    const QString text = m_searchInput->text();
 
     if (text.isEmpty()) {
         m_startSearchTimer->stop();
@@ -266,14 +269,7 @@ void DolphinSearchBox::slotSearchTextChanged()
 void DolphinSearchBox::slotReturnPressed()
 {
     emitSearchRequest();
-    emit returnPressed(m_searchInput->text());
-}
-
-void DolphinSearchBox::updateSearchInputParsing()
-{
-#ifdef HAVE_BALOO
-    m_searchInput->setParsingEnabled(m_contentButton->isChecked());
-#endif
+    emit returnPressed();
 }
 
 void DolphinSearchBox::slotFacetsButtonToggled()
@@ -314,7 +310,6 @@ void DolphinSearchBox::loadSettings()
     }
 
     m_facetsWidget->setVisible(SearchSettings::showFacetsWidget());
-    updateSearchInputParsing();
 }
 
 void DolphinSearchBox::saveSettings()
@@ -338,23 +333,14 @@ void DolphinSearchBox::init()
     m_searchLabel = new QLabel(this);
 
     // Create search box
-#ifdef HAVE_BALOO
-    m_queryParser.reset(new Baloo::NaturalFileQueryParser);
-    m_searchInput = new Baloo::QueryBuilder(m_queryParser.data(), this);
-    connect(m_searchInput, &Baloo::QueryBuilder::editingFinished,
-            this, &DolphinSearchBox::slotReturnPressed);
-    connect(m_searchInput, &Baloo::QueryBuilder::textChanged,
-            this, &DolphinSearchBox::slotSearchTextChanged);
-#else
-    m_searchInput = new KLineEdit(this);
+    m_searchInput = new QLineEdit(this);
     m_searchInput->installEventFilter(this);
-    m_searchInput->setClearButtonShown(true);
+    m_searchInput->setClearButtonEnabled(true);
     m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
-    connect(m_searchInput, &KLineEdit::returnPressed,
+    connect(m_searchInput, &QLineEdit::returnPressed,
             this, &DolphinSearchBox::slotReturnPressed);
-    connect(m_searchInput, &KLineEdit::textChanged,
+    connect(m_searchInput, &QLineEdit::textChanged,
             this, &DolphinSearchBox::slotSearchTextChanged);
-#endif
     setFocusProxy(m_searchInput);
 
     // Apply layout for the search input
@@ -376,8 +362,6 @@ void DolphinSearchBox::init()
     QButtonGroup* searchWhatGroup = new QButtonGroup(this);
     searchWhatGroup->addButton(m_fileNameButton);
     searchWhatGroup->addButton(m_contentButton);
-    connect(searchWhatGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
-            this, &DolphinSearchBox::updateSearchInputParsing);
 
     m_separator = new KSeparator(Qt::Vertical, this);
 
@@ -454,31 +438,26 @@ QUrl DolphinSearchBox::balooUrlForSearching() const
     const QString text = m_searchInput->text();
 
     Baloo::Query query;
-
-    if (m_contentButton->isChecked()) {
-        query = m_queryParser->parse(text, Baloo::NaturalQueryParser::DetectFilenamePattern);
-    } else {
-        query.setTerm(Baloo::Term(QLatin1String("filename"), text));
-    }
-
-    // Configure the query so that it returns files and takes the rating into account
-    query.addType("File");
     query.addType(m_facetsWidget->facetType());
 
-    Baloo::Term term(Baloo::Term::And);
-    Baloo::Term ratingTerm = m_facetsWidget->ratingTerm();
-
-    if (ratingTerm.isValid()) {
-        term.addSubTerm(query.term());
-        term.addSubTerm(ratingTerm);
+    QStringList queryStrings;
+    QString ratingQuery = m_facetsWidget->ratingTerm();
+    if (!ratingQuery.isEmpty()) {
+        queryStrings << ratingQuery;
+    }
 
-        query.setTerm(term);
+    if (m_contentButton->isChecked()) {
+        queryStrings << text;
+    } else if (!text.isEmpty()) {
+        queryStrings << QString::fromLatin1("filename:\"%1\"").arg(text);
     }
 
     if (m_fromHereButton->isChecked()) {
         query.setIncludeFolder(m_searchPath.toLocalFile());
     }
 
+    query.setSearchString(queryStrings.join(" "));
+
     return query.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
                                    "Query Results from '%1'", text));
 #else
@@ -490,7 +469,6 @@ void DolphinSearchBox::fromBalooSearchUrl(const QUrl& url)
 {
 #ifdef HAVE_BALOO
     const Baloo::Query query = Baloo::Query::fromSearchUrl(url);
-    const Baloo::Term term = query.term();
 
     // Block all signals to avoid unnecessary "searchRequest" signals
     // while we adjust the search text and the facet widget.
@@ -498,26 +476,23 @@ void DolphinSearchBox::fromBalooSearchUrl(const QUrl& url)
 
     const QString customDir = query.includeFolder();
     if (!customDir.isEmpty()) {
-        setSearchPath(customDir);
+        setSearchPath(QUrl::fromLocalFile(customDir));
     } else {
-        setSearchPath(QDir::homePath());
+        setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
     }
 
-    if (!query.searchString().isEmpty()) {
-        setText(query.searchString());
-    }
+    setText(query.searchString());
 
     QStringList types = query.types();
-    types.removeOne("File"); // We are only interested in facet widget types
     if (!types.isEmpty()) {
         m_facetsWidget->setFacetType(types.first());
     }
 
-    foreach (const Baloo::Term& subTerm, term.subTerms()) {
-        const QString property = subTerm.property();
-
-        if (property == QLatin1String("filename")) {
-            setText(subTerm.value().toString());
+    const QStringList subTerms = query.searchString().split(' ', QString::SkipEmptyParts);
+    foreach (const QString& subTerm, subTerms) {
+        if (subTerm.startsWith("filename:")) {
+            const QString value = subTerm.mid(9);
+            setText(value);
         } else if (m_facetsWidget->isRatingTerm(subTerm)) {
             m_facetsWidget->setRatingTerm(subTerm);
         }
@@ -525,6 +500,8 @@ void DolphinSearchBox::fromBalooSearchUrl(const QUrl& url)
 
     m_startSearchTimer->stop();
     blockSignals(false);
+#else
+    Q_UNUSED(url);
 #endif
 }