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 as published by the Free Software Foundation; either *
7 * version 2 of the License, or (at your option) any later version. *
8 * *
9 * This library 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 GNU *
12 * Library General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Library General Public License *
15 * along with this library; see the file COPYING.LIB. If not, write to *
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301, USA. *
18 *****************************************************************************/
19
20 #include "kfilemetadataconfigurationwidget.h"
21
22 #include <kconfig.h>
23 #include <kconfiggroup.h>
24 #include <kfilemetainfo.h>
25 #include <kfilemetainfoitem.h>
26 #include "knfotranslator_p.h"
27 #include <klocalizedstring.h>
28
29 #include <config-kdelibs4support.h>
30 #if ! KIO_NO_NEPOMUK
31 #define DISABLE_NEPOMUK_LEGACY
32 #include <resource.h>
33 #include <resourcemanager.h>
34 #include <property.h>
35 #include <variant.h>
36
37 #include "kfilemetadataprovider_p.h"
38 #endif
39
40 #include <QEvent>
41 #include <QListWidget>
42 #include <QVBoxLayout>
43
44 class Q_DECL_HIDDEN KFileMetaDataConfigurationWidget::Private
45 {
46 public:
47 Private(KFileMetaDataConfigurationWidget *parent);
48 ~Private();
49
50 void init();
51 void loadMetaData();
52 void addItem(const QUrl &uri);
53
54 /**
55 * Is invoked after the meta data model has finished the loading of
56 * meta data. The meta data labels will be added to the configuration
57 * list.
58 */
59 void slotLoadingFinished();
60
61 int m_visibleDataTypes;
62 KFileItemList m_fileItems;
63 #if ! KIO_NO_NEPOMUK
64 KFileMetaDataProvider *m_provider;
65 #endif
66 QListWidget *m_metaDataList;
67
68 private:
69 KFileMetaDataConfigurationWidget *const q;
70 };
71
Private(KFileMetaDataConfigurationWidget * parent)72 KFileMetaDataConfigurationWidget::Private::Private(KFileMetaDataConfigurationWidget *parent) :
73 m_visibleDataTypes(0),
74 m_fileItems(),
75 #if ! KIO_NO_NEPOMUK
76 m_provider(0),
77 #endif
78 m_metaDataList(nullptr),
79 q(parent)
80 {
81 m_metaDataList = new QListWidget(q);
82 m_metaDataList->setSelectionMode(QAbstractItemView::NoSelection);
83 m_metaDataList->setSortingEnabled(true);
84
85 QVBoxLayout *layout = new QVBoxLayout(q);
86 layout->addWidget(m_metaDataList);
87
88 #if ! KIO_NO_NEPOMUK
89 m_provider = new KFileMetaDataProvider(q);
90 #endif
91 }
92
~Private()93 KFileMetaDataConfigurationWidget::Private::~Private()
94 {
95 }
96
loadMetaData()97 void KFileMetaDataConfigurationWidget::Private::loadMetaData()
98 {
99 #if ! KIO_NO_NEPOMUK
100 m_provider->setItems(m_fileItems);
101 connect(m_provider, SIGNAL(loadingFinished()),
102 q, SLOT(slotLoadingFinished()));
103 #endif
104 }
105
addItem(const QUrl & uri)106 void KFileMetaDataConfigurationWidget::Private::addItem(const QUrl &uri)
107 {
108 // Meta information provided by Nepomuk that is already
109 // available from KFileItem as "fixed item" (see above)
110 // should not be shown as second entry.
111 static const char *const hiddenProperties[] = {
112 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#comment", // = fixed item kfileitem#comment
113 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#contentSize", // = fixed item kfileitem#size
114 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#lastModified", // = fixed item kfileitem#modified
115 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#plainTextContent" // hide this property always
116 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#mimeType", // = fixed item kfileitem#type
117 "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName", // hide this property always
118 "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", // = fixed item kfileitem#type
119 nullptr // mandatory last entry
120 };
121
122 int i = 0;
123 const QString key = uri.toString();
124 while (hiddenProperties[i] != nullptr) {
125 if (key == QLatin1String(hiddenProperties[i])) {
126 // the item is hidden
127 return;
128 }
129 ++i;
130 }
131
132 // the item is not hidden, add it to the list
133 KConfig config("kmetainformationrc", KConfig::NoGlobals);
134 KConfigGroup settings = config.group("Show");
135
136 #if ! KIO_NO_NEPOMUK
137 const QString label = (m_provider == 0)
138 ? KNfoTranslator::instance().translation(uri)
139 : m_provider->label(uri);
140 #else
141 const QString label = KNfoTranslator::instance().translation(uri);
142 #endif
143
144 QListWidgetItem *item = new QListWidgetItem(label, m_metaDataList);
145 item->setData(Qt::UserRole, key);
146 const bool show = settings.readEntry(key, true);
147 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
148 }
149
slotLoadingFinished()150 void KFileMetaDataConfigurationWidget::Private::slotLoadingFinished()
151 {
152 #if ! KIO_NO_NEPOMUK
153 // Get all meta information labels that are available for
154 // the currently shown file item and add them to the list.
155 Q_ASSERT(m_provider != 0);
156
157 const QHash<QUrl, Nepomuk::Variant> data = m_provider->data();
158 QHash<QUrl, Nepomuk::Variant>::const_iterator it = data.constBegin();
159 while (it != data.constEnd()) {
160 addItem(it.key());
161 ++it;
162 }
163 #endif
164 }
165
KFileMetaDataConfigurationWidget(QWidget * parent)166 KFileMetaDataConfigurationWidget::KFileMetaDataConfigurationWidget(QWidget *parent) :
167 QWidget(parent),
168 d(new Private(this))
169 {
170 }
171
~KFileMetaDataConfigurationWidget()172 KFileMetaDataConfigurationWidget::~KFileMetaDataConfigurationWidget()
173 {
174 delete d;
175 }
176
setItems(const KFileItemList & items)177 void KFileMetaDataConfigurationWidget::setItems(const KFileItemList &items)
178 {
179 d->m_fileItems = items;
180 }
181
items() const182 KFileItemList KFileMetaDataConfigurationWidget::items() const
183 {
184 return d->m_fileItems;
185 }
186
save()187 void KFileMetaDataConfigurationWidget::save()
188 {
189 KConfig config("kmetainformationrc", KConfig::NoGlobals);
190 KConfigGroup showGroup = config.group("Show");
191
192 const int count = d->m_metaDataList->count();
193 for (int i = 0; i < count; ++i) {
194 QListWidgetItem *item = d->m_metaDataList->item(i);
195 const bool show = (item->checkState() == Qt::Checked);
196 const QString key = item->data(Qt::UserRole).toString();
197 showGroup.writeEntry(key, show);
198 }
199
200 showGroup.sync();
201 }
202
event(QEvent * event)203 bool KFileMetaDataConfigurationWidget::event(QEvent *event)
204 {
205 if (event->type() == QEvent::Polish) {
206 // loadMetaData() must be invoked asynchronously, as the list
207 // must finish it's initialization first
208 QMetaObject::invokeMethod(this, "loadMetaData", Qt::QueuedConnection);
209 }
210 return QWidget::event(event);;
211 }
212
sizeHint() const213 QSize KFileMetaDataConfigurationWidget::sizeHint() const
214 {
215 return d->m_metaDataList->sizeHint();
216 }
217
218 #include "moc_kfilemetadataconfigurationwidget.cpp"
219