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  CDistributor CDistributorList::findByKeyOrAlias(const QString &keyOrAlias) const
20  {
21  if (keyOrAlias.isEmpty()) { return CDistributor(); }
22  for (const CDistributor &distributor : (*this))
23  {
24  if (distributor.matchesKeyOrAlias(keyOrAlias)) { return distributor; }
25  }
26  return CDistributor();
27  }
28 
30  {
31  // some stuipd hardcoded resolutions for distributors
32  if (model.getDistributor().hasValidDbKey()) { return model.getDistributor(); }
33  if (model.getModelString().startsWith("WOA", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("WOAI"); }
34  if (model.getDescription().contains("WOA", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("WOAI"); }
35  if (model.getDescription().contains("IVAO", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("IVAO"); }
36  if (model.getModelString().startsWith("PAI ", Qt::CaseInsensitive)) { return this->findByKeyOrAlias("PAI"); }
37  if (model.getDescription().startsWith("Project AI ", Qt::CaseInsensitive))
38  {
39  return this->findByKeyOrAlias("PAI");
40  }
41 
42  return CDistributor();
43  }
44 
46  {
48  }
49 
51  {
52  // key is not necessarily a DB key, so use complete data, happens when key is set from raw data
53  if (distributor.isLoadedFromDb()) { return distributor; }
54  if (distributor.hasValidDbKey())
55  {
56  const QString key(distributor.getDbKey());
57  CDistributor d(this->findByKey(key));
58  if (d.hasCompleteData()) { return d; }
59 
60  // more lenient search
61  return this->findByKeyOrAlias(key);
62  }
63  return CDistributor();
64  }
65 
67  const CAircraftModel &model) const
68  {
69  const CDistributor d = this->smartDistributorSelector(distributorPattern);
70  // key is not necessarily a DB key, so use complete data, happens when key is set from raw data
71  if (d.hasCompleteData()) { return d; }
72  return this->findByModelData(model);
73  }
74 
75  bool CDistributorList::matchesAnyKeyOrAlias(const QString &keyOrAlias) const
76  {
77  for (const CDistributor &distributor : (*this))
78  {
79  if (distributor.matchesKeyOrAlias(keyOrAlias)) { return true; }
80  }
81  return false;
82  }
83 
84  QStringList CDistributorList::getDbKeysAndAliases(bool sort) const
85  {
86  if (this->isEmpty()) { return QStringList(); }
87  QStringList sl;
88  for (const CDistributor &d : *this)
89  {
90  if (!d.hasValidDbKey()) { continue; }
91  sl.append(d.getDbKey());
92  if (d.hasAlias1()) { sl.append(d.getAlias1()); }
93  if (d.hasAlias2()) { sl.append(d.getAlias2()); }
94  }
95  if (sort) { sl.sort(); }
96  return sl;
97  }
98 
100  {
101  if (this->isEmpty()) { return CDistributorList(); }
102  CDistributorList distributors;
103  for (const CDistributor &distributor : (*this))
104  {
105  if (distributor.matchesSimulator(simulator)) { distributors.push_back(distributor); }
106  }
107  return distributors;
108  }
109 
111  {
112  return matchesSimulator(CSimulatorInfo::AllFsFamily);
113  }
114 
116 
118 } // 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
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 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.
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
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63