]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
SVN_SILENT: Coding style updates
[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 <nepomuk/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_fileNameButton(0),
61 m_contentButton(0),
62 m_separator(0),
63 m_fromHereButton(0),
64 m_everywhereButton(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
84 QFontMetrics metrics(m_fromHereButton->font());
85 const int maxWidth = metrics.averageCharWidth() * 15;
86
87 QString location = url.fileName();
88 if (location.isEmpty()) {
89 if (url.isLocalFile()) {
90 location = QLatin1String("/");
91 } else {
92 location = url.protocol() + QLatin1String(" - ") + url.host();
93 }
94 }
95
96 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
97 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
98
99 const bool showSearchFromButtons = url.isLocalFile();
100 m_separator->setVisible(showSearchFromButtons);
101 m_fromHereButton->setVisible(showSearchFromButtons);
102 m_everywhereButton->setVisible(showSearchFromButtons);
103 }
104
105 KUrl DolphinSearchBox::searchPath() const
106 {
107 return m_searchPath;
108 }
109
110 KUrl DolphinSearchBox::urlForSearching() const
111 {
112 KUrl url;
113 if (m_nepomukActivated && isSearchPathIndexed()) {
114 url = nepomukUrlForSearching();
115 } else {
116 url.setProtocol("filenamesearch");
117 url.addQueryItem("search", m_searchInput->text());
118 if (m_contentButton->isChecked()) {
119 url.addQueryItem("checkContent", "yes");
120 }
121
122 QString encodedUrl;
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();
128 } else {
129 encodedUrl = m_searchPath.url();
130 }
131 url.addQueryItem("url", encodedUrl);
132 }
133
134 return url;
135 }
136
137 void DolphinSearchBox::selectAll()
138 {
139 m_searchInput->selectAll();
140 }
141
142 bool DolphinSearchBox::event(QEvent* event)
143 {
144 if (event->type() == QEvent::Polish) {
145 init();
146 }
147 return QWidget::event(event);
148 }
149
150 void DolphinSearchBox::showEvent(QShowEvent* event)
151 {
152 if (!event->spontaneous()) {
153 #ifdef HAVE_NEPOMUK
154 m_nepomukActivated = (Nepomuk::ResourceManager::instance()->init() == 0);
155 #endif
156
157 m_searchInput->clear();
158 m_searchInput->setFocus();
159 m_startedSearching = false;
160 }
161 }
162
163 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
164 {
165 QWidget::keyReleaseEvent(event);
166 if (event->key() == Qt::Key_Escape) {
167 if (m_searchInput->text().isEmpty()) {
168 emit closeRequest();
169 } else {
170 m_searchInput->clear();
171 }
172 }
173 }
174
175 void DolphinSearchBox::emitSearchSignal()
176 {
177 m_startSearchTimer->stop();
178 m_startedSearching = true;
179 emit search(m_searchInput->text());
180 }
181
182 void DolphinSearchBox::slotConfigurationChanged()
183 {
184 if (m_startedSearching) {
185 emitSearchSignal();
186 }
187 }
188
189 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
190 {
191 if (text.isEmpty()) {
192 m_startSearchTimer->stop();
193 } else {
194 m_startSearchTimer->start();
195 }
196 emit searchTextChanged(text);
197 }
198
199 void DolphinSearchBox::slotReturnPressed(const QString& text)
200 {
201 emitSearchSignal();
202 emit returnPressed(text);
203 }
204
205 void DolphinSearchBox::initButton(QPushButton* button)
206 {
207 button->setAutoExclusive(true);
208 button->setFlat(true);
209 button->setCheckable(true);
210 connect(button, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
211 }
212
213 void DolphinSearchBox::loadSettings()
214 {
215 if (SearchSettings::location() == QLatin1String("Everywhere")) {
216 m_everywhereButton->setChecked(true);
217 } else {
218 m_fromHereButton->setChecked(true);
219 }
220
221 if (SearchSettings::what() == QLatin1String("Content")) {
222 m_contentButton->setChecked(true);
223 } else {
224 m_fileNameButton->setChecked(true);
225 }
226 }
227
228 void DolphinSearchBox::saveSettings()
229 {
230 SearchSettings::setLocation(m_fromHereButton->isChecked() ? "FromHere" : "Everywhere");
231 SearchSettings::setWhat(m_fileNameButton->isChecked() ? "FileName" : "Content");
232 SearchSettings::self()->writeConfig();
233 }
234
235 void DolphinSearchBox::init()
236 {
237 // Create close button
238 QToolButton* closeButton = new QToolButton(this);
239 closeButton->setAutoRaise(true);
240 closeButton->setIcon(KIcon("dialog-close"));
241 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
242 connect(closeButton, SIGNAL(clicked()), SIGNAL(closeRequest()));
243
244 // Create search label
245 QLabel* searchLabel = new QLabel(i18nc("@label:textbox", "Find:"), this);
246
247 // Create search box
248 m_searchInput = new KLineEdit(this);
249 m_searchInput->setClearButtonShown(true);
250 m_searchInput->setFont(KGlobalSettings::generalFont());
251 setFocusProxy(m_searchInput);
252 connect(m_searchInput, SIGNAL(returnPressed(QString)),
253 this, SLOT(slotReturnPressed(QString)));
254 connect(m_searchInput, SIGNAL(textChanged(QString)),
255 this, SLOT(slotSearchTextChanged(QString)));
256
257 // Apply layout for the search input
258 QHBoxLayout* searchInputLayout = new QHBoxLayout();
259 searchInputLayout->setMargin(0);
260 searchInputLayout->addWidget(closeButton);
261 searchInputLayout->addWidget(searchLabel);
262 searchInputLayout->addWidget(m_searchInput);
263
264 // Create "Filename" and "Content" button
265 m_fileNameButton = new QPushButton(this);
266 m_fileNameButton->setText(i18nc("action:button", "Filename"));
267 initButton(m_fileNameButton);
268
269 m_contentButton = new QPushButton();
270 m_contentButton->setText(i18nc("action:button", "Content"));
271 initButton(m_contentButton);;
272
273 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
274 searchWhatGroup->addButton(m_fileNameButton);
275 searchWhatGroup->addButton(m_contentButton);
276
277 m_separator = new KSeparator(Qt::Vertical, this);
278
279 // Create "From Here" and "Everywhere"button
280 m_fromHereButton = new QPushButton(this);
281 m_fromHereButton->setText(i18nc("action:button", "From Here"));
282 initButton(m_fromHereButton);
283
284 m_everywhereButton = new QPushButton(this);
285 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
286 initButton(m_everywhereButton);
287
288 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
289 searchLocationGroup->addButton(m_fromHereButton);
290 searchLocationGroup->addButton(m_everywhereButton);
291
292 // Apply layout for the options
293 QHBoxLayout* optionsLayout = new QHBoxLayout();
294 optionsLayout->setMargin(0);
295 optionsLayout->addWidget(m_fileNameButton);
296 optionsLayout->addWidget(m_contentButton);
297 optionsLayout->addWidget(m_separator);
298 optionsLayout->addWidget(m_fromHereButton);
299 optionsLayout->addWidget(m_everywhereButton);
300 optionsLayout->addStretch(1);
301
302 m_topLayout = new QVBoxLayout(this);
303 m_topLayout->addLayout(searchInputLayout);
304 m_topLayout->addLayout(optionsLayout);
305
306 searchLabel->setBuddy(m_searchInput);
307 loadSettings();
308
309 // The searching should be started automatically after the user did not change
310 // the text within one second
311 m_startSearchTimer = new QTimer(this);
312 m_startSearchTimer->setSingleShot(true);
313 m_startSearchTimer->setInterval(1000);
314 connect(m_startSearchTimer, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
315 }
316
317 bool DolphinSearchBox::isSearchPathIndexed() const
318 {
319 #ifdef HAVE_NEPOMUK
320 const QString path = m_searchPath.path();
321
322 const KConfig strigiConfig("nepomukstrigirc");
323 const QStringList indexedFolders = strigiConfig.group("General").readPathEntry("folders", QStringList());
324
325 // Check whether the current search path is part of an indexed folder
326 bool isIndexed = false;
327 foreach (const QString& indexedFolder, indexedFolders) {
328 if (path.startsWith(indexedFolder)) {
329 isIndexed = true;
330 break;
331 }
332 }
333
334 if (isIndexed) {
335 // The current search path is part of an indexed folder. Check whether no
336 // excluded folder is part of the search path.
337 const QStringList excludedFolders = strigiConfig.group("General").readPathEntry("exclude folders", QStringList());
338 foreach (const QString& excludedFolder, excludedFolders) {
339 if (path.startsWith(excludedFolder)) {
340 isIndexed = false;
341 break;
342 }
343 }
344 }
345
346 return isIndexed;
347 #else
348 return false;
349 #endif
350 }
351
352 KUrl DolphinSearchBox::nepomukUrlForSearching() const
353 {
354 #ifdef HAVE_NEPOMUK
355 Nepomuk::Query::AndTerm andTerm;
356
357 // Add input from search filter
358 const QString text = m_searchInput->text();
359 if (!text.isEmpty()) {
360 if (m_fileNameButton->isChecked()) {
361 QString regex = QRegExp::escape(text);
362 regex.replace("\\*", QLatin1String(".*"));
363 regex.replace("\\?", QLatin1String("."));
364 regex.replace("\\", "\\\\");
365 andTerm.addSubTerm(Nepomuk::Query::ComparisonTerm(
366 Nepomuk::Vocabulary::NFO::fileName(),
367 Nepomuk::Query::LiteralTerm(regex),
368 Nepomuk::Query::ComparisonTerm::Regexp));
369 } else {
370 const Nepomuk::Query::Query customQuery = Nepomuk::Query::QueryParser::parseQuery(text, Nepomuk::Query::QueryParser::DetectFilenamePattern);
371 if (customQuery.isValid()) {
372 andTerm.addSubTerm(customQuery.term());
373 }
374 }
375 }
376
377 Nepomuk::Query::FileQuery fileQuery;
378 fileQuery.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders);
379 fileQuery.setTerm(andTerm);
380 if (m_fromHereButton->isChecked()) {
381 const bool recursive = true;
382 fileQuery.addIncludeFolder(m_searchPath, recursive);
383 }
384
385 return fileQuery.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
386 "Query Results from '%1'",
387 text));
388 #else
389 return KUrl();
390 #endif
391 }
392
393 #include "dolphinsearchbox.moc"