File indexing completed on 2024-06-16 05:25:06

0001 /*
0002     This file is part of the Okteta Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2010, 2012 Alex Richardson <alex.richardson@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "structuredefinitionfile.hpp"
0010 
0011 #include <structureslogging.hpp>
0012 
0013 #include <QFile>
0014 #include <QDir>
0015 #include <QStringList>
0016 
0017 #include "datatypes/topleveldatainformation.hpp"
0018 
0019 #include "parsers/abstractstructureparser.hpp"
0020 #include "parsers/osdparser.hpp"
0021 #include "parsers/scriptfileparser.hpp"
0022 
0023 namespace Kasten {
0024 
0025 StructureDefinitionFile::StructureDefinitionFile(const StructureMetaData& metaData)
0026     : mMetaData(metaData)
0027 {
0028     const QFileInfo tmp(mMetaData.entryPath());
0029     const QString absoluteDir = tmp.absolutePath();
0030 
0031     const QString categoryId = mMetaData.categoryId();
0032     if (categoryId == QLatin1String("structure/js")) {
0033         const QString filename = absoluteDir + QLatin1String("/main.js");
0034         mParser.reset(new ScriptFileParser(mMetaData.id(), filename));
0035     } else if (categoryId == QLatin1String("structure")) {
0036         // by default use main.osd, only if it doesn't exist fall back to old behaviour
0037         QString filename = absoluteDir + QLatin1String("/main.osd");
0038         if (!QFile::exists(filename)) {
0039             filename = absoluteDir + QLatin1Char('/') + mMetaData.id() + QLatin1String(".osd");
0040         }
0041         mParser.reset(new OsdParser(mMetaData.id(), filename));
0042     } else {
0043         qCWarning(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "no valid parser found for plugin category '" << categoryId << "'";
0044     }
0045 }
0046 
0047 StructureDefinitionFile::~StructureDefinitionFile() = default;
0048 
0049 StructureMetaData StructureDefinitionFile::metaData() const
0050 {
0051     return mMetaData;
0052 }
0053 
0054 bool StructureDefinitionFile::isValid() const
0055 {
0056     return (mParser.get() != nullptr);
0057 }
0058 
0059 QVector<TopLevelDataInformation*> StructureDefinitionFile::structures() const
0060 {
0061     Q_CHECK_PTR(mParser);
0062     return mParser->parseStructures();
0063 }
0064 
0065 TopLevelDataInformation* StructureDefinitionFile::structure(const QString& name) const
0066 {
0067     Q_CHECK_PTR(mParser);
0068     const QVector<TopLevelDataInformation*> list = mParser->parseStructures();
0069     TopLevelDataInformation* ret = nullptr;
0070     for (auto* info : list) {
0071         if (info->actualDataInformation()->name() == name) {
0072             ret = info;
0073         } else {
0074             delete info; // we have no use for this element
0075         }
0076     }
0077 
0078     if (!ret) {
0079         qCWarning(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "could not find structure with name=" << name;
0080     }
0081     return ret; // not found
0082 }
0083 
0084 QStringList StructureDefinitionFile::structureNames() const
0085 {
0086     Q_CHECK_PTR(mParser);
0087     return mParser->parseStructureNames();
0088 }
0089 
0090 }