File indexing completed on 2024-12-15 03:44:59

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "aggregationelement.h"
0008 #include "product.h"
0009 #include "util.h"
0010 
0011 #include <QJsonArray>
0012 #include <QJsonObject>
0013 
0014 using namespace KUserFeedback::Console;
0015 
0016 static const struct {
0017     AggregationElement::Type type;
0018     const char *name;
0019 } aggregation_element_types_table[] {
0020     { AggregationElement::Value, "value" },
0021     { AggregationElement::Size, "size" }
0022 };
0023 
0024 AggregationElement::AggregationElement() = default;
0025 AggregationElement::~AggregationElement() = default;
0026 
0027 bool AggregationElement::isValid() const
0028 {
0029     return !m_entry.name().isEmpty();
0030 }
0031 
0032 SchemaEntry AggregationElement::schemaEntry() const
0033 {
0034     return m_entry;
0035 }
0036 
0037 void AggregationElement::setSchemaEntry(const SchemaEntry& entry)
0038 {
0039     m_entry = entry;
0040 }
0041 
0042 SchemaEntryElement AggregationElement::schemaEntryElement() const
0043 {
0044     return m_element;
0045 }
0046 
0047 void AggregationElement::setSchemaEntryElement(const SchemaEntryElement& element)
0048 {
0049     m_element = element;
0050 }
0051 
0052 AggregationElement::Type AggregationElement::type() const
0053 {
0054     return m_type;
0055 }
0056 
0057 void AggregationElement::setType(AggregationElement::Type t)
0058 {
0059     m_type = t;
0060 }
0061 
0062 QString AggregationElement::displayString() const
0063 {
0064     switch (m_type) {
0065         case Value:
0066            return m_entry.name() + QLatin1Char('.') + m_element.name();
0067         case Size:
0068             return m_entry.name() + QLatin1String("[size]");
0069     }
0070     Q_UNREACHABLE();
0071 }
0072 
0073 bool AggregationElement::operator==(const AggregationElement &other) const
0074 {
0075     if (m_type != other.m_type)
0076         return false;
0077 
0078     switch (m_type) {
0079         case Value:
0080             return m_element.name() == other.m_element.name() && m_entry.name() == other.m_entry.name();
0081         case Size:
0082             return m_element.name() == other.m_element.name();
0083     }
0084     Q_UNREACHABLE();
0085 }
0086 
0087 QJsonObject AggregationElement::toJsonObject() const
0088 {
0089     QJsonObject obj;
0090     obj.insert(QStringLiteral("type"), QLatin1String(aggregation_element_types_table[m_type].name));
0091     switch (m_type) {
0092         case Value:
0093             obj.insert(QStringLiteral("schemaEntry"), m_entry.name());
0094             obj.insert(QStringLiteral("schemaEntryElement"), m_element.name());
0095             break;
0096         case Size:
0097             obj.insert(QStringLiteral("schemaEntry"), m_entry.name());
0098             break;
0099     }
0100     return obj;
0101 }
0102 
0103 QVector<AggregationElement> AggregationElement::fromJson(const Product &product, const QJsonArray& a)
0104 {
0105     QVector<AggregationElement> elems;
0106     elems.reserve(a.size());
0107     for (const auto &v : a) {
0108         if (!v.isObject())
0109             continue;
0110         const auto obj = v.toObject();
0111 
0112         AggregationElement e;
0113         e.setType(Util::stringToEnum<AggregationElement::Type>(obj.value(QLatin1String("type")).toString(), aggregation_element_types_table));
0114         switch (e.type()) {
0115             case Value:
0116                 e.setSchemaEntry(product.schemaEntry(obj.value(QLatin1String("schemaEntry")).toString()));
0117                 e.setSchemaEntryElement(e.schemaEntry().element(obj.value(QLatin1String("schemaEntryElement")).toString()));
0118                 break;
0119             case Size:
0120                 e.setSchemaEntry(product.schemaEntry(obj.value(QLatin1String("schemaEntry")).toString()));
0121                 break;
0122         }
0123         elems.push_back(e);
0124     }
0125     return elems;
0126 }