1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "kit.h"
6
7#include <QUuid>
8
9const char ID_KEY[] = "PE.Profile.Id";
10const char DISPLAYNAME_KEY[] = "PE.Profile.Name";
11const char DATA_KEY[] = "PE.Profile.Data";
12
13class KitPrivate
14{
15public:
16 explicit KitPrivate(QString _id)
17 : id(_id)
18 {
19 if (id.isEmpty())
20 id = QUuid::createUuid().toString();
21 }
22
23 QString id;
24 QString unexpandedDisplayName;
25
26 QHash<QString, QVariant> data;
27
28 QString defaultOutput;
29};
30
31Kit::Kit(QString id, QObject *parent) : QObject(parent)
32 , d(std::make_unique<KitPrivate>(id))
33{
34}
35
36QString fromSetting(const QVariant &variant)
37{
38 const QByteArray ba = variant.toString().toUtf8();
39 if (ba.isEmpty())
40 return {};
41 return ba;
42}
43
44QString fromString(const QString &name)
45{
46 if (name.isEmpty())
47 return {};
48 return name.toUtf8();
49}
50
51Kit::Kit(const QVariantMap &data)
52 : d(std::make_unique<KitPrivate>(""))
53{
54 d->id = fromSetting(data.value(QLatin1String(ID_KEY)));
55
56 d->unexpandedDisplayName = data.value(QLatin1String(DISPLAYNAME_KEY),
57 d->unexpandedDisplayName).toString();
58
59 QVariantMap extra = data.value(QLatin1String(DATA_KEY)).toMap();
60 d->data.clear(); // remove default values
61 const QVariantMap::ConstIterator cend = extra.constEnd();
62 for (QVariantMap::ConstIterator it = extra.constBegin(); it != cend; ++it)
63 d->data.insert(fromString(it.key()), it.value());
64}
65
66QString Kit::displayName() const
67{
68 return d->unexpandedDisplayName;
69}
70
71void Kit::setUnexpandedDisplayName(const QString &name)
72{
73 if (d->unexpandedDisplayName == name)
74 return;
75
76 d->unexpandedDisplayName = name;
77}
78
79QString Kit::id() const
80{
81 return d->id;
82}
83
84QList<QString> Kit::allKeys() const
85{
86 return d->data.keys();
87}
88
89QVariant Kit::value(QString key, const QVariant &unset) const
90{
91 return d->data.value(key, unset);
92}
93
94bool Kit::hasValue(QString key) const
95{
96 return d->data.contains(key);
97}
98
99void Kit::setValue(QString key, const QVariant &value)
100{
101 if (d->data.value(key) == value)
102 return;
103 d->data.insert(key, value);
104}
105
106void Kit::setDefaultOutput(QString &defaultOutput)
107{
108 d->defaultOutput = defaultOutput;
109}
110
111const QString &Kit::getDefaultOutput() const
112{
113 return d->defaultOutput;
114}
115
116void Kit::copyFrom(const Kit &k)
117{
118 d->data = k.d->data;
119 d->unexpandedDisplayName = k.d->unexpandedDisplayName;
120 d->defaultOutput = k.d->defaultOutput;
121}
122
123Kit::Kit(const Kit &other)
124{
125 copyFrom(other);
126}
127
128Kit &Kit::operator=(const Kit &other)
129{
130 copyFrom(other);
131 return *this;
132}
133
134QVariantMap Kit::toMap() const
135{
136 using IdVariantConstIt = QHash<QString, QVariant>::ConstIterator;
137
138 QVariantMap data;
139 data.insert(QLatin1String(ID_KEY), d->id);
140 data.insert(QLatin1String(DISPLAYNAME_KEY), d->unexpandedDisplayName);
141
142 QVariantMap extra;
143
144 const IdVariantConstIt cend = d->data.constEnd();
145 for (IdVariantConstIt it = d->data.constBegin(); it != cend; ++it)
146 extra.insert(it.key(), it.value());
147 data.insert(QLatin1String(DATA_KEY), extra);
148
149 return data;
150}
151
152Kit::~Kit() = default;
153