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();
100 url
.setProtocol("filenamesearch");
101 url
.addQueryItem("search", m_searchInput
->text());
102 if (m_contentButton
->isChecked()) {
103 url
.addQueryItem("checkContent", "yes");
107 if (m_everywhereButton
->isChecked()) {
108 // It is very unlikely, that the majority of Dolphins target users
109 // mean "the whole harddisk" instead of "my home folder" when
110 // selecting the "Everywhere" button.
111 encodedUrl
= QDir::homePath();
113 encodedUrl
= m_searchPath
.url();
115 url
.addQueryItem("url", encodedUrl
);
121 bool DolphinSearchBox::event(QEvent
* event
)
123 if (event
->type() == QEvent::Polish
) {
126 return QWidget::event(event
);
129 void DolphinSearchBox::showEvent(QShowEvent
* event
)
131 if (!event
->spontaneous()) {
133 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
136 m_searchInput
->clear();
137 m_searchInput
->setFocus();
138 m_startedSearching
= false;
142 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
144 QWidget::keyReleaseEvent(event
);
145 if ((event
->key() == Qt::Key_Escape
)) {
146 if (m_searchInput
->text().isEmpty()) {
149 m_searchInput
->clear();
154 void DolphinSearchBox::emitSearchSignal()
156 m_startSearchTimer
->stop();
157 m_startedSearching
= true;
158 emit
search(m_searchInput
->text());
161 void DolphinSearchBox::slotConfigurationChanged()
163 if (m_startedSearching
) {
168 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
170 if (text
.isEmpty()) {
171 m_startSearchTimer
->stop();
173 m_startSearchTimer
->start();
175 emit
searchTextChanged(text
);
178 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
181 emit
returnPressed(text
);
184 void DolphinSearchBox::initButton(QPushButton
* button
)
186 button
->setAutoExclusive(true);
187 button
->setFlat(true);
188 button
->setCheckable(true);
189 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
192 void DolphinSearchBox::loadSettings()
194 if (SearchSettings::location() == QLatin1String("Everywhere")) {
195 m_everywhereButton
->setChecked(true);
197 m_fromHereButton
->setChecked(true);
200 if (SearchSettings::what() == QLatin1String("Content")) {
201 m_contentButton
->setChecked(true);
203 m_fileNameButton
->setChecked(true);
207 void DolphinSearchBox::saveSettings()
209 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
210 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
211 SearchSettings::self()->writeConfig();
214 void DolphinSearchBox::init()
216 // Create close button
217 QToolButton
* closeButton
= new QToolButton(this);
218 closeButton
->setAutoRaise(true);
219 closeButton
->setIcon(KIcon("dialog-close"));
220 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
221 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
223 // Create search label
224 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
227 m_searchInput
= new KLineEdit(this);
228 m_searchInput
->setClearButtonShown(true);
229 m_searchInput
->setFont(KGlobalSettings::generalFont());
230 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
231 this, SLOT(slotReturnPressed(QString
)));
232 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
233 this, SLOT(slotSearchTextChanged(QString
)));
235 // Apply layout for the search input
236 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
237 searchInputLayout
->setMargin(0);
238 searchInputLayout
->addWidget(closeButton
);
239 searchInputLayout
->addWidget(searchLabel
);
240 searchInputLayout
->addWidget(m_searchInput
);
242 // Create "From Here" and "Everywhere"button
243 m_fromHereButton
= new QPushButton(this);
244 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
245 initButton(m_fromHereButton
);
247 m_everywhereButton
= new QPushButton(this);
248 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
249 initButton(m_everywhereButton
);
251 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
252 searchLocationGroup
->addButton(m_fromHereButton
);
253 searchLocationGroup
->addButton(m_everywhereButton
);
255 // Create "Filename" and "Content" button
256 m_fileNameButton
= new QPushButton(this);
257 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
258 initButton(m_fileNameButton
);
260 m_contentButton
= new QPushButton();
261 m_contentButton
->setText(i18nc("action:button", "Content"));
262 initButton(m_contentButton
);;
264 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
265 searchWhatGroup
->addButton(m_fileNameButton
);
266 searchWhatGroup
->addButton(m_contentButton
);
268 // Apply layout for the options
269 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
270 optionsLayout
->setMargin(0);
271 optionsLayout
->addWidget(m_fromHereButton
);
272 optionsLayout
->addWidget(m_everywhereButton
);
273 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
));
274 optionsLayout
->addWidget(m_fileNameButton
);
275 optionsLayout
->addWidget(m_contentButton
);
276 optionsLayout
->addStretch(1);
278 m_topLayout
= new QVBoxLayout(this);
279 m_topLayout
->addLayout(searchInputLayout
);
280 m_topLayout
->addLayout(optionsLayout
);
282 searchLabel
->setBuddy(m_searchInput
);
285 // The searching should be started automatically after the user did not change
286 // the text within one second
287 m_startSearchTimer
= new QTimer(this);
288 m_startSearchTimer
->setSingleShot(true);
289 m_startSearchTimer
->setInterval(1000);
290 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
293 bool DolphinSearchBox::isSearchPathIndexed() const
296 const QString path
= m_searchPath
.path();
298 const KConfig
strigiConfig("nepomukstrigirc");
299 const QStringList indexedFolders
= strigiConfig
.group("General").readPathEntry("folders", QStringList());
301 // Check whether the current search path is part of an indexed folder
302 bool isIndexed
= false;
303 foreach (const QString
& indexedFolder
, indexedFolders
) {
304 if (path
.startsWith(indexedFolder
)) {
311 // The current search path is part of an indexed folder. Check whether no
312 // excluded folder is part of the search path.
313 const QStringList excludedFolders
= strigiConfig
.group("General").readPathEntry("exclude folders", QStringList());
314 foreach (const QString
& excludedFolder
, excludedFolders
) {
315 if (path
.startsWith(excludedFolder
)) {
328 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
331 Nepomuk::Query::AndTerm andTerm
;
333 // Add input from search filter
334 const QString text
= m_searchInput
->text();
335 if (!text
.isEmpty()) {
336 if (m_fileNameButton
->isChecked()) {
337 QString regex
= QRegExp::escape(text
);
338 regex
.replace("\\*", QLatin1String(".*"));
339 regex
.replace("\\?", QLatin1String("."));
340 regex
.replace("\\", "\\\\");
341 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
342 Nepomuk::Vocabulary::NFO::fileName(),
343 Nepomuk::Query::LiteralTerm(regex
),
344 Nepomuk::Query::ComparisonTerm::Regexp
));
346 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
347 if (customQuery
.isValid()) {
348 andTerm
.addSubTerm(customQuery
.term());
353 Nepomuk::Query::FileQuery fileQuery
;
354 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFiles
);
355 fileQuery
.setTerm(andTerm
);
356 if(m_fromHereButton
->isChecked())
357 fileQuery
.addIncludeFolder(m_searchPath
, false);
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"