swift
Loading...
Searching...
No Matches
user.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "misc/network/user.h"
5
6#include <QChar>
7#include <QRegularExpression>
8#include <QRegularExpressionMatch>
9#include <QStringBuilder>
10#include <Qt>
11#include <QtGlobal>
12
15#include "misc/obfuscation.h"
17#include "misc/propertyindexvariantmap.h" // needed for mixin::Index::apply
18#include "misc/statusmessage.h"
19#include "misc/stringutils.h"
20
21using namespace swift::misc::aviation;
22
23SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CUser)
24
25namespace swift::misc::network
26{
27 CUser::CUser(const CCallsign &callsign) : m_callsign(callsign) { this->deriveHomeBaseFromCallsign(); }
28
29 CUser::CUser(const QString &id, const QString &realname, const CCallsign &callsign)
30 : m_id(CObfuscation::decode(id)), m_realname(CObfuscation::decode(realname)), m_callsign(callsign)
31 {
32 this->deriveHomeBaseFromCallsign();
33 this->setRealName(m_realname); // extracts homebase if this is included in real name
34 }
35
36 CUser::CUser(const QString &id, const QString &realname, const QString &email, const QString &password,
37 const CCallsign &callsign)
38 : m_id(CObfuscation::decode(id)), m_realname(CObfuscation::decode(realname)),
39 m_email(CObfuscation::decode(email)), m_password(CObfuscation::decode(password)), m_callsign(callsign)
40 {
41 this->deriveHomeBaseFromCallsign();
42 this->setRealName(m_realname); // extracts homebase
43 }
44
46 {
47 if (!this->hasHomeBase())
48 {
49 return this->getRealName();
50 }
51 if (!this->hasRealName())
52 {
53 return this->getHomeBase().asString();
54 }
55 return this->getRealName() % separator % this->getHomeBase().asString();
56 }
57
59 {
60 if (this->hasRealName())
61 {
62 if (this->hasId())
63 {
64 return this->getRealName() % u" (" % this->getId() % QStringLiteral(")");
65 }
66 return this->getRealName();
67 }
68 return this->getId();
69 }
70
71 bool CUser::setCallsign(const CCallsign &callsign)
72 {
73 if (m_callsign == callsign)
74 {
75 return false;
76 }
77 m_callsign = callsign;
78 this->deriveHomeBaseFromCallsign();
79 return true;
80 }
81
83 {
84 Q_UNUSED(i18n)
85 if (m_realname.isEmpty()) return QStringLiteral("<no realname>");
86 QString s = m_realname;
87 if (this->hasId())
88 {
89 s += u" (" % m_id % u')';
90 }
91 if (this->hasCallsign())
92 {
93 s += u' ' % this->getCallsign().getStringAsSet();
94 }
95 return s;
96 }
97
98 void CUser::deriveHomeBaseFromCallsign()
99 {
100 if (m_callsign.isEmpty())
101 {
102 return;
103 }
104 if (m_homebase.isEmpty())
105 {
106 if (m_callsign.isAtcCallsign())
107 {
108 // option 1: real ATC station
109 // option 2: a copilot cmoning in Observer
110 if (m_callsign.isObserverCallsign())
111 {
112 // copilot
113 }
114 else
115 {
116 m_homebase = m_callsign.getIcaoCode();
117 }
118 }
119 }
120 }
121
122 void CUser::setRealName(const QString &realname)
123 {
124 QString rn(simplifyAccents(CObfuscation::decode(realname).simplified()));
125 if (rn.isEmpty())
126 {
127 m_realname.clear();
128 return;
129 }
130
131 if (!this->hasHomeBase())
132 {
133 // only apply stripping if home base is not explicitly given
134 // try to strip homebase: I understand the limitations, but we will have more correct hits as failures I
135 // assume
136 thread_local QRegularExpression tsRegex("(-\\s*|\\s)([A-Z]{4})$");
137 const QRegularExpressionMatch match = tsRegex.match(rn);
138 if (match.hasMatch())
139 {
140 const int pos = match.capturedStart(0);
141 const QString icao = match.captured(0).trimmed().right(4);
142 rn = QStringView { rn }.left(pos).trimmed().toString();
143 this->setHomeBase(CAirportIcaoCode(icao));
144 }
145 }
146
147 // do not beautify before stripping home base
148 m_realname = beautifyRealName(rn);
149 }
150
151 bool CUser::hasValidHomeBase() const { return m_homebase.hasValidIcaoCode(false); }
152
153 bool CUser::hasValidOrEmptyHomeBase() const { return m_homebase.isEmpty() || this->hasValidHomeBase(); }
154
156 {
158 // callsign optional
159 if (!this->hasId())
160 {
162 }
163 if (!this->hasRealName())
164 {
165 msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Invalid real name"));
166 }
167 if (!this->hasCredentials())
168 {
169 msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, u"Invalid credentials"));
170 }
171 return msgs;
172 }
173
175 {
176 if (!this->hasNumericId())
177 {
178 return m_id;
179 }
180 if (m_id.length() > 6)
181 {
182 return m_id;
183 }
184
185 static const QString zeros("0000000");
186 return zeros.left(7 - m_id.length()) % m_id;
187 }
188
190 {
191 if (m_id.isEmpty())
192 {
193 return -1;
194 }
195 if (is09OnlyString(m_id))
196 {
197 return m_id.toInt();
198 }
199 return -1;
200 }
201
203 {
204 if (m_id.isEmpty())
205 {
206 return false;
207 }
208 return is09OnlyString(m_id);
209 }
210
211 void CUser::updateMissingParts(const CUser &otherUser)
212 {
213 if (this == &otherUser)
214 {
215 return;
216 }
217 if (!this->hasRealName())
218 {
219 this->setRealName(otherUser.getRealName());
220 }
221 if (!this->hasId())
222 {
223 this->setId(otherUser.getId());
224 }
225 if (!this->hasValidEmail())
226 {
227 this->setEmail(otherUser.getEmail());
228 }
229 if (!this->hasCallsign())
230 {
231 this->setCallsign(otherUser.getCallsign());
232 }
233 }
234
236 {
237 if (this == &otherUser)
238 {
239 return;
240 }
241 this->updateMissingParts(otherUser);
242 otherUser.updateMissingParts(*this);
243 }
244
246 {
247 if (id.isEmpty())
248 {
249 return false;
250 }
251 if (!is09OnlyString(id))
252 {
253 return false;
254 }
255 const int i = id.toInt();
256 return i >= 100000 && i <= 9999999;
257 }
258
260 {
261 QString newRealName(realName.simplified().trimmed());
262 if (newRealName.isEmpty())
263 {
264 return newRealName;
265 }
266 if (is09OnlyString(newRealName))
267 {
268 return newRealName;
269 } // new VATSIM COD, allowing id as name, see
270 // https://discordapp.com/channels/539048679160676382/539846348275449887/599969308247851018
271
272 int uc = 0;
273 int lc = 0;
274 for (const QChar &ch : realName)
275 {
276 // Joe Doe -> valid
277 // jOE dOE -> invalid
278 // Joe McArthur -> valid
279 if (uc > 1 && lc > 2 && lc > uc)
280 {
281 return newRealName;
282 } // mixed case name, no need to beautify
283 if (ch.isLower())
284 {
285 lc++;
286 continue;
287 }
288 if (ch.isUpper())
289 {
290 uc++;
291 continue;
292 }
293 }
294
295 // simple title case beautifying
296 newRealName = newRealName.toLower();
297 QString::Iterator i = newRealName.begin();
298 bool upperNextChar = true;
299 while (i != newRealName.end())
300 {
301 if (i->isSpace() || *i == '-')
302 {
303 upperNextChar = true;
304 }
305 else if (upperNextChar)
306 {
307 const QChar u(i->toUpper());
308 *i = u;
309 upperNextChar = false;
310 }
311 ++i;
312 }
313 return newRealName;
314 }
315
317 {
318 if (index.isMyself())
319 {
320 return QVariant::fromValue(*this);
321 }
322 const auto i = index.frontCasted<ColumnIndex>();
323 switch (i)
324 {
325 case IndexEmail: return { m_email };
326 case IndexId: return { m_id };
327 case IndexId7Digit: return { this->get7DigitId() };
328 case IndexIdInteger: return QVariant::fromValue(this->getIntegerId());
329 case IndexPassword: return { m_password };
330 case IndexRealName: return { m_realname };
331 case IndexHomebase: return m_homebase.propertyByIndex(index.copyFrontRemoved());
332 case IndexCallsign: return m_callsign.propertyByIndex(index.copyFrontRemoved());
333 default: return CValueObject::propertyByIndex(index);
334 }
335 }
336
338 {
339 if (index.isMyself())
340 {
341 (*this) = variant.value<CUser>();
342 return;
343 }
344 const auto i = index.frontCasted<ColumnIndex>();
345 switch (i)
346 {
347 case IndexEmail: this->setEmail(variant.value<QString>()); break;
348 case IndexId: // fallthru
349 case IndexId7Digit: this->setId(variant.value<QString>()); break;
350 case IndexIdInteger: this->setId(QString::number(variant.toInt())); break;
351 case IndexPassword: this->setPassword(variant.value<QString>()); break;
352 case IndexRealName: this->setRealName(variant.value<QString>()); break;
353 case IndexHomebase: m_homebase.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
354 case IndexCallsign: m_callsign.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
355 default: CValueObject::setPropertyByIndex(index, variant); break;
356 }
357 }
358
359 int CUser::comparePropertyByIndex(CPropertyIndexRef index, const CUser &compareValue) const
360 {
361 if (index.isMyself())
362 {
363 return this->getRealName().compare(compareValue.getRealName(), Qt::CaseInsensitive);
364 }
365 const auto i = index.frontCasted<ColumnIndex>();
366 switch (i)
367 {
368 case IndexEmail: return m_email.compare(compareValue.getEmail(), Qt::CaseInsensitive);
369 case IndexId: return m_id.compare(compareValue.getId(), Qt::CaseInsensitive);
370 case IndexId7Digit: return this->get7DigitId().compare(compareValue.get7DigitId(), Qt::CaseInsensitive);
371 case IndexIdInteger: return swift::misc::Compare::compare(getIntegerId(), compareValue.getIntegerId());
372 case IndexRealName: return m_realname.compare(compareValue.getRealName(), Qt::CaseInsensitive);
373 case IndexHomebase:
374 return m_homebase.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getHomeBase());
375 case IndexCallsign:
376 return m_callsign.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getCallsign());
377 case IndexPassword: break;
378 default: return CValueObject::comparePropertyByIndex(index, compareValue);
379 }
380 Q_ASSERT_X(false, Q_FUNC_INFO, "compare failed");
381 return 0;
382 }
383
384 void CUser::setPassword(const QString &pw) { m_password = CObfuscation::decode(pw); }
385
386 void CUser::setEmail(const QString &email) { m_email = CObfuscation::decode(email); }
387
388 void CUser::setId(const QString &id) { m_id = CObfuscation::decode(id); }
389} // namespace swift::misc::network
Utility class to obfuscate strings in the source code to make them unreadable.
Definition obfuscation.h:19
static QString decode(const QString &inString, bool trimmed=true)
Decode string if it has the prefix, otherwise do nothing with it.
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 SeverityWarning
Status severities.
Status messages, e.g. from Core -> GUI.
Value object encapsulating information of airport ICAO data.
const QString & asString() const
Get code.
Value object encapsulating information of a callsign.
Definition callsign.h:30
bool isObserverCallsign() const
Observer callsign?
Definition callsign.cpp:318
const QString & getStringAsSet() const
Get callsign.
Definition callsign.h:99
bool isEmpty() const
Is empty?
Definition callsign.h:63
QString getIcaoCode() const
Get ICAO code, if this makes sense (EDDF_TWR -> EDDF).
Definition callsign.cpp:293
bool isAtcCallsign() const
ATC callsign.
Definition callsign.cpp:235
int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
Compare for index.
Definition mixinindex.h:203
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
bool hasValidOrEmptyHomeBase() const
Has valid or empty home base?
Definition user.cpp:153
bool hasRealName() const
Valid real name?
Definition user.h:80
bool hasNumericId() const
Has a numeric id?
Definition user.cpp:202
bool hasValidHomeBase() const
Has valid home base?
Definition user.cpp:151
int getIntegerId() const
Id as integer if possible, otherwise -1.
Definition user.cpp:189
static QString beautifyRealName(const QString &realName)
Beautify real name, e.g. "JOE DoE" -> "Joe Doe";.
Definition user.cpp:259
void setRealName(const QString &realname)
Set real name.
Definition user.cpp:122
QString getRealNameAndId() const
Real name and id.
Definition user.cpp:58
ColumnIndex
Properties by index.
Definition user.h:34
bool hasValidEmail() const
Valid email?
Definition user.h:116
int comparePropertyByIndex(CPropertyIndexRef index, const CUser &compareValue) const
Compare for index.
Definition user.cpp:359
QString get7DigitId() const
Numeric ids get a leading zeros if required.
Definition user.cpp:174
const QString & getId() const
Get id.
Definition user.h:119
void setId(const QString &id)
Set id.
Definition user.cpp:388
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition user.cpp:316
void setHomeBase(const aviation::CAirportIcaoCode &homebase)
Set homebase.
Definition user.h:137
CUser()=default
Default constructor.
const QString & getEmail() const
Get email.
Definition user.h:110
void setEmail(const QString &email)
Set email.
Definition user.cpp:386
bool hasId() const
Valid id?
Definition user.h:83
bool hasHomeBase() const
Has home base?
Definition user.h:92
bool setCallsign(const aviation::CCallsign &callsign)
Set associated callsign.
Definition user.cpp:71
void setPassword(const QString &pw)
Set password.
Definition user.cpp:384
const QString & getRealName() const
Get full name.
Definition user.h:59
const aviation::CCallsign & getCallsign() const
Get associated callsign.
Definition user.h:140
CStatusMessageList validate() const
Validate, provide details about issues.
Definition user.cpp:155
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition user.cpp:337
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition user.cpp:82
bool hasCredentials() const
Valid credentials?
Definition user.h:77
static bool isValidVatsimId(const QString &id)
Valid VATSIM id.
Definition user.cpp:245
QString getRealNameAndHomeBase(const QString &separator=QString(" ")) const
Real name + homebase.
Definition user.cpp:45
const aviation::CAirportIcaoCode & getHomeBase() const
Homebase.
Definition user.h:134
void synchronizeData(CUser &otherUser)
This and another user exchange missing data, This user has priority and overrides first.
Definition user.cpp:235
bool hasCallsign() const
Has associated callsign?
Definition user.h:89
void updateMissingParts(const CUser &otherUser)
Update missing parts in this object.
Definition user.cpp:211
int compare(T a, T b)
Compare arithmetic values.
bool is09OnlyString(const QString &testString)
String with 0-9 only.
SWIFT_MISC_EXPORT QString simplifyAccents(const QString &candidate)
Remove accents / diacritic marks from a string.
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
QString captured(QAnyStringView name) const const
qsizetype capturedStart(QAnyStringView name) const const
bool hasMatch() const const
typedef Iterator
QString::iterator begin()
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
QString::iterator end()
bool isEmpty() const const
QString left(qsizetype n) &&
QString number(double n, char format, int precision)
QString right(qsizetype n) &&
QString simplified() const const
QString toLower() const const
QString trimmed() const const
QStringView left(qsizetype length) const const
QString toString() const const
QStringView trimmed() const const
CaseInsensitive
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67