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),
69 DolphinSearchBox::~DolphinSearchBox()
74 QString
DolphinSearchBox::text() const
76 return m_searchInput
->text();
79 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
83 QFontMetrics
metrics(m_fromHereButton
->font());
84 const int maxWidth
= metrics
.averageCharWidth() * 15;
86 QString location
= url
.fileName();
87 if (location
.isEmpty()) {
88 if (url
.isLocalFile()) {
89 location
= QLatin1String("/");
91 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
95 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
96 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
99 KUrl
DolphinSearchBox::searchPath() const
104 KUrl
DolphinSearchBox::urlForSearching() const
107 if (m_nepomukActivated
&& isSearchPathIndexed()) {
108 url
= nepomukUrlForSearching();
110 url
.setProtocol("filenamesearch");
111 url
.addQueryItem("search", m_searchInput
->text());
112 if (m_contentButton
->isChecked()) {
113 url
.addQueryItem("checkContent", "yes");
117 if (m_everywhereButton
->isChecked()) {
118 // It is very unlikely, that the majority of Dolphins target users
119 // mean "the whole harddisk" instead of "my home folder" when
120 // selecting the "Everywhere" button.
121 encodedUrl
= QDir::homePath();
123 encodedUrl
= m_searchPath
.url();
125 url
.addQueryItem("url", encodedUrl
);
131 bool DolphinSearchBox::event(QEvent
* event
)
133 if (event
->type() == QEvent::Polish
) {
136 return QWidget::event(event
);
139 void DolphinSearchBox::showEvent(QShowEvent
* event
)
141 if (!event
->spontaneous()) {
143 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
146 m_searchInput
->clear();
147 m_searchInput
->setFocus();
148 m_startedSearching
= false;
152 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
154 QWidget::keyReleaseEvent(event
);
155 if ((event
->key() == Qt::Key_Escape
)) {
156 if (m_searchInput
->text().isEmpty()) {
159 m_searchInput
->clear();
164 void DolphinSearchBox::emitSearchSignal()
166 m_startSearchTimer
->stop();
167 m_startedSearching
= true;
168 emit
search(m_searchInput
->text());
171 void DolphinSearchBox::slotConfigurationChanged()
173 if (m_startedSearching
) {
178 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
180 if (text
.isEmpty()) {
181 m_startSearchTimer
->stop();
183 m_startSearchTimer
->start();
185 emit
searchTextChanged(text
);
188 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
191 emit
returnPressed(text
);
194 void DolphinSearchBox::initButton(QPushButton
* button
)
196 button
->setAutoExclusive(true);
197 button
->setFlat(true);
198 button
->setCheckable(true);
199 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
202 void DolphinSearchBox::loadSettings()
204 if (SearchSettings::location() == QLatin1String("Everywhere")) {
205 m_everywhereButton
->setChecked(true);
207 m_fromHereButton
->setChecked(true);
210 if (SearchSettings::what() == QLatin1String("Content")) {
211 m_contentButton
->setChecked(true);
213 m_fileNameButton
->setChecked(true);
217 void DolphinSearchBox::saveSettings()
219 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
220 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
221 SearchSettings::self()->writeConfig();
224 void DolphinSearchBox::init()
226 // Create close button
227 QToolButton
* closeButton
= new QToolButton(this);
228 closeButton
->setAutoRaise(true);
229 closeButton
->setIcon(KIcon("dialog-close"));
230 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
231 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
233 // Create search label
234 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
237 m_searchInput
= new KLineEdit(this);
238 m_searchInput
->setClearButtonShown(true);
239 m_searchInput
->setFont(KGlobalSettings::generalFont());
240 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
241 this, SLOT(slotReturnPressed(QString
)));
242 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
243 this, SLOT(slotSearchTextChanged(QString
)));
245 // Apply layout for the search input
246 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
247 searchInputLayout
->setMargin(0);
248 searchInputLayout
->addWidget(closeButton
);
249 searchInputLayout
->addWidget(searchLabel
);
250 searchInputLayout
->addWidget(m_searchInput
);
252 // Create "From Here" and "Everywhere"button
253 m_fromHereButton
= new QPushButton(this);
254 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
255 initButton(m_fromHereButton
);
257 m_everywhereButton
= new QPushButton(this);
258 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
259 initButton(m_everywhereButton
);
261 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
262 searchLocationGroup
->addButton(m_fromHereButton
);
263 searchLocationGroup
->addButton(m_everywhereButton
);
265 // Create "Filename" and "Content" button
266 m_fileNameButton
= new QPushButton(this);
267 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
268 initButton(m_fileNameButton
);
270 m_contentButton
= new QPushButton();
271 m_contentButton
->setText(i18nc("action:button", "Content"));
272 initButton(m_contentButton
);;
274 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
275 searchWhatGroup
->addButton(m_fileNameButton
);
276 searchWhatGroup
->addButton(m_contentButton
);
278 // Apply layout for the options
279 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
280 optionsLayout
->setMargin(0);
281 optionsLayout
->addWidget(m_fromHereButton
);
282 optionsLayout
->addWidget(m_everywhereButton
);
283 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
));
284 optionsLayout
->addWidget(m_fileNameButton
);
285 optionsLayout
->addWidget(m_contentButton
);
286 optionsLayout
->addStretch(1);
288 m_topLayout
= new QVBoxLayout(this);
289 m_topLayout
->addLayout(searchInputLayout
);
290 m_topLayout
->addLayout(optionsLayout
);
292 searchLabel
->setBuddy(m_searchInput
);
295 // The searching should be started automatically after the user did not change
296 // the text within one second
297 m_startSearchTimer
= new QTimer(this);
298 m_startSearchTimer
->setSingleShot(true);
299 m_startSearchTimer
->setInterval(1000);
300 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
303 bool DolphinSearchBox::isSearchPathIndexed() const
306 const QString path
= m_searchPath
.path();
308 const KConfig
strigiConfig("nepomukstrigirc");
309 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
311 // Check whether the current search path is part of an indexed folder
312 bool isIndexed
= false;
313 foreach (const QString
& indexedFolder
, indexedFolders
) {
314 if (path
.startsWith(indexedFolder
)) {
321 // The current search path is part of an indexed folder. Check whether no
322 // excluded folder is part of the search path.
323 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
324 foreach (const QString
& excludedFolder
, excludedFolders
) {
325 if (path
.startsWith(excludedFolder
)) {
338 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
341 Nepomuk::Query::AndTerm andTerm
;
343 // Add input from search filter
344 const QString text
= m_searchInput
->text();
345 if (!text
.isEmpty()) {
346 if (m_fileNameButton
->isChecked()) {
347 QString regex
= QRegExp::escape(text
);
348 regex
.replace("\\*", QLatin1String(".*"));
349 regex
.replace("\\?", QLatin1String("."));
350 regex
.replace("\\", "\\\\");
351 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
352 Nepomuk::Vocabulary::NFO::fileName(),
353 Nepomuk::Query::LiteralTerm(regex
),
354 Nepomuk::Query::ComparisonTerm::Regexp
));
356 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
357 if (customQuery
.isValid()) {
358 andTerm
.addSubTerm(customQuery
.term());
363 Nepomuk::Query::FileQuery fileQuery
;
364 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFiles
);
365 fileQuery
.setTerm(andTerm
);
366 if(m_fromHereButton
->isChecked())
367 fileQuery
.addIncludeFolder(m_searchPath
, false);
369 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
370 "Query Results from '%1'",
377 #include "dolphinsearchbox.moc"