]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/search/dolphinsearchbox.cpp
Dolphin Facet Widgets: Implement date based filtering
[dolphin.git] / src / search / dolphinsearchbox.cpp
index ef9c2bfcf627b7712f6dbc6e1136fe9bf0160171..e6b738a9314e91ffea14ea702721d0c30c156077 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "dolphin_searchsettings.h"
 #include "dolphinfacetswidget.h"
-#include "dolphinsearchinformation.h"
 
 #include <KIcon>
 #include <KLineEdit>
 #include <QToolButton>
 #include <QVBoxLayout>
 
-#include <config-nepomuk.h>
-#ifdef HAVE_NEPOMUK
-    #include <Nepomuk2/Query/AndTerm>
-    #include <Nepomuk2/Query/FileQuery>
-    #include <Nepomuk2/Query/LiteralTerm>
-    #include <Nepomuk2/Query/OrTerm>
-    #include <Nepomuk2/Query/Query>
-    #include <Nepomuk2/Query/QueryParser>
-    #include <Nepomuk2/Query/ResourceTypeTerm>
-    #include <Nepomuk2/Query/ComparisonTerm>
-    #include <Nepomuk2/ResourceManager>
-    #include <Nepomuk2/Vocabulary/NFO>
+#include <config-baloo.h>
+#ifdef HAVE_BALOO
+    #include <baloo/query.h>
+    #include <baloo/term.h>
+    #include <baloo/indexerconfig.h>
 #endif
 
 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
     QWidget(parent),
     m_startedSearching(false),
     m_readOnly(false),
+    m_active(true),
     m_topLayout(0),
     m_searchLabel(0),
     m_searchInput(0),
@@ -114,8 +107,11 @@ void DolphinSearchBox::setSearchPath(const KUrl& url)
     m_fromHereButton->setVisible(showSearchFromButtons);
     m_everywhereButton->setVisible(showSearchFromButtons);
 
-    const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
-    const bool hasFacetsSupport = searchInfo.isIndexingEnabled() && searchInfo.isPathIndexed(m_searchPath);
+    bool hasFacetsSupport = false;
+#ifdef HAVE_BALOO
+    const Baloo::IndexerConfig searchInfo;
+    hasFacetsSupport = searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile());
+#endif
     m_facetsWidget->setEnabled(hasFacetsSupport);
 }
 
