Commit 064f4f2b authored by Stefan Brüns's avatar Stefan Brüns

Split handling of on-demand extractor process from IndexedDataRetriever

Summary:
To allow a unification of the IndexedDataRetriever job and FileFetchJob,
move the process handling into a separate class and only keep
the UserMetaData handling.

As a small optimization, interleave the forking of the external process,
UserMetaData retrieval and joining of the external process. Also reuse
the QFileInfo in the constructor to determine if the file is writable and
thus has editable metadata.

Beyond the optimization, no user visible changes.

Depends on D25247

Test Plan:
When using a location not indexed by baloo, metadata is
still shown as before

Reviewers: #baloo, ngraham, astippich

Reviewed By: #baloo, ngraham, astippich

Tags: #baloo

Differential Revision: https://phabricator.kde.org/D25248
parent bb533b2d
......@@ -16,6 +16,7 @@ set(widgets_SRCS
kcommentwidget.cpp
keditcommentdialog.cpp
metadatafilter.cpp
ondemandextractor.cpp
widgetfactory.cpp
filefetchjob.cpp
)
......
......@@ -20,19 +20,18 @@
#include "indexeddataretriever.h"
#include "filemetadatautil_p.h"
#include "widgetsdebug.h"
#include "extractorutil_p.h"
#include <QDataStream>
#include <QProcess>
#include <QFileInfo>
#include <QStandardPaths>
using namespace Baloo;
IndexedDataRetriever::IndexedDataRetriever(const QString& fileUrl, QObject* parent): KJob(parent)
{
m_url = QFileInfo(fileUrl).canonicalFilePath();
QFileInfo fileInfo(fileUrl);
m_url = fileInfo.canonicalFilePath();
m_canEdit = fileInfo.isWritable();
connect(&m_extractor, &Private::OnDemandExtractor::fileFinished, this, &IndexedDataRetriever::slotIndexedFile);
}
IndexedDataRetriever::~IndexedDataRetriever()
......@@ -41,34 +40,23 @@ IndexedDataRetriever::~IndexedDataRetriever()
void IndexedDataRetriever::start()
{
const QString exe = QStandardPaths::findExecutable(QLatin1String("baloo_filemetadata_temp_extractor"));
m_process = new QProcess(this);
m_process->setReadChannel(QProcess::StandardOutput);
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &IndexedDataRetriever::slotIndexedFile);
m_process->start(exe, QStringList() << m_url);
}
void IndexedDataRetriever::slotIndexedFile(int exitCode, QProcess::ExitStatus exitStatus)
{
if (exitStatus == QProcess::CrashExit) {
qCWarning(WIDGETS) << "Extractor crashed when processing" << m_url;
}
QByteArray data = m_process->readAllStandardOutput();
QDataStream in(&data, QIODevice::ReadOnly);
KFileMetaData::PropertyMap properties;
in >> properties;
m_data = Baloo::Private::toNamedVariantMap(properties);
m_extractor.process(m_url);
KFileMetaData::UserMetaData umd(m_url);
if (umd.isSupported()) {
m_canEdit = QFileInfo(m_url).isWritable();
m_data = Baloo::Private::convertUserMetaData(umd);
m_canEdit &= true;
} else {
m_data.clear();
m_canEdit &= false;
}
}
QVariantMap attributes = Baloo::Private::convertUserMetaData(umd);
m_data.unite(attributes);
void IndexedDataRetriever::slotIndexedFile(QProcess::ExitStatus status)
{
if (status == QProcess::NormalExit) {
QVariantMap properties = Baloo::Private::toNamedVariantMap(m_extractor.properties());
m_data.unite(properties);
}
emitResult();
......
......@@ -22,7 +22,7 @@
#define INDEXEDDATARETRIEVER_H
#include <KJob>
#include <QProcess>
#include "ondemandextractor.h"
#include <QVariant>
namespace Baloo {
......@@ -40,11 +40,11 @@ public:
bool canEdit() const;
private Q_SLOTS:
void slotIndexedFile(int exitCode, QProcess::ExitStatus exitStatus);
void slotIndexedFile(QProcess::ExitStatus exitStatus);
private:
QString m_url;
QProcess* m_process;
Private::OnDemandExtractor m_extractor;
QVariantMap m_data;
bool m_canEdit = false;
};
......
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright (C) 2019 Stefan Brüns <stefan.bruens@rwth-aachen.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "ondemandextractor.h"
#include "widgetsdebug.h"
#include "extractorutil_p.h"
#include <QDataStream>
#include <QStandardPaths>
namespace Baloo {
namespace Private {
OnDemandExtractor::OnDemandExtractor(QObject* parent) : QObject(parent)
{
}
OnDemandExtractor::~OnDemandExtractor()
{
}
void OnDemandExtractor::process(const QString& filePath)
{
const QString exe = QStandardPaths::findExecutable(QLatin1String("baloo_filemetadata_temp_extractor"));
m_process.setReadChannel(QProcess::StandardOutput);
connect(&m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &OnDemandExtractor::slotIndexedFile);
m_process.start(exe, QStringList{ filePath });
}
void OnDemandExtractor::slotIndexedFile(int, QProcess::ExitStatus exitStatus)
{
if (exitStatus == QProcess::CrashExit) {
qCWarning(WIDGETS) << "Extractor crashed when processing" << m_process.arguments();
emit fileFinished(exitStatus);
return;
}
QByteArray data = m_process.readAllStandardOutput();
QDataStream in(&data, QIODevice::ReadOnly);
m_properties.clear();
in >> m_properties;
emit fileFinished(QProcess::NormalExit);
}
KFileMetaData::PropertyMap OnDemandExtractor::properties() const
{
return m_properties;
}
} // namespace Private
} // namespace Baloo
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright (C) 2019 Stefan Brüns <stefan.bruens@rwth-aachen.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef ONDEMANDEXTRACTOR_H
#define ONDEMANDEXTRACTOR_H
#include <QProcess>
#include <KFileMetaData/Properties>
namespace Baloo {
namespace Private {
class OnDemandExtractor : public QObject
{
Q_OBJECT
public:
explicit OnDemandExtractor(QObject* parent = nullptr);
~OnDemandExtractor();
void process(const QString& filePath);
KFileMetaData::PropertyMap properties() const;
Q_SIGNALS:
void fileFinished(QProcess::ExitStatus exitStatus);
private Q_SLOTS:
void slotIndexedFile(int exitCode, QProcess::ExitStatus exitStatus);
private:
QProcess m_process;
KFileMetaData::PropertyMap m_properties;
};
} // namespace Private
} // namespace Baloo
#endif // ONDEMANDEXTRACTOR_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment