KIO
kfilemetadataconfigurationwidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************** 00002 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> * 00003 * * 00004 * This library is free software; you can redistribute it and/or * 00005 * modify it under the terms of the GNU Library General Public * 00006 * License as published by the Free Software Foundation; either * 00007 * version 2 of the License, or (at your option) any later version. * 00008 * * 00009 * This library is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00012 * Library General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU Library General Public License * 00015 * along with this library; see the file COPYING.LIB. If not, write to * 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * 00017 * Boston, MA 02110-1301, USA. * 00018 *****************************************************************************/ 00019 00020 #include "kfilemetadataconfigurationwidget.h" 00021 00022 #include <kconfig.h> 00023 #include <kconfiggroup.h> 00024 #include <kfilemetainfo.h> 00025 #include <kfilemetainfoitem.h> 00026 #include "kfilemetadataprovider_p.h" 00027 #include "knfotranslator_p.h" 00028 #include <klocale.h> 00029 00030 #include <config-kio.h> 00031 #ifndef KIO_NO_NEPOMUK 00032 #define DISABLE_NEPOMUK_LEGACY 00033 #include <resource.h> 00034 #include <resourcemanager.h> 00035 #include <property.h> 00036 #include <variant.h> 00037 #endif 00038 00039 #include <QEvent> 00040 #include <QListWidget> 00041 #include <QVBoxLayout> 00042 00043 class KFileMetaDataConfigurationWidget::Private 00044 { 00045 public: 00046 Private(KFileMetaDataConfigurationWidget* parent); 00047 ~Private(); 00048 00049 void init(); 00050 void loadMetaData(); 00051 void addItem(const KUrl& uri); 00052 00058 void slotLoadingFinished(); 00059 00060 int m_visibleDataTypes; 00061 KFileItemList m_fileItems; 00062 KFileMetaDataProvider* m_provider; 00063 QListWidget* m_metaDataList; 00064 00065 private: 00066 KFileMetaDataConfigurationWidget* const q; 00067 }; 00068 00069 KFileMetaDataConfigurationWidget::Private::Private(KFileMetaDataConfigurationWidget* parent) : 00070 m_visibleDataTypes(0), 00071 m_fileItems(), 00072 m_provider(0), 00073 m_metaDataList(0), 00074 q(parent) 00075 { 00076 m_metaDataList = new QListWidget(q); 00077 m_metaDataList->setSelectionMode(QAbstractItemView::NoSelection); 00078 m_metaDataList->setSortingEnabled(true); 00079 00080 QVBoxLayout* layout = new QVBoxLayout(q); 00081 layout->addWidget(m_metaDataList); 00082 00083 m_provider = new KFileMetaDataProvider(q); 00084 } 00085 00086 KFileMetaDataConfigurationWidget::Private::~Private() 00087 { 00088 } 00089 00090 void KFileMetaDataConfigurationWidget::Private::loadMetaData() 00091 { 00092 #ifndef KIO_NO_NEPOMUK 00093 m_provider->setItems(m_fileItems); 00094 connect(m_provider, SIGNAL(loadingFinished()), 00095 q, SLOT(slotLoadingFinished())); 00096 #endif 00097 } 00098 00099 void KFileMetaDataConfigurationWidget::Private::addItem(const KUrl& uri) 00100 { 00101 // Meta information provided by Nepomuk that is already 00102 // available from KFileItem as "fixed item" (see above) 00103 // should not be shown as second entry. 00104 static const char* const hiddenProperties[] = { 00105 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#comment", // = fixed item kfileitem#comment 00106 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#contentSize", // = fixed item kfileitem#size 00107 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#lastModified", // = fixed item kfileitem#modified 00108 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#plainTextContent" // hide this property always 00109 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#mimeType", // = fixed item kfileitem#type 00110 "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName", // hide this property always 00111 "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", // = fixed item kfileitem#type 00112 0 // mandatory last entry 00113 }; 00114 00115 int i = 0; 00116 const QString key = uri.url(); 00117 while (hiddenProperties[i] != 0) { 00118 if (key == QLatin1String(hiddenProperties[i])) { 00119 // the item is hidden 00120 return; 00121 } 00122 ++i; 00123 } 00124 00125 // the item is not hidden, add it to the list 00126 KConfig config("kmetainformationrc", KConfig::NoGlobals); 00127 KConfigGroup settings = config.group("Show"); 00128 00129 const QString label = (m_provider == 0) 00130 ? KNfoTranslator::instance().translation(uri) 00131 : m_provider->label(uri); 00132 00133 QListWidgetItem* item = new QListWidgetItem(label, m_metaDataList); 00134 item->setData(Qt::UserRole, key); 00135 const bool show = settings.readEntry(key, true); 00136 item->setCheckState(show ? Qt::Checked : Qt::Unchecked); 00137 } 00138 00139 void KFileMetaDataConfigurationWidget::Private::slotLoadingFinished() 00140 { 00141 #ifndef KIO_NO_NEPOMUK 00142 // Get all meta information labels that are available for 00143 // the currently shown file item and add them to the list. 00144 Q_ASSERT(m_provider != 0); 00145 00146 const QHash<KUrl, Nepomuk::Variant> data = m_provider->data(); 00147 QHash<KUrl, Nepomuk::Variant>::const_iterator it = data.constBegin(); 00148 while (it != data.constEnd()) { 00149 addItem(it.key()); 00150 ++it; 00151 } 00152 #endif 00153 } 00154 00155 KFileMetaDataConfigurationWidget::KFileMetaDataConfigurationWidget(QWidget* parent) : 00156 QWidget(parent), 00157 d(new Private(this)) 00158 { 00159 } 00160 00161 KFileMetaDataConfigurationWidget::~KFileMetaDataConfigurationWidget() 00162 { 00163 delete d; 00164 } 00165 00166 void KFileMetaDataConfigurationWidget::setItems(const KFileItemList& items) 00167 { 00168 d->m_fileItems = items; 00169 } 00170 00171 KFileItemList KFileMetaDataConfigurationWidget::items() const 00172 { 00173 return d->m_fileItems; 00174 } 00175 00176 void KFileMetaDataConfigurationWidget::save() 00177 { 00178 KConfig config("kmetainformationrc", KConfig::NoGlobals); 00179 KConfigGroup showGroup = config.group("Show"); 00180 00181 const int count = d->m_metaDataList->count(); 00182 for (int i = 0; i < count; ++i) { 00183 QListWidgetItem* item = d->m_metaDataList->item(i); 00184 const bool show = (item->checkState() == Qt::Checked); 00185 const QString key = item->data(Qt::UserRole).toString(); 00186 showGroup.writeEntry(key, show); 00187 } 00188 00189 showGroup.sync(); 00190 } 00191 00192 bool KFileMetaDataConfigurationWidget::event(QEvent* event) 00193 { 00194 if (event->type() == QEvent::Polish) { 00195 // loadMetaData() must be invoked asynchronously, as the list 00196 // must finish it's initialization first 00197 QMetaObject::invokeMethod(this, "loadMetaData", Qt::QueuedConnection); 00198 } 00199 return QWidget::event(event);; 00200 } 00201 00202 QSize KFileMetaDataConfigurationWidget::sizeHint() const 00203 { 00204 return d->m_metaDataList->sizeHint(); 00205 } 00206 00207 00208 #include "kfilemetadataconfigurationwidget.moc"
KDE 4.6 API Reference