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;
85 const QString fileName
= metrics
.elidedText(url
.fileName(), Qt::ElideMiddle
, maxWidth
);
86 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", fileName
));
89 KUrl
DolphinSearchBox::searchPath() const
94 KUrl
DolphinSearchBox::urlForSearching() const
97 if (m_nepomukActivated
&& isSearchPathIndexed()) {
98 url
= nepomukUrlForSearching();
101 url
.setProtocol("filenamesearch");
102 url
.addQueryItem("search", m_searchInput
->text());
103 if (m_contentButton
->isChecked()) {
104 url
.addQueryItem("checkContent", "yes");
106 if (m_everywhereButton
->isChecked()) {
107 // It is very unlikely, that the majority of Dolphins target users
108 // mean "the whole harddisk" instead of "my home folder" when
109 // selecting the "Everywhere" button.
110 url
.setPath(QDir::homePath());
117 bool DolphinSearchBox::event(QEvent
* event
)
119 if (event
->type() == QEvent::Polish
) {
122 return QWidget::event(event
);
125 void DolphinSearchBox::showEvent(QShowEvent
* event
)
127 if (!event
->spontaneous()) {
129 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
132 m_searchInput
->clear();
133 m_searchInput
->setFocus();
134 m_startedSearching
= false;
138 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
140 QWidget::keyReleaseEvent(event
);
141 if ((event
->key() == Qt::Key_Escape
)) {
142 if (m_searchInput
->text().isEmpty()) {
145 m_searchInput
->clear();
150 void DolphinSearchBox::emitSearchSignal()
152 m_startSearchTimer
->stop();
153 m_startedSearching
= true;
154 emit
search(m_searchInput
->text());
157 void DolphinSearchBox::slotConfigurationChanged()
159 if (m_startedSearching
) {
164 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
166 if (text
.isEmpty()) {
167 m_startSearchTimer
->stop();
169 m_startSearchTimer
->start();
171 emit
searchTextChanged(text
);
174 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
177 emit
returnPressed(text
);
180 void DolphinSearchBox::initButton(QPushButton
* button
)
182 button
->setAutoExclusive(true);
183 button
->setFlat(true);
184 button
->setCheckable(true);
185 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
188 void DolphinSearchBox::loadSettings()
190 if (SearchSettings::location() == QLatin1String("Everywhere")) {
191 m_everywhereButton
->setChecked(true);
193 m_fromHereButton
->setChecked(true);
196 if (SearchSettings::what() == QLatin1String("Content")) {
197 m_contentButton
->setChecked(true);
199 m_fileNameButton
->setChecked(true);
203 void DolphinSearchBox::saveSettings()
205 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
206 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
207 SearchSettings::self()->writeConfig();
210 void DolphinSearchBox::init()
212 // Create close button
213 QToolButton
* closeButton
= new QToolButton(this);
214 closeButton
->setAutoRaise(true);
215 closeButton
->setIcon(KIcon("dialog-close"));
216 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
217 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
219 // Create search label
220 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
223 m_searchInput
= new KLineEdit(this);
224 m_searchInput
->setClearButtonShown(true);
225 m_searchInput
->setFont(KGlobalSettings::generalFont());
226 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
227 this, SLOT(slotReturnPressed(QString
)));
228 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
229 this, SLOT(slotSearchTextChanged(QString
)));
231 // Apply layout for the search input
232 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
233 searchInputLayout
->setMargin(0);
234 searchInputLayout
->addWidget(closeButton
);
235 searchInputLayout
->addWidget(searchLabel
);
236 searchInputLayout
->addWidget(m_searchInput
);
238 // Create "From Here" and "Everywhere"button
239 m_fromHereButton
= new QPushButton(this);
240 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
241 initButton(m_fromHereButton
);
243 m_everywhereButton
= new QPushButton(this);
244 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
245 initButton(m_everywhereButton
);
247 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
248 searchLocationGroup
->addButton(m_fromHereButton
);
249 searchLocationGroup
->addButton(m_everywhereButton
);
251 // Create "Filename" and "Content" button
252 m_fileNameButton
= new QPushButton(this);
253 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
254 initButton(m_fileNameButton
);
256 m_contentButton
= new QPushButton();
257 m_contentButton
->setText(i18nc("action:button", "Content"));
258 initButton(m_contentButton
);;
260 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
261 searchWhatGroup
->addButton(m_fileNameButton
);
262 searchWhatGroup
->addButton(m_contentButton
);
264 // Apply layout for the options
265 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
266 optionsLayout
->setMargin(0);
267 optionsLayout
->addWidget(m_fromHereButton
);
268 optionsLayout
->addWidget(m_everywhereButton
);
269 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
));
270 optionsLayout
->addWidget(m_fileNameButton
);
271 optionsLayout
->addWidget(m_contentButton
);
272 optionsLayout
->addStretch(1);
274 m_topLayout
= new QVBoxLayout(this);
275 m_topLayout
->addLayout(searchInputLayout
);
276 m_topLayout
->addLayout(optionsLayout
);
278 searchLabel
->setBuddy(m_searchInput
);
281 // The searching should be started automatically after the user did not change
282 // the text within one second
283 m_startSearchTimer
= new QTimer(this);
284 m_startSearchTimer
->setSingleShot(true);
285 m_startSearchTimer
->setInterval(1000);
286 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
289 bool DolphinSearchBox::isSearchPathIndexed() const
292 const QString path
= m_searchPath
.path();
294 const KConfig
strigiConfig("nepomukstrigirc");
295 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
297 // Check whether the current search path is part of an indexed folder
298 bool isIndexed
= false;
299 foreach (const QString
& indexedFolder
, indexedFolders
) {
300 if (path
.startsWith(indexedFolder
)) {
307 // The current search path is part of an indexed folder. Check whether no
308 // excluded folder is part of the search path.
309 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
310 foreach (const QString
& excludedFolder
, excludedFolders
) {
311 if (path
.startsWith(excludedFolder
)) {
324 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
327 Nepomuk::Query::AndTerm andTerm
;
329 // Add input from search filter
330 const QString text
= m_searchInput
->text();
331 if (!text
.isEmpty()) {
332 if (m_fileNameButton
->isChecked()) {
333 QString regex
= QRegExp::escape(text
);
334 regex
.replace("\\*", QLatin1String(".*"));
335 regex
.replace("\\?", QLatin1String("."));
336 regex
.replace("\\", "\\\\");
337 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
338 Nepomuk::Vocabulary::NFO::fileName(),
339 Nepomuk::Query::LiteralTerm(regex
),
340 Nepomuk::Query::ComparisonTerm::Regexp
));
342 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
343 if (customQuery
.isValid()) {
344 andTerm
.addSubTerm(customQuery
.term());
349 Nepomuk::Query::FileQuery fileQuery
;
350 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFiles
);
351 fileQuery
.setTerm(andTerm
);
352 if(m_fromHereButton
->isChecked())
353 fileQuery
.addIncludeFolder(m_searchPath
, false);
355 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
356 "Query Results from '%1'",
363 #include "dolphinsearchbox.moc"