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>
38 #include <QToolButton>
39 #include <QVBoxLayout>
41 #include <config-nepomuk.h>
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>
50 #include <nepomuk/comparisonterm.h>
51 #include <nepomuk/nfo.h>
54 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
56 m_startedSearching(false),
57 m_nepomukActivated(false),
61 m_everywhereButton(0),
70 DolphinSearchBox::~DolphinSearchBox()
75 QString
DolphinSearchBox::text() const
77 return m_searchInput
->text();
80 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
83 m_filterButton
->setVisible(m_nepomukActivated
&& isSearchPathIndexed());
86 KUrl
DolphinSearchBox::searchPath() const
91 KUrl
DolphinSearchBox::urlForSearching() const
94 if (m_nepomukActivated
&& isSearchPathIndexed()) {
95 url
= nepomukUrlForSearching();
98 url
.setProtocol("filenamesearch");
99 url
.addQueryItem("search", m_searchInput
->text());
100 if (m_contentButton
->isChecked()) {
101 url
.addQueryItem("checkContent", "yes");
103 if (m_everywhereButton
->isChecked()) {
104 // It is very unlikely, that the majority of Dolphins target users
105 // mean "the whole harddisk" instead of "my home folder" when
106 // selecting the "Everywhere" button.
107 url
.setPath(QDir::homePath());
114 bool DolphinSearchBox::event(QEvent
* event
)
116 if (event
->type() == QEvent::Polish
) {
119 return QWidget::event(event
);
122 void DolphinSearchBox::showEvent(QShowEvent
* event
)
124 if (!event
->spontaneous()) {
126 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
129 m_searchInput
->clear();
130 m_searchInput
->setFocus();
131 m_startedSearching
= false;
135 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
137 QWidget::keyReleaseEvent(event
);
138 if ((event
->key() == Qt::Key_Escape
)) {
139 if (m_searchInput
->text().isEmpty()) {
142 m_searchInput
->clear();
147 void DolphinSearchBox::emitSearchSignal()
149 m_startSearchTimer
->stop();
150 m_startedSearching
= true;
151 emit
search(m_searchInput
->text());
154 void DolphinSearchBox::slotConfigurationChanged()
156 if (m_startedSearching
) {
161 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
163 if (text
.isEmpty()) {
164 m_startSearchTimer
->stop();
166 m_startSearchTimer
->start();
168 emit
searchTextChanged(text
);
171 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
174 emit
returnPressed(text
);
177 void DolphinSearchBox::initButton(QPushButton
* button
)
179 button
->setAutoExclusive(true);
180 button
->setFlat(true);
181 button
->setCheckable(true);
182 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
185 void DolphinSearchBox::loadSettings()
187 if (SearchSettings::location() == QLatin1String("Everywhere")) {
188 m_everywhereButton
->setChecked(true);
190 m_fromHereButton
->setChecked(true);
193 if (SearchSettings::what() == QLatin1String("Content")) {
194 m_contentButton
->setChecked(true);
196 m_fileNameButton
->setChecked(true);
200 void DolphinSearchBox::saveSettings()
202 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
203 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
204 SearchSettings::self()->writeConfig();
207 void DolphinSearchBox::init()
209 // Create close button
210 QToolButton
* closeButton
= new QToolButton(this);
211 closeButton
->setAutoRaise(true);
212 closeButton
->setIcon(KIcon("dialog-close"));
213 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
214 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
216 // Create search label
217 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
220 m_searchInput
= new KLineEdit(this);
221 m_searchInput
->setClearButtonShown(true);
222 m_searchInput
->setFont(KGlobalSettings::generalFont());
223 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
224 this, SLOT(slotReturnPressed(QString
)));
225 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
226 this, SLOT(slotSearchTextChanged(QString
)));
228 // Apply layout for the search input
229 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
230 searchInputLayout
->setMargin(0);
231 searchInputLayout
->addWidget(closeButton
);
232 searchInputLayout
->addWidget(searchLabel
);
233 searchInputLayout
->addWidget(m_searchInput
);
235 // Create "From Here" and "Everywhere"button
236 m_fromHereButton
= new QPushButton();
237 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
238 initButton(m_fromHereButton
);
240 m_everywhereButton
= new QPushButton(this);
241 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
242 initButton(m_everywhereButton
);
244 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
245 searchLocationGroup
->addButton(m_fromHereButton
);
246 searchLocationGroup
->addButton(m_everywhereButton
);
248 // Create "Filename" and "Content" button
249 m_fileNameButton
= new QPushButton(this);
250 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
251 initButton(m_fileNameButton
);
253 m_contentButton
= new QPushButton();
254 m_contentButton
->setText(i18nc("action:button", "Content"));
255 initButton(m_contentButton
);;
257 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
258 searchWhatGroup
->addButton(m_fileNameButton
);
259 searchWhatGroup
->addButton(m_contentButton
);
261 // Create "Filter" button
262 m_filterButton
= new QToolButton(this);
263 m_filterButton
->setIcon(KIcon("view-filter"));
264 m_filterButton
->setAutoRaise(true);
265 m_filterButton
->setCheckable(true);
266 m_filterButton
->hide();
267 //connect(m_filterButton, SIGNAL(toggled(bool)), this, SLOT(setFilterWidgetsVisible(bool)));
269 // Apply layout for the options
270 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
271 optionsLayout
->setMargin(0);
272 optionsLayout
->addWidget(m_fromHereButton
);
273 optionsLayout
->addWidget(m_everywhereButton
);
274 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
));
275 optionsLayout
->addWidget(m_fileNameButton
);
276 optionsLayout
->addWidget(m_contentButton
);
277 optionsLayout
->addStretch(1);
278 optionsLayout
->addWidget(m_filterButton
);
280 m_topLayout
= new QVBoxLayout(this);
281 m_topLayout
->addLayout(searchInputLayout
);
282 m_topLayout
->addLayout(optionsLayout
);
284 searchLabel
->setBuddy(m_searchInput
);
287 // The searching should be started automatically after the user did not change
288 // the text within one second
289 m_startSearchTimer
= new QTimer(this);
290 m_startSearchTimer
->setSingleShot(true);
291 m_startSearchTimer
->setInterval(1000);
292 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
295 bool DolphinSearchBox::isSearchPathIndexed() const
298 const QString path
= m_searchPath
.path();
300 const KConfig
strigiConfig("nepomukstrigirc");
301 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
303 // Check whether the current search path is part of an indexed folder
304 bool isIndexed
= false;
305 foreach (const QString
& indexedFolder
, indexedFolders
) {
306 if (path
.startsWith(indexedFolder
)) {
313 // The current search path is part of an indexed folder. Check whether no
314 // excluded folder is part of the search path.
315 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
316 foreach (const QString
& excludedFolder
, excludedFolders
) {
317 if (path
.startsWith(excludedFolder
)) {
330 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
333 Nepomuk::Query::AndTerm andTerm
;
335 // Add input from search filter
336 const QString text
= m_searchInput
->text();
337 if (!text
.isEmpty()) {
338 if (m_fileNameButton
->isChecked()) {
339 QString regex
= QRegExp::escape(text
);
340 regex
.replace("\\*", QLatin1String(".*"));
341 regex
.replace("\\?", QLatin1String("."));
342 regex
.replace("\\", "\\\\");
343 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
344 Nepomuk::Vocabulary::NFO::fileName(),
345 Nepomuk::Query::LiteralTerm(regex
),
346 Nepomuk::Query::ComparisonTerm::Regexp
));
348 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
349 if (customQuery
.isValid()) {
350 andTerm
.addSubTerm(customQuery
.term());
355 Nepomuk::Query::FileQuery fileQuery
;
356 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFiles
);
357 fileQuery
.setTerm(andTerm
);
359 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
360 "Query Results from '%1'",
367 #include "dolphinsearchbox.moc"