]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchoptionsconfigurator.cpp
adopt internal interfaces to the new Nepomuk search API
[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_criteria(),
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 criteriaString;
166 foreach(const SearchCriterionSelector* criterion, m_criteria) {
167 if (!criteriaString.isEmpty()) {
168 criteriaString += ',';
169 }
170 const int index = static_cast<int>(criterion->type());
171 criteriaString += g_criterionItems[index].settingsName;
172 }
173 SearchSettings::setCriteria(criteriaString);
174
175 SearchSettings::self()->writeConfig();
176 }
177
178 KUrl DolphinSearchOptionsConfigurator::nepomukUrl() const
179 {
180 Nepomuk::Query::Query query;
181 if (m_criteria.size() == 1) {
182 query.setTerm(m_criteria.first()->queryTerm());
183 } else {
184 Nepomuk::Query::AndTerm andTerm;
185 foreach (const SearchCriterionSelector* criterion, m_criteria) {
186 const Nepomuk::Query::Term term = criterion->queryTerm();
187 andTerm.addSubTerm(term);
188 }
189 query.setTerm(andTerm);
190 }
191
192 // TODO: respect m_customSearchQuery
193
194 return query.toSearchUrl();
195 }
196
197 void DolphinSearchOptionsConfigurator::setCustomSearchQuery(const QString& searchQuery)
198 {
199 m_customSearchQuery = searchQuery.simplified();
200
201 const bool enabled = hasSearchParameters();
202 m_searchButton->setEnabled(enabled);
203 m_saveButton->setEnabled(enabled);
204 }
205
206 void DolphinSearchOptionsConfigurator::showEvent(QShowEvent* event)
207 {
208 if (!event->spontaneous() && !m_initialized) {
209 // restore the UI layout of the last session
210 const QString location = SearchSettings::location();
211 for (unsigned int i = 0; i < sizeof(g_locationItems) / sizeof(SettingsItem); ++i) {
212 if (g_locationItems[i].settingsName == location) {
213 m_locationBox->setCurrentIndex(i);
214 break;
215 }
216 }
217
218 const QString what = SearchSettings::what();
219 for (unsigned int i = 0; i < sizeof(g_whatItems) / sizeof(SettingsItem); ++i) {
220 if (g_whatItems[i].settingsName == what) {
221 m_whatBox->setCurrentIndex(i);
222 break;
223 }
224 }
225
226 const QString criteria = SearchSettings::criteria();
227 QStringList criteriaList = criteria.split(',');
228 foreach (const QString& criterionName, criteriaList) {
229 for (unsigned int i = 0; i < sizeof(g_criterionItems) / sizeof(CriterionItem); ++i) {
230 if (g_criterionItems[i].settingsName == criterionName) {
231 const SearchCriterionSelector::Type type = g_criterionItems[i].type;
232 addCriterion(new SearchCriterionSelector(type, this));
233 break;
234 }
235 }
236 }
237
238 m_initialized = true;
239 }
240 QWidget::showEvent(event);
241 }
242
243 void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked()
244 {
245 SearchCriterionSelector* selector = new SearchCriterionSelector(SearchCriterionSelector::Date, this);
246 addCriterion(selector);
247 }
248
249 void DolphinSearchOptionsConfigurator::slotCriterionChanged()
250 {
251 const bool enabled = hasSearchParameters();
252 m_searchButton->setEnabled(enabled);
253 m_saveButton->setEnabled(enabled);
254 }
255
256 void DolphinSearchOptionsConfigurator::removeCriterion()
257 {
258 SearchCriterionSelector* criterion = qobject_cast<SearchCriterionSelector*>(sender());
259 Q_ASSERT(criterion != 0);
260 m_vBoxLayout->removeWidget(criterion);
261
262 const int index = m_criteria.indexOf(criterion);
263 m_criteria.removeAt(index);
264
265 criterion->deleteLater();
266
267 updateSelectorButton();
268 }
269
270 void DolphinSearchOptionsConfigurator::updateSelectorButton()
271 {
272 const int selectors = m_vBoxLayout->count() - 1;
273 m_addSelectorButton->setEnabled(selectors < 10);
274 }
275
276 void DolphinSearchOptionsConfigurator::saveQuery()
277 {
278 KDialog dialog(0, Qt::Dialog);
279
280 QWidget* container = new QWidget(&dialog);
281
282 QLabel* label = new QLabel(i18nc("@label", "Name:"), container);
283 KLineEdit* lineEdit = new KLineEdit(container);
284 lineEdit->setMinimumWidth(250);
285
286 QHBoxLayout* layout = new QHBoxLayout(container);
287 layout->addWidget(label, Qt::AlignRight);
288 layout->addWidget(lineEdit);
289
290 dialog.setMainWidget(container);
291 dialog.setCaption(i18nc("@title:window", "Save Search Options"));
292 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
293 dialog.setDefaultButton(KDialog::Ok);
294 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Save"));
295
296 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
297 "SaveSearchOptionsDialog");
298 dialog.restoreDialogSize(dialogConfig);
299 dialog.exec(); // TODO...
300 }
301
302 void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector* criterion)
303 {
304 connect(criterion, SIGNAL(removeCriterion()), this, SLOT(removeCriterion()));
305 connect(criterion, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged()));
306
307 // insert the new selector before the KSeparator at the bottom
308 const int index = m_vBoxLayout->count() - 1;
309 m_vBoxLayout->insertWidget(index, criterion);
310 updateSelectorButton();
311
312 m_criteria.append(criterion);
313 }
314
315 bool DolphinSearchOptionsConfigurator::hasSearchParameters() const
316 {
317 if (!m_customSearchQuery.isEmpty()) {
318 // performance optimization: if a custom search query is defined,
319 // there is no need to call the (quite expensive) method nepomukUrl()
320 return true;
321 }
322 return true; //nepomukUrl().path() != QLatin1String("/");
323 }
324
325 #include "dolphinsearchoptionsconfigurator.moc"