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