kio: further simplify metadata reading

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2016-03-31 01:10:00 +00:00
parent d9e49d1617
commit 0c602fc82d
5 changed files with 24 additions and 236 deletions

View file

@ -214,7 +214,6 @@ set(kfile_STAT_SRCS
kfile/kurlcombobox.cpp kfile/kurlcombobox.cpp
kfile/kurlrequester.cpp kfile/kurlrequester.cpp
kfile/kurlrequesterdialog.cpp kfile/kurlrequesterdialog.cpp
kfile/kfilemetadatareader.cpp
kfile/kfilemetadataprovider.cpp kfile/kfilemetadataprovider.cpp
kfile/kfilesharedialog.cpp kfile/kfilesharedialog.cpp
kfile/kfsprocess.cpp kfile/kfsprocess.cpp

View file

@ -20,67 +20,15 @@
#include "kfilemetadataprovider_p.h" #include "kfilemetadataprovider_p.h"
#include <kfileitem.h> #include <kfileitem.h>
#include <kfilemetadatareader_p.h>
#include <knfotranslator_p.h> #include <knfotranslator_p.h>
#include <klocale.h> #include <klocale.h>
#include <kstandarddirs.h> #include <kstandarddirs.h>
#include <kurl.h> #include <kurl.h>
#include <kdebug.h>
#include <QLabel> #include <QLabel>
#include <QDir> #include <QDir>
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 class KFileMetaDataProvider::Private
{ {
@ -88,7 +36,7 @@ public:
Private(KFileMetaDataProvider* parent); Private(KFileMetaDataProvider* parent);
~Private(); ~Private();
void slotLoadingFinished(); void readMetadata();
void slotMetaDataUpdateDone(); void slotMetaDataUpdateDone();
void slotLinkActivated(const QString& link); void slotLinkActivated(const QString& link);
@ -110,11 +58,9 @@ public:
QList<KFileItem> m_fileItems; QList<KFileItem> m_fileItems;
QList<KUrl> m_urls;
QHash<KUrl, QVariant> m_data; QHash<KUrl, QVariant> m_data;
QList<KFileMetaDataReader*> m_metaDataReaders;
KFileMetaDataReader* m_latestMetaDataReader;
private: private:
KFileMetaDataProvider* const q; KFileMetaDataProvider* const q;
}; };
@ -122,37 +68,28 @@ private:
KFileMetaDataProvider::Private::Private(KFileMetaDataProvider* parent) : KFileMetaDataProvider::Private::Private(KFileMetaDataProvider* parent) :
m_fileItems(), m_fileItems(),
m_data(), m_data(),
m_metaDataReaders(),
m_latestMetaDataReader(0),
q(parent) q(parent)
{ {
} }
KFileMetaDataProvider::Private::~Private() KFileMetaDataProvider::Private::~Private()
{ {
qDeleteAll(m_metaDataReaders);
} }
void KFileMetaDataProvider::Private::slotLoadingFinished() void KFileMetaDataProvider::Private::readMetadata()
{ {
KFileMetaDataReader* finishedMetaDataReader = qobject_cast<KFileMetaDataReader*>(q->sender()); #warning implement multi-URL metadata support
// The process that has emitted the finished() signal if (m_urls.count() > 1) {
// will get deleted and removed from m_metaDataReaders. kWarning() << "the API does not handle multile URLs metadata";
for (int i = 0; i < m_metaDataReaders.count(); ++i) { }
KFileMetaDataReader* metaDataReader = m_metaDataReaders[i]; const QString path = m_urls.first().toLocalFile();
if (metaDataReader == finishedMetaDataReader) { KFileMetaInfo metaInfo(path, KFileMetaInfo::Fastest);
m_metaDataReaders.removeAt(i); const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items();
if (metaDataReader != m_latestMetaDataReader) { foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) {
// Ignore data of older processs, as the data got const QString uriString = metaInfoItem.name();
// obsolete by m_latestMetaDataReader. const QVariant value = metaInfoItem.value();
metaDataReader->deleteLater(); m_data.insert(uriString, value);
return;
}
}
} }
m_data = m_latestMetaDataReader->metaData();
m_latestMetaDataReader->deleteLater();
if (m_fileItems.count() == 1) { if (m_fileItems.count() == 1) {
// TODO: Handle case if remote URLs are used properly. isDir() does // TODO: Handle case if remote URLs are used properly. isDir() does
@ -214,10 +151,11 @@ QList<QString> KFileMetaDataProvider::Private::resourceList() const
QWidget* KFileMetaDataProvider::Private::createValueWidget(const QString& value, QWidget* parent) QWidget* KFileMetaDataProvider::Private::createValueWidget(const QString& value, QWidget* parent)
{ {
ValueWidget* valueWidget = new ValueWidget(parent); QLabel* valueWidget = new QLabel(parent);
valueWidget->setWordWrap(true); valueWidget->setWordWrap(true);
valueWidget->setAlignment(Qt::AlignTop | Qt::AlignLeft); 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))); connect(valueWidget, SIGNAL(linkActivated(QString)), q, SLOT(slotLinkActivated(QString)));
return valueWidget; return valueWidget;
} }
@ -242,18 +180,14 @@ void KFileMetaDataProvider::setItems(const KFileItemList& items)
} }
Q_PRIVATE_SLOT(d,void slotDataChangeStarted()) Q_PRIVATE_SLOT(d,void slotDataChangeStarted())
Q_PRIVATE_SLOT(d,void slotDataChangeFinished()) Q_PRIVATE_SLOT(d,void slotDataChangeFinished())
QList<KUrl> urls; d->m_urls.clear();
foreach (const KFileItem& item, items) { foreach (const KFileItem& item, items) {
const KUrl url = item.url(); const KUrl url = item.url();
if (url.isValid()) { if (url.isValid()) {
urls.append(url); d->m_urls.append(url);
} }
} }
d->readMetadata();
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();
} }
QString KFileMetaDataProvider::label(const KUrl& metaDataUri) const QString KFileMetaDataProvider::label(const KUrl& metaDataUri) const

