File indexing completed on 2024-05-05 16:08:25

0001 /*****************************************************************************
0002  * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at>                      *
0003  *                                                                           *
0004  * This library is free software; you can redistribute it and/or             *
0005  * modify it under the terms of the GNU Library General Public               *
0006  * License as published by the Free Software Foundation; either              *
0007  * version 2 of the License, or (at your option) any later version.          *
0008  *                                                                           *
0009  * This library is distributed in the hope that it will be useful,           *
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
0012  * Library General Public License for more details.                          *
0013  *                                                                           *
0014  * You should have received a copy of the GNU Library General Public License *
0015  * along with this library; see the file COPYING.LIB.  If not, write to      *
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
0017  * Boston, MA 02110-1301, USA.                                               *
0018  *****************************************************************************/
0019 
0020 #include "kfilemetadataconfigurationwidget.h"
0021 
0022 #include <kconfig.h>
0023 #include <kconfiggroup.h>
0024 #include <kfilemetainfo.h>
0025 #include <kfilemetainfoitem.h>
0026 #include "knfotranslator_p.h"
0027 #include <klocalizedstring.h>
0028 
0029 #include <config-kdelibs4support.h>
0030 #if ! KIO_NO_NEPOMUK
0031 #define DISABLE_NEPOMUK_LEGACY
0032 #include <resource.h>
0033 #include <resourcemanager.h>
0034 #include <property.h>
0035 #include <variant.h>
0036 
0037 #include "kfilemetadataprovider_p.h"
0038 #endif
0039 
0040 #include <QEvent>
0041 #include <QListWidget>
0042 #include <QVBoxLayout>
0043 
0044 class Q_DECL_HIDDEN KFileMetaDataConfigurationWidget::Private
0045 {
0046 public:
0047     Private(KFileMetaDataConfigurationWidget *parent);
0048     ~Private();
0049 
0050     void init();
0051     void loadMetaData();
0052     void addItem(const QUrl &uri);
0053 
0054     /**
0055      * Is invoked after the meta data model has finished the loading of
0056      * meta data. The meta data labels will be added to the configuration
0057      * list.
0058      */
0059     void slotLoadingFinished();
0060 
0061     int m_visibleDataTypes;
0062     KFileItemList m_fileItems;
0063 #if ! KIO_NO_NEPOMUK
0064     KFileMetaDataProvider *m_provider;
0065 #endif
0066     QListWidget *m_metaDataList;
0067 
0068 private:
0069     KFileMetaDataConfigurationWidget *const q;
0070 };
0071 
0072 KFileMetaDataConfigurationWidget::Private::Private(KFileMetaDataConfigurationWidget *parent) :
0073     m_visibleDataTypes(0),
0074     m_fileItems(),
0075 #if ! KIO_NO_NEPOMUK
0076     m_provider(0),
0077 #endif
0078     m_metaDataList(nullptr),
0079     q(parent)
0080 {
0081     m_metaDataList = new QListWidget(q);
0082     m_metaDataList->setSelectionMode(QAbstractItemView::NoSelection);
0083     m_metaDataList->setSortingEnabled(true);
0084 
0085     QVBoxLayout *layout = new QVBoxLayout(q);
0086     layout->addWidget(m_metaDataList);
0087 
0088 #if ! KIO_NO_NEPOMUK
0089     m_provider = new KFileMetaDataProvider(q);
0090 #endif
0091 }
0092 
0093 KFileMetaDataConfigurationWidget::Private::~Private()
0094 {
0095 }
0096 
0097 void KFileMetaDataConfigurationWidget::Private::loadMetaData()
0098 {
0099 #if ! KIO_NO_NEPOMUK
0100     m_provider->setItems(m_fileItems);
0101     connect(m_provider, SIGNAL(loadingFinished()),
0102             q, SLOT(slotLoadingFinished()));
0103 #endif
0104 }
0105 
0106 void KFileMetaDataConfigurationWidget::Private::addItem(const QUrl &uri)
0107 {
0108     // Meta information provided by Nepomuk that is already
0109     // available from KFileItem as "fixed item" (see above)
0110     // should not be shown as second entry.
0111     static const char *const hiddenProperties[] = {
0112         "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#comment",         // = fixed item kfileitem#comment
0113         "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#contentSize",     // = fixed item kfileitem#size
0114         "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#lastModified",    // = fixed item kfileitem#modified
0115         "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#plainTextContent" // hide this property always
0116         "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#mimeType",        // = fixed item kfileitem#type
0117         "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName",        // hide this property always
0118         "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",                          // = fixed item kfileitem#type
0119         nullptr // mandatory last entry
0120     };
0121 
0122     int i = 0;
0123     const QString key = uri.toString();
0124     while (hiddenProperties[i] != nullptr) {
0125         if (key == QLatin1String(hiddenProperties[i])) {
0126             // the item is hidden
0127             return;
0128         }
0129         ++i;
0130     }
0131 
0132     // the item is not hidden, add it to the list
0133     KConfig config("kmetainformationrc", KConfig::NoGlobals);
0134     KConfigGroup settings = config.group("Show");
0135 
0136 #if ! KIO_NO_NEPOMUK
0137     const QString label = (m_provider == 0)
0138                           ? KNfoTranslator::instance().translation(uri)
0139                           : m_provider->label(uri);
0140 #else
0141     const QString label = KNfoTranslator::instance().translation(uri);
0142 #endif
0143 
0144     QListWidgetItem *item = new QListWidgetItem(label, m_metaDataList);
0145     item->setData(Qt::UserRole, key);
0146     const bool show = settings.readEntry(key, true);
0147     item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
0148 }
0149 
0150 void KFileMetaDataConfigurationWidget::Private::slotLoadingFinished()
0151 {
0152 #if ! KIO_NO_NEPOMUK
0153     // Get all meta information labels that are available for
0154     // the currently shown file item and add them to the list.
0155     Q_ASSERT(m_provider != 0);
0156 
0157     const QHash<QUrl, Nepomuk::Variant> data = m_provider->data();
0158     QHash<QUrl, Nepomuk::Variant>::const_iterator it = data.constBegin();
0159     while (it != data.constEnd()) {
0160         addItem(it.key());
0161         ++it;
0162     }
0163 #endif
0164 }
0165 
0166 KFileMetaDataConfigurationWidget::KFileMetaDataConfigurationWidget(QWidget *parent) :
0167     QWidget(parent),
0168     d(new Private(this))
0169 {
0170 }
0171 
0172 KFileMetaDataConfigurationWidget::~KFileMetaDataConfigurationWidget()
0173 {
0174     delete d;
0175 }
0176 
0177 void KFileMetaDataConfigurationWidget::setItems(const KFileItemList &items)
0178 {
0179     d->m_fileItems = items;
0180 }
0181 
0182 KFileItemList KFileMetaDataConfigurationWidget::items() const
0183 {
0184     return d->m_fileItems;
0185 }
0186 
0187 void KFileMetaDataConfigurationWidget::save()
0188 {
0189     KConfig config("kmetainformationrc", KConfig::NoGlobals);
0190     KConfigGroup showGroup = config.group("Show");
0191 
0192     const int count = d->m_metaDataList->count();
0193     for (int i = 0; i < count; ++i) {
0194         QListWidgetItem *item = d->m_metaDataList->item(i);
0195         const bool show = (item->checkState() == Qt::Checked);
0196         const QString key = item->data(Qt::UserRole).toString();
0197         showGroup.writeEntry(key, show);
0198     }
0199 
0200     showGroup.sync();
0201 }
0202 
0203 bool KFileMetaDataConfigurationWidget::event(QEvent *event)
0204 {
0205     if (event->type() == QEvent::Polish) {
0206         // loadMetaData() must be invoked asynchronously, as the list
0207         // must finish it's initialization first
0208         QMetaObject::invokeMethod(this, "loadMetaData", Qt::QueuedConnection);
0209     }
0210     return QWidget::event(event);;
0211 }
0212 
0213 QSize KFileMetaDataConfigurationWidget::sizeHint() const
0214 {
0215     return d->m_metaDataList->sizeHint();
0216 }
0217 
0218 #include "moc_kfilemetadataconfigurationwidget.cpp"