]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
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"
23 #include "dolphinsearchinformation.h"
28 #include <kseparator.h>
30 #include <QButtonGroup>
33 #include <QFormLayout>
34 #include <QHBoxLayout>
37 #include <QPushButton>
38 #include <QScrollArea>
40 #include <QToolButton>
41 #include <QVBoxLayout>
43 #include <config-nepomuk.h>
45 #include <Nepomuk/Query/AndTerm>
46 #include <Nepomuk/Query/FileQuery>
47 #include <Nepomuk/Query/LiteralTerm>
48 #include <Nepomuk/Query/Query>
49 #include <Nepomuk/Query/QueryParser>
50 #include <Nepomuk/Query/ResourceTypeTerm>
51 #include <Nepomuk/Query/ComparisonTerm>
52 #include <Nepomuk/ResourceManager>
53 #include <Nepomuk/Vocabulary/NFO>
56 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
58 m_startedSearching(false),
59 m_nepomukActivated(false),
66 m_everywhereButton(0),
72 DolphinSearchBox::~DolphinSearchBox()
77 QString
DolphinSearchBox::text() const
79 return m_searchInput
->text();
82 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
86 QFontMetrics
metrics(m_fromHereButton
->font());
87 const int maxWidth
= metrics
.averageCharWidth() * 15;
89 QString location
= url
.fileName();
90 if (location
.isEmpty()) {
91 if (url
.isLocalFile()) {
92 location
= QLatin1String("/");
94 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
98 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
99 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
101 const bool showSearchFromButtons
= url
.isLocalFile();
102 m_separator
->setVisible(showSearchFromButtons
);
103 m_fromHereButton
->setVisible(showSearchFromButtons
);
104 m_everywhereButton
->setVisible(showSearchFromButtons
);
107 KUrl
DolphinSearchBox::searchPath() const
112 KUrl
DolphinSearchBox::urlForSearching() const
115 const DolphinSearchInformation
& searchInfo
= DolphinSearchInformation::instance();
116 if (searchInfo
.isIndexingEnabled() && searchInfo
.isPathIndexed(url
)) {
117 url
= nepomukUrlForSearching();
119 url
.setProtocol("filenamesearch");
120 url
.addQueryItem("search", m_searchInput
->text());
121 if (m_contentButton
->isChecked()) {
122 url
.addQueryItem("checkContent", "yes");
126 if (m_everywhereButton
->isChecked()) {
127 // It is very unlikely, that the majority of Dolphins target users
128 // mean "the whole harddisk" instead of "my home folder" when
129 // selecting the "Everywhere" button.
130 encodedUrl
= QDir::homePath();
132 encodedUrl
= m_searchPath
.url();
134 url
.addQueryItem("url", encodedUrl
);
140 void DolphinSearchBox::selectAll()
142 m_searchInput
->selectAll();
145 bool DolphinSearchBox::event(QEvent
* event
)
147 if (event
->type() == QEvent::Polish
) {
150 return QWidget::event(event
);
153 void DolphinSearchBox::showEvent(QShowEvent
* event
)
155 if (!event
->spontaneous()) {
157 m_nepomukActivated
= (Nepomuk::ResourceManager::instance()->init() == 0);
160 m_searchInput
->clear();
161 m_searchInput
->setFocus();
162 m_startedSearching
= false;
166 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
168 QWidget::keyReleaseEvent(event
);
169 if (event
->key() == Qt::Key_Escape
) {
170 if (m_searchInput
->text().isEmpty()) {
173 m_searchInput
->clear();
178 void DolphinSearchBox::emitSearchSignal()
180 m_startSearchTimer
->stop();
181 m_startedSearching
= true;
182 emit
search(m_searchInput
->text());
185 void DolphinSearchBox::slotConfigurationChanged()
187 if (m_startedSearching
) {
192 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
194 if (text
.isEmpty()) {
195 m_startSearchTimer
->stop();
197 m_startSearchTimer
->start();
199 emit
searchTextChanged(text
);
202 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
205 emit
returnPressed(text
);
208 void DolphinSearchBox::initButton(QPushButton
* button
)
210 button
->setAutoExclusive(true);
211 button
->setFlat(true);
212 button
->setCheckable(true);
213 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
216 void DolphinSearchBox::loadSettings()
218 if (SearchSettings::location() == QLatin1String("Everywhere")) {
219 m_everywhereButton
->setChecked(true);
221 m_fromHereButton
->setChecked(true);
224 if (SearchSettings::what() == QLatin1String("Content")) {
225 m_contentButton
->setChecked(true);
227 m_fileNameButton
->setChecked(true);
231 void DolphinSearchBox::saveSettings()
233 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
234 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
235 SearchSettings::self()->writeConfig();
238 void DolphinSearchBox::init()
240 // Create close button
241 QToolButton
* closeButton
= new QToolButton(this);
242 closeButton
->setAutoRaise(true);
243 closeButton
->setIcon(KIcon("dialog-close"));
244 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
245 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
247 // Create search label
248 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
251 m_searchInput
= new KLineEdit(this);
252 m_searchInput
->setClearButtonShown(true);
253 m_searchInput
->setFont(KGlobalSettings::generalFont());
254 setFocusProxy(m_searchInput
);
255 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
256 this, SLOT(slotReturnPressed(QString
)));
257 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
258 this, SLOT(slotSearchTextChanged(QString
)));
260 // Apply layout for the search input
261 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
262 searchInputLayout
->setMargin(0);
263 searchInputLayout
->addWidget(closeButton
);
264 searchInputLayout
->addWidget(searchLabel
);
265 searchInputLayout
->addWidget(m_searchInput
);
267 // Create "Filename" and "Content" button
268 m_fileNameButton
= new QPushButton(this);
269 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
270 initButton(m_fileNameButton
);
272 m_contentButton
= new QPushButton();
273 m_contentButton
->setText(i18nc("action:button", "Content"));
274 initButton(m_contentButton
);;
276 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
277 searchWhatGroup
->addButton(m_fileNameButton
);
278 searchWhatGroup
->addButton(m_contentButton
);
280 m_separator
= new KSeparator(Qt::Vertical
, this);
282 // Create "From Here" and "Everywhere"button
283 m_fromHereButton
= new QPushButton(this);
284 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
285 initButton(m_fromHereButton
);
287 m_everywhereButton
= new QPushButton(this);
288 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
289 initButton(m_everywhereButton
);
291 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
292 searchLocationGroup
->addButton(m_fromHereButton
);
293 searchLocationGroup
->addButton(m_everywhereButton
);
295 // Apply layout for the options
296 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
297 optionsLayout
->setMargin(0);
298 optionsLayout
->addWidget(m_fileNameButton
);
299 optionsLayout
->addWidget(m_contentButton
);
300 optionsLayout
->addWidget(m_separator
);
301 optionsLayout
->addWidget(m_fromHereButton
);
302 optionsLayout
->addWidget(m_everywhereButton
);
303 optionsLayout
->addStretch(1);
305 // Put the options into a QScrollArea. This prevents increasing the view width
306 // in case that not enough width for the options is available.
307 QWidget
* optionsContainer
= new QWidget(this);
308 optionsContainer
->setLayout(optionsLayout
);
309 QScrollArea
* optionsScrollArea
= new QScrollArea(this);
310 optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
311 optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
312 optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
313 optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
314 optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
315 optionsScrollArea
->setWidget(optionsContainer
);
316 optionsScrollArea
->setWidgetResizable(true);
318 m_topLayout
= new QVBoxLayout(this);
319 m_topLayout
->addLayout(searchInputLayout
);
320 m_topLayout
->addWidget(optionsScrollArea
);
322 searchLabel
->setBuddy(m_searchInput
);
325 // The searching should be started automatically after the user did not change
326 // the text within one second
327 m_startSearchTimer
= new QTimer(this);
328 m_startSearchTimer
->setSingleShot(true);
329 m_startSearchTimer
->setInterval(1000);
330 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
333 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
336 Nepomuk::Query::AndTerm andTerm
;
338 // Add input from search filter
339 const QString text
= m_searchInput
->text();
340 if (!text
.isEmpty()) {
341 if (m_fileNameButton
->isChecked()) {
342 QString regex
= QRegExp::escape(text
);
343 regex
.replace("\\*", QLatin1String(".*"));
344 regex
.replace("\\?", QLatin1String("."));
345 regex
.replace("\\", "\\\\");
346 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
347 Nepomuk::Vocabulary::NFO::fileName(),
348 Nepomuk::Query::LiteralTerm(regex
),
349 Nepomuk::Query::ComparisonTerm::Regexp
));
351 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
352 if (customQuery
.isValid()) {
353 andTerm
.addSubTerm(customQuery
.term());
358 Nepomuk::Query::FileQuery fileQuery
;
359 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders
);
360 fileQuery
.setTerm(andTerm
);
361 if (m_fromHereButton
->isChecked()) {
362 const bool recursive
= true;
363 fileQuery
.addIncludeFolder(m_searchPath
, recursive
);
366 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
367 "Query Results from '%1'",
374 #include "dolphinsearchbox.moc"