From: Peter Penz Date: Tue, 26 Oct 2010 13:11:37 +0000 (+0000) Subject: Apply the facets-patch written by Sebastian Trüg, which allows to filter search resul... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/537ad8a89186a2119ddc8b41b60b6888b62495fa Apply the facets-patch written by Sebastian Trüg, which allows to filter search results in a very comfortable way. A lot of finetuning is necessary (e.g. the custom filter-prototypes from Dolphin will get removed etc). This will be done during the next weeks. svn path=/trunk/KDE/kdebase/apps/; revision=1189951 --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 47f44c497..9d8df1adf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index f24dd1343..da94ffcd5 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -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 index 000000000..3c930209c --- /dev/null +++ b/src/panels/facets/facetpanel.cpp @@ -0,0 +1,128 @@ +/*************************************************************************** + * Copyright (C) 2010 by Sebastian Trueg * + * * + * 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 +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + + +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(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 index 000000000..6673c7953 --- /dev/null +++ b/src/panels/facets/facetpanel.h @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2010 by Sebastian Trueg * + * * + * 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 + +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 diff --git a/src/search/dolphinsearchbox.cpp b/src/search/dolphinsearchbox.cpp index f12df73f3..54f3d0985 100644 --- a/src/search/dolphinsearchbox.cpp +++ b/src/search/dolphinsearchbox.cpp @@ -47,6 +47,7 @@ #include #include #include + #include #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"