From 0c602fc82de5353bfb9a4d92e4a1413d6bdb06d9 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Thu, 31 Mar 2016 01:10:00 +0000 Subject: [PATCH] kio: further simplify metadata reading Signed-off-by: Ivailo Monev --- kio/CMakeLists.txt | 1 - kio/kfile/kfilemetadataprovider.cpp | 108 ++++++---------------------- kio/kfile/kfilemetadataprovider_p.h | 8 +-- kio/kfile/kfilemetadatareader.cpp | 64 ----------------- kio/kfile/kfilemetadatareader_p.h | 79 -------------------- 5 files changed, 24 insertions(+), 236 deletions(-) delete mode 100644 kio/kfile/kfilemetadatareader.cpp delete mode 100644 kio/kfile/kfilemetadatareader_p.h diff --git a/kio/CMakeLists.txt b/kio/CMakeLists.txt index 381fa4e8..550ee36d 100644 --- a/kio/CMakeLists.txt +++ b/kio/CMakeLists.txt @@ -214,7 +214,6 @@ set(kfile_STAT_SRCS kfile/kurlcombobox.cpp kfile/kurlrequester.cpp kfile/kurlrequesterdialog.cpp - kfile/kfilemetadatareader.cpp kfile/kfilemetadataprovider.cpp kfile/kfilesharedialog.cpp kfile/kfsprocess.cpp diff --git a/kio/kfile/kfilemetadataprovider.cpp b/kio/kfile/kfilemetadataprovider.cpp index c1ac5cf1..14160f0b 100644 --- a/kio/kfile/kfilemetadataprovider.cpp +++ b/kio/kfile/kfilemetadataprovider.cpp @@ -20,67 +20,15 @@ #include "kfilemetadataprovider_p.h" #include -#include #include #include #include #include +#include #include #include -namespace { - static QString plainText(const QString& richText) - { - QString plainText; - plainText.reserve(richText.length()); - - bool skip = false; - for (int i = 0; i < richText.length(); ++i) { - const QChar c = richText.at(i); - if (c == QLatin1Char('<')) { - skip = true; - } else if (c == QLatin1Char('>')) { - skip = false; - } else if (!skip) { - plainText.append(c); - } - } - - return plainText; - } -} - -// The default size hint of QLabel tries to return a square size. -// This does not work well in combination with layouts that use -// heightForWidth(): In this case it is possible that the content -// of a label might get clipped. By specifying a size hint -// with a maximum width that is necessary to contain the whole text, -// using heightForWidth() assures having a non-clipped text. -class ValueWidget : public QLabel -{ -public: - explicit ValueWidget(QWidget* parent = 0); - virtual QSize sizeHint() const; -}; - -ValueWidget::ValueWidget(QWidget* parent) : - QLabel(parent) -{ -} - -QSize ValueWidget::sizeHint() const -{ - QFontMetrics metrics(font()); - // TODO: QLabel internally provides already a method sizeForWidth(), - // that would be sufficient. However this method is not accessible, so - // as workaround the tags from a richtext are removed manually here to - // have a proper size hint. - return metrics.size(Qt::TextSingleLine, plainText(text())); -} - - - class KFileMetaDataProvider::Private { @@ -88,7 +36,7 @@ public: Private(KFileMetaDataProvider* parent); ~Private(); - void slotLoadingFinished(); + void readMetadata(); void slotMetaDataUpdateDone(); void slotLinkActivated(const QString& link); @@ -110,11 +58,9 @@ public: QList m_fileItems; + QList m_urls; QHash m_data; - QList m_metaDataReaders; - KFileMetaDataReader* m_latestMetaDataReader; - private: KFileMetaDataProvider* const q; }; @@ -122,37 +68,28 @@ private: KFileMetaDataProvider::Private::Private(KFileMetaDataProvider* parent) : m_fileItems(), m_data(), - m_metaDataReaders(), - m_latestMetaDataReader(0), q(parent) { } KFileMetaDataProvider::Private::~Private() { - qDeleteAll(m_metaDataReaders); } -void KFileMetaDataProvider::Private::slotLoadingFinished() +void KFileMetaDataProvider::Private::readMetadata() { - KFileMetaDataReader* finishedMetaDataReader = qobject_cast(q->sender()); - // The process that has emitted the finished() signal - // will get deleted and removed from m_metaDataReaders. - for (int i = 0; i < m_metaDataReaders.count(); ++i) { - KFileMetaDataReader* metaDataReader = m_metaDataReaders[i]; - if (metaDataReader == finishedMetaDataReader) { - m_metaDataReaders.removeAt(i); - if (metaDataReader != m_latestMetaDataReader) { - // Ignore data of older processs, as the data got - // obsolete by m_latestMetaDataReader. - metaDataReader->deleteLater(); - return; - } - } +#warning implement multi-URL metadata support + if (m_urls.count() > 1) { + kWarning() << "the API does not handle multile URLs metadata"; + } + const QString path = m_urls.first().toLocalFile(); + KFileMetaInfo metaInfo(path, KFileMetaInfo::Fastest); + const QHash metaInfoItems = metaInfo.items(); + foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) { + const QString uriString = metaInfoItem.name(); + const QVariant value = metaInfoItem.value(); + m_data.insert(uriString, value); } - - m_data = m_latestMetaDataReader->metaData(); - m_latestMetaDataReader->deleteLater(); if (m_fileItems.count() == 1) { // TODO: Handle case if remote URLs are used properly. isDir() does @@ -214,10 +151,11 @@ QList KFileMetaDataProvider::Private::resourceList() const QWidget* KFileMetaDataProvider::Private::createValueWidget(const QString& value, QWidget* parent) { - ValueWidget* valueWidget = new ValueWidget(parent); + QLabel* valueWidget = new QLabel(parent); valueWidget->setWordWrap(true); valueWidget->setAlignment(Qt::AlignTop | Qt::AlignLeft); - valueWidget->setText(plainText(value)); + valueWidget->setTextFormat(Qt::PlainText); + valueWidget->setText(value); connect(valueWidget, SIGNAL(linkActivated(QString)), q, SLOT(slotLinkActivated(QString))); return valueWidget; } @@ -242,18 +180,14 @@ void KFileMetaDataProvider::setItems(const KFileItemList& items) } Q_PRIVATE_SLOT(d,void slotDataChangeStarted()) Q_PRIVATE_SLOT(d,void slotDataChangeFinished()) - QList urls; + d->m_urls.clear(); foreach (const KFileItem& item, items) { const KUrl url = item.url(); if (url.isValid()) { - urls.append(url); + d->m_urls.append(url); } } - - d->m_latestMetaDataReader = new KFileMetaDataReader(urls); - connect(d->m_latestMetaDataReader, SIGNAL(finished()), this, SLOT(slotLoadingFinished())); - d->m_metaDataReaders.append(d->m_latestMetaDataReader); - d->m_latestMetaDataReader->start(); + d->readMetadata(); } QString KFileMetaDataProvider::label(const KUrl& metaDataUri) const diff --git a/kio/kfile/kfilemetadataprovider_p.h b/kio/kfile/kfilemetadataprovider_p.h index d8e18d57..2a1018ea 100644 --- a/kio/kfile/kfilemetadataprovider_p.h +++ b/kio/kfile/kfilemetadataprovider_p.h @@ -36,10 +36,9 @@ class QWidget; /** * @brief Provides the data for the KMetaDataWidget. * - * The default implementation provides all meta data - * that are available due to Strigi and Nepomuk. If custom - * meta data should be added, the method KFileMetaDataProvider::loadData() - * must be overwritten. + * The default implementation provides all meta data that are available due to + * Strigi. If custom meta data should be added, the method + * KFileMetaDataProvider::loadData() must be overwritten. * * @see KFileMetaDataWidget */ @@ -118,7 +117,6 @@ private: class Private; Private* const d; - Q_PRIVATE_SLOT(d, void slotLoadingFinished()) Q_PRIVATE_SLOT(d, void slotLinkActivated(const QString&)) friend class KLoadMetaDataThread; // invokes KMetaDataObject::loadData() diff --git a/kio/kfile/kfilemetadatareader.cpp b/kio/kfile/kfilemetadatareader.cpp deleted file mode 100644 index 293a90a0..00000000 --- a/kio/kfile/kfilemetadatareader.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/***************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Library General Public * - * License as published by the Free Software Foundation; either * - * version 2 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * - * Library General Public License for more details. * - * * - * You should have received a copy of the GNU Library General Public License * - * along with this library; see the file COPYING.LIB. If not, write to * - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * - * Boston, MA 02110-1301, USA. * - *****************************************************************************/ - -#include "kfilemetadatareader_p.h" - -#include -#include -#include - -KFileMetaDataReader::KFileMetaDataReader(const QList& urls, QObject* parent) : - QObject(parent) -{ - if (urls.count() > 1) { - kWarning() << i18n("more then one URL was passed"); - } - m_urls = urls; -} - -KFileMetaDataReader::~KFileMetaDataReader() -{ -} - -void KFileMetaDataReader::start() -{ -#warning implement multi-URL metadata support - foreach (const KUrl& url, m_urls) { - // Currently only the meta-data of one file is supported. - // It might be an option to read all meta-data and show - // ranges for each key. - - const QString path = url.toLocalFile(); - KFileMetaInfo metaInfo(path, KFileMetaInfo::Fastest); - const QHash metaInfoItems = metaInfo.items(); - foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) { - const QString uriString = metaInfoItem.name(); - const QVariant value(metaInfoItem.value()); - m_metaData.insert(uriString, value); - } - } - emit finished(); -} - -QHash KFileMetaDataReader::metaData() const -{ - return m_metaData; -} - -#include "moc_kfilemetadatareader_p.cpp" diff --git a/kio/kfile/kfilemetadatareader_p.h b/kio/kfile/kfilemetadatareader_p.h deleted file mode 100644 index 5144bc94..00000000 --- a/kio/kfile/kfilemetadatareader_p.h +++ /dev/null @@ -1,79 +0,0 @@ -/***************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Library General Public * - * License as published by the Free Software Foundation; either * - * version 2 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * - * Library General Public License for more details. * - * * - * You should have received a copy of the GNU Library General Public License * - * along with this library; see the file COPYING.LIB. If not, write to * - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * - * Boston, MA 02110-1301, USA. * - *****************************************************************************/ - -#ifndef KFILEMETADATAREADER_H -#define KFILEMETADATAREADER_H - -#include - -#include -#include -#include -#include -#include - -/** - * @brief Provides metadata extracted from files. - * - * The reading of the metadata is done asynchronously in a process. - * This assures that the caller won't get blocked and also prevents - * that the caller crashes in case if a metadata-analyzer plugin is instable. - * - * @since 4.7 - * @internal - */ -class KFileMetaDataReader : public QObject -{ - Q_OBJECT - -public: - /** - * @param urls List of files where the metadata should be extracted from. - * @param parent Parent object. - */ - explicit KFileMetaDataReader(const QList& urls, QObject* parent = 0); - virtual ~KFileMetaDataReader(); - - /** - * Starts the reading of the metadata inside a custom process. - * The signal finished() will get emitted if the reading has been finished. - * Use metaData() to access the read metadata. - */ - void start(); - - /** - * @return The read metadata of the given files. The method provides valid values - * after the signal finished() has been emitted. If it is invoked before - * an empty hash-table will be returned. - */ - QHash metaData() const; - -Q_SIGNALS: - /** - * Is emitted if the reading of the metadata inside a custom process has been finished. - * The method metaData() can be used afterwards to access the metadata. - */ - void finished(); - -private: - QList m_urls; - QHash m_metaData; -}; - -#endif