swift
Loading...
Searching...
No Matches
distributor.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QJsonValue>
7#include <QStringBuilder>
8#include <Qt>
9#include <QtGlobal>
10
11#include "misc/logcategories.h"
12#include "misc/statusmessage.h"
13#include "misc/stringutils.h"
14
15SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::simulation, CDistributor)
16
17namespace swift::misc::simulation
18{
19 CDistributor::CDistributor(const QString &key) { this->setDbKey(unifyKeyOrAlias(key)); }
20
21 CDistributor::CDistributor(const QString &id, const QString &description, const QString &alias1,
22 const QString &alias2, const CSimulatorInfo &simulator)
23 : m_description(description), m_alias1(alias1), m_alias2(alias2), m_simulator(simulator)
24 {
25 this->setDbKey(unifyKeyOrAlias(id));
26 }
27
29 {
30 if (!this->getDbKey().isEmpty() && !this->getDescription().isEmpty())
31 {
32 return this->getDbKey() % u' ' % this->getDescription();
33 }
34 if (!this->getDbKey().isEmpty())
35 {
36 return this->getDbKey();
37 }
38 return {};
39 }
40
41 bool CDistributor::matchesKeyOrAlias(const QString &keyOrAlias) const
42 {
43 QString s(keyOrAlias.trimmed().toUpper());
44 if (s.isEmpty())
45 {
46 return false;
47 }
48 return (getDbKey() == s || getAlias1() == s || getAlias2() == s);
49 }
50
51 bool CDistributor::matchesKeyOrAlias(const CDistributor &distributor) const
52 {
53 if (distributor.hasValidDbKey() && this->matchesKeyOrAlias(distributor.getDbKey()))
54 {
55 return true;
56 }
57 if (distributor.hasAlias1() && this->matchesKeyOrAlias(distributor.getAlias1()))
58 {
59 return true;
60 }
61 return (distributor.hasAlias2() && this->matchesKeyOrAlias(distributor.getAlias2()));
62 }
63
65 {
66 return m_simulator.matchesAny(simulator);
67 }
68
70 {
71 if (index.isMyself())
72 {
73 return QVariant::fromValue(*this);
74 }
75 if (IDatastoreObjectWithStringKey::canHandleIndex(index))
76 {
77 return IDatastoreObjectWithStringKey::propertyByIndex(index);
78 }
80 {
81 return IOrderable::propertyByIndex(index);
82 }
83
84 const auto i = index.frontCasted<ColumnIndex>();
85 switch (i)
86 {
87 case IndexAlias1: return QVariant::fromValue(m_alias1);
88 case IndexAlias2: return QVariant::fromValue(m_alias2);
89 case IndexDescription: return QVariant::fromValue(m_description);
90 case IndexSimulator: return m_simulator.propertyByIndex(index.copyFrontRemoved());
91 default: return CValueObject::propertyByIndex(index);
92 }
93 }
94
96 {
97 if (index.isMyself())
98 {
99 (*this) = variant.value<CDistributor>();
100 return;
101 }
102 if (IDatastoreObjectWithStringKey::canHandleIndex(index))
103 {
104 IDatastoreObjectWithStringKey::setPropertyByIndex(index, variant);
105 return;
106 }
108 {
109 IOrderable::setPropertyByIndex(index, variant);
110 return;
111 }
112
113 const auto i = index.frontCasted<ColumnIndex>();
114 switch (i)
115 {
116 case IndexAlias1: m_alias1 = variant.value<QString>(); break;
117 case IndexAlias2: m_alias2 = variant.value<QString>(); break;
118 case IndexDescription: m_description = variant.value<QString>(); break;
119 case IndexSimulator: m_simulator.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
120 default: CValueObject::setPropertyByIndex(index, variant); break;
121 }
122 }
123
125 {
126 if (IDatastoreObjectWithStringKey::canHandleIndex(index))
127 {
128 return IDatastoreObjectWithStringKey::comparePropertyByIndex(index, compareValue);
129 }
131 {
132 return IOrderable::comparePropertyByIndex(index, compareValue);
133 }
134
135 const auto i = index.frontCasted<ColumnIndex>();
136 switch (i)
137 {
138 case IndexAlias1: return m_alias1.compare(compareValue.m_alias1, Qt::CaseInsensitive);
139 case IndexAlias2: return m_alias2.compare(compareValue.m_alias2, Qt::CaseInsensitive);
140 case IndexDescription: return m_description.compare(compareValue.getDescription(), Qt::CaseInsensitive);
141 case IndexSimulator:
142 return m_simulator.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.m_simulator);
143 default: break;
144 }
145 Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
146 return 0;
147 }
148
150 {
151 Q_UNUSED(i18n)
152 QString s = QStringLiteral("Id: '%1'").arg(m_dbKey);
153 if (this->hasAlias1())
154 {
155 s.append(" ").append(m_alias1);
156 }
157 if (this->hasAlias2())
158 {
159 s.append(" ").append(m_alias2);
160 }
161 return s;
162 }
163
164 bool CDistributor::hasCompleteData() const { return !m_description.isEmpty() && !m_dbKey.isEmpty(); }
165
167 {
168 static const CLogCategoryList cats(CLogCategoryList(this).withValidation());
170 if (!hasValidDbKey())
171 {
172 msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, u"Distributor: missing id"));
173 }
174 if (!hasDescription())
175 {
176 msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, u"Distributor: missing description"));
177 }
178 return msgs;
179 }
180
181 void CDistributor::updateMissingParts(const CDistributor &otherDistributor)
182 {
183 if (!this->hasValidDbKey() && otherDistributor.hasValidDbKey())
184 {
185 // we have no DB data, but the other one has
186 // so we change roles. We take the DB object as base, and update our parts
187 CDistributor copy(otherDistributor);
188 copy.updateMissingParts(*this);
189 *this = copy;
190 return;
191 }
192
193 if (!this->hasAlias1())
194 {
195 this->setAlias1(otherDistributor.getAlias1());
196 }
197 if (!this->hasAlias2())
198 {
199 this->setAlias1(otherDistributor.getAlias2());
200 }
201 if (!this->hasDescription())
202 {
203 this->setDescription(otherDistributor.getDescription());
204 }
205 }
206
208 {
209 if (!existsKey(json, prefix))
210 {
211 // when using relationship, this can be null
212 return {};
213 }
214
215 const QString description(json.value(prefix % u"description").toString());
216 if (description.isEmpty())
217 {
218 // stub, only key, maybe also timestamps
219 CDistributor distributorStub;
220 distributorStub.setKeyVersionTimestampFromDatabaseJson(json, prefix);
221 return distributorStub;
222 }
223
224 const CSimulatorInfo simulator = CSimulatorInfo::fromDatabaseJson(json, prefix);
225 const QString alias1(json.value(prefix % u"alias1").toString());
226 const QString alias2(json.value(prefix % u"alias2").toString());
227 Q_ASSERT_X(!description.isEmpty(), Q_FUNC_INFO, "Missing description");
228 CDistributor distributor("", description, alias1, alias2, simulator);
229 distributor.setKeyVersionTimestampFromDatabaseJson(json, prefix);
230 distributor.setLoadedFromDb(true);
231 return distributor;
232 }
233
235 {
236 static const QString k("P3D");
237 return k;
238 }
239
241 {
242 static const QString k("XP");
243 return k;
244 }
245
247 {
248 static const QString k("FG");
249 return k;
250 }
251
253 {
254 static const QSet<QString> fsFamily({ standardP3D(), standardMsfs(), standardMsfs2024() });
255 return fsFamily;
256 }
257
259 {
260 static const QSet<QString> mp({ xplaneBlueBell(), xplaneXcsl() });
261 return mp;
262 }
263
265 {
266 static const QString k("BB");
267 return k;
268 }
269
271 {
272 static const QString k("XCSL");
273 return k;
274 }
275
277 {
278 static const QString k("MSFS");
279 return k;
280 }
281
283 {
284 static const QString k("MSFS2024");
285 return k;
286 }
287
288 QString CDistributor::unifyKeyOrAlias(const QString &value)
289 {
290 return removeChars(value.trimmed().toUpper(), [](QChar c) { return !c.isLetterOrNumber(); });
291 }
292
293} // namespace swift::misc::simulation
A sequence of log categories.
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
Streamable status message, e.g.
static constexpr auto SeverityError
Status severities.
Status messages, e.g. from Core -> GUI.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
Definition orderable.cpp:34
int comparePropertyByIndex(CPropertyIndexRef index, const IOrderable &compareValue) const
Compare for index.
Definition orderable.cpp:77
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition orderable.cpp:61
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition orderable.cpp:44
void setLoadedFromDb(bool loaded)
Mark as loaded from DB.
Definition datastore.h:207
static bool existsKey(const QJsonObject &json, const QString &prefix=QString())
Is a key available?
bool hasValidDbKey() const
Has valid DB key.
Definition datastore.h:195
const QString & getDbKey() const
Get DB key.
Definition datastore.h:180
void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Set key and timestamp values.
void setDbKey(const QString &key)
Set the DB key.
Definition datastore.h:192
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
const QString & getAlias1() const
Get alias1.
Definition distributor.h:68
void setDescription(const QString &description)
Set description.
Definition distributor.h:62
QString getIdAndDescription() const
Get id and description.
static const QSet< QString > & standardAllFsFamily()
Hardcoded keys for standard models.
static const QString & xplaneXcsl()
Hardcoded keys for standard models.
int comparePropertyByIndex(CPropertyIndexRef index, const CDistributor &compareValue) const
Compare for index.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
static const QSet< QString > & xplaneMostPopular()
Hardcoded keys for standard models.
static const QString & standardMsfs()
Hardcoded keys for standard models.
static const QString & standardXPlane()
Hardcoded keys for standard models.
bool matchesKeyOrAlias(const QString &keyOrAlias) const
Matches key or alias.
static const QString & standardFlightGear()
Hardcoded keys for standard models.
void updateMissingParts(const CDistributor &otherDistributor)
Update missing parts.
bool hasDescription() const
Has description.
Definition distributor.h:65
bool hasCompleteData() const
Complete data?
QString convertToQString(bool i18n=false) const
Cast as QString.
const QString & getAlias2() const
Get alias2.
Definition distributor.h:71
static CDistributor fromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Object from JSON.
static const QString & standardP3D()
Hardcoded keys for standard models.
static const QString & standardMsfs2024()
Hardcoded keys for standard models.
swift::misc::CStatusMessageList validate() const
Validate data.
void setAlias1(const QString &alias)
Set alias1.
Definition distributor.h:74
static const QString & xplaneBlueBell()
Hardcoded keys for standard models.
const QString & getDescription() const
Get description.
Definition distributor.h:56
CDistributor()=default
Default constructor.
bool matchesSimulator(const CSimulatorInfo &simulator) const
Matches simulator.
Simple hardcoded info about the corresponding simulator.
static CSimulatorInfo fromDatabaseJson(const QJsonObject &json, const QString &prefix)
From database JSON.
QString removeChars(const QString &s, F predicate)
Return a string with characters removed that match the given predicate.
Definition stringutils.h:34
QString & append(QChar ch)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString toUpper() const const
QString trimmed() const const
CaseInsensitive
QVariant fromValue(T &&value)
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67