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