swift
applicationinfo.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/applicationinfo.h"
5 
6 #include <QDir>
7 #include <QStringBuilder>
8 
9 #include "config/buildconfig.h"
10 #include "misc/comparefunctions.h"
11 #include "misc/iconlist.h"
12 
13 using namespace swift::config;
14 
16 
17 namespace swift::misc
18 {
19  CApplicationInfo::CApplicationInfo(Application app)
20  : m_app(app), m_wordSize(CBuildConfig::buildWordSize()), m_exePath(QCoreApplication::applicationDirPath()),
21  m_version(CBuildConfig::getVersionString()), m_compileInfo(CBuildConfig::compiledWithInfo()),
22  m_platform(CBuildConfig::getPlatformString()), m_process(CProcessInfo::currentProcess())
23  {
24  if (app == CApplicationInfo::Unknown) { m_app = guessApplication(); }
25  }
26 
28  {
29  static const QString unknown("unknown");
30  static const QString launcher("launcher");
31  static const QString core("core");
32  static const QString gui("gui");
33  static const QString mapping("mapping tool");
34  static const QString unitTest("unit test");
35  static const QString sample("sample");
36 
37  switch (getApplication())
38  {
39  case Launcher: return launcher;
40  case PilotClientCore: return core;
41  case PilotClientGui: return gui;
42  case MappingTool: return mapping;
43  case UnitTest: return unitTest;
44  case Sample: return sample;
45  default: break;
46  }
47  return unknown;
48  }
49 
51  {
52  if (this->getExecutablePath().isEmpty()) { return false; }
53  const QDir d(this->getExecutablePath());
54  return d.exists();
55  }
56 
58  {
59  const Application a = this->getApplication();
60  return a == CApplicationInfo::Sample || a == CApplicationInfo::UnitTest;
61  }
62 
64  {
65  const Application a = this->getApplication();
66  return a == CApplicationInfo::UnitTest;
67  }
68 
69  bool CApplicationInfo::isNull() const { return this->getApplication() == Unknown && m_exePath.isNull(); }
70 
71  QString CApplicationInfo::asOtherSwiftVersionString(const QString &separator) const
72  {
73  return u"Version; " % this->getVersionString() % u" os: " % this->getPlatform() % separator % u"exe.path: " %
74  this->getExecutablePath() % separator % u"app.data: " % this->getApplicationDataDirectory();
75  }
76 
77  QString CApplicationInfo::convertToQString(bool i18n) const
78  {
79  return QStringLiteral("{ %1, %2, %3, %4 }")
80  .arg(this->getApplicationAsString(), m_exePath, m_version, m_process.convertToQString(i18n));
81  }
82 
84  {
85  switch (getApplication())
86  {
87  case Launcher: return CIcons::SwiftLauncher16;
88  case PilotClientCore: return CIcons::SwiftCore16;
89  case PilotClientGui: return CIcons::Swift16;
90  case MappingTool: return CIcons::SwiftDatabase16;
91  default: break;
92  }
93  return CIcons::StandardIconUnknown16;
94  }
95 
97  {
98  if (index.isMyself()) { return QVariant::fromValue(*this); }
99  const ColumnIndex i = index.frontCasted<ColumnIndex>();
100  switch (i)
101  {
102  case IndexApplication: return QVariant::fromValue(m_app);
103  case IndexApplicationAsString: return QVariant::fromValue(this->getApplicationAsString());
104  case IndexApplicationDataPath: return QVariant::fromValue(this->getApplicationDataDirectory());
105  case IndexCompileInfo: return QVariant::fromValue(this->getCompileInfo());
106  case IndexExecutablePath: return QVariant::fromValue(this->getExecutablePath());
107  case IndexExecutablePathExisting: return QVariant::fromValue(this->isExecutablePathExisting());
108  case IndexPlatformInfo: return QVariant::fromValue(this->getPlatform());
109  case IndexProcessInfo: return m_process.propertyByIndex(index.copyFrontRemoved());
110  case IndexVersionString: return QVariant::fromValue(this->getVersionString());
111  case IndexWordSize: return QVariant::fromValue(this->getWordSize());
112  default: break;
113  }
114  return CValueObject::propertyByIndex(index);
115  }
116 
117  void CApplicationInfo::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
118  {
119  if (index.isMyself())
120  {
121  (*this) = variant.value<CApplicationInfo>();
122  return;
123  }
124  const ColumnIndex i = index.frontCasted<ColumnIndex>();
125  switch (i)
126  {
127  case IndexApplication: this->setApplication(static_cast<Application>(variant.toInt())); break;
128  case IndexApplicationAsString: break;
129  case IndexApplicationDataPath: this->setApplicationDataDirectory(variant.toString()); break;
130  case IndexCompileInfo: this->setCompileInfo(variant.toString()); break;
131  case IndexExecutablePath: this->setExecutablePath(variant.toString()); break;
132  case IndexExecutablePathExisting: break;
133  case IndexPlatformInfo: this->setPlatformInfo(variant.toString()); break;
134  case IndexProcessInfo: m_process.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
135  case IndexVersionString: this->setVersionString(variant.toString()); break;
136  case IndexWordSize: this->setWordSize(variant.toInt()); break;
137  default: break;
138  }
139  CValueObject::setPropertyByIndex(index, variant);
140  }
141 
143  {
144  if (index.isMyself()) { return this->getExecutablePath().compare(compareValue.getExecutablePath()); }
145  const ColumnIndex i = index.frontCasted<ColumnIndex>();
146  switch (i)
147  {
148  case IndexApplicationDataPath:
149  return this->getApplicationDataDirectory().compare(compareValue.getApplicationDataDirectory());
150  case IndexCompileInfo: return this->getCompileInfo().compare(compareValue.getCompileInfo());
151  case IndexExecutablePath: return this->getExecutablePath().compare(compareValue.getExecutablePath());
152  case IndexExecutablePathExisting:
153  return Compare::compare(this->isExecutablePathExisting(), compareValue.isExecutablePathExisting());
154  case IndexPlatformInfo: return this->getPlatform().compare(compareValue.getPlatform());
155  case IndexProcessInfo:
156  return this->getProcessInfo().processName().compare(compareValue.getProcessInfo().processName());
157  case IndexVersionString: return this->getVersionString().compare(compareValue.getVersionString());
158  case IndexWordSize: return Compare::compare(this->getWordSize(), compareValue.getWordSize());
159  case IndexApplication:
160  case IndexApplicationAsString: return Compare::compare(m_app, compareValue.m_app);
161  default: return CValueObject::comparePropertyByIndex(index.copyFrontRemoved(), compareValue);
162  }
163  }
164 
166  {
167  static const QString s("swift pilot client GUI");
168  return s;
169  }
170 
172  {
173  static const QString s("swift launcher");
174  return s;
175  }
176 
178  {
179  static const QString s("swift mapping tool");
180  return s;
181  }
182 
184  {
185  static const QString s("swift core");
186  return s;
187  }
188 
190  {
191  static const CApplicationInfo info(CApplicationInfo::Unknown);
192  return info;
193  }
194 
195  const QString &CApplicationInfo::fileName()
196  {
197  static const QString fn("appinfo.json");
198  return fn;
199  }
200 
202  {
203  static const CApplicationInfo n;
204  return n;
205  }
206 
207  CApplicationInfo::Application CApplicationInfo::guessApplication()
208  {
209  const QString a(QCoreApplication::instance()->applicationName().toLower());
210  if (a.contains("test")) { return CApplicationInfo::UnitTest; } // names like testcore
211  if (a.contains("sample")) { return CApplicationInfo::Sample; }
212  if (a.contains("core")) { return CApplicationInfo::PilotClientCore; }
213  if (a.contains("launcher")) { return CApplicationInfo::Launcher; }
214  if (a.contains("gui")) { return CApplicationInfo::PilotClientGui; }
215  if (a.contains("data") || a.contains("mapping")) { return CApplicationInfo::MappingTool; }
216  return CApplicationInfo::Unknown;
217  }
218 } // namespace swift::misc
Build configuration, also used to secure VATSIM key.
Definition: buildconfig.h:16
Description of a swift application.
int comparePropertyByIndex(CPropertyIndexRef index, const CApplicationInfo &compareValue) const
Compare for index.
CIcons::IconIndex toIcon() const
As icon, not implemented by all classes.
Application getApplication() const
Get application.
static const QString & swiftCore()
Name of swift core.
void setVersionString(const QString &version)
Set version string.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
static const CApplicationInfo & autoInfo()
Info automatically initialized.
static const QString & fileName()
File name of the application info file.
const QString & getExecutablePath() const
Get executable path.
Application
Enumeration of application roles.
ColumnIndex
Properties by index.
bool isExecutablePathExisting() const
Is the executable path existing?
const QString & getPlatform() const
Get platform.
const QString & getApplicationAsString() const
get application as string
static const CApplicationInfo & null()
NULL info.
bool isUnitTest() const
Unit test.
int getWordSize() const
Word size.
void setExecutablePath(const QString &exePath)
Set executable path.
const QString & getApplicationDataDirectory() const
Set application data dir.
const QString & getVersionString() const
Get version string.
QString asOtherSwiftVersionString(const QString &separator=" | ") const
Formatted info.
void setCompileInfo(const QString &info)
Compile info.
QString convertToQString(bool i18n=false) const
Cast as QString.
bool isSampleOrUnitTest() const
Sample or unit test.
const CProcessInfo & getProcessInfo() const
Get process info.
void setWordSize(int size)
Word size.
static const QString & swiftPilotClientGui()
Name of pilot client GUI.
void setApplication(Application app)
Set application.
static const QString & swiftMappingTool()
Name of swift mapping tool.
void setApplicationDataDirectory(const QString &appDataDir)
Set application data dir.
static const QString & swiftLauncher()
Name of swift launcher.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
const QString & getCompileInfo() const
Compile info.
bool isNull() const
Null object.
void setPlatformInfo(const QString &platform)
Set platform.
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition: icons.h:32
Value class identifying a process, with a PID and a name.
Definition: processinfo.h:22
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: processinfo.cpp:28
const QString & processName() const
Get the process name.
Definition: processinfo.h:46
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.
int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
Compare for index.
Definition: mixinindex.h:187
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
Free functions in swift::misc.
QString applicationName()
Get application name.
Definition: filelogger.cpp:23
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67