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