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