KFileMetaData

externalwriter.cpp
1/*
2 This file is part of the KFileMetaData project
3 SPDX-FileCopyrightText: 2016 Varun Joshi <varunj.1011@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "externalwriter.h"
9#include "properties.h"
10#include "propertyinfo.h"
11#include "kfilemetadata_debug.h"
12
13#include <QDir>
14#include <QProcess>
15#include <QJsonDocument>
16#include <QJsonObject>
17#include <QJsonArray>
18
19#define WRITER_TIMEOUT_MS 30000
20
21using namespace KFileMetaData;
22
23class KFileMetaData::ExternalWriterPrivate
24{
25public:
26 QString path;
27 QStringList writeMimetypes;
28 QString mainPath;
29};
30
31
32ExternalWriter::ExternalWriter(QObject* parent)
33 : WriterPlugin(parent),
34 d_ptr(new ExternalWriterPrivate)
35{
36}
37
38ExternalWriter::ExternalWriter(const QString& pluginPath)
39 : WriterPlugin(nullptr),
40 d_ptr(new ExternalWriterPrivate)
41{
42 Q_D(ExternalWriter);
43 d->path = pluginPath;
44
45 QDir pluginDir(pluginPath);
46 QStringList pluginDirContents = pluginDir.entryList();
47
48 if (!pluginDirContents.contains(QStringLiteral("manifest.json"))) {
49 qCDebug(KFILEMETADATA_LOG) << "Path does not seem to contain a valid plugin";
50 return;
51 }
52
53 QFile manifest(pluginDir.filePath(QStringLiteral("manifest.json")));
54 manifest.open(QIODevice::ReadOnly);
55 QJsonDocument manifestDoc = QJsonDocument::fromJson(manifest.readAll());
56 if (!manifestDoc.isObject()) {
57 qCDebug(KFILEMETADATA_LOG) << "Manifest does not seem to be a valid JSON Object";
58 return;
59 }
60
61 QJsonObject rootObject = manifestDoc.object();
62 const QJsonArray mimetypesArray = rootObject.value(QStringLiteral("mimetypes")).toArray();
63 QStringList mimetypes;
64 for (const QJsonValue &mimetype : mimetypesArray) {
65 mimetypes << mimetype.toString();
66 }
67
68 d->writeMimetypes.append(mimetypes);
69 d->mainPath = pluginDir.absoluteFilePath(rootObject[QStringLiteral("main")].toString());
70}
71
72ExternalWriter::~ExternalWriter() = default;
73
74QStringList ExternalWriter::writeMimetypes() const
75{
76 Q_D(const ExternalWriter);
77 return d->writeMimetypes;
78}
79
80void ExternalWriter::write(const WriteData& data)
81{
82 Q_D(ExternalWriter);
83 QJsonDocument writeData;
84 QJsonObject rootObject;
85 QJsonObject propertiesObject;
86 QByteArray output;
87 QByteArray errorOutput;
88
90
91 for (auto i = properties.constBegin(); i != properties.constEnd(); ++i) {
92 PropertyInfo propertyInfo(i.key());
93 propertiesObject[propertyInfo.name()] = QJsonValue::fromVariant(i.value());
94 }
95
96 rootObject[QStringLiteral("path")] = QJsonValue(data.inputUrl());
97 rootObject[QStringLiteral("mimetype")] = data.inputMimetype();
98 rootObject[QStringLiteral("properties")] = propertiesObject;
99 writeData.setObject(rootObject);
100
101 QProcess writerProcess;
102 writerProcess.start(d->mainPath, QStringList(), QIODevice::ReadWrite);
103 writerProcess.write(writeData.toJson());
104 writerProcess.closeWriteChannel();
105 writerProcess.waitForFinished(WRITER_TIMEOUT_MS);
106
107 errorOutput = writerProcess.readAllStandardError();
108
109 if (writerProcess.exitStatus()) {
110 qCDebug(KFILEMETADATA_LOG) << "Something went wrong while trying to write data";
111 qCDebug(KFILEMETADATA_LOG) << errorOutput;
112 return;
113 }
114
115 output = writerProcess.readAll();
116
117 QJsonDocument writerExitData = QJsonDocument::fromJson(output);
118 if (!writerExitData.isObject()) {
119 return;
120 }
121 QJsonObject outputRootObject = writerExitData.object();
122
123 if (outputRootObject[QStringLiteral("status")].toString() != QStringLiteral("OK")) {
124 qCDebug(KFILEMETADATA_LOG) << outputRootObject[QStringLiteral("error")].toString();
125 qCDebug(KFILEMETADATA_LOG) << errorOutput;
126 }
127
128}
129
130#include "moc_externalwriter.cpp"
The PropertyInfo class can be used to obtain extra information about any property.
The WriteData class stores all the data to be written to a file.
Definition writedata.h:30
PropertyMultiMap properties() const
Definition writedata.cpp:85
The WriterPlugin is the base class for all file metadata writers.
char * toString(const EngineQuery &query)
The KFileMetaData namespace.
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
KGuiItem properties()
QByteArray readAll()
qint64 write(const QByteArray &data)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isObject() const const
QJsonObject object() const const
void setObject(const QJsonObject &object)
QByteArray toJson(JsonFormat format) const const
QJsonValue value(QLatin1StringView key) const const
QJsonValue fromVariant(const QVariant &variant)
QJsonArray toArray() const const
void append(QList< T > &&value)
void closeWriteChannel()
QProcess::ExitStatus exitStatus() const const
QByteArray readAllStandardError()
void start(OpenMode mode)
bool waitForFinished(int msecs)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 11:54:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.