File indexing completed on 2025-01-05 05:23:46
0001 /* 0002 This file is part of the Okteta Kasten Framework, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2022 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "structureenabledlist.hpp" 0010 0011 // Qt 0012 #include <QRegularExpression> 0013 #include <QStringList> 0014 0015 namespace { 0016 inline QString wildCard() { return QStringLiteral("*"); } 0017 } 0018 0019 void StructureEnabledList::setEnabledStructures(const QStringList& enabledStructures) 0020 { 0021 m_enabledList.clear(); 0022 0023 QRegularExpression regex(QStringLiteral("'(.+)':'(.+)'")); 0024 m_enabledList.reserve(enabledStructures.size()); 0025 for (const QString& expression : enabledStructures) { 0026 QRegularExpressionMatch match = regex.match(expression); 0027 if (match.hasMatch()) { 0028 const QString id = match.captured(1); 0029 const QString structure = match.captured(2); 0030 m_enabledList.append(StructureEnabledData(id, structure)); 0031 } 0032 } 0033 } 0034 0035 void StructureEnabledList::removeStructures(const QHash<QString, QStringList>& structures) 0036 { 0037 auto it = m_enabledList.begin(); 0038 while (it != m_enabledList.end()) { 0039 auto idIt = structures.constFind(it->id); 0040 bool remove = false; 0041 if (idIt != structures.constEnd()) { 0042 if ((it->structure != ::wildCard()) && !idIt.value().contains(it->structure)) { 0043 remove = true; 0044 } 0045 } else { 0046 remove = true; 0047 } 0048 if (remove) { 0049 it = m_enabledList.erase(it); 0050 } else { 0051 ++it; 0052 } 0053 } 0054 } 0055 0056 void StructureEnabledList::setEnabled(const QString& id, bool isEnabled) 0057 { 0058 bool isListed = false; 0059 for (StructureEnabledData& data : m_enabledList) { 0060 if (data.id == id) { 0061 isListed = true; 0062 data.isEnabled = isEnabled; 0063 } 0064 } 0065 if (!isListed && isEnabled) { 0066 m_enabledList.append(StructureEnabledData(id, {wildCard()})); 0067 } 0068 } 0069 0070 bool StructureEnabledList::isEnabled(const QString& id) const 0071 { 0072 return std::any_of(m_enabledList.begin(), m_enabledList.end(), [&id](const StructureEnabledData& data) { 0073 return (data.id == id) && data.isEnabled; 0074 }); 0075 } 0076 0077 QStringList StructureEnabledList::toQStringList() const 0078 { 0079 const QString expressionTemplate = QStringLiteral("\'%1\':\'%2\'"); 0080 0081 QStringList enabledStructures; 0082 for (const StructureEnabledData& data : m_enabledList) { 0083 if (data.isEnabled) { 0084 enabledStructures.append(expressionTemplate.arg(data.id, data.structure)); 0085 } 0086 } 0087 return enabledStructures; 0088 }