KFileMetaData

externalwriter.cpp
1 /*
2  This file is part of the KFileMetaData project
3  SPDX-FileCopyrightText: 2016 Varun Joshi <[email protected]>
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 
21 using namespace KFileMetaData;
22 
23 class KFileMetaData::ExternalWriterPrivate
24 {
25 public:
26  QString path;
27  QStringList writeMimetypes;
28  QString mainPath;
29 };
30 
31 
32 ExternalWriter::ExternalWriter(QObject* parent)
33  : WriterPlugin(parent),
34  d_ptr(new ExternalWriterPrivate)
35 {
36 }
37 
38 ExternalWriter::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 
72 ExternalWriter::~ExternalWriter() = default;
73 
74 QStringList ExternalWriter::writeMimetypes() const
75 {
76  Q_D(const ExternalWriter);
77  return d->writeMimetypes;
78 }
79 
80 void ExternalWriter::write(const WriteData& data)
81 {
82  Q_D(ExternalWriter);
83  QJsonDocument writeData;
85  QJsonObject propertiesObject;
86  QByteArray output;
87  QByteArray errorOutput;
88 
89  const PropertyMultiMap properties = data.properties();
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 }
void start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode)
QJsonObject object() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool waitForFinished(int msecs)
T value() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
char * toString(const T &value)
QProcess::ExitStatus exitStatus() const const
virtual QVariant rootObject()
bool isObject() const const
KGuiItem properties()
QByteArray readAllStandardError()
QByteArray toJson() const const
void setObject(const QJsonObject &object)
QByteArray readAll()
QJsonValue fromVariant(const QVariant &variant)
void closeWriteChannel()
Q_D(Todo)
qint64 write(const char *data, qint64 maxSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri May 27 2022 03:47:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.