]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Apply the facets-patch written by Sebastian TrĂ¼g, which allows to filter search resul...
authorPeter Penz <peter.penz19@gmail.com>
Tue, 26 Oct 2010 13:11:37 +0000 (13:11 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Tue, 26 Oct 2010 13:11:37 +0000 (13:11 +0000)
svn path=/trunk/KDE/kdebase/apps/; revision=1189951

src/CMakeLists.txt
src/dolphinmainwindow.cpp
src/panels/facets/facetpanel.cpp [new file with mode: 0644]
src/panels/facets/facetpanel.h [new file with mode: 0644]
src/search/dolphinsearchbox.cpp

index 47f44c4970a210dc0cd5d5e76f0470b2e4dea3e9..9d8df1adf4ae91358d55f3fe9ad356187b905272 100644 (file)
@@ -70,7 +70,7 @@ kde4_add_library(dolphinprivate SHARED ${dolphinprivate_LIB_SRCS})
 
 target_link_libraries(dolphinprivate ${KDE4_KFILE_LIBS} konq ${KDE4_KNEWSTUFF3_LIBS} ${QIMAGEBLITZ_LIBRARIES})
 if (Nepomuk_FOUND)
-  target_link_libraries(dolphinprivate ${NEPOMUK_LIBRARIES} ${NEPOMUK_QUERY_LIBRARIES} ${SOPRANO_LIBRARIES})
+  target_link_libraries(dolphinprivate ${NEPOMUK_LIBRARIES} ${NEPOMUK_QUERY_LIBRARIES} nepomukutils ${SOPRANO_LIBRARIES})
 endif (Nepomuk_FOUND)
 
 
@@ -104,6 +104,7 @@ set(dolphin_SRCS
     dolphincontextmenu.cpp
     filterbar/filterbar.cpp
     main.cpp
+    panels/facets/facetpanel.cpp
     panels/information/informationpanel.cpp
     panels/information/informationpanelcontent.cpp
     panels/information/pixmapviewer.cpp
@@ -189,6 +190,7 @@ if (Nepomuk_FOUND)
         ${NEPOMUK_LIBRARIES}
         ${SOPRANO_LIBRARIES}
         ${NEPOMUK_QUERY_LIBRARIES}
+        nepomukutils
     )
 endif (Nepomuk_FOUND)
 
index f24dd134340f1aec6e3ea0ba419a992da13fc7f0..da94ffcd501d87d4376e702436ca12792ef1d6fe 100644 (file)
@@ -28,6 +28,7 @@
 #include "dolphinnewfilemenu.h"
 #include "dolphinviewcontainer.h"
 #include "mainwindowadaptor.h"
+#include "panels/facets/facetpanel.h"
 #include "panels/folders/folderspanel.h"
 #include "panels/places/placespanel.h"
 #include "panels/information/informationpanel.h"
@@ -1562,6 +1563,20 @@ void DolphinMainWindow::setupDockWidgets()
     connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
             this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
 
+    // setup "Facets"
+   QDockWidget* facetDock = new QDockWidget(i18nc("@title:window", "Filter"));
+   facetDock->setObjectName("facetDock");
+   facetDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
+   Panel* facetPanel = new FacetPanel(facetDock);
+   connect(facetPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
+   facetDock->setWidget(facetPanel);
+
+   QAction* facetAction = facetDock->toggleViewAction();
+   facetAction->setIcon(KIcon("dialog-facet"));
+   addDockWidget(Qt::RightDockWidgetArea, facetDock);
+   connect(this, SIGNAL(urlChanged(KUrl)),
+           facetPanel, SLOT(setUrl(KUrl)));
+
     // setup "Terminal"
 #ifndef Q_OS_WIN
     QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
@@ -1616,6 +1631,7 @@ void DolphinMainWindow::setupDockWidgets()
     panelsMenu->addAction(placesAction);
     panelsMenu->addAction(infoAction);
     panelsMenu->addAction(foldersAction);
+    panelsMenu->addAction(facetAction);
 #ifndef Q_OS_WIN
     panelsMenu->addAction(terminalAction);
 #endif
diff --git a/src/panels/facets/facetpanel.cpp b/src/panels/facets/facetpanel.cpp
new file mode 100644 (file)
index 0000000..3c93020
--- /dev/null
@@ -0,0 +1,128 @@
+/***************************************************************************
+ *   Copyright (C) 2010 by Sebastian Trueg <trueg@kde.org>                  *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#include "facetpanel.h"
+
+#include <nepomuk/filequery.h>
+#include <nepomuk/facetwidget.h>
+#include <Nepomuk/Query/FileQuery>
+#include <Nepomuk/Query/Term>
+
+#include <kfileitem.h>
+#include <kio/jobclasses.h>
+#include <kio/job.h>
+
+#include <QtGui/QVBoxLayout>
+#include <QtGui/QTreeView>
+#include <QtGui/QPushButton>
+#include <kdebug.h>
+
+
+FacetPanel::FacetPanel(QWidget* parent)
+    : Panel(parent)
+{
+    QVBoxLayout* layout = new QVBoxLayout(this);
+    m_buttonRemoveFolderRestriction = new QPushButton( i18n( "Remove folder restriction" ), this );
+    connect( m_buttonRemoveFolderRestriction, SIGNAL( clicked() ), SLOT( slotRemoveFolderRestrictionClicked() ) );
+
+    layout->addWidget(m_buttonRemoveFolderRestriction);
+
+    m_facetWidget = new Nepomuk::Utils::FacetWidget( this );
+    layout->addWidget( m_facetWidget, 1 );
+    connect(m_facetWidget, SIGNAL(facetsChanged()), this, SLOT(slotFacetsChanged()) );
+
+    // init to empty panel
+    setQuery(Nepomuk::Query::Query());
+}
+
+
+FacetPanel::~FacetPanel()
+{
+}
+
+void FacetPanel::setUrl(const KUrl& url)
+{
+    kDebug() << url;
+    Panel::setUrl(url);
+
+    // disable us
+    setQuery(Nepomuk::Query::Query());
+
+    // get the query from the item
+    m_lastSetUrlStatJob = KIO::stat(url, KIO::HideProgressInfo);
+    connect(m_lastSetUrlStatJob, SIGNAL(result(KJob*)),
+            this, SLOT(slotSetUrlStatFinished(KJob*)));
+}
+
+
+void FacetPanel::setQuery(const Nepomuk::Query::Query& query)
+{
+    kDebug() << query << query.isValid() << query.toSparqlQuery();
+
+    if (query.isValid()) {
+        m_buttonRemoveFolderRestriction->setVisible( query.isFileQuery() && !query.toFileQuery().includeFolders().isEmpty() );
+        m_unfacetedRestQuery = query;
+        m_unfacetedRestQuery.setTerm( m_facetWidget->extractFacetsFromTerm( query.term() ) );
+        m_facetWidget->setClientQuery( query );
+        kDebug() << "Rest query after facets:" << m_unfacetedRestQuery;
+        setEnabled(true);
+    }
+    else {
+        m_unfacetedRestQuery = Nepomuk::Query::Query();
+        setEnabled(false);
+    }
+}
+
+
+void FacetPanel::slotSetUrlStatFinished(KJob* job)
+{
+    m_lastSetUrlStatJob = 0;
+    kDebug() << url();
+    const KIO::UDSEntry uds = static_cast<KIO::StatJob*>(job)->statResult();
+    const QString nepomukQueryStr = uds.stringValue( KIO::UDSEntry::UDS_NEPOMUK_QUERY );
+    kDebug() << nepomukQueryStr;
+    Nepomuk::Query::FileQuery nepomukQuery;
+    if ( !nepomukQueryStr.isEmpty() ) {
+        nepomukQuery = Nepomuk::Query::Query::fromString( nepomukQueryStr );
+    }
+    else if ( url().isLocalFile() ) {
+        // fallback query for local file URLs
+        nepomukQuery.addIncludeFolder(url(), false);
+    }
+    kDebug() << nepomukQuery;
+    setQuery(nepomukQuery);
+}
+
+
+void FacetPanel::slotFacetsChanged()
+{
+    Nepomuk::Query::Query query( m_unfacetedRestQuery && m_facetWidget->queryTerm() );
+    kDebug() << query;
+    emit urlActivated( query.toSearchUrl() );
+}
+
+
+void FacetPanel::slotRemoveFolderRestrictionClicked()
+{
+    Nepomuk::Query::FileQuery query( m_unfacetedRestQuery && m_facetWidget->queryTerm() );
+    query.setIncludeFolders( KUrl::List() );
+    query.setExcludeFolders( KUrl::List() );
+    m_facetWidget->setClientQuery( query );
+    emit urlActivated( query.toSearchUrl() );
+}
diff --git a/src/panels/facets/facetpanel.h b/src/panels/facets/facetpanel.h
new file mode 100644 (file)
index 0000000..6673c79
--- /dev/null
@@ -0,0 +1,67 @@
+/***************************************************************************
+ *   Copyright (C) 2010 by Sebastian Trueg <trueg@kde.org>                  *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#ifndef FACETPANEL_H
+#define FACETPANEL_H
+
+#include "../panel.h"
+#include <nepomuk/query.h>
+
+class KJob;
+class QPushButton;
+
+namespace Nepomuk {
+    namespace Utils {
+        class FacetWidget;
+    }
+}
+
+class FacetPanel : public Panel
+{
+    Q_OBJECT
+
+public:
+    FacetPanel(QWidget* parent = 0);
+    ~FacetPanel();
+
+public slots:
+    void setUrl(const KUrl& url);
+    void setQuery(const Nepomuk::Query::Query& query);
+
+signals:
+    void urlActivated( const KUrl& url );
+
+private slots:
+    void slotSetUrlStatFinished(KJob*);
+    void slotFacetsChanged();
+    void slotRemoveFolderRestrictionClicked();
+
+private:
+    bool urlChanged() {
+        return true;
+    }
+
+    KJob* m_lastSetUrlStatJob;
+
+    QPushButton* m_buttonRemoveFolderRestriction;
+    Nepomuk::Utils::FacetWidget* m_facetWidget;
+    Nepomuk::Query::Query m_unfacetedRestQuery;
+};
+
+#endif // FACETPANEL_H
index f12df73f3cd3500f912a7e1d2df7b5e358c037fa..54f3d098595260d52647f4757de32b338db9218b 100644 (file)
@@ -47,6 +47,7 @@
     #include <nepomuk/queryparser.h>
     #include <nepomuk/resourcemanager.h>
     #include <nepomuk/resourcetypeterm.h>
+    #include <nepomuk/comparisonterm.h>
     #include "nfo.h"
 
     #include "filters/datesearchfilterwidget.h"
@@ -96,10 +97,7 @@ KUrl DolphinSearchBox::searchPath() const
 KUrl DolphinSearchBox::urlForSearching() const
 {
     KUrl url;
-    if (isSearchPathIndexed() && !m_fileNameButton->isChecked()) {
-        // TODO: Currently Nepomuk is not used for searching filenames. Nepomuk
-        // is capable to provide this, but further investigations are necessary to
-        // also support regular expressions.
+    if (isSearchPathIndexed()) {
         url = nepomukUrlForSearching();
     } else {
         url = m_searchPath;
@@ -334,6 +332,7 @@ void DolphinSearchBox::init()
 
 bool DolphinSearchBox::isSearchPathIndexed() const
 {
+    return true;
 #ifdef HAVE_NEPOMUK
     const QString path = m_searchPath.path();
 
@@ -354,7 +353,8 @@ bool DolphinSearchBox::isSearchPathIndexed() const
         // excluded folder is part of the search path.
         const QStringList excludedFolders = strigiConfig.group("General").readPathEntry("exclude folders", QStringList());
         foreach (const QString& excludedFolder, excludedFolders) {
-            if (excludedFolder.startsWith(path)) {
+            // trueg: this is still not correct since there might be an include folder in the exclude folder
+            if (path.startsWith(excludedFolder)) {
                 isIndexed = false;
                 break;
             }
@@ -383,9 +383,23 @@ KUrl DolphinSearchBox::nepomukUrlForSearching() const
     // Add input from search filter
     const QString text = m_searchInput->text();
     if (!text.isEmpty()) {
-        const Nepomuk::Query::Query customQuery = Nepomuk::Query::QueryParser::parseQuery(text);
-        if (customQuery.isValid()) {
-            andTerm.addSubTerm(customQuery.term());
+        if ( m_fileNameButton->isChecked() ) {
+            QString regex = QRegExp::escape(text);
+            regex.replace("\\*", QLatin1String( ".*" ));
+            regex.replace("\\?", QLatin1String( "." ));
+            regex.replace("\\", "\\\\");
+            regex.prepend('^');
+            regex.append('$');
+            andTerm.addSubTerm( Nepomuk::Query::ComparisonTerm(
+                                    Nepomuk::Vocabulary::NFO::fileName(),
+                                    Nepomuk::Query::LiteralTerm( regex ),
+                                    Nepomuk::Query::ComparisonTerm::Regexp ) );
+        }
+        else {
+            const Nepomuk::Query::Query customQuery = Nepomuk::Query::QueryParser::parseQuery(text, Nepomuk::Query::QueryParser::DetectFilenamePattern);
+            if (customQuery.isValid()) {
+                andTerm.addSubTerm(customQuery.term());
+            }
         }
     }
 
@@ -393,13 +407,12 @@ KUrl DolphinSearchBox::nepomukUrlForSearching() const
     fileQuery.setFileMode(Nepomuk::Query::FileQuery::QueryFiles);
     fileQuery.setTerm(andTerm);
 
-    if (fileQuery.isValid()) {
-        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));
-    }
-#endif
+    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));
+#else
     return KUrl();
+#endif
 }
 
 #include "dolphinsearchbox.moc"