1/*
2 SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "helper.h"
8#include <QDebug>
9#include <QDir>
10#include <QFile>
11#include <QFileInfo>
12#include <QJsonDocument>
13#include <QStandardPaths>
14
15using namespace Purpose;
16
17QJsonObject Purpose::readPluginType(const QString &pluginType)
18{
19 const QString lookup = QStringLiteral("purpose/types/") + pluginType + QStringLiteral("PluginType.json");
20
21 QString path = QStringLiteral(":/") + lookup;
22 if (!QFileInfo::exists(file: path)) {
23 path = QStandardPaths::locate(type: QStandardPaths::GenericDataLocation, fileName: lookup);
24 if (path.isEmpty()) {
25 qWarning() << "Couldn't find" << lookup << QStandardPaths::standardLocations(type: QStandardPaths::GenericDataLocation);
26 return QJsonObject();
27 }
28 }
29 QFile typeFile(path);
30 if (!typeFile.open(flags: QFile::ReadOnly)) {
31 qWarning() << "Couldn't open" << lookup;
32 return QJsonObject();
33 }
34
35 QJsonParseError error;
36 QJsonDocument doc = QJsonDocument::fromJson(json: typeFile.readAll(), error: &error);
37 if (error.error) {
38 qWarning() << "JSON error in " << path << error.offset << ":" << error.errorString();
39 return QJsonObject();
40 }
41
42 Q_ASSERT(doc.isObject());
43 return doc.object();
44}
45
46KPluginMetaData Purpose::createMetaData(const QString &file)
47{
48 const QFileInfo fi(file);
49 const QString fileName = fi.absoluteFilePath();
50 QJsonObject metaData;
51
52 QFile f(fileName);
53 if (f.open(flags: QIODevice::ReadOnly)) {
54 const QJsonDocument doc = QJsonDocument::fromJson(json: f.readAll());
55 metaData = doc.object();
56 }
57
58 QDir dir = fi.dir().filePath(QStringLiteral("contents/code"));
59 QStringList mainFile = dir.entryList(nameFilters: {QStringLiteral("main.*")}, filters: QDir::Files);
60 if (mainFile.isEmpty()) {
61 qWarning() << "no main file for" << file;
62 return KPluginMetaData();
63 }
64
65 auto info = KPluginMetaData(metaData, dir.absoluteFilePath(fileName: mainFile.first()));
66 Q_ASSERT(info.isValid() && !info.rawData().isEmpty());
67 return info;
68}
69

source code of purpose/src/helper.cpp