1 /*****************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 *****************************************************************************/
19 #include "kmetadataconfigurationdialog.h"
20 #include "kmetadatawidget.h"
23 #include <config-nepomuk.h>
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>
33 #include <QListWidget>
34 #include <QVBoxLayout>
36 class KMetaDataConfigurationDialog::Private
39 Private(KMetaDataConfigurationDialog
* parent
, KMetaDataWidget
* metaDataWidget
);
44 QString
tunedLabel(const QString
& label
) const;
46 int m_visibleDataTypes
;
47 KMetaDataWidget
* m_metaDataWidget
;
48 QListWidget
* m_metaDataList
;
51 KMetaDataConfigurationDialog
* const q
;
54 KMetaDataConfigurationDialog::Private::Private(KMetaDataConfigurationDialog
* parent
,
55 KMetaDataWidget
* metaDataWidget
) :
58 m_visibleDataTypes
= 0;
59 m_metaDataWidget
= metaDataWidget
;
61 q
->setCaption(i18nc("@title:window", "Configure Shown Data"));
62 q
->setButtons(KDialog::Ok
| KDialog::Cancel
);
63 q
->setDefaultButton(KDialog::Ok
);
65 QWidget
* mainWidget
= new QWidget(q
);
66 QVBoxLayout
* topLayout
= new QVBoxLayout(mainWidget
);
68 QLabel
* label
= new QLabel(i18nc("@label:textbox",
69 "Configure which data should "
72 m_metaDataList
= new QListWidget(q
);
73 m_metaDataList
->setSelectionMode(QAbstractItemView::NoSelection
);
75 topLayout
->addWidget(label
);
76 topLayout
->addWidget(m_metaDataList
);
78 q
->setMainWidget(mainWidget
);
82 const KConfigGroup
dialogConfig(KGlobal::config(), "Nepomuk KMetaDataConfigurationDialog");
83 q
->restoreDialogSize(dialogConfig
);
86 KMetaDataConfigurationDialog::Private::~Private()
88 KConfigGroup
dialogConfig(KGlobal::config(), "Nepomuk KMetaDataConfigurationDialog");
89 q
->saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
92 void KMetaDataConfigurationDialog::Private::loadMetaData()
94 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
95 KConfigGroup settings
= config
.group("Show");
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();
111 typedef QPair
<QString
, QString
> FixedItem
;
112 QList
<FixedItem
> fixedItems
;
113 if (visibleDataTypes
& KMetaDataWidget::TypeData
) {
114 fixedItems
.append(FixedItem("type", i18nc("@item::inlistbox", "Type")));
116 if (visibleDataTypes
& KMetaDataWidget::SizeData
) {
117 fixedItems
.append(FixedItem("size", i18nc("@item::inlistbox", "Size")));
119 if (visibleDataTypes
& KMetaDataWidget::ModifiedData
) {
120 fixedItems
.append(FixedItem("modified", i18nc("@item::inlistbox", "Modified")));
122 if (visibleDataTypes
& KMetaDataWidget::OwnerData
) {
123 fixedItems
.append(FixedItem("owner", i18nc("@item::inlistbox", "Owner")));
125 if (visibleDataTypes
& KMetaDataWidget::PermissionsData
) {
126 fixedItems
.append(FixedItem("permissions", i18nc("@item::inlistbox", "Permissions")));
129 if (Nepomuk::ResourceManager::instance()->init() == 0) {
130 if (visibleDataTypes
& KMetaDataWidget::RatingData
) {
131 fixedItems
.append(FixedItem("rating", i18nc("@item::inlistbox", "Rating")));
133 if (visibleDataTypes
& KMetaDataWidget::TagsData
) {
134 fixedItems
.append(FixedItem("tags", i18nc("@item::inlistbox", "Tags")));
136 if (visibleDataTypes
& KMetaDataWidget::CommentData
) {
137 fixedItems
.append(FixedItem("comment", i18nc("@item::inlistbox", "Comment")));
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
);
152 if (Nepomuk::ResourceManager::instance()->init() != 0) {
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
164 const KFileItemList items
= m_metaDataWidget
->items();
165 if (items
.count() != 1) {
166 // TODO: handle als usecases for more than one item:
169 Nepomuk::Resource
res(items
.first().nepomukUri());
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();
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
194 while (hiddenProperties
[i
] != 0) {
195 if (key
== hiddenProperties
[i
]) {
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
);
219 QString
KMetaDataConfigurationDialog::Private::tunedLabel(const QString
& label
) const
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()) {
229 tunedLabel
+= label
[i
].toLower();
231 tunedLabel
+= label
[i
];
238 KMetaDataConfigurationDialog::KMetaDataConfigurationDialog(QWidget
* parent
,
240 KDialog(parent
, flags
),
241 d(new Private(this, 0))
245 KMetaDataConfigurationDialog::KMetaDataConfigurationDialog(KMetaDataWidget
* metaDataWidget
,
248 KDialog(parent
, flags
),
249 d(new Private(this, metaDataWidget
))
253 KMetaDataConfigurationDialog::~KMetaDataConfigurationDialog()
257 void KMetaDataConfigurationDialog::slotButtonClicked(int button
)
259 if (button
== KDialog::Ok
) {
260 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
261 KConfigGroup showGroup
= config
.group("Show");
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
);
273 if (d
->m_metaDataWidget
!= 0) {
275 d
->m_metaDataWidget
->setVisibleDataTypes(d
->m_metaDataWidget
->visibleDataTypes());
279 KDialog::slotButtonClicked(button
);
283 #include "kmetadataconfigurationdialog.moc"