1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 * **************************************************************************/
20 #include "dolphinsearchbox.h"
22 #include "dolphin_searchsettings.h"
25 #include <klineedit.h>
27 #include <kseparator.h>
29 #include <QButtonGroup>
32 #include <QFormLayout>
33 #include <QHBoxLayout>
36 #include <QPushButton>
37 #include <QToolButton>
38 #include <QVBoxLayout>
40 #include <config-nepomuk.h>
42 #define DISABLE_NEPOMUK_LEGACY
43 #include <nepomuk/andterm.h>
44 #include <nepomuk/filequery.h>
45 #include <nepomuk/literalterm.h>
46 #include <nepomuk/query.h>
47 #include <nepomuk/queryparser.h>
48 #include <nepomuk/resourcemanager.h>
49 #include <nepomuk/resourcetypeterm.h>
52 #include "filters/datesearchfilterwidget.h"
53 #include "filters/ratingsearchfilterwidget.h"
54 #include "filters/tagsearchfilterwidget.h"
57 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
59 m_startedSearching(false),
60 m_nepomukActivated(false),
64 m_everywhereButton(0),
68 m_filterWidgetsLayout(0),
74 DolphinSearchBox::~DolphinSearchBox()
79 QString
DolphinSearchBox::text() const
81 return m_searchInput
->text();
84 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
87 m_filterButton
->setVisible(isSearchPathIndexed());
90 KUrl
DolphinSearchBox::searchPath() const
95 KUrl
DolphinSearchBox::urlForSearching() const
98 if (isSearchPathIndexed() && !m_fileNameButton
->isChecked()) {
99 // TODO: Currently Nepomuk is not used for searching filenames. Nepomuk
100 // is capable to provide this, but further investigations are necessary to
101 // also support regular expressions.
102 url
= nepomukUrlForSearching();
105 url
.setProtocol("filenamesearch");
106 url
.addQueryItem("search", m_searchInput
->text());
107 if (m_contentButton
->isChecked()) {
108 url
.addQueryItem("checkContent", "yes");
110 if (m_everywhereButton
->isChecked()) {
111 // It is very unlikely, that the majority of Dolphins target users
112 // mean "the whole harddisk" instead of "my home folder" when
113 // selecting the "Everywhere" button.
114 url
.setPath(QDir::homePath());
121 bool DolphinSearchBox::event(QEvent
* event
)
123 if (event
->type() == QEvent::Polish
) {
125 } else if (event
->type() == QEvent::KeyPress
) {
126 if (static_cast<QKeyEvent
* >(event
)->key() == Qt::Key_Escape
) {
127 m_searchInput
->clear();
130 return QWidget::event(event
);
133 void DolphinSearchBox::showEvent(QShowEvent
* event
)
135 if (!event
->spontaneous()) {
137 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
140 m_searchInput
->clear();
141 m_searchInput
->setFocus();
142 m_startedSearching
= false;
146 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
148 QWidget::keyReleaseEvent(event
);
149 if ((event
->key() == Qt::Key_Escape
)) {
150 if (m_searchInput
->text().isEmpty()) {
153 m_searchInput
->clear();
158 void DolphinSearchBox::emitSearchSignal()
160 m_startedSearching
= true;
161 emit
search(m_searchInput
->text());
164 void DolphinSearchBox::slotConfigurationChanged()
166 if (m_startedSearching
) {
171 void DolphinSearchBox::setFilterWidgetsVisible(bool visible
)
175 if (m_filterWidgetsLayout
== 0) {
176 m_filterWidgetsLayout
= new QFormLayout(this);
177 m_filterWidgetsLayout
->setSpacing(0);
179 m_filterWidgets
.append(new DateSearchFilterWidget(this));
180 m_filterWidgets
.append(new RatingSearchFilterWidget(this));
181 m_filterWidgets
.append(new TagSearchFilterWidget(this));
183 foreach (AbstractSearchFilterWidget
* filterWidget
, m_filterWidgets
) {
184 const QString labelText
= filterWidget
->filterLabel() + QLatin1Char(':');
185 QLabel
* label
= new QLabel(labelText
, this);
186 m_filterWidgetsLayout
->addRow(label
, filterWidget
);
187 connect(filterWidget
, SIGNAL(filterChanged()), this, SLOT(emitSearchSignal()));
190 m_topLayout
->addLayout(m_filterWidgetsLayout
);
192 m_topLayout
->removeItem(m_filterWidgetsLayout
);
199 void DolphinSearchBox::initButton(QPushButton
* button
)
201 button
->setAutoExclusive(true);
202 button
->setFlat(true);
203 button
->setCheckable(true);
204 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
207 void DolphinSearchBox::loadSettings()
209 if (SearchSettings::location() == QLatin1String("Everywhere")) {
210 m_everywhereButton
->setChecked(true);
212 m_fromHereButton
->setChecked(true);
215 if (SearchSettings::what() == QLatin1String("Content")) {
216 m_contentButton
->setChecked(true);
218 m_fileNameButton
->setChecked(true);
222 void DolphinSearchBox::saveSettings()
224 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
225 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
226 SearchSettings::self()->writeConfig();
229 void DolphinSearchBox::init()
231 // Create close button
232 QToolButton
* closeButton
= new QToolButton(this);
233 closeButton
->setAutoRaise(true);
234 closeButton
->setIcon(KIcon("dialog-close"));
235 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
236 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
238 // Create search label
239 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
242 m_searchInput
= new KLineEdit(this);
243 m_searchInput
->setClearButtonShown(true);
244 m_searchInput
->setFont(KGlobalSettings::generalFont());
245 connect(m_searchInput
, SIGNAL(returnPressed()),
246 this, SLOT(emitSearchSignal()));
247 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
248 this, SIGNAL(searchTextChanged(QString
)));
250 // Apply layout for the search input
251 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
252 searchInputLayout
->setMargin(0);
253 searchInputLayout
->addWidget(closeButton
);
254 searchInputLayout
->addWidget(searchLabel
);
255 searchInputLayout
->addWidget(m_searchInput
);
257 // Create "From Here" and "Everywhere"button
258 m_fromHereButton
= new QPushButton();
259 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
260 initButton(m_fromHereButton
);
262 m_everywhereButton
= new QPushButton(this);
263 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
264 initButton(m_everywhereButton
);
266 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
267 searchLocationGroup
->addButton(m_fromHereButton
);
268 searchLocationGroup
->addButton(m_everywhereButton
);
270 // Create "Filename" and "Content" button
271 m_fileNameButton
= new QPushButton(this);
272 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
273 initButton(m_fileNameButton
);
275 m_contentButton
= new QPushButton();
276 m_contentButton
->setText(i18nc("action:button", "Content"));
277 initButton(m_contentButton
);;
279 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
280 searchWhatGroup
->addButton(m_fileNameButton
);
281 searchWhatGroup
->addButton(m_contentButton
);
283 // Create "Filter" button
284 m_filterButton
= new QToolButton(this);
285 m_filterButton
->setIcon(KIcon("view-filter"));
286 m_filterButton
->setAutoRaise(true);
287 m_filterButton
->setCheckable(true);
288 m_filterButton
->hide();
289 connect(m_filterButton
, SIGNAL(toggled(bool)), this, SLOT(setFilterWidgetsVisible(bool)));
291 // Apply layout for the options
292 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
293 optionsLayout
->setMargin(0);
294 optionsLayout
->addWidget(m_fromHereButton
);
295 optionsLayout
->addWidget(m_everywhereButton
);
296 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
));
297 optionsLayout
->addWidget(m_fileNameButton
);
298 optionsLayout
->addWidget(m_contentButton
);
299 optionsLayout
->addStretch(1);
300 optionsLayout
->addWidget(m_filterButton
);
302 m_topLayout
= new QVBoxLayout(this);
303 m_topLayout
->addLayout(searchInputLayout
);
304 m_topLayout
->addLayout(optionsLayout
);
306 searchLabel
->setBuddy(m_searchInput
);
310 bool DolphinSearchBox::isSearchPathIndexed() const
313 const QString path
= m_searchPath
.path();
315 const KConfig
strigiConfig("nepomukstrigirc");
316 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
318 // Check whether the current search path is part of an indexed folder
319 bool isIndexed
= false;
320 foreach (const QString
& indexedFolder
, indexedFolders
) {
321 if (path
.startsWith(indexedFolder
)) {
328 // The current search path is part of an indexed folder. Check whether no
329 // excluded folder is part of the search path.
330 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
331 foreach (const QString
& excludedFolder
, excludedFolders
) {
332 if (excludedFolder
.startsWith(path
)) {
345 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
348 Nepomuk::Query::AndTerm andTerm
;
351 foreach (const AbstractSearchFilterWidget
* filterWidget
, m_filterWidgets
) {
352 const Nepomuk::Query::Term term
= filterWidget
->queryTerm();
353 if (term
.isValid()) {
354 andTerm
.addSubTerm(term
);
358 // Add input from search filter
359 const QString text
= m_searchInput
->text();
360 if (!text
.isEmpty()) {
361 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
);
362 if (customQuery
.isValid()) {
363 andTerm
.addSubTerm(customQuery
.term());
367 Nepomuk::Query::FileQuery fileQuery
;
368 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFiles
);
369 fileQuery
.setTerm(andTerm
);
371 if (fileQuery
.isValid()) {
372 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
373 "Query Results from '%1'",
380 #include "dolphinsearchbox.moc"