KFileMetaData

externalwriter.cpp
1 /*
2  * This file is part of the KFileMetaData project
3  * Copyright (C) 2016 Varun Joshi <[email protected]>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) version 3, or any
9  * later version accepted by the membership of KDE e.V. (or its
10  * successor approved by the membership of KDE e.V.), which shall
11  * act as a proxy defined in Section 6 of version 3 of the license.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include "externalwriter.h"
24 #include "properties.h"
25 #include "propertyinfo.h"
26 #include "kfilemetadata_debug.h"
27 
28 #include <QDir>
29 #include <QProcess>
30 #include <QJsonDocument>
31 #include <QJsonObject>
32 #include <QJsonArray>
33 
34 #define WRITER_TIMEOUT_MS 30000
35 
36 using namespace KFileMetaData;
37 
38 class Q_DECL_HIDDEN ExternalWriter::ExternalWriterPrivate
39 {
40 public:
41  QString path;
42  QStringList writeMimetypes;
43  QString mainPath;
44 };
45 
46 
47 ExternalWriter::ExternalWriter(QObject* parent)
48  : WriterPlugin(parent),
49  d_ptr(new ExternalWriterPrivate)
50 {
51 }
52 
53 ExternalWriter::~ExternalWriter()
54 {
55  delete d_ptr;
56 }
57 
58 ExternalWriter::ExternalWriter(const QString& pluginPath)
59  : WriterPlugin(new QObject()),
60  d_ptr(new ExternalWriterPrivate)
61 {
62  Q_D(ExternalWriter);
63  d->path = pluginPath;
64 
65  QDir pluginDir(pluginPath);
66  QStringList pluginDirContents = pluginDir.entryList();
67 
68  if (!pluginDirContents.contains(QStringLiteral("manifest.json"))) {
69  qCDebug(KFILEMETADATA_LOG) << "Path does not seem to contain a valid plugin";
70  return;
71  }
72 
73  QFile manifest(pluginDir.filePath(QStringLiteral("manifest.json")));
74  manifest.open(QIODevice::ReadOnly);
75  QJsonDocument manifestDoc = QJsonDocument::fromJson(manifest.readAll());
76  if (!manifestDoc.isObject()) {
77  qCDebug(KFILEMETADATA_LOG) << "Manifest does not seem to be a valid JSON Object";
78  return;
79  }
80 
81  QJsonObject rootObject = manifestDoc.object();
82  const QJsonArray mimetypesArray = rootObject.value(QStringLiteral("mimetypes")).toArray();
83  QStringList mimetypes;
84  for (const QJsonValue &mimetype : mimetypesArray) {
85  mimetypes << mimetype.toString();
86  }
87 
88  d->writeMimetypes.append(mimetypes);
89  d->mainPath = pluginDir.filePath(rootObject[QStringLiteral("main")].toString());
90 }
91 
92 QStringList ExternalWriter::writeMimetypes() const
93 {
94  Q_D(const ExternalWriter);
95  return d->writeMimetypes;
96 }
97 
98 void ExternalWriter::write(const WriteData& data)
99 {
100  Q_D(ExternalWriter);
101  QJsonDocument writeData;
102  QJsonObject rootObject;
103  QJsonObject propertiesObject;
104  QByteArray output;
105  QByteArray errorOutput;
106 
107  const QMap<Property::Property, QVariant> properties = data.getAllProperties();
108 
110  while (i != properties.constEnd()) {
111  PropertyInfo propertyInfo(i.key());
112  propertiesObject[propertyInfo.name()] = QJsonValue::fromVariant(properties[i.key()]);
113  }
114 
115  rootObject[QStringLiteral("path")] = QJsonValue(data.inputUrl());
116  rootObject[QStringLiteral("mimetype")] = data.inputMimetype();
117  rootObject[QStringLiteral("properties")] = propertiesObject;
118  writeData.setObject(rootObject);
119 
120  QProcess writerProcess;
121  writerProcess.start(d->mainPath, QStringList(), QIODevice::ReadWrite);
122  writerProcess.write(writeData.toJson());
123  writerProcess.closeWriteChannel();
124  writerProcess.waitForFinished(WRITER_TIMEOUT_MS);
125 
126  errorOutput = writerProcess.readAllStandardError();
127 
128  if (writerProcess.exitStatus()) {
129  qCDebug(KFILEMETADATA_LOG) << "Something went wrong while trying to write data";
130  qCDebug(KFILEMETADATA_LOG) << errorOutput;
131  return;
132  }
133 
134  output = writerProcess.readAll();
135 
136  QJsonDocument writerExitData = QJsonDocument::fromJson(output);
137  if (!writerExitData.isObject()) {
138  return;
139  }
140  QJsonObject outputRootObject = writerExitData.object();
141 
142  if (outputRootObject[QStringLiteral("status")].toString() != QStringLiteral("OK")) {
143  qCDebug(KFILEMETADATA_LOG) << outputRootObject[QStringLiteral("error")].toString();
144  qCDebug(KFILEMETADATA_LOG) << errorOutput;
145  }
146 
147 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
The PropertyInfo class can be used to obtain extra information about any property.
Definition: propertyinfo.h:41
QByteArray toJson() const const
QJsonObject object() const const
QMap::const_iterator constBegin() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool isObject() const const
QJsonValue fromVariant(const QVariant &variant)
KGuiItem properties()
QJsonArray toArray() const const
QMap::const_iterator constEnd() const const
QByteArray readAll()
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
virtual bool open(QIODevice::OpenMode mode) override
<KFileMetaData/Properties>
const Key key(const T &value, const Key &defaultKey) const const
char * toString(const T &value)
qint64 write(const char *data, qint64 maxSize)
void setObject(const QJsonObject &object)
QJsonValue value(const QString &key) const const
void closeWriteChannel()
QProcess::ExitStatus exitStatus() const const
void start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode)
QByteArray readAllStandardError()
bool waitForFinished(int msecs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 22:55:38 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.