swift
Loading...
Searching...
No Matches
airlineicaocodelist.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
9#include "misc/country.h"
10#include "misc/logcategories.h"
11#include "misc/setbuilder.h"
12
13SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::aviation, CAirlineIcaoCode, CAirlineIcaoCodeList)
14
15namespace swift::misc::aviation
16{
18 {
19 static const QStringList cats(CLogCategories::aviation());
20 return cats;
21 }
22
26
27 CAirlineIcaoCodeList::CAirlineIcaoCodeList(std::initializer_list<CAirlineIcaoCode> il)
29 {}
30
32 {
34 {
35 return {};
36 }
37 return this->findBy([&](const CAirlineIcaoCode &code) { return code.matchesDesignator(designator); });
38 }
39
41 {
43 {
44 return {};
45 }
46 return this->findBy([&](const CAirlineIcaoCode &code) { return code.matchesIataCode(iata); });
47 }
48
50 {
51 const CAirlineIcaoCodeList codes = this->findByIataCode(iata);
52 return codes.size() == 1 ? codes.front() : CAirlineIcaoCode();
53 }
54
56 {
57 if (designatorOrIata.isEmpty())
58 {
59 return {};
60 }
61 return this->findBy(
62 [&](const CAirlineIcaoCode &code) { return code.matchesDesignatorOrIataCode(designatorOrIata); });
63 }
64
66 {
68 {
69 return {};
70 }
71 return this->findBy([&](const CAirlineIcaoCode &code) { return code.matchesVDesignator(designator); });
72 }
73
75 bool preferOperatingAirlines) const
76 {
77 CAirlineIcaoCodeList codes = this->findByVDesignator(designator);
78 if (codes.size() > 1 && preferOperatingAirlines)
79 {
81 }
82 return codes.size() == 1 ? codes.front() : CAirlineIcaoCode();
83 }
84
86 {
87 if (designatorOrIata.isEmpty())
88 {
89 return {};
90 }
91 return this->findBy(
92 [&](const CAirlineIcaoCode &code) { return code.matchesVDesignatorOrIataCode(designatorOrIata); });
93 }
94
96 {
97 if (isoCode.length() != 2)
98 {
99 return {};
100 }
101 const QString iso(isoCode.toUpper());
102 return this->findBy([&](const CAirlineIcaoCode &code) { return code.getCountry().getIsoCode() == iso; });
103 }
104
106 {
107 if (containedString.isEmpty())
108 {
109 return {};
110 }
111 return this->findBy(
112 [&](const CAirlineIcaoCode &code) { return code.isContainedInSimplifiedName(containedString); });
113 }
114
116 {
117 if (candidate.isEmpty())
118 {
119 return {};
120 }
121 return this->findBy([&](const CAirlineIcaoCode &code) { return code.matchesTelephonyDesignator(candidate); });
122 }
123
125 {
126 if (candidate.isEmpty())
127 {
128 return {};
129 }
130 return this->findBy(
131 [&](const CAirlineIcaoCode &code) { return code.matchesNamesOrTelephonyDesignator(candidate); });
132 }
133
135 {
136 return this->findBy([&](const CAirlineIcaoCode &code) { return code.isMilitary() == military; });
137 }
138
140 {
141 return this->findBy([](const CAirlineIcaoCode &code) { return code.hasValidDesignator(); });
142 }
143
145 {
146 return this->findBy([](const CAirlineIcaoCode &code) { return !code.hasValidDesignator(); });
147 }
148
150 const CCallsign &callsign) const
151 {
152 if (icaoPattern.hasValidDbKey())
153 {
154 return icaoPattern;
155 }
156 const CAirlineIcaoCode patternUsed = icaoPattern.thisOrCallsignCode(callsign);
157
158 // search by parts
159 CAirlineIcaoCodeList codesFound;
160 if (patternUsed.hasValidDesignator())
161 {
162 if (patternUsed.isVirtualAirline())
163 {
164 // we can tell for sure we search an VA
165 codesFound = this->findByVDesignator(patternUsed.getVDesignator());
166 }
167 else
168 {
169 // we do not know if we are looking for an VA
170 codesFound = this->findByDesignator(patternUsed.getDesignator());
171 }
172 }
173 else
174 {
175 codesFound = this->findByIataCode(patternUsed.getIataCode());
176 }
177
178 if (codesFound.size() == 1)
179 {
180 return codesFound.front();
181 }
182 if (codesFound.isEmpty())
183 {
184 // nothing found so far
185 codesFound = this->findByNamesOrTelephonyDesignator(patternUsed.getName());
186 codesFound = codesFound.ifPossibleReduceByTelephonyDesignator(patternUsed.getTelephonyDesignator());
187 codesFound = codesFound.ifPossibleReduceByCountry(patternUsed.getCountryIso());
188 }
189 else
190 {
191 // further reduce
192 bool reduced = false;
193 codesFound = codesFound.ifPossibleReduceNameTelephonyCountry(
194 callsign, patternUsed.getName(), patternUsed.getTelephonyDesignator(), patternUsed.getCountryIso(),
195 reduced, QString(), nullptr);
196 }
197
198 return codesFound.frontOrDefault();
199 }
200
202 const CCallsign &cs, const QString &airlineName, const QString &telephony, const QString &countryIso,
203 bool &reduced, const QString &loginfo, CStatusMessageList *log) const
204 {
205 reduced = false;
206 if (this->isEmpty())
207 {
208 if (log)
209 {
210 CCallsign::addLogDetailsToList(log, cs, loginfo % u" Empty input list, cannot reduce",
212 }
213 return *this;
214 }
215
216 if (telephony.isEmpty() && airlineName.isEmpty() && countryIso.isEmpty())
217 {
218 if (log)
219 {
221 loginfo % u" No name/telephony/country, cannot reduce " %
222 QString::number(this->size()) % u" entries",
224 }
225 return *this;
226 }
227
228 CAirlineIcaoCodeList step1Data =
229 airlineName.isEmpty() ? *this : this->findByNamesOrTelephonyDesignator(airlineName);
230 if (step1Data.isEmpty() || step1Data.size() == this->size())
231 {
232 if (log)
233 {
235 log, cs, loginfo % QStringLiteral(" cannot reduce by '%1'").arg(airlineName), getLogCategories());
236 }
237 step1Data = *this;
238 }
239 else
240 {
241 reduced = true;
242 if (log)
243 {
244 CCallsign::addLogDetailsToList(log, cs, loginfo % QStringLiteral(" reduced by '%1'").arg(airlineName),
246 }
247 }
248 if (step1Data.size() == 1)
249 {
250 return step1Data;
251 }
252
253 CAirlineIcaoCodeList step2Data =
254 telephony.isEmpty() ? step1Data : step1Data.findByNamesOrTelephonyDesignator(telephony);
255 if (step2Data.isEmpty() || step2Data.size() == this->size())
256 {
257 if (log)
258 {
260 loginfo % QStringLiteral(" cannot reduce by name '%1'").arg(telephony),
262 }
263 step2Data = step1Data;
264 }
265 else
266 {
267 reduced = true;
268 if (log)
269 {
271 log, cs, loginfo % QStringLiteral(" reduced by telephony '%1'").arg(telephony), getLogCategories());
272 }
273 }
274 if (step2Data.size() == 1)
275 {
276 return step2Data;
277 }
278
279 CAirlineIcaoCodeList step3Data = countryIso.isEmpty() ? step2Data : step2Data.findByCountryIsoCode(countryIso);
280 if (step3Data.isEmpty() || step3Data.size() == this->size())
281 {
282 if (log)
283 {
285 log, cs, loginfo % QStringLiteral(" cannot reduce by country '%1'").arg(countryIso),
287 }
288 step3Data = step2Data;
289 }
290 else
291 {
292 reduced = true;
293 if (log)
294 {
295 CCallsign::addLogDetailsToList(log, cs, loginfo % QStringLiteral(" reduced by '%1'").arg(countryIso),
297 }
298 }
299 return step3Data;
300 }
301
303 {
304 if (countryIso.isEmpty())
305 {
306 return *this;
307 }
308 if (this->isEmpty())
309 {
310 return *this;
311 }
312 const CAirlineIcaoCodeList found = this->findByCountryIsoCode(countryIso);
313 if (found.size() == this->size() || found.isEmpty())
314 {
315 return *this;
316 }
317 return found;
318 }
319
322 {
323 if (telephonyDesignator.isEmpty())
324 {
325 return *this;
326 }
327 if (this->isEmpty())
328 {
329 return *this;
330 }
331 const CAirlineIcaoCodeList found = this->findByTelephonyDesignator(telephonyDesignator);
332 if (found.size() == this->size() || found.isEmpty())
333 {
334 return *this;
335 }
336 return found;
337 }
338
340 {
341 if (this->isEmpty() || callsign.isEmpty())
342 {
343 return {};
344 }
345 const QString airline = callsign.getAirlinePrefix().toUpper();
346 if (airline.isEmpty())
347 {
348 return {};
349 }
350 const CAirlineIcaoCode airlineCode = (airline.length() == 3) ?
353 return airlineCode;
354 }
355
357 CAirlineIcaoCodeList *inconsistent)
358 {
360 for (const QJsonValue &value : array)
361 {
362 const CAirlineIcaoCode icao(CAirlineIcaoCode::fromDatabaseJson(value.toObject()));
363 const bool incomplete = !icao.hasCompleteData();
364 if (incomplete)
365 {
366 if (ignoreIncomplete)
367 {
368 continue;
369 }
370 if (inconsistent)
371 {
372 inconsistent->push_back(icao);
373 continue;
374 }
375 }
376 codes.push_back(icao);
377 }
378 return codes;
379 }
380
382 {
383 QStringList c;
384 for (const CAirlineIcaoCode &icao : *this)
385 {
386 if (combinedString)
387 {
388 if (!icao.hasValidDbKey())
389 {
390 continue;
391 }
392 const QString cs(icao.getCombinedStringWithKey());
393 if (cs.isEmpty())
394 {
395 continue;
396 }
397 c.append(cs);
398 }
399 else
400 {
401 const QString d(icao.getDesignator());
402 if (c.contains(d) || d.isEmpty())
403 {
404 continue;
405 }
406 c.append(d);
407 }
408 }
409 if (sort)
410 {
411 c.sort();
412 }
413 return c;
414 }
415
417 {
418 QStringList c;
419 for (const CAirlineIcaoCode &icao : *this)
420 {
421 if (!icao.hasValidDesignator())
422 {
423 continue;
424 }
425 const QString cs(icao.getDesignatorNameCountry());
426 if (cs.isEmpty())
427 {
428 continue;
429 }
430 c.append(cs);
431 }
432 if (sort)
433 {
434 c.sort();
435 }
436 return c;
437 }
438
440 {
441 QStringList c;
442 for (const CAirlineIcaoCode &icao : *this)
443 {
444 if (!icao.hasValidDbKey())
445 {
446 continue;
447 }
448 const QString cs(icao.getNameWithKey());
449 if (cs.isEmpty())
450 {
451 continue;
452 }
453 c.append(cs);
454 }
455 if (sort)
456 {
457 c.sort();
458 }
459 return c;
460 }
461
463 {
464 CSetBuilder<QString> designators;
465 for (const CAirlineIcaoCode &icao : *this)
466 {
467 if (!icao.hasValidDesignator())
468 {
469 continue;
470 }
471 designators.insert(icao.getDesignator());
472 }
473 return designators;
474 }
475
477 {
478 CSetBuilder<QString> designators;
479 for (const CAirlineIcaoCode &icao : *this)
480 {
481 if (!icao.hasValidDesignator())
482 {
483 continue;
484 }
485 designators.insert(icao.getVDesignator());
486 }
487 return designators;
488 }
489
491 {
492 if (designator.isEmpty())
493 {
494 return false;
495 }
496 return this->contains(&CAirlineIcaoCode::getDesignator, designator.toUpper());
497 }
498
500 {
501 if (vDesignator.isEmpty())
502 {
503 return false;
504 }
505 if (vDesignator.length() < 4)
506 {
507 return this->containsDesignator(vDesignator);
508 }
509 return this->contains(&CAirlineIcaoCode::getVDesignator, vDesignator.toUpper());
510 }
511
513 {
515 for (const CAirlineIcaoCode &code : *this)
516 {
517 if (!code.hasValidDbKey())
518 {
519 continue;
520 }
521 map.insert(code.getDbKey(), code);
522 }
523 return map;
524 }
525} // namespace swift::misc::aviation
QMap< int, CAirlineIcaoCode > AirlineIcaoIdMap
Airline ICAO/id list.
const QString & getIsoCode() const
DB ISO code.
Definition country.h:55
static const QString & aviation()
Aviation specific.
auto findFirstByOrDefault(Predicate p, const Value &def) const
Definition range.h:70
size_type size() const
Returns number of elements in the sequence.
Definition sequence.h:273
const_reference frontOrDefault() const
Access the first element, or a default-initialized value if the sequence is empty.
Definition sequence.h:239
CSequence findBy(Predicate p) const
Definition sequence.h:407
int removeIf(Predicate p)
Remove elements for which a given predicate returns true.
Definition sequence.h:458
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
reference front()
Access the first element.
Definition sequence.h:225
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Build a QSet more efficiently when calling insert() in a for loop.
Definition setbuilder.h:25
void insert(const T &value)
Add an element to the set. Runs in amortized constant time.
Definition setbuilder.h:29
Status messages, e.g. from Core -> GUI.
Value object for ICAO classification.
QString getCombinedStringWithKey() const
Comined string with key.
bool isVirtualAirline() const
Virtual airline.
static bool isValidAirlineDesignator(const QString &airline)
Valid designator?
bool isContainedInSimplifiedName(const QString &candidate) const
Does simplified name contain the candidate.
bool matchesDesignator(const QString &designator) const
Matches designator string?
bool isMilitary() const
Military, air force or such?
const QString & getIataCode() const
IATA code.
const CCountry & getCountry() const
Get country, e.g. "FRANCE".
const QString & getTelephonyDesignator() const
Telephony designator such as "Speedbird".
bool hasCompleteData() const
Complete data.
bool hasValidDesignator() const
Airline designator available?
static CAirlineIcaoCode fromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
From our DB JSON.
bool matchesTelephonyDesignator(const QString &candidate) const
Matches telephony designator (aka callsign, not to be confused with CCallsign).
bool matchesVDesignator(const QString &designator) const
Matches v-designator string?
static bool isValidIataCode(const QString &iataCode)
Valid IATA code?
bool matchesNamesOrTelephonyDesignator(const QString &candidate) const
Relaxed check by name or telephony designator (aka callsign, not to be confused with CCallsign).
bool matchesVDesignatorOrIataCode(const QString &candidate) const
Matches IATA code or v-designator?
bool matchesDesignatorOrIataCode(const QString &candidate) const
Matches IATA code or designator?
bool matchesIataCode(const QString &iata) const
Matches IATA code?
QString getDesignatorNameCountry() const
Combined string designator, name, country.
QString getVDesignator() const
Get airline, e.g. "DLH", but "VMVA" for virtual airlines.
CAirlineIcaoCode thisOrCallsignCode(const CCallsign &callsign) const
What is better, the callsign airline code or this code. Return the better one.
QString getNameWithKey() const
Name plus key, e.g. "Lufthansa (3421)".
const QString & getDesignator() const
Get airline, e.g. "DLH".
const QString & getName() const
Get name, e.g. "Lufthansa".
const QString & getCountryIso() const
Get country, e.g. "FR".
Value object encapsulating a list of ICAO codes.
CAirlineIcaoCode findByUniqueIataCodeOrDefault(const QString &iata) const
Find by IATA code if this is unique, otherwise return default object.
CAirlineIcaoCodeList ifPossibleReduceNameTelephonyCountry(const swift::misc::aviation::CCallsign &cs, const QString &airlineName, const QString &telephony, const QString &countryIso, bool &reduced, const QString &logInfo, CStatusMessageList *log) const
Reduce by airline name/telephone designator, ISO country.
CAirlineIcaoCodeList findByMilitary(bool military) const
Find by military flag.
CSequence()=default
Default constructor.
QSet< QString > allVDesignators() const
All designators.
QStringList toIcaoDesignatorNameCountryCompleterStrings(bool sort=true) const
String list for completion by ICAO designator plus Name.
CAirlineIcaoCodeList findByNamesOrTelephonyDesignator(const QString &candidate) const
Find by names or telephony designator (aka callsign, not to be confused with CCallsign).
bool containsVDesignator(const QString &vDesignator) const
Contains given designator?
CAirlineIcaoCodeList findByValidDesignator() const
The ones with a valid designator.
CAirlineIcaoCodeList findByTelephonyDesignator(const QString &candidate) const
Find by names or telephony designator (aka callsign, not to be confused with CCallsign).
AirlineIcaoIdMap toIdMap() const
To id map.
CAirlineIcaoCodeList findByVDesignatorOrIataCode(const QString &designatorOrIata) const
Find by v-designator or IATA code.
static const QStringList & getLogCategories()
Categories.
CAirlineIcaoCodeList()=default
Default constructor.
CAirlineIcaoCodeList findByInvalidDesignator() const
The ones with an invalid designator.
static CAirlineIcaoCodeList fromDatabaseJson(const QJsonArray &array, bool ignoreIncomplete=true, CAirlineIcaoCodeList *inconsistent=nullptr)
From our DB JSON.
CAirlineIcaoCodeList ifPossibleReduceByCountry(const QString &countryIso) const
Reduce by ISO country.
CAirlineIcaoCode findBestMatchByCallsign(const CCallsign &callsign) const
Use callsign to conclude airline.
CAirlineIcaoCodeList findByIataCode(const QString &iata) const
Find by IATA code Not unique because of virtual airlines and ceased airlines.
CAirlineIcaoCodeList findByDesignatorOrIataCode(const QString &designatorOrIata) const
Find by designator or IATA code.
CAirlineIcaoCodeList findByCountryIsoCode(const QString &isoCode) const
Find by country code.
QStringList toNameCompleterStrings(bool sort=true) const
String list for completion by name.
CAirlineIcaoCodeList findBySimplifiedNameContaining(const QString &containedString) const
Find if simplified name contains search string.
CAirlineIcaoCodeList ifPossibleReduceByTelephonyDesignator(const QString &telephonyDesignator) const
Reduce by telephony designator.
CAirlineIcaoCode findByUniqueVDesignatorOrDefault(const QString &designator, bool preferOperatingAirlines) const
Find by ICAO code if this is unique, otherwise return default object.
QStringList toIcaoDesignatorCompleterStrings(bool combinedString=true, bool sort=true) const
String list for completion by ICAO designator.
CAirlineIcaoCodeList findByDesignator(const QString &designator) const
Find by designator Not unique because of virtual airlines.
bool containsDesignator(const QString &designator) const
Contains given designator?
QSet< QString > allDesignators() const
All designators.
CAirlineIcaoCode smartAirlineIcaoSelector(const CAirlineIcaoCode &icaoPattern, const CCallsign &callsign) const
Best selection by given pattern.
CAirlineIcaoCodeList findByVDesignator(const QString &designator) const
Find by v-designator, this should be unique.
Value object encapsulating information of a callsign.
Definition callsign.h:30
static void addLogDetailsToList(CStatusMessageList *log, const CCallsign &callsign, const QString &message, const QStringList &extraCategories={}, CStatusMessage::StatusSeverity s=CStatusMessage::SeverityInfo)
Specialized log for matching / reverse lookup.
Definition callsign.cpp:155
bool isEmpty() const
Is empty?
Definition callsign.h:63
QString getAirlinePrefix() const
Airline suffix (e.g. DLH1234 -> DLH) if applicable.
Definition callsign.cpp:348
bool hasValidDbKey() const
Has valid DB key.
Definition datastore.h:102
void append(QList< T > &&value)
bool isEmpty() const const
qsizetype length() const const
QString number(double n, char format, int precision)
QString toUpper() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
void sort(Qt::CaseSensitivity cs)
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition sequence.h:63