swift
globalsetup.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 <QJsonObject>
7 #include <QStringBuilder>
8 #include <QStringList>
9 #include <QSysInfo>
10 #include <QVersionNumber>
11 
12 #include "config/buildconfig.h"
13 #include "core/application.h"
14 #include "misc/crashhandler.h"
15 #include "misc/directoryutils.h"
16 #include "misc/json.h"
17 #include "misc/network/server.h"
18 #include "misc/network/user.h"
19 #include "misc/stringutils.h"
20 #include "misc/swiftdirectories.h"
21 
22 using namespace swift::config;
23 using namespace swift::misc;
24 using namespace swift::misc::json;
25 using namespace swift::misc::network;
26 
28 
29 namespace swift::core::data
30 {
31  CGlobalSetup::CGlobalSetup() : CIdentifiable("CGlobalSetup") {}
32 
34 
36 
38 
40 
41  const CUrl &CGlobalSetup::getSwiftSharedUrl() const { return m_sharedUrl; }
43 
44  CUrl CGlobalSetup::getHelpPageUrl(const QString &context) const
45  {
46  // we display in the standard browser,
47  // so the user will realize if the URL does not work
48  CUrl url = m_onlineHelpUrl;
49  if (url.isEmpty()) { return url; }
50 
51  // context string something like "application.moreSpecific.evenMoreSpecific"
52  QString c = "client";
53  if (QCoreApplication::instance()) { c = QCoreApplication::instance()->applicationName(); }
54  if (!context.isEmpty()) { c += "." + context; }
55  url.appendQuery("context", c);
56  return url;
57  }
58 
60 
62  {
63  return getDbRootDirectoryUrl()
64  .withAppendedPath("/service/jsonauthenticate.php")
65  .withSwitchedScheme("https", m_dbHttpsPort);
66  }
67 
68  bool CGlobalSetup::dbDebugFlag() const { return m_dbDebugFlag && CBuildConfig::isLocalDeveloperDebugBuild(); }
69 
70  void CGlobalSetup::setServerDebugFlag(bool debug) { m_dbDebugFlag = debug; }
71 
73  {
74  return m_sharedUrl.withAppendedPath(CGlobalSetup::schemaVersionString() + "/dbdata/");
75  }
76 
78  {
79  static const CServerList hardcoded({ CServer::fscFsdServer(), CServer::esTowerView() });
80  CServerList testServers(m_predefinedServers);
81  testServers.addIfAddressNotExists(hardcoded);
82  return testServers;
83  }
84 
86  {
87  if (m_mappingMinimumVersion.isEmpty()) { return false; }
88  const QVersionNumber min = QVersionNumber::fromString(this->getMappingMinimumVersionString());
89  return CBuildConfig::getVersion() >= min;
90  }
91 
92  QString CGlobalSetup::convertToQString(bool i18n) const { return convertToQString(", ", i18n); }
93 
94  QString CGlobalSetup::convertToQString(const QString &separator, bool i18n) const
95  {
96  QString s = u"Global setup loaded: " % separator % u"Mapping min.version: " %
97  this->getMappingMinimumVersionString() % separator
98 
99  % u"Help URL: " % m_onlineHelpUrl.toQString(i18n) % separator;
100  s += u"DB root directory: " % getDbRootDirectoryUrl().toQString(i18n) % separator % u"ICAO DB reader: " %
101  getDbIcaoReaderUrl().toQString(i18n) % separator % u"Model DB reader: " %
102  getDbModelReaderUrl().toQString(i18n) % separator % u"Airport DB reader: " %
103  getDbAirportReaderUrl().toQString(i18n) % separator % u"DB home page: " %
104  getDbHomePageUrl().toQString(i18n) % separator % u"DB login service: " %
105  getDbLoginServiceUrl().toQString(i18n) % separator;
106  s += u"VATSIM METARs: " % getVatsimMetarsUrl().toQString(i18n) % separator % u"VATSIM data file: " %
107  getVatsimDataFileUrl().toQString(i18n) % separator % u"VATSIM server file: " %
108  getVatsimServerFileUrl().toQString(i18n) % separator
109 
110  % u"Predefined servers: " % getPredefinedServers().toQString(i18n) % separator
111 
112  % u"AFV voice server URL: " % getAfvApiServerUrl().toQString(i18n) % separator
113 
114  % u"AFV map URL: " % getAfvMapUrl().toQString(i18n);
115 
116  return s;
117  }
118 
120  {
121  if (index.isMyself()) { return CVariant::fromValue(*this); }
122 
123  const ColumnIndex i = index.frontCasted<ColumnIndex>();
124  switch (i)
125  {
126  case IndexDbRootDirectory: return QVariant::fromValue(m_dbRootDirectoryUrl);
127  case IndexDbHttpsPort: return QVariant::fromValue(m_dbHttpsPort);
128  case IndexDbLoginService: return QVariant::fromValue(this->getDbLoginServiceUrl());
129  case IndexVatsimStatus: return QVariant::fromValue(m_vatsimStatusFileUrl);
130  case IndexVatsimData: return QVariant::fromValue(m_vatsimDataFileUrl);
131  case IndexVatsimServer: return QVariant::fromValue(m_vatsimServerFileUrl);
132  case IndexVatsimHttpFsd: return QVariant::fromValue(m_vatsimFsdHttpUrl);
133  case IndexVatsimMetars: return QVariant::fromValue(m_vatsimMetarsUrl);
134  case IndexSharedUrl: return QVariant::fromValue(m_sharedUrl);
135  case IndexOnlineHelpUrl: return QVariant::fromValue(m_onlineHelpUrl);
136  case IndexMappingMinimumVersion: return QVariant::fromValue(m_mappingMinimumVersion);
137  case IndexPredefinedServers: return QVariant::fromValue(m_predefinedServers);
138  case IndexAfvApiServerUrl: return QVariant::fromValue(m_afvApiServerUrl);
139  case IndexAfvMapUrl: return QVariant::fromValue(m_afvMapUrl);
140  default: return CValueObject::propertyByIndex(index);
141  }
142  }
143 
144  void CGlobalSetup::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
145  {
146  if (index.isMyself())
147  {
148  (*this) = variant.value<CGlobalSetup>();
149  return;
150  }
151 
152  const ColumnIndex i = index.frontCasted<ColumnIndex>();
153  switch (i)
154  {
155  case IndexDbRootDirectory: m_dbRootDirectoryUrl.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
156  case IndexDbHttpsPort: m_dbHttpsPort = variant.toInt(); break;
157  case IndexDbLoginService: break; // cannot be changed
158  case IndexVatsimData: m_vatsimDataFileUrl = variant.value<CUrl>(); break;
159  case IndexVatsimServer: m_vatsimServerFileUrl = variant.value<CUrl>(); break;
160  case IndexVatsimHttpFsd: m_vatsimFsdHttpUrl = variant.value<CUrl>(); break;
161  case IndexVatsimMetars: m_vatsimMetarsUrl = variant.value<CUrl>(); break;
162  case IndexSharedUrl: m_sharedUrl = variant.value<CUrl>(); break;
163  case IndexOnlineHelpUrl: m_onlineHelpUrl = variant.value<CUrl>(); break;
164  case IndexMappingMinimumVersion: m_mappingMinimumVersion = variant.toString(); break;
165  case IndexPredefinedServers: m_predefinedServers = variant.value<CServerList>(); break;
166  case IndexAfvApiServerUrl: m_afvApiServerUrl = variant.value<CUrl>(); break;
167  case IndexAfvMapUrl: m_afvMapUrl = variant.value<CUrl>(); break;
168  default: CValueObject::setPropertyByIndex(index, variant); break;
169  }
170  }
171 
173  {
174  // This is not the current swift version, but the schema version
175  static const QString v("0.7.0");
176  return v;
177  }
178 } // namespace swift::core::data
Global settings for readers, debug flags, etc.
Definition: globalsetup.h:31
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
const swift::misc::network::CUrl & getSwiftSharedUrl() const
Shared URL.
Definition: globalsetup.cpp:41
swift::misc::network::CUrl getVatsimServerFileUrl() const
VATSIM server file URL.
Definition: globalsetup.h:108
bool dbDebugFlag() const
Debug flag.
Definition: globalsetup.cpp:68
bool isSwiftVersionMinimumMappingVersion() const
Meets the minimum mapping version.
Definition: globalsetup.cpp:85
const QString & getMappingMinimumVersionString() const
Creating mappings requires at least this version or higher.
Definition: globalsetup.h:124
swift::misc::network::CUrl getHelpPageUrl(const QString &context={}) const
Help page URL.
Definition: globalsetup.cpp:44
swift::misc::network::CUrl getDbIcaoReaderUrl() const
ICAO reader URL.
Definition: globalsetup.cpp:33
swift::misc::network::CUrl getDbAirportReaderUrl() const
Airport reader URL.
Definition: globalsetup.cpp:37
swift::misc::network::CUrl getDbHomePageUrl() const
Home page url.
Definition: globalsetup.cpp:42
swift::misc::network::CUrl getDbInfoReaderUrl() const
Info data reader URL.
Definition: globalsetup.cpp:39
ColumnIndex
Properties by index.
Definition: globalsetup.h:35
swift::misc::network::CUrl getDbLoginServiceUrl() const
Login service.
Definition: globalsetup.cpp:61
swift::misc::network::CUrl getLegalDirectoryUrl() const
Legal directory URL.
Definition: globalsetup.cpp:59
const swift::misc::network::CUrl & getDbRootDirectoryUrl() const
Root directory of DB.
Definition: globalsetup.h:65
void setServerDebugFlag(bool debug)
Set debug flag.
Definition: globalsetup.cpp:70
const swift::misc::network::CUrl & getVatsimDataFileUrl() const
VATSIM data file URL.
Definition: globalsetup.h:105
static const QString & schemaVersionString()
Schema version (shared files, bootstrap file)
swift::misc::network::CServerList getPredefinedServersPlusHardcodedServers() const
Predefined plus hardcoded.
Definition: globalsetup.cpp:77
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: globalsetup.cpp:92
const swift::misc::network::CUrl & getVatsimMetarsUrl() const
VATSIM METAR URL.
Definition: globalsetup.h:99
swift::misc::network::CUrl getDbModelReaderUrl() const
Model reader URL.
Definition: globalsetup.cpp:35
swift::misc::network::CUrl getAfvMapUrl() const
AFV map URL.
Definition: globalsetup.h:140
const swift::misc::network::CServerList & getPredefinedServers() const
Predefined servers.
Definition: globalsetup.h:117
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
swift::misc::network::CUrl getSharedDbDataDirectoryUrl()
Get shared DB data directory URL.
Definition: globalsetup.cpp:72
swift::misc::network::CUrl getAfvApiServerUrl() const
AFV voice server URL.
Definition: globalsetup.h:137
Base class with a member CIdentifier to be inherited by a class which has an identity in the environm...
Definition: identifiable.h:24
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.
static CVariant fromValue(T &&value)
Construct a variant from a value.
Definition: variant.h:139
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Value object encapsulating a list of servers.
Definition: serverlist.h:23
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition: url.h:27
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: url.cpp:245
CUrl withSwitchedScheme(const QString &protocol, int port) const
Switch protocol.
Definition: url.cpp:123
bool isEmpty() const
Empty.
Definition: url.cpp:54
CUrl withAppendedPath(const QString &path) const
Append path.
Definition: url.cpp:115
void appendQuery(const QString &queryToAppend)
Append query.
Definition: url.cpp:66
Core data traits (aka cached values) and classes.
Free functions in swift::misc.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67