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 "knfotranslator_p.h" 00027 #include <klocale.h> 00028 00029 #include <config-kio.h> 00030 #ifndef KIO_NO_NEPOMUK 00031 #define DISABLE_NEPOMUK_LEGACY 00032 #include <resource.h> 00033 #include <resourcemanager.h> 00034 #include <property.h> 00035 #include <variant.h> 00036 00037 #include "kfilemetadataprovider_p.h" 00038 #endif 00039 00040 #include <QEvent> 00041 #include <QListWidget> 00042 #include <QVBoxLayout> 00043 00044 class KFileMetaDataConfigurationWidget::Private 00045 { 00046 public: 00047 Private(KFileMetaDataConfigurationWidget* parent); 00048 ~Private(); 00049 00050 void init(); 00051 void loadMetaData(); 00052 void addItem(const KUrl& uri); 00053 00059 void slotLoadingFinished(); 00060 00061 int m_visibleDataTypes; 00062 KFileItemList m_fileItems; 00063 #ifndef KIO_NO_NEPOMUK 00064 KFileMetaDataProvider* m_provider; 00065 #endif 00066 QListWidget* m_metaDataList; 00067 00068 private: 00069 KFileMetaDataConfigurationWidget* const q; 00070 }; 00071 00072 KFileMetaDataConfigurationWidget::Private::Private(KFileMetaDataConfigurationWidget* parent) : 00073 m_visibleDataTypes(0), 00074 m_fileItems(), 00075 #ifndef KIO_NO_NEPOMUK 00076 m_provider(0), 00077 #endif 00078 m_metaDataList(0), 00079 q(parent) 00080 { 00081 m_metaDataList = new QListWidget(q); 00082 m_metaDataList->setSelectionMode(QAbstractItemView::NoSelection); 00083 m_metaDataList->setSortingEnabled(true); 00084 00085 QVBoxLayout* layout = new QVBoxLayout(q); 00086 layout->addWidget(m_metaDataList); 00087 00088 #ifndef KIO_NO_NEPOMUK 00089 m_provider = new KFileMetaDataProvider(q); 00090 #endif 00091 } 00092 00093 KFileMetaDataConfigurationWidget::Private::~Private() 00094 { 00095 } 00096 00097 void KFileMetaDataConfigurationWidget::Private::loadMetaData() 00098 { 00099 #ifndef KIO_NO_NEPOMUK 00100 m_provider->setItems(m_fileItems); 00101 connect(m_provider, SIGNAL(loadingFinished()), 00102 q, SLOT(slotLoadingFinished())); 00103 #endif 00104 } 00105 00106 void KFileMetaDataConfigurationWidget::Private::addItem(const KUrl& uri) 00107 { 00108 // Meta information provided by Nepomuk that is already 00109 // available from KFileItem as "fixed item" (see above) 00110 // should not be shown as second entry. 00111 static const char* const hiddenProperties[] = { 00112 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#comment", // = fixed item kfileitem#comment 00113 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#contentSize", // = fixed item kfileitem#size 00114 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#lastModified", // = fixed item kfileitem#modified 00115 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#plainTextContent" // hide this property always 00116 "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#mimeType", // = fixed item kfileitem#type 00117 "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName", // hide this property always 00118 "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", // = fixed item kfileitem#type 00119 0 // mandatory last entry 00120 }; 00121 00122 int i = 0; 00123 const QString key = uri.url(); 00124 while (hiddenProperties[i] != 0) { 00125 if (key == QLatin1String(hiddenProperties[i])) { 00126 // the item is hidden 00127 return; 00128 } 00129 ++i; 00130 } 00131 00132 // the item is not hidden, add it to the list 00133 KConfig config("kmetainformationrc", KConfig::NoGlobals); 00134 KConfigGroup settings = config.group("Show"); 00135 00136 #ifndef KIO_NO_NEPOMUK 00137 const QString label = (m_provider == 0) 00138 ? KNfoTranslator::instance().translation(uri) 00139 : m_provider->label(uri); 00140 #else 00141 const QString label = KNfoTranslator::instance().translation(uri); 00142 #endif 00143 00144 QListWidgetItem* item = new QListWidgetItem(label, m_metaDataList); 00145 item->setData(Qt::UserRole, key); 00146 const bool show = settings.readEntry(key, true); 00147 item->setCheckState(show ? Qt::Checked : Qt::Unchecked); 00148 } 00149 00150 void KFileMetaDataConfigurationWidget::Private::slotLoadingFinished() 00151 { 00152 #ifndef KIO_NO_NEPOMUK 00153 // Get all meta information labels that are available for 00154 // the currently shown file item and add them to the list. 00155 Q_ASSERT(m_provider != 0); 00156 00157 const QHash<KUrl, Nepomuk::Variant> data = m_provider->data(); 00158 QHash<KUrl, Nepomuk::Variant>::const_iterator it = data.constBegin(); 00159 while (it != data.constEnd()) { 00160 addItem(it.key()); 00161 ++it; 00162 } 00163 #endif 00164 } 00165 00166 KFileMetaDataConfigurationWidget::KFileMetaDataConfigurationWidget(QWidget* parent) : 00167 QWidget(parent), 00168 d(new Private(this)) 00169 { 00170 } 00171 00172 KFileMetaDataConfigurationWidget::~KFileMetaDataConfigurationWidget() 00173 { 00174 delete d; 00175 } 00176 00177 void KFileMetaDataConfigurationWidget::setItems(const KFileItemList& items) 00178 { 00179 d->m_fileItems = items; 00180 } 00181 00182 KFileItemList KFileMetaDataConfigurationWidget::items() const 00183 { 00184 return d->m_fileItems; 00185 } 00186 00187 void KFileMetaDataConfigurationWidget::save() 00188 { 00189 KConfig config("kmetainformationrc", KConfig::NoGlobals); 00190 KConfigGroup showGroup = config.group("Show"); 00191 00192 const int count = d->m_metaDataList->count(); 00193 for (int i = 0; i < count; ++i) { 00194 QListWidgetItem* item = d->m_metaDataList->item(i); 00195 const bool show = (item->checkState() == Qt::Checked); 00196 const QString key = item->data(Qt::UserRole).toString(); 00197 showGroup.writeEntry(key, show); 00198 } 00199 00200 showGroup.sync(); 00201 } 00202 00203 bool KFileMetaDataConfigurationWidget::event(QEvent* event) 00204 { 00205 if (event->type() == QEvent::Polish) { 00206 // loadMetaData() must be invoked asynchronously, as the list 00207 // must finish it's initialization first 00208 QMetaObject::invokeMethod(this, "loadMetaData", Qt::QueuedConnection); 00209 } 00210 return QWidget::event(event);; 00211 } 00212 00213 QSize KFileMetaDataConfigurationWidget::sizeHint() const 00214 { 00215 return d->m_metaDataList->sizeHint(); 00216 } 00217 00218 00219 #include "kfilemetadataconfigurationwidget.moc"
KDE 4.7 API Reference