swift
platform.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/platform.h"
5 
6 #include <QJsonValue>
7 #include <QtGlobal>
8 
9 #include "config/buildconfig.h"
10 #include "misc/comparefunctions.h"
11 #include "misc/icons.h"
12 
13 using namespace swift::config;
14 
16 
17 namespace swift::misc
18 {
19  CPlatform::CPlatform(const QString &p) : m_platform(stringToPlatform(p)) {}
20 
21  CPlatform::CPlatform(Platform p) : m_platform(p) {}
22 
24  {
25  if (this->isSinglePlatform()) { return static_cast<PlatformFlag>(m_platform); }
26  return UnknownOs;
27  }
28 
29  bool CPlatform::matchesAny(const CPlatform &otherPlatform) const
30  {
31  return (this->m_platform & otherPlatform.m_platform) > 0;
32  }
33 
35  {
36  const Platform p = this->getPlatform();
37  int c = 0;
38  if (p.testFlag(Win32)) c++;
39  if (p.testFlag(Win64)) c++;
40  if (p.testFlag(MacOS)) c++;
41  if (p.testFlag(Linux)) c++;
42  return c;
43  }
44 
45  bool CPlatform::isSinglePlatform() const { return this->numberPlatforms() == 1; }
46 
48  {
49  const Platform p = this->getPlatform();
50  return p.testFlag(Win32) || p.testFlag(Win64);
51  }
52 
53  QString CPlatform::getPlatformName() const { return this->convertToQString(true); }
54 
55  CIcons::IconIndex CPlatform::toIcon() const { return CIcons::StandardIconEmpty; }
56 
57  QString CPlatform::convertToQString(bool i18n) const
58  {
59  Q_UNUSED(i18n);
60  switch (m_platform)
61  {
62  case Win32: return QStringLiteral("Win32");
63  case Win64: return QStringLiteral("Win64");
64  case Linux: return QStringLiteral("Linux");
65  case MacOS: return QStringLiteral("MacOSX");
66  default: break;
67  }
68  return QStringLiteral("unknown");
69  }
70 
72  {
73  if (index.isMyself()) { return QVariant::fromValue(*this); }
74  const ColumnIndex i = index.frontCasted<ColumnIndex>();
75  switch (i)
76  {
77  case IndexPlatform: return QVariant::fromValue(m_platform);
78  default: return CValueObject::propertyByIndex(index);
79  }
80  }
81 
82  void CPlatform::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
83  {
84  if (index.isMyself())
85  {
86  (*this) = variant.value<CPlatform>();
87  return;
88  }
89  const ColumnIndex i = index.frontCasted<ColumnIndex>();
90  switch (i)
91  {
92  case IndexPlatform: this->setPlatform(static_cast<Platform>(variant.toInt())); break;
93  default: CValueObject::setPropertyByIndex(index, variant); break;
94  }
95  }
96 
97  int CPlatform::comparePropertyByIndex(CPropertyIndexRef index, const CPlatform &compareValue) const
98  {
99  if (index.isMyself()) { return Compare::compare(m_platform, compareValue.m_platform); }
100  const ColumnIndex i = index.frontCasted<ColumnIndex>();
101  switch (i)
102  {
103  case IndexPlatform: return Compare::compare(m_platform, compareValue.m_platform);
104  default: Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison possible");
105  }
106  return 0;
107  }
108 
109  CPlatform::Platform CPlatform::stringToPlatform(const QString &str)
110  {
111  const QString s(str.trimmed().toLower());
112  if (s.contains("win"))
113  {
114  if (s.contains("32")) return Win32;
115  return Win64;
116  }
117  if (s.contains("linux")) return Linux;
118  if (s.contains("mac") || s.contains("osx")) return MacOS;
119 
120  // special ones
121  if (s.contains("debian")) return Linux;
122  if (s.contains("darwin")) return MacOS;
123  if (s.contains("all")) return All;
124  if (s.contains("independent")) return Independent;
125 
126  return UnknownOs;
127  }
128 
129  const CPlatform &CPlatform::stringToPlatformObject(const QString &str)
130  {
131  switch (stringToPlatform(str))
132  {
133  case Win32: return CPlatform::win32Platform();
134  case Win64: return CPlatform::win64Platform();
135  case Linux: return CPlatform::linuxPlatform();
136  case MacOS: return CPlatform::macOSPlatform();
137  default: break;
138  }
139  return unknownOs();
140  }
141 
142  namespace private_ns
143  {
144  const CPlatform &currentPlatformImpl()
145  {
146  if (CBuildConfig::isRunningOnWindowsNtPlatform())
147  {
148  const int wordSize = CBuildConfig::buildWordSize();
149  return wordSize == 64 ? CPlatform::win64Platform() : CPlatform::win32Platform();
150  }
151 
152  if (CBuildConfig::isRunningOnLinuxPlatform()) { return CPlatform::linuxPlatform(); }
153  if (CBuildConfig::isRunningOnMacOSPlatform()) { return CPlatform::macOSPlatform(); }
154  return CPlatform::unknownOs();
155  }
156  } // namespace private_ns
157 
159  {
160  static const CPlatform p = private_ns::currentPlatformImpl();
161  return p;
162  }
163 
164  bool CPlatform::isCurrentPlatform(const QString &platform)
165  {
166  if (platform.isEmpty()) { return false; }
168  }
169 
170  bool CPlatform::isCurrentPlatform(const CPlatform &platform) { return platform == CPlatform::currentPlatform(); }
171 
173  {
174  if (platform == currentPlatform()) { return true; }
175  if (platform.isAnyWindows() && currentPlatform().isAnyWindows()) { return true; }
176  if (platform == CPlatform::allOs()) { return true; }
177  if (platform == CPlatform::independent()) { return true; }
178  return false;
179  }
180 
182  {
183  static const CPlatform p(Win32);
184  return p;
185  }
186 
188  {
189  static const CPlatform p(Win64);
190  return p;
191  }
192 
194  {
195  static const CPlatform p(Linux);
196  return p;
197  }
198 
200  {
201  static const CPlatform p(MacOS);
202  return p;
203  }
204 
206  {
207  static const CPlatform p(UnknownOs);
208  return p;
209  }
210 
212  {
213  static const CPlatform p(All);
214  return p;
215  }
216 
218  {
219  static const CPlatform p(All);
220  return p;
221  }
222 } // namespace swift::misc
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition: icons.h:32
Platform (i.e.
Definition: platform.h:24
bool matchesAny(const CPlatform &otherPlatform) const
Matches any other platform.
Definition: platform.cpp:29
static bool isCurrentPlatform(const QString &platform)
Is this the current platform.
Definition: platform.cpp:164
QString getPlatformName() const
Name of platform.
Definition: platform.cpp:53
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: platform.cpp:82
static const CPlatform & stringToPlatformObject(const QString &str)
Convert to enum.
Definition: platform.cpp:129
static Platform stringToPlatform(const QString &str)
Convert to enum.
Definition: platform.cpp:109
static const CPlatform & linuxPlatform()
Linux.
Definition: platform.cpp:193
static bool canRunOnCurrentPlatform(const CPlatform &platform)
Can run on this platform.
Definition: platform.cpp:172
static const CPlatform & macOSPlatform()
Mac OS.
Definition: platform.cpp:199
CIcons::IconIndex toIcon() const
Representing icon.
Definition: platform.cpp:55
static const CPlatform & unknownOs()
Unknown OS.
Definition: platform.cpp:205
bool isSinglePlatform() const
Single platform?
Definition: platform.cpp:45
CPlatform()
Constructor.
Definition: platform.h:49
void setPlatform(Platform p)
Set platform.
Definition: platform.h:79
static const CPlatform & win32Platform()
Win32.
Definition: platform.cpp:181
Platform getPlatform() const
Platform.
Definition: platform.h:58
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: platform.cpp:57
int numberPlatforms() const
Number of supported platforms.
Definition: platform.cpp:34
int comparePropertyByIndex(CPropertyIndexRef index, const CPlatform &compareValue) const
Compare for index.
Definition: platform.cpp:97
static const CPlatform & currentPlatform()
Current platform.
Definition: platform.cpp:158
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: platform.cpp:71
static const CPlatform & win64Platform()
Win64.
Definition: platform.cpp:187
static const CPlatform & allOs()
All OS.
Definition: platform.cpp:211
PlatformFlag getPlatformFlag() const
Platform flag.
Definition: platform.cpp:23
bool isAnyWindows() const
Any Windows.
Definition: platform.cpp:47
static const CPlatform & independent()
Independent OS.
Definition: platform.cpp:217
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
ColumnIndex
Base class enums.
Definition: mixinindex.h:44
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.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67