swift
distributorlist.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 <tuple>
7 
10 
11 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::simulation, CDistributor, CDistributorList)
12 
13 namespace swift::misc::simulation
14 {
16 
18  {
19  if (keyOrAlias.isEmpty()) { return {}; }
20  for (const CDistributor &distributor : (*this))
21  {
22  if (distributor.matchesKeyOrAlias(keyOrAlias)) { return distributor; }
23  }
24  return {};
25  }
26 
28  {
29  // some stuipd hardcoded resolutions for distributors
30  if (model.getDistributor().hasValidDbKey()) { return model.getDistributor(); }
31  if (model.getModelString().startsWith("WOA", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("WOAI"); }
32  if (model.getDescription().contains("WOA", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("WOAI"); }
33  if (model.getDescription().contains("IVAO", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("IVAO"); }
34  if (model.getModelString().startsWith("PAI ", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("PAI"); }
35  if (model.getDescription().startsWith("Project AI ", Qt::CaseInsensitive))
36  {
37  return this->findByKeyOrAlias("PAI");
38  }
39 
40  return {};
41  }
42 
44  {
46  }
47 
49  {
50  // key is not necessarily a DB key, so use complete data, happens when key is set from raw data
51  if (distributor.isLoadedFromDb()) { return distributor; }
52  if (distributor.hasValidDbKey())
53  {
54  const QString key(distributor.getDbKey());
55  CDistributor d(this->findByKey(key));
56  if (d.hasCompleteData()) { return d; }
57 
58  // more lenient search
59  return this->findByKeyOrAlias(key);
60  }
61  return {};
62  }
63 
65  const CAircraftModel &model) const
66  {
67  const CDistributor d = this->smartDistributorSelector(distributorPattern);
68  // key is not necessarily a DB key, so use complete data, happens when key is set from raw data
69  if (d.hasCompleteData()) { return d; }
70  return this->findByModelData(model);
71  }
72 
73  bool CDistributorList::matchesAnyKeyOrAlias(const QString &keyOrAlias) const
74  {
75  return std::any_of(cbegin(), cend(),
76  [&](const CDistributor &distributor) { return distributor.matchesKeyOrAlias(keyOrAlias); });
77  }
78 
80  {
81  if (this->isEmpty()) { return {}; }
82  QStringList sl;
83  for (const CDistributor &d : *this)
84  {
85  if (!d.hasValidDbKey()) { continue; }
86  sl.append(d.getDbKey());
87  if (d.hasAlias1()) { sl.append(d.getAlias1()); }
88  if (d.hasAlias2()) { sl.append(d.getAlias2()); }
89  }
90  if (sort) { sl.sort(); }
91  return sl;
92  }
93 
95  {
96  if (this->isEmpty()) { return {}; }
97  CDistributorList distributors;
98  for (const CDistributor &distributor : (*this))
99  {
100  if (distributor.matchesSimulator(simulator)) { distributors.push_back(distributor); }
101  }
102  return distributors;
103  }
104 
106  {
107  return matchesSimulator(CSimulatorInfo::AllFsFamily);
108  }
109 
111 
113 } // namespace swift::misc::simulation
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition: range.h:109
int removeIf(Predicate p)
Remove elements for which a given predicate returns true.
Definition: sequence.h:446
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
const_iterator cbegin() const
Returns const iterator at the beginning of the sequence.
Definition: sequence.h:169
const_iterator cend() const
Returns const iterator one past the end of the sequence.
Definition: sequence.h:178
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
void sort(Predicate p)
In-place sort by a given comparator predicate.
Definition: sequence.h:560
CDistributor findByKey(QString key, const CDistributor &notFound=CDistributor()) const
Object with key, notFound otherwise.
CDistributorList findByKeys(const QSet< QString > &keys) const
Object with key, notFound otherwise.
bool isLoadedFromDb() const
Loaded from DB.
Definition: datastore.h:204
bool hasValidDbKey() const
Has valid DB key.
Definition: datastore.h:195
const QString & getDbKey() const
Get DB key.
Definition: datastore.h:180
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
const QString & getModelString() const
Model key, either queried or loaded from simulator model.
const QString & getDescription() const
Descriptive text.
const CDistributor & getDistributor() const
Get distributor.
Value object encapsulating information of software distributor.
Definition: distributor.h:33
static const QSet< QString > & standardAllFsFamily()
Hardcoded keys for standard models.
bool matchesKeyOrAlias(const QString &keyOrAlias) const
Matches key or alias.
Definition: distributor.cpp:38
bool hasCompleteData() const
Complete data?
const QString & getAlias2() const
Get alias2.
Definition: distributor.h:71
const QString & getAlias1() const
Get alias1.
Definition: distributor.h:68
Value object encapsulating a list of distributors.
CDistributor findByModelData(const CAircraftModel &model) const
Find by model string.
CDistributorList matchesSimulator(const CSimulatorInfo &simulator) const
Find for given simulator.
CDistributor findByKeyOrAlias(const QString &keyOrAlias) const
Find by id or alias.
bool isCompletelyFromDb() const
All data from DB?
QStringList getDbKeysAndAliases(bool sort) const
All DB keys and aliases.
int removeIfNotLoadedFromDb()
Remove distributors not from DB.
CDistributor smartDistributorSelector(const CDistributor &distributorPattern) const
Best match by given pattern.
CDistributorList matchesAnyFsFamily() const
Find all for all FS simulators.
CDistributorList()=default
Default constructor.
bool matchesAnyKeyOrAlias(const QString &keyOrAlias) const
At least is matching key or alias.
CDistributorList findFsFamilyStandard() const
Find the FS family standard distributors.
Simple hardcoded info about the corresponding simulator.
Definition: simulatorinfo.h:41
void append(QList< T > &&value)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
void sort(Qt::CaseSensitivity cs)
CaseInsensitive
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63