File indexing completed on 2025-01-05 05:23:47

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 "structuresmanager.hpp"
0010 
0011 // tool
0012 #include "structureviewpreferences.hpp"
0013 #include "structuredefinitionfile.hpp"
0014 #include <structureslogging.hpp>
0015 // Qt
0016 #include <QStandardPaths>
0017 #include <QDir>
0018 // Std
0019 #include <algorithm>
0020 
0021 namespace Kasten {
0022 
0023 StructuresManager::StructuresManager(QObject* parent)
0024     : QObject(parent)
0025 {
0026     mConfig = KSharedConfig::openConfig(QStringLiteral("oktetastructuresrc"),
0027                                         KSharedConfig::FullConfig, QStandardPaths::ConfigLocation);
0028     reloadPaths();
0029 }
0030 
0031 StructuresManager::~StructuresManager()
0032 {
0033     qDeleteAll(mDefs);
0034 }
0035 
0036 void StructuresManager::reloadPaths()
0037 {
0038     qDeleteAll(mDefs);
0039     mDefs.clear();
0040     mLoadedFiles.clear();
0041     QStringList paths;
0042     const QStringList structuresDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation,
0043                                                                  QStringLiteral("okteta/structures"), QStandardPaths::LocateDirectory);
0044     for (const QString& structuresDir : structuresDirs) {
0045         const QStringList entries = QDir(structuresDir).entryList(QDir::Dirs);
0046         for (const QString& e : entries) {
0047             const QString structureBasePath = structuresDir + QLatin1Char('/') + e;
0048             const QStringList desktopFiles =
0049                 QDir(structureBasePath).entryList(QStringList(QStringLiteral("*.desktop")));
0050             for (const QString& desktopFile : desktopFiles) {
0051                 paths << structureBasePath + QLatin1Char('/') + desktopFile;
0052             }
0053         }
0054     }
0055 
0056     qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "found structures: " << paths;
0057 
0058     for (const QString& path : std::as_const(paths)) {
0059         StructureMetaData structure(path);
0060         addStructDef(structure);
0061     }
0062 }
0063 
0064 void StructuresManager::addStructDef(const StructureMetaData& metaData)
0065 {
0066     const QString id = metaData.id();
0067     if (mDefs.contains(id)) {
0068         qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "Skipping structure already loaded: " << id;
0069         return;
0070     }
0071 
0072     auto* def = new StructureDefinitionFile(metaData);
0073     mDefs.insert(id, def);
0074 }
0075 
0076 StructureDefinitionFile* StructuresManager::definition(const QString& pluginName) const
0077 {
0078     const auto definitionIt = mDefs.find(pluginName);
0079     if (definitionIt == mDefs.end()) {
0080         qCWarning(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "could not find structuredefinitionFile with name=" << pluginName;
0081         return nullptr;
0082     }
0083 
0084     return definitionIt.value();
0085 }
0086 
0087 QMap<QString, StructureDefinitionFile*> StructuresManager::structureDefs() const
0088 {
0089     return mDefs;
0090 }
0091 
0092 KSharedConfig::Ptr StructuresManager::config() const
0093 {
0094     return mConfig;
0095 }
0096 
0097 }
0098 
0099 #include "moc_structuresmanager.cpp"