View file

@ -36,10 +36,9 @@ class QWidget;
/** /**
* @brief Provides the data for the KMetaDataWidget. * @brief Provides the data for the KMetaDataWidget.
* *
* The default implementation provides all meta data * The default implementation provides all meta data that are available due to
* that are available due to Strigi and Nepomuk. If custom * Strigi. If custom meta data should be added, the method
* meta data should be added, the method KFileMetaDataProvider::loadData() * KFileMetaDataProvider::loadData() must be overwritten.
* must be overwritten.
* *
* @see KFileMetaDataWidget * @see KFileMetaDataWidget
*/ */
@ -118,7 +117,6 @@ private:
class Private; class Private;
Private* const d; Private* const d;
Q_PRIVATE_SLOT(d, void slotLoadingFinished())
Q_PRIVATE_SLOT(d, void slotLinkActivated(const QString&)) Q_PRIVATE_SLOT(d, void slotLinkActivated(const QString&))
friend class KLoadMetaDataThread; // invokes KMetaDataObject::loadData() friend class KLoadMetaDataThread; // invokes KMetaDataObject::loadData()

View file

@ -1,64 +0,0 @@
/*****************************************************************************
* Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
* *
* 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 <kfilemetainfo.h>
#include <kdebug.h>
#include <klocale.h>
KFileMetaDataReader::KFileMetaDataReader(const QList<KUrl>& 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<QString, KFileMetaInfoItem> 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<KUrl, QVariant> KFileMetaDataReader::metaData() const
{
return m_metaData;
}
#include "moc_kfilemetadatareader_p.cpp"

View file

@ -1,79 +0,0 @@
/*****************************************************************************
* Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
* *
* 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 <kurl.h>
#include <QtCore/QHash>
#include <QtCore/QList>
#include <QtCore/QObject>
#include <QtCore/QProcess>
#include <QtCore/QString>
/**
* @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<KUrl>& 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<KUrl, QVariant> 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<KUrl> m_urls;
QHash<KUrl, QVariant> m_metaData;
};
#endif