swift
Loading...
Searching...
No Matches
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
11#include "misc/icons.h"
12
13using namespace swift::config;
14
16
17namespace 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())
26 {
27 return static_cast<PlatformFlag>(m_platform);
28 }
29 return UnknownOs;
30 }
31
32 bool CPlatform::matchesAny(const CPlatform &otherPlatform) const
33 {
34 return (this->m_platform & otherPlatform.m_platform) > 0;
35 }
36
38 {
39 const Platform p = this->getPlatform();
40 int c = 0;
41 if (p.testFlag(Win32)) c++;
42 if (p.testFlag(Win64)) c++;
43 if (p.testFlag(MacOS)) c++;
44 if (p.testFlag(Linux)) c++;
45 return c;
46 }
47
48 bool CPlatform::isSinglePlatform() const { return this->numberPlatforms() == 1; }
49
51 {
52 const Platform p = this->getPlatform();
53 return p.testFlag(Win32) || p.testFlag(Win64);
54 }
55
56 QString CPlatform::getPlatformName() const { return this->convertToQString(true); }
57
58 CIcons::IconIndex CPlatform::toIcon() const { return CIcons::StandardIconEmpty; }
59
61 {
62 Q_UNUSED(i18n);
63 switch (m_platform)
64 {
65 case Win32: return QStringLiteral("Win32");
66 case Win64: return QStringLiteral("Win64");
67 case Linux: return QStringLiteral("Linux");
68 case MacOS: return QStringLiteral("MacOSX");
69 default: break;
70 }
71 return QStringLiteral("unknown");
72 }
73
75 {
76 if (index.isMyself())
77 {
78 return QVariant::fromValue(*this);
79 }
80 const auto i = index.frontCasted<ColumnIndex>();
81 switch (i)
82 {
83 case IndexPlatform: return QVariant::fromValue(m_platform);
84 default: return CValueObject::propertyByIndex(index);
85 }
86 }
87
89 {
90 if (index.isMyself())
91 {
92 (*this) = variant.value<CPlatform>();
93 return;
94 }
95 const auto i = index.frontCasted<ColumnIndex>();
96 switch (i)
97 {
98 case IndexPlatform: this->setPlatform(static_cast<Platform>(variant.toInt())); break;
99 default: CValueObject::setPropertyByIndex(index, variant); break;
100 }
101 }
102
104 {
105 if (index.isMyself())
106 {
107 return Compare::compare(m_platform, compareValue.m_platform);
108 }
109 const auto i = index.frontCasted<ColumnIndex>();
110 switch (i)
111 {
112 case IndexPlatform: return Compare::compare(m_platform, compareValue.m_platform);
113 default: Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison possible");
114 }
115 return 0;
116 }
117
118 CPlatform::Platform CPlatform::stringToPlatform(const QString &str)
119 {
120 const QString s(str.trimmed().toLower());
121 if (s.contains("win"))
122 {
123 if (s.contains("32")) return Win32;
124 return Win64;
125 }
126 if (s.contains("linux")) return Linux;
127 if (s.contains("mac") || s.contains("osx")) return MacOS;
128
129 // special ones
130 if (s.contains("debian")) return Linux;
131 if (s.contains("darwin")) return MacOS;
132 if (s.contains("all")) return All;
133 if (s.contains("independent")) return Independent;
134
135 return UnknownOs;
136 }
137
139 {
140 switch (stringToPlatform(str))
141 {
142 case Win32: return CPlatform::win32Platform();
143 case Win64: return CPlatform::win64Platform();
144 case Linux: return CPlatform::linuxPlatform();
145 case MacOS: return CPlatform::macOSPlatform();
146 default: break;
147 }
148 return unknownOs();
149 }
150
151 namespace private_ns
152 {
153 const CPlatform &currentPlatformImpl()
154 {
156 {
157 const int wordSize = CBuildConfig::buildWordSize();
158 return wordSize == 64 ? CPlatform::win64Platform() : CPlatform::win32Platform();
159 }
160
162 {
164 }
166 {
168 }
169 return CPlatform::unknownOs();
170 }
171 } // namespace private_ns
172
174 {
175 static const CPlatform p = private_ns::currentPlatformImpl();
176 return p;
177 }
178
180 {
181 if (platform.isEmpty())
182 {
183 return false;
184 }
186 }
187
188 bool CPlatform::isCurrentPlatform(const CPlatform &platform) { return platform == CPlatform::currentPlatform(); }
189
191 {
192 if (platform == currentPlatform())
193 {
194 return true;
195 }
196 if (platform.isAnyWindows() && currentPlatform().isAnyWindows())
197 {
198 return true;
199 }
200 if (platform == CPlatform::allOs())
201 {
202 return true;
203 }
204 if (platform == CPlatform::independent())
205 {
206 return true;
207 }
208 return false;
209 }
210
212 {
213 static const CPlatform p(Win32);
214 return p;
215 }
216
218 {
219 static const CPlatform p(Win64);
220 return p;
221 }
222
224 {
225 static const CPlatform p(Linux);
226 return p;
227 }
228
230 {
231 static const CPlatform p(MacOS);
232 return p;
233 }
234
236 {
237 static const CPlatform p(UnknownOs);
238 return p;
239 }
240
242 {
243 static const CPlatform p(All);
244 return p;
245 }
246
248 {
249 static const CPlatform p(All);
250 return p;
251 }
252} // namespace swift::misc
static constexpr bool isRunningOnMacOSPlatform()
Running on MacOS platform?
static constexpr bool isRunningOnWindowsNtPlatform()
Running on Windows NT platform?
static constexpr bool isRunningOnLinuxPlatform()
Running on Linux platform?
static int buildWordSize()
Whether this swift application is build as 32 or 64bit application.
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:32
static bool isCurrentPlatform(const QString &platform)
Is this the current platform.
Definition platform.cpp:179
QString getPlatformName() const
Name of platform.
Definition platform.cpp:56
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition platform.cpp:88
static const CPlatform & stringToPlatformObject(const QString &str)
Convert to enum.
Definition platform.cpp:138
static Platform stringToPlatform(const QString &str)
Convert to enum.
Definition platform.cpp:118
static const CPlatform & linuxPlatform()
Linux.
Definition platform.cpp:223
static bool canRunOnCurrentPlatform(const CPlatform &platform)
Can run on this platform.
Definition platform.cpp:190
static const CPlatform & macOSPlatform()
Mac OS.
Definition platform.cpp:229
CIcons::IconIndex toIcon() const
Representing icon.
Definition platform.cpp:58
CPlatform()=default
Constructor.
static const CPlatform & unknownOs()
Unknown OS.
Definition platform.cpp:235
bool isSinglePlatform() const
Single platform?
Definition platform.cpp:48
void setPlatform(Platform p)
Set platform.
Definition platform.h:79
static const CPlatform & win32Platform()
Win32.
Definition platform.cpp:211
Platform getPlatform() const
Platform.
Definition platform.h:58
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition platform.cpp:60
int numberPlatforms() const
Number of supported platforms.
Definition platform.cpp:37
int comparePropertyByIndex(CPropertyIndexRef index, const CPlatform &compareValue) const
Compare for index.
Definition platform.cpp:103
static const CPlatform & currentPlatform()
Current platform.
Definition platform.cpp:173
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition platform.cpp:74
static const CPlatform & win64Platform()
Win64.
Definition platform.cpp:217
static const CPlatform & allOs()
All OS.
Definition platform.cpp:241
ColumnIndex
Properties by index.
Definition platform.h:28
PlatformFlag getPlatformFlag() const
Platform flag.
Definition platform.cpp:23
bool isAnyWindows() const
Any Windows.
Definition platform.cpp:50
static const CPlatform & independent()
Independent OS.
Definition platform.cpp:247
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.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
int compare(T a, T b)
Compare arithmetic values.
Free functions in swift::misc.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString toLower() const const
QString trimmed() const const
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67