Purpose

helper.cpp
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(path)) {
24 if (path.isEmpty()) {
25 qWarning() << "Couldn't find" << lookup << QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
26 return QJsonObject();
27 }
28 }
29 QFile typeFile(path);
30 if (!typeFile.open(QFile::ReadOnly)) {
31 qWarning() << "Couldn't open" << lookup;
32 return QJsonObject();
33 }
34
36 QJsonDocument doc = QJsonDocument::fromJson(typeFile.readAll(), &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(QIODevice::ReadOnly)) {
54 const QJsonDocument doc = QJsonDocument::fromJson(f.readAll());
55 metaData = doc.object();
56 }
57
58 QDir dir = fi.dir().filePath(QStringLiteral("contents/code"));
59 QStringList mainFile = dir.entryList({QStringLiteral("main.*")}, 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(mainFile.first()));
66 Q_ASSERT(info.isValid() && !info.rawData().isEmpty());
67 return info;
68}
QString path(const QString &relativePath)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KIOCORE_EXPORT QString dir(const QString &fileClass)
bool exists() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isObject() const const
QJsonObject object() const const
T & first()
bool isEmpty() const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QStringList standardLocations(StandardLocation type)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.