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