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