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 <QScrollArea>
39 #include <QToolButton>
40 #include <QVBoxLayout>
42 #include <config-nepomuk.h>
44 #include <nepomuk/andterm.h>
45 #include <nepomuk/filequery.h>
46 #include <nepomuk/literalterm.h>
47 #include <nepomuk/query.h>
48 #include <nepomuk/queryparser.h>
49 #include <nepomuk/resourcemanager.h>
50 #include <nepomuk/resourcetypeterm.h>
51 #include <nepomuk/comparisonterm.h>
52 #include <nepomuk/nfo.h>
55 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
57 m_startedSearching(false),
58 m_nepomukActivated(false),
65 m_everywhereButton(0),
71 DolphinSearchBox::~DolphinSearchBox()
76 QString
DolphinSearchBox::text() const
78 return m_searchInput
->text();
81 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
85 QFontMetrics
metrics(m_fromHereButton
->font());
86 const int maxWidth
= metrics
.averageCharWidth() * 15;
88 QString location
= url
.fileName();
89 if (location
.isEmpty()) {
90 if (url
.isLocalFile()) {
91 location
= QLatin1String("/");
93 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
97 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
98 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
100 const bool showSearchFromButtons
= url
.isLocalFile();
101 m_separator
->setVisible(showSearchFromButtons
);
102 m_fromHereButton
->setVisible(showSearchFromButtons
);
103 m_everywhereButton
->setVisible(showSearchFromButtons
);
106 KUrl
DolphinSearchBox::searchPath() const
111 KUrl
DolphinSearchBox::urlForSearching() const
114 if (m_nepomukActivated
&& isSearchPathIndexed()) {
115 url
= nepomukUrlForSearching();
117 url
.setProtocol("filenamesearch");
118 url
.addQueryItem("search", m_searchInput
->text());
119 if (m_contentButton
->isChecked()) {
120 url
.addQueryItem("checkContent", "yes");
124 if (m_everywhereButton
->isChecked()) {
125 // It is very unlikely, that the majority of Dolphins target users
126 // mean "the whole harddisk" instead of "my home folder" when
127 // selecting the "Everywhere" button.
128 encodedUrl
= QDir::homePath();
130 encodedUrl
= m_searchPath
.url();
132 url
.addQueryItem("url", encodedUrl
);
138 void DolphinSearchBox::selectAll()
140 m_searchInput
->selectAll();
143 bool DolphinSearchBox::event(QEvent
* event
)
145 if (event
->type() == QEvent::Polish
) {
148 return QWidget::event(event
);
151 void DolphinSearchBox::showEvent(QShowEvent
* event
)
153 if (!event
->spontaneous()) {
155 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
158 m_searchInput
->clear();
159 m_searchInput
->setFocus();
160 m_startedSearching
= false;
164 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
166 QWidget::keyReleaseEvent(event
);
167 if (event
->key() == Qt::Key_Escape
) {
168 if (m_searchInput
->text().isEmpty()) {
171 m_searchInput
->clear();
176 void DolphinSearchBox::emitSearchSignal()
178 m_startSearchTimer
->stop();
179 m_startedSearching
= true;
180 emit
search(m_searchInput
->text());
183 void DolphinSearchBox::slotConfigurationChanged()
185 if (m_startedSearching
) {
190 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
192 if (text
.isEmpty()) {
193 m_startSearchTimer
->stop();
195 m_startSearchTimer
->start();
197 emit
searchTextChanged(text
);
200 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
203 emit
returnPressed(text
);
206 void DolphinSearchBox::initButton(QPushButton
* button
)
208 button
->setAutoExclusive(true);
209 button
->setFlat(true);
210 button
->setCheckable(true);
211 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
214 void DolphinSearchBox::loadSettings()
216 if (SearchSettings::location() == QLatin1String("Everywhere")) {
217 m_everywhereButton
->setChecked(true);
219 m_fromHereButton
->setChecked(true);
222 if (SearchSettings::what() == QLatin1String("Content")) {
223 m_contentButton
->setChecked(true);
225 m_fileNameButton
->setChecked(true);
229 void DolphinSearchBox::saveSettings()
231 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
232 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
233 SearchSettings::self()->writeConfig();
236 void DolphinSearchBox::init()
238 // Create close button
239 QToolButton
* closeButton
= new QToolButton(this);
240 closeButton
->setAutoRaise(true);
241 closeButton
->setIcon(KIcon("dialog-close"));
242 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
243 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
245 // Create search label
246 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
249 m_searchInput
= new KLineEdit(this);
250 m_searchInput
->setClearButtonShown(true);
251 m_searchInput
->setFont(KGlobalSettings::generalFont());
252 setFocusProxy(m_searchInput
);
253 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
254 this, SLOT(slotReturnPressed(QString
)));
255 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
256 this, SLOT(slotSearchTextChanged(QString
)));
258 // Apply layout for the search input
259 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
260 searchInputLayout
->setMargin(0);
261 searchInputLayout
->addWidget(closeButton
);
262 searchInputLayout
->addWidget(searchLabel
);
263 searchInputLayout
->addWidget(m_searchInput
);
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 m_separator
= new KSeparator(Qt::Vertical
, this);
280 // Create "From Here" and "Everywhere"button
281 m_fromHereButton
= new QPushButton(this);
282 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
283 initButton(m_fromHereButton
);
285 m_everywhereButton
= new QPushButton(this);
286 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
287 initButton(m_everywhereButton
);
289 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
290 searchLocationGroup
->addButton(m_fromHereButton
);
291 searchLocationGroup
->addButton(m_everywhereButton
);
293 // Apply layout for the options
294 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
295 optionsLayout
->setMargin(0);
296 optionsLayout
->addWidget(m_fileNameButton
);
297 optionsLayout
->addWidget(m_contentButton
);
298 optionsLayout
->addWidget(m_separator
);
299 optionsLayout
->addWidget(m_fromHereButton
);
300 optionsLayout
->addWidget(m_everywhereButton
);
301 optionsLayout
->addStretch(1);
303 // Put the options into a QScrollArea. This prevents increasing the view width
304 // in case that not enough width for the options is available.
305 QWidget
* optionsContainer
= new QWidget(this);
306 optionsContainer
->setLayout(optionsLayout
);
307 QScrollArea
* optionsScrollArea
= new QScrollArea(this);
308 optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
309 optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
310 optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
311 optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
312 optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
313 optionsScrollArea
->setWidget(optionsContainer
);
314 optionsScrollArea
->setWidgetResizable(true);
316 m_topLayout
= new QVBoxLayout(this);
317 m_topLayout
->addLayout(searchInputLayout
);
318 m_topLayout
->addWidget(optionsScrollArea
);
320 searchLabel
->setBuddy(m_searchInput
);
323 // The searching should be started automatically after the user did not change
324 // the text within one second
325 m_startSearchTimer
= new QTimer(this);
326 m_startSearchTimer
->setSingleShot(true);
327 m_startSearchTimer
->setInterval(1000);
328 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
331 bool DolphinSearchBox::isSearchPathIndexed() const
334 const QString path
= m_searchPath
.path();
336 const KConfig
strigiConfig("nepomukstrigirc");
337 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
339 // Check whether the current search path is part of an indexed folder
340 bool isIndexed
= false;
341 foreach (const QString
& indexedFolder
, indexedFolders
) {
342 if (path
.startsWith(indexedFolder
)) {
349 // The current search path is part of an indexed folder. Check whether no
350 // excluded folder is part of the search path.
351 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
352 foreach (const QString
& excludedFolder
, excludedFolders
) {
353 if (path
.startsWith(excludedFolder
)) {
366 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
369 Nepomuk::Query::AndTerm andTerm
;
371 // Add input from search filter
372 const QString text
= m_searchInput
->text();
373 if (!text
.isEmpty()) {
374 if (m_fileNameButton
->isChecked()) {
375 QString regex
= QRegExp::escape(text
);
376 regex
.replace("\\*", QLatin1String(".*"));
377 regex
.replace("\\?", QLatin1String("."));
378 regex
.replace("\\", "\\\\");
379 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
380 Nepomuk::Vocabulary::NFO::fileName(),
381 Nepomuk::Query::LiteralTerm(regex
),
382 Nepomuk::Query::ComparisonTerm::Regexp
));
384 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
385 if (customQuery
.isValid()) {
386 andTerm
.addSubTerm(customQuery
.term());
391 Nepomuk::Query::FileQuery fileQuery
;
392 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders
);
393 fileQuery
.setTerm(andTerm
);
394 if (m_fromHereButton
->isChecked()) {
395 const bool recursive
= true;
396 fileQuery
.addIncludeFolder(m_searchPath
, recursive
);
399 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
400 "Query Results from '%1'",
407 #include "dolphinsearchbox.moc"