@@ -127,9 +123,13 @@ KUrl DolphinSearchBox::searchPath() const
 KUrl DolphinSearchBox::urlForSearching() const
 {
     KUrl url;
-    const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
-    if (searchInfo.isIndexingEnabled() && searchInfo.isPathIndexed(m_searchPath)) {
-        url = nepomukUrlForSearching();
+    bool useBalooSearch = false;
+#ifdef HAVE_BALOO
+    const Baloo::IndexerConfig searchInfo;
+    useBalooSearch = searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile());
+#endif
+    if (useBalooSearch) {
+        url = balooUrlForSearching();
     } else {
         url.setProtocol("filenamesearch");
         url.addQueryItem("search", m_searchInput->text());
@@ -171,6 +171,22 @@ bool DolphinSearchBox::isReadOnly() const
     return m_readOnly;
 }
 
+void DolphinSearchBox::setActive(bool active)
+{
+    if (active != m_active) {
+        m_active = active;
+
+        if (active) {
+            emit activated();
+        }
+    }
+}
+
+bool DolphinSearchBox::isActive() const
+{
+    return m_active;
+}
+
 bool DolphinSearchBox::event(QEvent* event)
 {
     if (event->type() == QEvent::Polish) {
@@ -199,6 +215,21 @@ void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
     }
 }
 
+bool DolphinSearchBox::eventFilter(QObject* obj, QEvent* event)
+{
+    switch (event->type()) {
+    case QEvent::FocusIn:
+        setActive(true);
+        setFocus();
+        break;
+
+    default:
+        break;
+    }
+
+    return QObject::eventFilter(obj, event);
+}
+
 void DolphinSearchBox::emitSearchRequest()
 {
     m_startSearchTimer->stop();
@@ -253,6 +284,7 @@ void DolphinSearchBox::slotFacetChanged()
 
 void DolphinSearchBox::initButton(QToolButton* button)
 {
+    button->installEventFilter(this);
     button->setAutoExclusive(true);
     button->setAutoRaise(true);
     button->setCheckable(true);
@@ -298,6 +330,7 @@ void DolphinSearchBox::init()
 
     // Create search box
     m_searchInput = new KLineEdit(this);
+    m_searchInput->installEventFilter(this);
     m_searchInput->setClearButtonShown(true);
     m_searchInput->setFont(KGlobalSettings::generalFont());
     setFocusProxy(m_searchInput);
@@ -348,6 +381,7 @@ void DolphinSearchBox::init()
     connect(m_facetsToggleButton, SIGNAL(clicked()), this, SLOT(slotFacetsButtonToggled()));
 
     m_facetsWidget = new DolphinFacetsWidget(this);
+    m_facetsWidget->installEventFilter(this);
     m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
     connect(m_facetsWidget, SIGNAL(facetChanged()), this, SLOT(slotFacetChanged()));
 
@@ -395,52 +429,36 @@ void DolphinSearchBox::init()
     applyReadOnlyState();
 }
 
-KUrl DolphinSearchBox::nepomukUrlForSearching() const
+KUrl DolphinSearchBox::balooUrlForSearching() const
 {
-#ifdef HAVE_NEPOMUK
-    // Create the term for the text from the input-field
-    // dependent on whether a searching for content or
-    // filename is done
+#ifdef HAVE_BALOO
     const QString text = m_searchInput->text();
-    Nepomuk2::Query::Term searchLabelTerm;
-    if (m_contentButton->isChecked()) {
-        // Let Nepomuk parse the query
-        searchLabelTerm = Nepomuk2::Query::QueryParser::parseQuery(text, Nepomuk2::Query::QueryParser::DetectFilenamePattern).term();
-    } else {
-        // Search the text in the filename only
-        QString regex = QRegExp::escape(text);
-        regex.replace("\\*", QLatin1String(".*"));
-        regex.replace("\\?", QLatin1String("."));
-        regex.replace("\\", "\\\\");
-        searchLabelTerm = Nepomuk2::Query::ComparisonTerm(
-                                Nepomuk2::Vocabulary::NFO::fileName(),
-                                Nepomuk2::Query::LiteralTerm(regex),
-                                Nepomuk2::Query::ComparisonTerm::Regexp);
+
+    Baloo::Query query;
+    query.addType("File");
+    query.addTypes(m_facetsWidget->facetTypes());
+
+    Baloo::Term term(Baloo::Term::And);
+
+    Baloo::Term ratingTerm = m_facetsWidget->ratingTerm();
+    if (ratingTerm.isValid()) {
+        term.addSubTerm(ratingTerm);
     }
 
-    // Get the term from the facets and merge it with the
-    // created term from the input-field.
-    Nepomuk2::Query::Term facetsTerm = m_facetsWidget->facetsTerm();
-
-    Nepomuk2::Query::FileQuery fileQuery;
-    fileQuery.setFileMode(Nepomuk2::Query::FileQuery::QueryFilesAndFolders);
-    if (facetsTerm.isValid()) {
-        Nepomuk2::Query::AndTerm andTerm;
-        andTerm.addSubTerm(searchLabelTerm);
-        andTerm.addSubTerm(facetsTerm);
-        fileQuery.setTerm(andTerm);
+    if (m_contentButton->isChecked()) {
+        query.setSearchString(text);
     } else {
-        fileQuery.setTerm(searchLabelTerm);
+        term.addSubTerm(Baloo::Term(QLatin1String("filename"), text));
     }
 
     if (m_fromHereButton->isChecked()) {
-        const bool recursive = true;
-        fileQuery.addIncludeFolder(m_searchPath, recursive);
+        query.addCustomOption("includeFolder", m_searchPath.toLocalFile());
     }
 
-    return fileQuery.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
-                                       "Query Results from '%1'",
-                                       text));
+    query.setTerm(term);
+
+    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
     return KUrl();
 #endif
@@ -448,9 +466,9 @@ KUrl DolphinSearchBox::nepomukUrlForSearching() const
 
 void DolphinSearchBox::applyReadOnlyState()
 {
-#ifdef HAVE_NEPOMUK
+#ifdef HAVE_BALOO
     if (m_readOnly) {
-        m_searchLabel->setText(Nepomuk2::Query::Query::titleFromQueryUrl(m_readOnlyQuery));
+        m_searchLabel->setText(Baloo::Query::titleFromQueryUrl(m_readOnlyQuery));
     } else {
 #else
     {