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),
64 m_everywhereButton(0),
70 DolphinSearchBox::~DolphinSearchBox()
75 QString
DolphinSearchBox::text() const
77 return m_searchInput
->text();
80 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
84 QFontMetrics
metrics(m_fromHereButton
->font());
85 const int maxWidth
= metrics
.averageCharWidth() * 15;
87 QString location
= url
.fileName();
88 if (location
.isEmpty()) {
89 if (url
.isLocalFile()) {
90 location
= QLatin1String("/");
92 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
96 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
97 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
99 const bool showSearchFromButtons
= url
.isLocalFile();
100 m_separator
->setVisible(showSearchFromButtons
);
101 m_fromHereButton
->setVisible(showSearchFromButtons
);
102 m_everywhereButton
->setVisible(showSearchFromButtons
);
105 KUrl
DolphinSearchBox::searchPath() const
110 KUrl
DolphinSearchBox::urlForSearching() const
113 if (m_nepomukActivated
&& isSearchPathIndexed()) {
114 url
= nepomukUrlForSearching();
116 url
.setProtocol("filenamesearch");
117 url
.addQueryItem("search", m_searchInput
->text());
118 if (m_contentButton
->isChecked()) {
119 url
.addQueryItem("checkContent", "yes");
123 if (m_everywhereButton
->isChecked()) {
124 // It is very unlikely, that the majority of Dolphins target users
125 // mean "the whole harddisk" instead of "my home folder" when
126 // selecting the "Everywhere" button.
127 encodedUrl
= QDir::homePath();
129 encodedUrl
= m_searchPath
.url();
131 url
.addQueryItem("url", encodedUrl
);
137 bool DolphinSearchBox::event(QEvent
* event
)
139 if (event
->type() == QEvent::Polish
) {
142 return QWidget::event(event
);
145 void DolphinSearchBox::showEvent(QShowEvent
* event
)
147 if (!event
->spontaneous()) {
149 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
152 m_searchInput
->clear();
153 m_searchInput
->setFocus();
154 m_startedSearching
= false;
158 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
160 QWidget::keyReleaseEvent(event
);
161 if ((event
->key() == Qt::Key_Escape
)) {
162 if (m_searchInput
->text().isEmpty()) {
165 m_searchInput
->clear();
170 void DolphinSearchBox::emitSearchSignal()
172 m_startSearchTimer
->stop();
173 m_startedSearching
= true;
174 emit
search(m_searchInput
->text());
177 void DolphinSearchBox::slotConfigurationChanged()
179 if (m_startedSearching
) {
184 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
186 if (text
.isEmpty()) {
187 m_startSearchTimer
->stop();
189 m_startSearchTimer
->start();
191 emit
searchTextChanged(text
);
194 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
197 emit
returnPressed(text
);
200 void DolphinSearchBox::initButton(QPushButton
* button
)
202 button
->setAutoExclusive(true);
203 button
->setFlat(true);
204 button
->setCheckable(true);
205 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
208 void DolphinSearchBox::loadSettings()
210 if (SearchSettings::location() == QLatin1String("Everywhere")) {
211 m_everywhereButton
->setChecked(true);
213 m_fromHereButton
->setChecked(true);
216 if (SearchSettings::what() == QLatin1String("Content")) {
217 m_contentButton
->setChecked(true);
219 m_fileNameButton
->setChecked(true);
223 void DolphinSearchBox::saveSettings()
225 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
226 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
227 SearchSettings::self()->writeConfig();
230 void DolphinSearchBox::init()
232 // Create close button
233 QToolButton
* closeButton
= new QToolButton(this);
234 closeButton
->setAutoRaise(true);
235 closeButton
->setIcon(KIcon("dialog-close"));
236 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
237 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
239 // Create search label
240 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
243 m_searchInput
= new KLineEdit(this);
244 m_searchInput
->setClearButtonShown(true);
245 m_searchInput
->setFont(KGlobalSettings::generalFont());
246 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
247 this, SLOT(slotReturnPressed(QString
)));
248 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
249 this, SLOT(slotSearchTextChanged(QString
)));
251 // Apply layout for the search input
252 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
253 searchInputLayout
->setMargin(0);
254 searchInputLayout
->addWidget(closeButton
);
255 searchInputLayout
->addWidget(searchLabel
);
256 searchInputLayout
->addWidget(m_searchInput
);
258 // Create "Filename" and "Content" button
259 m_fileNameButton
= new QPushButton(this);
260 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
261 initButton(m_fileNameButton
);
263 m_contentButton
= new QPushButton();
264 m_contentButton
->setText(i18nc("action:button", "Content"));
265 initButton(m_contentButton
);;
267 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
268 searchWhatGroup
->addButton(m_fileNameButton
);
269 searchWhatGroup
->addButton(m_contentButton
);
271 m_separator
= new KSeparator(Qt::Vertical
, this);
273 // Create "From Here" and "Everywhere"button
274 m_fromHereButton
= new QPushButton(this);
275 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
276 initButton(m_fromHereButton
);
278 m_everywhereButton
= new QPushButton(this);
279 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
280 initButton(m_everywhereButton
);
282 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
283 searchLocationGroup
->addButton(m_fromHereButton
);
284 searchLocationGroup
->addButton(m_everywhereButton
);
286 // Apply layout for the options
287 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
288 optionsLayout
->setMargin(0);
289 optionsLayout
->addWidget(m_fileNameButton
);
290 optionsLayout
->addWidget(m_contentButton
);
291 optionsLayout
->addWidget(m_separator
);
292 optionsLayout
->addWidget(m_fromHereButton
);
293 optionsLayout
->addWidget(m_everywhereButton
);
294 optionsLayout
->addStretch(1);
296 m_topLayout
= new QVBoxLayout(this);
297 m_topLayout
->addLayout(searchInputLayout
);
298 m_topLayout
->addLayout(optionsLayout
);
300 searchLabel
->setBuddy(m_searchInput
);
303 // The searching should be started automatically after the user did not change
304 // the text within one second
305 m_startSearchTimer
= new QTimer(this);
306 m_startSearchTimer
->setSingleShot(true);
307 m_startSearchTimer
->setInterval(1000);
308 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
311 bool DolphinSearchBox::isSearchPathIndexed() const
314 const QString path
= m_searchPath
.path();
316 const KConfig
strigiConfig("nepomukstrigirc");
317 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
319 // Check whether the current search path is part of an indexed folder
320 bool isIndexed
= false;
321 foreach (const QString
& indexedFolder
, indexedFolders
) {
322 if (path
.startsWith(indexedFolder
)) {
329 // The current search path is part of an indexed folder. Check whether no
330 // excluded folder is part of the search path.
331 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
332 foreach (const QString
& excludedFolder
, excludedFolders
) {
333 if (path
.startsWith(excludedFolder
)) {
346 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
349 Nepomuk::Query::AndTerm andTerm
;
351 // Add input from search filter
352 const QString text
= m_searchInput
->text();
353 if (!text
.isEmpty()) {
354 if (m_fileNameButton
->isChecked()) {
355 QString regex
= QRegExp::escape(text
);
356 regex
.replace("\\*", QLatin1String(".*"));
357 regex
.replace("\\?", QLatin1String("."));
358 regex
.replace("\\", "\\\\");
359 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
360 Nepomuk::Vocabulary::NFO::fileName(),
361 Nepomuk::Query::LiteralTerm(regex
),
362 Nepomuk::Query::ComparisonTerm::Regexp
));
364 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
365 if (customQuery
.isValid()) {
366 andTerm
.addSubTerm(customQuery
.term());
371 Nepomuk::Query::FileQuery fileQuery
;
372 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFiles
);
373 fileQuery
.setTerm(andTerm
);
374 if(m_fromHereButton
->isChecked())
375 fileQuery
.addIncludeFolder(m_searchPath
, false);
377 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
378 "Query Results from '%1'",
385 #include "dolphinsearchbox.moc"