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