]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchoptionsconfigurator.cpp
Prepare the search criterion selector to use the new Nepomuk::Query::Term instead...
[dolphin.git] / src / search / dolphinsearchoptionsconfigurator.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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 "dolphinsearchoptionsconfigurator.h"
21
22 #include "dolphin_searchsettings.h"
23 #include "searchcriterionselector.h"
24
25 #include <nepomuk/andterm.h>
26 #include <nepomuk/query.h>
27 #include <nepomuk/term.h>
28
29 #include <kcombobox.h>
30 #include <kdialog.h>
31 #include <kicon.h>
32 #include <klineedit.h>
33 #include <klocale.h>
34 #include <kseparator.h>
35
36 #include <QButtonGroup>
37 #include <QHBoxLayout>
38 #include <QLabel>
39 #include <QPushButton>
40 #include <QShowEvent>
41 #include <QVBoxLayout>
42
43 struct SettingsItem
44 {
45 const char* settingsName;
46 const char* text;
47 };
48
49 // Contains the settings names and translated texts
50 // for each item of the location-combo-box.
51 static const SettingsItem g_locationItems[] = {
52 {"Everywhere", I18N_NOOP2("@label", "Everywhere")},
53 {"From Here", I18N_NOOP2("@label", "From Here")}
54 };
55
56 // Contains the settings names and translated texts
57 // for each item of the what-combobox.
58 static const SettingsItem g_whatItems[] = {
59 {"All", I18N_NOOP2("@label", "All")},
60 {"Images", I18N_NOOP2("@label", "Images")},
61 {"Text", I18N_NOOP2("@label", "Text")},
62 {"Filenames", I18N_NOOP2("@label", "Filenames")}
63 };
64
65 struct CriterionItem
66 {
67 const char* settingsName;
68 SearchCriterionSelector::Type type;
69 };
70
71 // Contains the settings names for type
72 // of availabe search criterion.
73 static const CriterionItem g_criterionItems[] = {
74 {"Date", SearchCriterionSelector::Date},
75 {"Size", SearchCriterionSelector::Size},
76 {"Tag", SearchCriterionSelector::Tag},
77 {"Raging", SearchCriterionSelector::Rating}
78 };
79
80 DolphinSearchOptionsConfigurator::DolphinSearchOptionsConfigurator(QWidget* parent) :
81 QWidget(parent),
82 m_initialized(false),
83 m_locationBox(0),
84 m_whatBox(0),
85 m_addSelectorButton(0),
86 m_searchButton(0),
87 m_saveButton(0),
88 m_vBoxLayout(0),
89 m_criterions(),
90 m_customSearchQuery()
91 {
92 m_vBoxLayout = new QVBoxLayout(this);
93
94 // add "search" configuration
95 QLabel* searchLabel = new QLabel(i18nc("@label", "Search:"));
96
97 m_locationBox = new KComboBox(this);
98 for (unsigned int i = 0; i < sizeof(g_locationItems) / sizeof(SettingsItem); ++i) {
99 m_locationBox->addItem(g_locationItems[i].text);
100 }
101
102 // add "what" configuration
103 QLabel* whatLabel = new QLabel(i18nc("@label", "What:"));
104
105 m_whatBox = new KComboBox(this);
106 for (unsigned int i = 0; i < sizeof(g_whatItems) / sizeof(SettingsItem); ++i) {
107 m_whatBox->addItem(g_whatItems[i].text);
108 }
109
110 // add "Add selector" button
111 m_addSelectorButton = new QPushButton(this);
112 m_addSelectorButton->setIcon(KIcon("list-add"));
113 m_addSelectorButton->setToolTip(i18nc("@info", "Add search option"));
114 m_addSelectorButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
115 connect(m_addSelectorButton, SIGNAL(clicked()), this, SLOT(slotAddSelectorButtonClicked()));
116
117 // add button "Search"
118 m_searchButton = new QPushButton(this);
119 m_searchButton->setIcon(KIcon("edit-find"));
120 m_searchButton->setText(i18nc("@action:button", "Search"));
121 m_searchButton->setToolTip(i18nc("@info", "Start searching"));
122 m_searchButton->setEnabled(false);
123 connect(m_searchButton, SIGNAL(clicked()), this, SIGNAL(searchOptionsChanged()));
124
125 // add button "Save"
126 m_saveButton = new QPushButton(this);
127 m_saveButton->setIcon(KIcon("document-save"));
128 m_saveButton->setText(i18nc("@action:button", "Save"));
129 m_saveButton->setToolTip(i18nc("@info", "Save search options"));
130 m_saveButton->setEnabled(false);
131 connect(m_saveButton, SIGNAL(clicked()), this, SLOT(saveQuery()));
132
133 // add button "Close"
134 QPushButton* closeButton = new QPushButton(this);
135 closeButton->setIcon(KIcon("dialog-close"));
136 closeButton->setText(i18nc("@action:button", "Close"));
137 closeButton->setToolTip(i18nc("@info", "Close search options"));
138 connect(closeButton, SIGNAL(clicked()), this, SLOT(hide()));
139
140 QHBoxLayout* topLineLayout = new QHBoxLayout();
141 topLineLayout->addWidget(m_addSelectorButton);
142 topLineLayout->addWidget(searchLabel);
143 topLineLayout->addWidget(m_locationBox);
144 topLineLayout->addWidget(whatLabel);
145 topLineLayout->addWidget(m_whatBox);
146 topLineLayout->addWidget(new QWidget(this), 1); // filler
147 topLineLayout->addWidget(m_searchButton);
148 topLineLayout->addWidget(m_saveButton);
149 topLineLayout->addWidget(closeButton);
150
151 m_vBoxLayout->addWidget(new KSeparator(this));
152 m_vBoxLayout->addLayout(topLineLayout);
153 m_vBoxLayout->addWidget(new KSeparator(this));
154 }
155
156 DolphinSearchOptionsConfigurator::~DolphinSearchOptionsConfigurator()
157 {
158 // store the UI configuration
159 const int locationIndex = m_locationBox->currentIndex();
160 SearchSettings::setLocation(g_locationItems[locationIndex].settingsName);
161
162 const int whatIndex = m_whatBox->currentIndex();
163 SearchSettings::setWhat(g_whatItems[whatIndex].settingsName);
164
165 QString criterionsString;
166 foreach(const SearchCriterionSelector* criterion, m_criterions) {
167 if (!criterionsString.isEmpty()) {
168 criterionsString += ',';
169 }
170 const int index = static_cast<int>(criterion->type());
171 criterionsString += g_criterionItems[index].settingsName;
172 }
173 SearchSettings::setCriterions(criterionsString);
174
175 SearchSettings::self()->writeConfig();
176 }
177
178 KUrl DolphinSearchOptionsConfigurator::nepomukUrl() const
179 {
180 Nepomuk::Query::AndTerm andTerm;
181 foreach (const SearchCriterionSelector* criterion, m_criterions) {
182 const Nepomuk::Query::Term term = criterion->queryTerm();
183 andTerm.addSubTerm(term);
184 }
185
186 // TODO: respect m_customSearchQuery
187
188 Nepomuk::Query::Query query;
189 query.setTerm(andTerm);
190 return query.toSearchUrl();
191
192 /*QString searchOptions;
193 QString searchString = m_customSearchQuery;
194 if (!searchString.isEmpty() && !searchOptions.isEmpty()) {
195 searchString += ' ' + searchOptions;
196 } else if (!searchOptions.isEmpty()) {
197 searchString += searchOptions;
198 }
199
200 searchString.insert(0, QLatin1String("nepomuksearch:/"));
201 return KUrl(searchString);*/
202 }
203
204 void DolphinSearchOptionsConfigurator::setCustomSearchQuery(const QString& searchQuery)
205 {
206 m_customSearchQuery = searchQuery.simplified();
207
208 const bool enabled = hasSearchParameters();
209 m_searchButton->setEnabled(enabled);
210 m_saveButton->setEnabled(enabled);
211 }
212
213 void DolphinSearchOptionsConfigurator::showEvent(QShowEvent* event)
214 {
215 if (!event->spontaneous() && !m_initialized) {
216 // restore the UI layout of the last session
217 const QString location = SearchSettings::location();
218 for (unsigned int i = 0; i < sizeof(g_locationItems) / sizeof(SettingsItem); ++i) {
219 if (g_locationItems[i].settingsName == location) {
220 m_locationBox->setCurrentIndex(i);
221 break;
222 }
223 }
224
225 const QString what = SearchSettings::what();
226 for (unsigned int i = 0; i < sizeof(g_whatItems) / sizeof(SettingsItem); ++i) {
227 if (g_whatItems[i].settingsName == what) {
228 m_whatBox->setCurrentIndex(i);
229 break;
230 }
231 }
232
233 const QString criterions = SearchSettings::criterions();
234 QStringList criterionsList = criterions.split(',');
235 foreach (const QString& criterionName, criterionsList) {
236 for (unsigned int i = 0; i < sizeof(g_criterionItems) / sizeof(CriterionItem); ++i) {
237 if (g_criterionItems[i].settingsName == criterionName) {
238 const SearchCriterionSelector::Type type = g_criterionItems[i].type;
239 addCriterion(new SearchCriterionSelector(type, this));
240 break;
241 }
242 }
243 }
244
245 m_initialized = true;
246 }
247 QWidget::showEvent(event);
248 }
249
250 void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked()
251 {
252 SearchCriterionSelector* selector = new SearchCriterionSelector(SearchCriterionSelector::Date, this);
253 addCriterion(selector);
254 }
255
256 void DolphinSearchOptionsConfigurator::slotCriterionChanged()
257 {
258 const bool enabled = hasSearchParameters();
259 m_searchButton->setEnabled(enabled);
260 m_saveButton->setEnabled(enabled);
261 }
262
263 void DolphinSearchOptionsConfigurator::removeCriterion()
264 {
265 SearchCriterionSelector* criterion = qobject_cast<SearchCriterionSelector*>(sender());
266 Q_ASSERT(criterion != 0);
267 m_vBoxLayout->removeWidget(criterion);
268
269 const int index = m_criterions.indexOf(criterion);
270 m_criterions.removeAt(index);
271
272 criterion->deleteLater();
273
274 updateSelectorButton();
275 }
276
277 void DolphinSearchOptionsConfigurator::updateSelectorButton()
278 {
279 const int selectors = m_vBoxLayout->count() - 1;
280 m_addSelectorButton->setEnabled(selectors < 10);
281 }
282
283 void DolphinSearchOptionsConfigurator::saveQuery()
284 {
285 KDialog dialog(0, Qt::Dialog);
286
287 QWidget* container = new QWidget(&dialog);
288
289 QLabel* label = new QLabel(i18nc("@label", "Name:"), container);
290 KLineEdit* lineEdit = new KLineEdit(container);
291 lineEdit->setMinimumWidth(250);
292
293 QHBoxLayout* layout = new QHBoxLayout(container);
294 layout->addWidget(label, Qt::AlignRight);
295 layout->addWidget(lineEdit);
296
297 dialog.setMainWidget(container);
298 dialog.setCaption(i18nc("@title:window", "Save Search Options"));
299 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
300 dialog.setDefaultButton(KDialog::Ok);
301 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Save"));
302
303 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
304 "SaveSearchOptionsDialog");
305 dialog.restoreDialogSize(dialogConfig);
306 dialog.exec(); // TODO...
307 }
308
309 void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector* criterion)
310 {
311 connect(criterion, SIGNAL(removeCriterion()), this, SLOT(removeCriterion()));
312 connect(criterion, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged()));
313
314 // insert the new selector before the KSeparator at the bottom
315 const int index = m_vBoxLayout->count() - 1;
316 m_vBoxLayout->insertWidget(index, criterion);
317 updateSelectorButton();
318
319 m_criterions.append(criterion);
320 }
321
322 bool DolphinSearchOptionsConfigurator::hasSearchParameters() const
323 {
324 if (!m_customSearchQuery.isEmpty()) {
325 // performance optimization: if a custom search query is defined,
326 // there is no need to call the (quite expensive) method nepomukUrl()
327 return true;
328 }
329 return nepomukUrl().path() != QLatin1String("/");
330 }
331
332 #include "dolphinsearchoptionsconfigurator.moc"