1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "metadataconfigurationdialog.h"
22 #include "metadatawidget.h"
26 #include <config-nepomuk.h>
28 #define DISABLE_NEPOMUK_LEGACY
29 #include <Nepomuk/Resource>
30 #include <Nepomuk/ResourceManager>
31 #include <Nepomuk/Types/Property>
32 #include <Nepomuk/Variant>
36 #include <QListWidget>
37 #include <QVBoxLayout>
39 class MetaDataConfigurationDialog::Private
42 Private(MetaDataConfigurationDialog
* parent
, MetaDataWidget
* metaDataWidget
);
47 QString
tunedLabel(const QString
& label
) const;
50 MetaDataWidget
* m_metaDataWidget
;
51 QListWidget
* m_metaDataList
;
54 MetaDataConfigurationDialog
* const q
;
57 MetaDataConfigurationDialog::Private::Private(MetaDataConfigurationDialog
* parent
,
58 MetaDataWidget
* metaDataWidget
) :
62 m_metaDataWidget
= metaDataWidget
;
64 q
->setCaption(i18nc("@title:window", "Configure Shown Data"));
65 q
->setButtons(KDialog::Ok
| KDialog::Cancel
);
66 q
->setDefaultButton(KDialog::Ok
);
68 QWidget
* mainWidget
= new QWidget(q
);
69 QVBoxLayout
* topLayout
= new QVBoxLayout(mainWidget
);
71 QLabel
* label
= new QLabel(i18nc("@label:textbox",
72 "Configure which data should "
75 m_metaDataList
= new QListWidget(q
);
76 m_metaDataList
->setSelectionMode(QAbstractItemView::NoSelection
);
78 topLayout
->addWidget(label
);
79 topLayout
->addWidget(m_metaDataList
);
81 q
->setMainWidget(mainWidget
);
85 const KConfigGroup
dialogConfig(KGlobal::config(), "Nepomuk MetaDataConfigurationDialog");
86 q
->restoreDialogSize(dialogConfig
);
89 MetaDataConfigurationDialog::Private::~Private()
91 KConfigGroup
dialogConfig(KGlobal::config(), "Nepomuk MetaDataConfigurationDialog");
92 q
->saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
95 void MetaDataConfigurationDialog::Private::loadMetaData()
97 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
98 KConfigGroup settings
= config
.group("Show");
100 // Add fixed meta data items where the visibility does not
101 // depend on the currently used URL.
103 if (m_metaDataWidget
!= 0) {
104 hiddenData
= m_metaDataWidget
->hiddenData();
107 typedef QPair
<QString
, QString
> FixedItem
;
108 QList
<FixedItem
> fixedItems
;
109 if (!(hiddenData
& MetaDataWidget::TypeData
)) {
110 fixedItems
.append(FixedItem("type", i18nc("@item::inlistbox", "Type")));
112 if (!(hiddenData
& MetaDataWidget::SizeData
)) {
113 fixedItems
.append(FixedItem("size", i18nc("@item::inlistbox", "Size")));
115 if (!(hiddenData
& MetaDataWidget::ModifiedData
)) {
116 fixedItems
.append(FixedItem("modified", i18nc("@item::inlistbox", "Modified")));
118 if (!(hiddenData
& MetaDataWidget::OwnerData
)) {
119 fixedItems
.append(FixedItem("owner", i18nc("@item::inlistbox", "Owner")));
121 if (!(hiddenData
& MetaDataWidget::PermissionsData
)) {
122 fixedItems
.append(FixedItem("permissions", i18nc("@item::inlistbox", "Permissions")));
125 if (Nepomuk::ResourceManager::instance()->init() == 0) {
126 if (!(hiddenData
& MetaDataWidget::RatingData
)) {
127 fixedItems
.append(FixedItem("rating", i18nc("@item::inlistbox", "Rating")));
129 if (!(hiddenData
& MetaDataWidget::TagsData
)) {
130 fixedItems
.append(FixedItem("tags", i18nc("@item::inlistbox", "Tags")));
132 if (!(hiddenData
& MetaDataWidget::CommentData
)) {
133 fixedItems
.append(FixedItem("comment", i18nc("@item::inlistbox", "Comment")));
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
);
148 if (Nepomuk::ResourceManager::instance()->init() != 0) {
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
160 const KFileItemList items
= m_metaDataWidget
->items();
161 if (items
.count() != 1) {
162 // TODO: handle als usecases for more than one item:
165 Nepomuk::Resource
res(items
.first().nepomukUri());
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();
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
189 while (hiddenProperties
[i
] != 0) {
190 if (key
== hiddenProperties
[i
]) {
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
);
214 QString
MetaDataConfigurationDialog::Private::tunedLabel(const QString
& label
) const
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()) {
224 tunedLabel
+= label
[i
].toLower();
226 tunedLabel
+= label
[i
];
233 MetaDataConfigurationDialog::MetaDataConfigurationDialog(QWidget
* parent
,
235 KDialog(parent
, flags
),
236 d(new Private(this, 0))
240 MetaDataConfigurationDialog::MetaDataConfigurationDialog(MetaDataWidget
* metaDataWidget
,
243 KDialog(parent
, flags
),
244 d(new Private(this, metaDataWidget
))
248 MetaDataConfigurationDialog::~MetaDataConfigurationDialog()
252 void MetaDataConfigurationDialog::slotButtonClicked(int button
)
254 if (button
== KDialog::Ok
) {
255 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
256 KConfigGroup showGroup
= config
.group("Show");
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
);
268 if (d
->m_metaDataWidget
!= 0) {
270 d
->m_metaDataWidget
->setHiddenData(d
->m_metaDataWidget
->hiddenData());
274 KDialog::slotButtonClicked(button
);
278 #include "metadataconfigurationdialog.moc"