]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
- Automatically show the filter-panel when a searching is done
[dolphin.git] / src / search / dolphinsearchbox.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 * **************************************************************************/
19
20 #include "dolphinsearchbox.h"
21
22 #include "dolphin_searchsettings.h"
23
24 #include <kicon.h>
25 #include <klineedit.h>
26 #include <klocale.h>
27 #include <kseparator.h>
28
29 #include <QButtonGroup>
30 #include <QDir>
31 #include <QEvent>
32 #include <QFormLayout>
33 #include <QHBoxLayout>
34 #include <QKeyEvent>
35 #include <QLabel>
36 #include <QPushButton>
37 #include <QTimer>
38 #include <QToolButton>
39 #include <QVBoxLayout>
40
41 #include <config-nepomuk.h>
42 #ifdef HAVE_NEPOMUK
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 "nfo.h"
52 #endif
53
54 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
55 QWidget(parent),
56 m_startedSearching(false),
57 m_nepomukActivated(false),
58 m_topLayout(0),
59 m_searchInput(0),
60 m_fromHereButton(0),
61 m_everywhereButton(0),
62 m_fileNameButton(0),
63 m_contentButton(0),
64 m_filterButton(0),
65 m_searchPath(),
66 m_startSearchTimer(0)
67 {
68 }
69
70 DolphinSearchBox::~DolphinSearchBox()
71 {
72 saveSettings();
73 }
74
75 QString DolphinSearchBox::text() const
76 {
77 return m_searchInput->text();
78 }
79
80 void DolphinSearchBox::setSearchPath(const KUrl& url)
81 {
82 m_searchPath = url;
83 m_filterButton->setVisible(isSearchPathIndexed());
84 }
85
86 KUrl DolphinSearchBox::searchPath() const
87 {
88 return m_searchPath;
89 }
90
91 KUrl DolphinSearchBox::urlForSearching() const
92 {
93 KUrl url;
94 if (isSearchPathIndexed()) {
95 url = nepomukUrlForSearching();
96 } else {
97 url = m_searchPath;
98 url.setProtocol("filenamesearch");
99 url.addQueryItem("search", m_searchInput->text());
100 if (m_contentButton->isChecked()) {
101 url.addQueryItem("checkContent", "yes");
102 }
103 if (m_everywhereButton->isChecked()) {
104 // It is very unlikely, that the majority of Dolphins target users
105 // mean "the whole harddisk" instead of "my home folder" when
106 // selecting the "Everywhere" button.
107 url.setPath(QDir::homePath());
108 }
109 }
110
111 return url;
112 }
113
114 bool DolphinSearchBox::event(QEvent* event)
115 {
116 if (event->type() == QEvent::Polish) {
117 init();
118 } else if (event->type() == QEvent::KeyPress) {
119 if (static_cast<QKeyEvent* >(event)->key() == Qt::Key_Escape) {
120 m_searchInput->clear();
121 }
122 }
123 return QWidget::event(event);
124 }
125
126 void DolphinSearchBox::showEvent(QShowEvent* event)
127 {
128 if (!event->spontaneous()) {
129 #ifdef HAVE_NEPOMUK
130 m_nepomukActivated = (Nepomuk::ResourceManager::instance()->init() == 0);
131 #endif
132
133 m_searchInput->clear();
134 m_searchInput->setFocus();
135 m_startedSearching = false;
136 }
137 }
138
139 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
140 {
141 QWidget::keyReleaseEvent(event);
142 if ((event->key() == Qt::Key_Escape)) {
143 if (m_searchInput->text().isEmpty()) {
144 emit closeRequest();
145 } else {
146 m_searchInput->clear();
147 }
148 }
149 }
150
151 void DolphinSearchBox::emitSearchSignal()
152 {
153 m_startSearchTimer->stop();
154 m_startedSearching = true;
155 emit search(m_searchInput->text());
156 }
157
158 void DolphinSearchBox::slotConfigurationChanged()
159 {
160 if (m_startedSearching) {
161 emitSearchSignal();
162 }
163 }
164
165 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
166 {
167 if (text.isEmpty()) {
168 m_startSearchTimer->stop();
169 } else {
170 m_startSearchTimer->start();
171 }
172 emit searchTextChanged(text);
173 }
174
175 void DolphinSearchBox::slotReturnPressed(const QString& text)
176 {
177 emitSearchSignal();
178 emit returnPressed(text);
179 }
180
181 void DolphinSearchBox::initButton(QPushButton* button)
182 {
183 button->setAutoExclusive(true);
184 button->setFlat(true);
185 button->setCheckable(true);
186 connect(button, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
187 }
188
189 void DolphinSearchBox::loadSettings()
190 {
191 if (SearchSettings::location() == QLatin1String("Everywhere")) {
192 m_everywhereButton->setChecked(true);
193 } else {
194 m_fromHereButton->setChecked(true);
195 }
196
197 if (SearchSettings::what() == QLatin1String("Content")) {
198 m_contentButton->setChecked(true);
199 } else {
200 m_fileNameButton->setChecked(true);
201 }
202 }
203
204 void DolphinSearchBox::saveSettings()
205 {
206 SearchSettings::setLocation(m_fromHereButton->isChecked() ? "FromHere" : "Everywhere");
207 SearchSettings::setWhat(m_fileNameButton->isChecked() ? "FileName" : "Content");
208 SearchSettings::self()->writeConfig();
209 }
210
211 void DolphinSearchBox::init()
212 {
213 // Create close button
214 QToolButton* closeButton = new QToolButton(this);
215 closeButton->setAutoRaise(true);
216 closeButton->setIcon(KIcon("dialog-close"));
217 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
218 connect(closeButton, SIGNAL(clicked()), SIGNAL(closeRequest()));
219
220 // Create search label
221 QLabel* searchLabel = new QLabel(i18nc("@label:textbox", "Find:"), this);
222
223 // Create search box
224 m_searchInput = new KLineEdit(this);
225 m_searchInput->setClearButtonShown(true);
226 m_searchInput->setFont(KGlobalSettings::generalFont());
227 connect(m_searchInput, SIGNAL(returnPressed(QString)),
228 this, SLOT(slotReturnPressed(QString)));
229 connect(m_searchInput, SIGNAL(textChanged(QString)),
230 this, SLOT(slotSearchTextChanged(QString)));
231
232 // Apply layout for the search input
233 QHBoxLayout* searchInputLayout = new QHBoxLayout();
234 searchInputLayout->setMargin(0);
235 searchInputLayout->addWidget(closeButton);
236 searchInputLayout->addWidget(searchLabel);
237 searchInputLayout->addWidget(m_searchInput);
238
239 // Create "From Here" and "Everywhere"button
240 m_fromHereButton = new QPushButton();
241 m_fromHereButton->setText(i18nc("action:button", "From Here"));
242 initButton(m_fromHereButton);
243
244 m_everywhereButton = new QPushButton(this);
245 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
246 initButton(m_everywhereButton);
247
248 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
249 searchLocationGroup->addButton(m_fromHereButton);
250 searchLocationGroup->addButton(m_everywhereButton);
251
252 // Create "Filename" and "Content" button
253 m_fileNameButton = new QPushButton(this);
254 m_fileNameButton->setText(i18nc("action:button", "Filename"));
255 initButton(m_fileNameButton);
256
257 m_contentButton = new QPushButton();
258 m_contentButton->setText(i18nc("action:button", "Content"));
259 initButton(m_contentButton);;
260
261 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
262 searchWhatGroup->addButton(m_fileNameButton);
263 searchWhatGroup->addButton(m_contentButton);
264
265 // Create "Filter" button
266 m_filterButton = new QToolButton(this);
267 m_filterButton->setIcon(KIcon("view-filter"));
268 m_filterButton->setAutoRaise(true);
269 m_filterButton->setCheckable(true);
270 m_filterButton->hide();
271 //connect(m_filterButton, SIGNAL(toggled(bool)), this, SLOT(setFilterWidgetsVisible(bool)));
272
273 // Apply layout for the options
274 QHBoxLayout* optionsLayout = new QHBoxLayout();
275 optionsLayout->setMargin(0);
276 optionsLayout->addWidget(m_fromHereButton);
277 optionsLayout->addWidget(m_everywhereButton);
278 optionsLayout->addWidget(new KSeparator(Qt::Vertical));
279 optionsLayout->addWidget(m_fileNameButton);
280 optionsLayout->addWidget(m_contentButton);
281 optionsLayout->addStretch(1);
282 optionsLayout->addWidget(m_filterButton);
283
284 m_topLayout = new QVBoxLayout(this);
285 m_topLayout->addLayout(searchInputLayout);
286 m_topLayout->addLayout(optionsLayout);
287
288 searchLabel->setBuddy(m_searchInput);
289 loadSettings();
290
291 // The searching should be started automatically after the user did not change
292 // the text within one second
293 m_startSearchTimer = new QTimer(this);
294 m_startSearchTimer->setSingleShot(true);
295 m_startSearchTimer->setInterval(1000);
296 connect(m_startSearchTimer, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
297 }
298
299 bool DolphinSearchBox::isSearchPathIndexed() const
300 {
301 #ifdef HAVE_NEPOMUK
302 const QString path = m_searchPath.path();
303
304 const KConfig strigiConfig("nepomukstrigirc");
305 const QStringList indexedFolders = strigiConfig.group("General").readPathEntry("folders", QStringList());
306
307 // Check whether the current search path is part of an indexed folder
308 bool isIndexed = false;
309 foreach (const QString& indexedFolder, indexedFolders) {
310 if (path.startsWith(indexedFolder)) {
311 isIndexed = true;
312 break;
313 }
314 }
315
316 if (isIndexed) {
317 // The current search path is part of an indexed folder. Check whether no
318 // excluded folder is part of the search path.
319 const QStringList excludedFolders = strigiConfig.group("General").readPathEntry("exclude folders", QStringList());
320 foreach (const QString& excludedFolder, excludedFolders) {
321 if (path.startsWith(excludedFolder)) {
322 isIndexed = false;
323 break;
324 }
325 }
326 }
327
328 return isIndexed;
329 #else
330 return false;
331 #endif
332 }
333
334 KUrl DolphinSearchBox::nepomukUrlForSearching() const
335 {
336 #ifdef HAVE_NEPOMUK
337 Nepomuk::Query::AndTerm andTerm;
338
339 // Add input from search filter
340 const QString text = m_searchInput->text();
341 if (!text.isEmpty()) {
342 if (m_fileNameButton->isChecked()) {
343 QString regex = QRegExp::escape(text);
344 regex.replace("\\*", QLatin1String(".*"));
345 regex.replace("\\?", QLatin1String("."));
346 regex.replace("\\", "\\\\");
347 andTerm.addSubTerm(Nepomuk::Query::ComparisonTerm(
348 Nepomuk::Vocabulary::NFO::fileName(),
349 Nepomuk::Query::LiteralTerm(regex),
350 Nepomuk::Query::ComparisonTerm::Regexp));
351 } else {
352 const Nepomuk::Query::Query customQuery = Nepomuk::Query::QueryParser::parseQuery(text, Nepomuk::Query::QueryParser::DetectFilenamePattern);
353 if (customQuery.isValid()) {
354 andTerm.addSubTerm(customQuery.term());
355 }
356 }
357 }
358
359 Nepomuk::Query::FileQuery fileQuery;
360 fileQuery.setFileMode(Nepomuk::Query::FileQuery::QueryFiles);
361 fileQuery.setTerm(andTerm);
362
363 return fileQuery.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
364 "Query Results from '%1'",
365 text));
366 #else
367 return KUrl();
368 #endif
369 }
370
371 #include "dolphinsearchbox.moc"