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