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