]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/metadataconfigurationdialog.cpp
Fixes double inserting of terms, interestingly this problem did not existed before...
[dolphin.git] / src / panels / information / metadataconfigurationdialog.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 "metadataconfigurationdialog.h"
21
22 #include "metadatawidget.h"
23
24 #include <klocale.h>
25
26 #include <config-nepomuk.h>
27 #ifdef HAVE_NEPOMUK
28 #define DISABLE_NEPOMUK_LEGACY
29 #include <Nepomuk/Resource>
30 #include <Nepomuk/ResourceManager>
31 #include <Nepomuk/Types/Property>
32 #include <Nepomuk/Variant>
33 #endif
34
35 #include <QLabel>
36 #include <QListWidget>
37 #include <QVBoxLayout>
38
39 class MetaDataConfigurationDialog::Private
40 {
41 public:
42 Private(MetaDataConfigurationDialog* parent, MetaDataWidget* metaDataWidget);
43 ~Private();
44
45 void init();
46 void loadMetaData();
47 QString tunedLabel(const QString& label) const;
48
49 int m_hiddenData;
50 MetaDataWidget* m_metaDataWidget;
51 QListWidget* m_metaDataList;
52
53 private:
54 MetaDataConfigurationDialog* const q;
55 };
56
57 MetaDataConfigurationDialog::Private::Private(MetaDataConfigurationDialog* parent,
58 MetaDataWidget* metaDataWidget) :
59 q(parent)
60 {
61 m_hiddenData = 0;
62 m_metaDataWidget = metaDataWidget;
63
64 q->setCaption(i18nc("@title:window", "Configure Shown Data"));
65 q->setButtons(KDialog::Ok | KDialog::Cancel);
66 q->setDefaultButton(KDialog::Ok);
67
68 QWidget* mainWidget = new QWidget(q);
69 QVBoxLayout* topLayout = new QVBoxLayout(mainWidget);
70
71 QLabel* label = new QLabel(i18nc("@label:textbox",
72 "Configure which data should "
73 "be shown."), q);
74
75 m_metaDataList = new QListWidget(q);
76 m_metaDataList->setSelectionMode(QAbstractItemView::NoSelection);
77
78 topLayout->addWidget(label);
79 topLayout->addWidget(m_metaDataList);
80
81 q->setMainWidget(mainWidget);
82
83 loadMetaData();
84
85 const KConfigGroup dialogConfig(KGlobal::config(), "Nepomuk MetaDataConfigurationDialog");
86 q->restoreDialogSize(dialogConfig);
87 }
88
89 MetaDataConfigurationDialog::Private::~Private()
90 {
91 KConfigGroup dialogConfig(KGlobal::config(), "Nepomuk MetaDataConfigurationDialog");
92 q->saveDialogSize(dialogConfig, KConfigBase::Persistent);
93 }
94
95 void MetaDataConfigurationDialog::Private::loadMetaData()
96 {
97 KConfig config("kmetainformationrc", KConfig::NoGlobals);
98 KConfigGroup settings = config.group("Show");
99
100 // Add fixed meta data items where the visibility does not
101 // depend on the currently used URL.
102 int hiddenData = 0;
103 if (m_metaDataWidget != 0) {
104 hiddenData = m_metaDataWidget->hiddenData();
105 }
106
107 typedef QPair<QString, QString> FixedItem;
108 QList<FixedItem> fixedItems;
109 if (!(hiddenData & MetaDataWidget::TypeData)) {
110 fixedItems.append(FixedItem("type", i18nc("@item::inlistbox", "Type")));
111 }
112 if (!(hiddenData & MetaDataWidget::SizeData)) {
113 fixedItems.append(FixedItem("size", i18nc("@item::inlistbox", "Size")));
114 }
115 if (!(hiddenData & MetaDataWidget::ModifiedData)) {
116 fixedItems.append(FixedItem("modified", i18nc("@item::inlistbox", "Modified")));
117 }
118 if (!(hiddenData & MetaDataWidget::OwnerData)) {
119 fixedItems.append(FixedItem("owner", i18nc("@item::inlistbox", "Owner")));
120 }
121 if (!(hiddenData & MetaDataWidget::PermissionsData)) {
122 fixedItems.append(FixedItem("permissions", i18nc("@item::inlistbox", "Permissions")));
123 }
124 #ifdef HAVE_NEPOMUK
125 if (Nepomuk::ResourceManager::instance()->init() == 0) {
126 if (!(hiddenData & MetaDataWidget::RatingData)) {
127 fixedItems.append(FixedItem("rating", i18nc("@item::inlistbox", "Rating")));
128 }
129 if (!(hiddenData & MetaDataWidget::TagsData)) {
130 fixedItems.append(FixedItem("tags", i18nc("@item::inlistbox", "Tags")));
131 }
132 if (!(hiddenData & MetaDataWidget::CommentData)) {
133 fixedItems.append(FixedItem("comment", i18nc("@item::inlistbox", "Comment")));
134 }
135 }
136 #endif
137
138 foreach (const FixedItem& fixedItem, fixedItems) {
139 const QString key = fixedItem.first;
140 const QString label = fixedItem.second;
141 QListWidgetItem* item = new QListWidgetItem(label, m_metaDataList);
142 item->setData(Qt::UserRole, key);
143 const bool show = settings.readEntry(key, true);
144 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
145 }
146
147 #ifdef HAVE_NEPOMUK
148 if (Nepomuk::ResourceManager::instance()->init() != 0) {
149 return;
150 }
151
152 // Get all meta information labels that are available for
153 // the currently shown file item and add them to the list.
154 if (m_metaDataWidget == 0) {
155 // TODO: in this case all available meta data from the system
156 // should be added.
157 return;
158 }
159
160 const KFileItemList items = m_metaDataWidget->items();
161 if (items.count() != 1) {
162 // TODO: handle als usecases for more than one item:
163 return;
164 }
165 Nepomuk::Resource res(items.first().nepomukUri());
166
167 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
168 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
169 while (it != properties.constEnd()) {
170 Nepomuk::Types::Property prop(it.key());
171 const QString key = prop.name();
172
173 // Meta information provided by Nepomuk that is already
174 // available from KFileItem as "fixed item" (see above)
175 // should not be shown as second entry.
176 static const char* hiddenProperties[] = {
177 "contentSize", // = fixed item "size"
178 "fileExtension", // ~ fixed item "type"
179 "hasTag", // = fixed item "tags"
180 "name", // not shown as part of the meta data widget
181 "lastModified", // = fixed item "modified"
182 "size", // = fixed item "size"
183 "mimeType", // = fixed item "type"
184 "numericRating", // = fixed item "rating"
185 0 // mandatory last entry
186 };
187 bool skip = false;
188 int i = 0;
189 while (hiddenProperties[i] != 0) {
190 if (key == hiddenProperties[i]) {
191 skip = true;
192 break;
193 }
194 ++i;
195 }
196
197 if (!skip) {
198 // TODO #1: use Nepomuk::formatValue(res, prop) if available
199 // instead of it.value().toString()
200 // TODO #2: using tunedLabel() is a workaround for KDE 4.3 (4.4?) until
201 // we get translated labels
202 const QString label = tunedLabel(prop.label());
203 QListWidgetItem* item = new QListWidgetItem(label, m_metaDataList);
204 item->setData(Qt::UserRole, key);
205 const bool show = settings.readEntry(key, true);
206 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
207 }
208
209 ++it;
210 }
211 #endif
212 }
213
214 QString MetaDataConfigurationDialog::Private::tunedLabel(const QString& label) const
215 {
216 QString tunedLabel;
217 const int labelLength = label.length();
218 if (labelLength > 0) {
219 tunedLabel.reserve(labelLength);
220 tunedLabel = label[0].toUpper();
221 for (int i = 1; i < labelLength; ++i) {
222 if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
223 tunedLabel += ' ';
224 tunedLabel += label[i].toLower();
225 } else {
226 tunedLabel += label[i];
227 }
228 }
229 }
230 return tunedLabel;
231 }
232
233 MetaDataConfigurationDialog::MetaDataConfigurationDialog(QWidget* parent,
234 Qt::WFlags flags) :
235 KDialog(parent, flags),
236 d(new Private(this, 0))
237 {
238 }
239
240 MetaDataConfigurationDialog::MetaDataConfigurationDialog(MetaDataWidget* metaDataWidget,
241 QWidget* parent,
242 Qt::WFlags flags) :
243 KDialog(parent, flags),
244 d(new Private(this, metaDataWidget))
245 {
246 }
247
248 MetaDataConfigurationDialog::~MetaDataConfigurationDialog()
249 {
250 }
251
252 void MetaDataConfigurationDialog::slotButtonClicked(int button)
253 {
254 if (button == KDialog::Ok) {
255 KConfig config("kmetainformationrc", KConfig::NoGlobals);
256 KConfigGroup showGroup = config.group("Show");
257
258 const int count = d->m_metaDataList->count();
259 for (int i = 0; i < count; ++i) {
260 QListWidgetItem* item = d->m_metaDataList->item(i);
261 const bool show = (item->checkState() == Qt::Checked);
262 const QString key = item->data(Qt::UserRole).toString();
263 showGroup.writeEntry(key, show);
264 }
265
266 showGroup.sync();
267
268 if (d->m_metaDataWidget != 0) {
269 // trigger an update
270 d->m_metaDataWidget->setHiddenData(d->m_metaDataWidget->hiddenData());
271 }
272 accept();
273 } else {
274 KDialog::slotButtonClicked(button);
275 }
276 }
277
278 #include "metadataconfigurationdialog.moc"