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 #include <nepomuk/andterm.h>
43 #include <nepomuk/filequery.h>
44 #include <nepomuk/literalterm.h>
45 #include <nepomuk/query.h>
46 #include <nepomuk/queryparser.h>
47 #include <nepomuk/resourcemanager.h>
48 #include <nepomuk/resourcetypeterm.h>
51 #include "filters/datesearchfilterwidget.h"
52 #include "filters/ratingsearchfilterwidget.h"
53 #include "filters/tagsearchfilterwidget.h"
56 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
58 m_startedSearching(false),
59 m_nepomukActivated(false),
63 m_everywhereButton(0),
67 m_filterWidgetsLayout(0),
73 DolphinSearchBox::~DolphinSearchBox()
78 QString
DolphinSearchBox::text() const
80 return m_searchInput
->text();
83 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
86 m_filterButton
->setVisible(isSearchPathIndexed());
89 KUrl
DolphinSearchBox::searchPath() const
94 KUrl
DolphinSearchBox::urlForSearching() const
97 if (isSearchPathIndexed() && !m_fileNameButton
->isChecked()) {
98 // TODO: Currently Nepomuk is not used for searching filenames. Nepomuk
99 // is capable to provide this, but further investigations are necessary to
100 // also support regular expressions.
101 url
= nepomukUrlForSearching();
104 url
.setProtocol("filenamesearch");
105 url
.addQueryItem("search", m_searchInput
->text());
106 if (m_contentButton
->isChecked()) {
107 url
.addQueryItem("checkContent", "yes");
109 if (m_everywhereButton
->isChecked()) {
110 // It is very unlikely, that the majority of Dolphins target users
111 // mean "the whole harddisk" instead of "my home folder" when
112 // selecting the "Everywhere" button.
113 url
.setPath(QDir::homePath());
120 bool DolphinSearchBox::event(QEvent
* event
)
122 if (event
->type() == QEvent::Polish
) {
124 } else if (event
->type() == QEvent::KeyPress
) {
125 if (static_cast<QKeyEvent
* >(event
)->key() == Qt::Key_Escape
) {
126 m_searchInput
->clear();
129 return QWidget::event(event
);
132 void DolphinSearchBox::showEvent(QShowEvent
* event
)
134 if (!event
->spontaneous()) {
136 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
139 m_searchInput
->clear();
140 m_searchInput
->setFocus();
141 m_startedSearching
= false;
145 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
147 QWidget::keyReleaseEvent(event
);
148 if ((event
->key() == Qt::Key_Escape
)) {
149 if (m_searchInput
->text().isEmpty()) {
152 m_searchInput
->clear();
157 void DolphinSearchBox::emitSearchSignal()
159 m_startedSearching
= true;
160 emit
search(m_searchInput
->text());
163 void DolphinSearchBox::slotConfigurationChanged()
165 if (m_startedSearching
) {
170 void DolphinSearchBox::setFilterWidgetsVisible(bool visible
)
174 if (m_filterWidgetsLayout
== 0) {
175 m_filterWidgetsLayout
= new QFormLayout(this);
176 m_filterWidgetsLayout
->setSpacing(0);
178 m_filterWidgets
.append(new DateSearchFilterWidget(this));
179 m_filterWidgets
.append(new RatingSearchFilterWidget(this));
180 m_filterWidgets
.append(new TagSearchFilterWidget(this));
182 foreach (AbstractSearchFilterWidget
* filterWidget
, m_filterWidgets
) {
183 const QString labelText
= filterWidget
->filterLabel() + QLatin1Char(':');
184 QLabel
* label
= new QLabel(labelText
, this);
185 m_filterWidgetsLayout
->addRow(label
, filterWidget
);
186 connect(filterWidget
, SIGNAL(filterChanged()), this, SLOT(emitSearchSignal()));
189 m_topLayout
->addLayout(m_filterWidgetsLayout
);
191 m_topLayout
->removeItem(m_filterWidgetsLayout
);
198 void DolphinSearchBox::initButton(QPushButton
* button
)
200 button
->setAutoExclusive(true);
201 button
->setFlat(true);
202 button
->setCheckable(true);
203 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
206 void DolphinSearchBox::loadSettings()
208 if (SearchSettings::location() == QLatin1String("Everywhere")) {
209 m_everywhereButton
->setChecked(true);
211 m_fromHereButton
->setChecked(true);
214 if (SearchSettings::what() == QLatin1String("Content")) {
215 m_contentButton
->setChecked(true);
217 m_fileNameButton
->setChecked(true);
221 void DolphinSearchBox::saveSettings()
223 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
224 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
225 SearchSettings::self()->writeConfig();
228 void DolphinSearchBox::init()
230 // Create close button
231 QToolButton
* closeButton
= new QToolButton(this);
232 closeButton
->setAutoRaise(true);
233 closeButton
->setIcon(KIcon("dialog-close"));
234 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
235 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
237 // Create search label
238 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
241 m_searchInput
= new KLineEdit(this);
242 m_searchInput
->setClearButtonShown(true);
243 m_searchInput
->setFont(KGlobalSettings::generalFont());
244 connect(m_searchInput
, SIGNAL(returnPressed()),
245 this, SLOT(emitSearchSignal()));
246 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
247 this, SIGNAL(searchTextChanged(QString
)));
249 // Apply layout for the search input
250 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
251 searchInputLayout
->setMargin(0);
252 searchInputLayout
->addWidget(closeButton
);
253 searchInputLayout
->addWidget(searchLabel
);
254 searchInputLayout
->addWidget(m_searchInput
);
256 // Create "From Here" and "Everywhere"button
257 m_fromHereButton
= new QPushButton();
258 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
259 initButton(m_fromHereButton
);
261 m_everywhereButton
= new QPushButton(this);
262 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
263 initButton(m_everywhereButton
);
265 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
266 searchLocationGroup
->addButton(m_fromHereButton
);
267 searchLocationGroup
->addButton(m_everywhereButton
);
269 // Create "Filename" and "Content" button
270 m_fileNameButton
= new QPushButton(this);
271 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
272 initButton(m_fileNameButton
);
274 m_contentButton
= new QPushButton();
275 m_contentButton
->setText(i18nc("action:button", "Content"));
276 initButton(m_contentButton
);;
278 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
279 searchWhatGroup
->addButton(m_fileNameButton
);
280 searchWhatGroup
->addButton(m_contentButton
);
282 // Create "Filter" button
283 m_filterButton
= new QToolButton(this);
284 m_filterButton
->setIcon(KIcon("view-filter"));
285 m_filterButton
->setAutoRaise(true);
286 m_filterButton
->setCheckable(true);
287 m_filterButton
->hide();
288 connect(m_filterButton
, SIGNAL(toggled(bool)), this, SLOT(setFilterWidgetsVisible(bool)));
290 // Apply layout for the options
291 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
292 optionsLayout
->setMargin(0);
293 optionsLayout
->addWidget(m_fromHereButton
);
294 optionsLayout
->addWidget(m_everywhereButton
);
295 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
));
296 optionsLayout
->addWidget(m_fileNameButton
);
297 optionsLayout
->addWidget(m_contentButton
);
298 optionsLayout
->addStretch(1);
299 optionsLayout
->addWidget(m_filterButton
);
301 m_topLayout
= new QVBoxLayout(this);
302 m_topLayout
->addLayout(searchInputLayout
);
303 m_topLayout
->addLayout(optionsLayout
);
305 searchLabel
->setBuddy(m_searchInput
);
309 bool DolphinSearchBox::isSearchPathIndexed() const
312 const QString path
= m_searchPath
.path();
314 const KConfig
strigiConfig("nepomukstrigirc");
315 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
317 // Check whether the current search path is part of an indexed folder
318 bool isIndexed
= false;
319 foreach (const QString
& indexedFolder
, indexedFolders
) {
320 if (path
.startsWith(indexedFolder
)) {
327 // The current search path is part of an indexed folder. Check whether no
328 // excluded folder is part of the search path.
329 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
330 foreach (const QString
& excludedFolder
, excludedFolders
) {
331 if (excludedFolder
.startsWith(path
)) {
344 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
347 Nepomuk::Query::AndTerm andTerm
;
350 foreach (const AbstractSearchFilterWidget
* filterWidget
, m_filterWidgets
) {
351 const Nepomuk::Query::Term term
= filterWidget
->queryTerm();
352 if (term
.isValid()) {
353 andTerm
.addSubTerm(term
);
357 // Add input from search filter
358 const QString text
= m_searchInput
->text();
359 if (!text
.isEmpty()) {
360 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
);
361 if (customQuery
.isValid()) {
362 andTerm
.addSubTerm(customQuery
.term());
366 Nepomuk::Query::FileQuery fileQuery
;
367 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFiles
);
368 fileQuery
.setTerm(andTerm
);
370 if (fileQuery
.isValid()) {
371 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
372 "Query Results from '%1'",
379 #include "dolphinsearchbox.moc"