Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qoperatingsystemversion.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#if !defined(Q_OS_DARWIN) && !defined(Q_OS_WIN)
8#endif
9
10#if defined(Q_OS_DARWIN)
11#include <QtCore/private/qcore_mac_p.h>
12#endif
13
14#include <qversionnumber.h>
15#include <qdebug.h>
16
17#ifdef Q_OS_ANDROID
18#include <QtCore/private/qjnihelpers_p.h>
19#include <QJniObject>
20#endif
21
23
119{
121}
122
124{
125 static const QOperatingSystemVersionBase v = current_impl();
126 return v;
127}
128
129#if !defined(Q_OS_DARWIN) && !defined(Q_OS_WIN)
130QOperatingSystemVersionBase QOperatingSystemVersionBase::current_impl()
131{
133 version.m_os = currentType();
134#ifdef Q_OS_ANDROID
135#ifndef QT_BOOTSTRAPPED
136 const QVersionNumber v = QVersionNumber::fromString(QJniObject::getStaticObjectField(
137 "android/os/Build$VERSION", "RELEASE", "Ljava/lang/String;").toString());
138 if (!v.isNull()) {
139 version.m_major = v.majorVersion();
140 version.m_minor = v.minorVersion();
141 version.m_micro = v.microVersion();
142 return version;
143 }
144#endif
145
146 version.m_major = -1;
147 version.m_minor = -1;
148
149 static const struct {
150 uint major : 4;
151 uint minor : 4;
152 } versions[] = {
153 { 1, 0 }, // API level 1
154 { 1, 1 }, // API level 2
155 { 1, 5 }, // API level 3
156 { 1, 6 }, // API level 4
157 { 2, 0 }, // API level 5
158 { 2, 0 }, // API level 6
159 { 2, 1 }, // API level 7
160 { 2, 2 }, // API level 8
161 { 2, 3 }, // API level 9
162 { 2, 3 }, // API level 10
163 { 3, 0 }, // API level 11
164 { 3, 1 }, // API level 12
165 { 3, 2 }, // API level 13
166 { 4, 0 }, // API level 14
167 { 4, 0 }, // API level 15
168 { 4, 1 }, // API level 16
169 { 4, 2 }, // API level 17
170 { 4, 3 }, // API level 18
171 { 4, 4 }, // API level 19
172 { 4, 4 }, // API level 20
173 { 5, 0 }, // API level 21
174 { 5, 1 }, // API level 22
175 { 6, 0 }, // API level 23
176 { 7, 0 }, // API level 24
177 { 7, 1 }, // API level 25
178 { 8, 0 }, // API level 26
179 { 8, 1 }, // API level 27
180 { 9, 0 }, // API level 28
181 { 10, 0 }, // API level 29
182 { 11, 0 }, // API level 30
183 { 12, 0 }, // API level 31
184 { 12, 0 }, // API level 32
185 { 13, 0 }, // API level 33
186 };
187
188 // This will give us at least the first 2 version components
189 const size_t versionIdx = QtAndroidPrivate::androidSdkVersion() - 1;
190 if (versionIdx < sizeof(versions) / sizeof(versions[0])) {
191 version.m_major = versions[versionIdx].major;
192 version.m_minor = versions[versionIdx].minor;
193 }
194
195 // API level 6 was exactly version 2.0.1
196 version.m_micro = versionIdx == 5 ? 1 : -1;
197#else
198 version.m_major = -1;
199 version.m_minor = -1;
200 version.m_micro = -1;
201#endif
202 return version;
203}
204#endif
205
206static inline int compareVersionComponents(int lhs, int rhs)
207{
208 return lhs >= 0 && rhs >= 0 ? lhs - rhs : 0;
209}
210
213{
214 if (v1.m_major == v2.m_major) {
215 if (v1.m_minor == v2.m_minor) {
216 return compareVersionComponents(v1.m_micro, v2.m_micro);
217 }
218 return compareVersionComponents(v1.m_minor, v2.m_minor);
219 }
220 return compareVersionComponents(v1.m_major, v2.m_major);
221}
222
310{
312}
313
315{
316 switch (osversion.type()) {
318 return QStringLiteral("Windows");
320 if (osversion.majorVersion() < 10)
321 return QStringLiteral("Mac OS");
322 if (osversion.majorVersion() == 10 && osversion.minorVersion() < 8)
323 return QStringLiteral("Mac OS X");
324 if (osversion.majorVersion() == 10 && osversion.minorVersion() < 12)
325 return QStringLiteral("OS X");
326 return QStringLiteral("macOS");
327 }
329 if (osversion.majorVersion() < 4)
330 return QStringLiteral("iPhone OS");
331 return QStringLiteral("iOS");
332 }
334 return QStringLiteral("tvOS");
336 return QStringLiteral("watchOS");
338 return QStringLiteral("Android");
340 default:
341 return QString();
342 }
343}
344
351bool QOperatingSystemVersion::isAnyOfType(std::initializer_list<OSType> types) const
352{
353 // ### Qt7: Remove this function
354 return std::find(types.begin(), types.end(), type()) != types.end();
355}
356
357bool QOperatingSystemVersionBase::isAnyOfType(std::initializer_list<OSType> types, OSType type)
358{
359 return std::find(types.begin(), types.end(), type) != types.end();
360}
361
362#ifndef QT_BOOTSTRAPPED
363
371
379
387
395
403
411
419
427
435
443
451
459
467
474
481
489
497
505
513
521
529
537
545
553
560
566
574
583
592
600
608
617
625
633
642
650
658
666
674
682
689
696
703
704#endif // !QT_BOOTSTRAPPED
705
706#ifndef QT_NO_DEBUG_STREAM
708{
709 QDebugStateSaver saver(debug);
710 debug.nospace();
711 debug << "QOperatingSystemVersion(" << ov.name()
712 << ", " << ov.majorVersion() << '.' << ov.minorVersion()
713 << '.' << ov.microVersion() << ')';
714 return debug;
715}
716#endif // !QT_NO_DEBUG_STREAM
717
\inmodule QtCore
\inmodule QtCore
static Q_CORE_EXPORT QOperatingSystemVersionBase current()
static Q_CORE_EXPORT bool isAnyOfType(std::initializer_list< OSType > types, OSType type)
static Q_CORE_EXPORT int compare(QOperatingSystemVersionBase v1, QOperatingSystemVersionBase v2)
static constexpr OSType currentType()
static constexpr QOperatingSystemVersionBase Windows11_21H2
\variable QOperatingSystemVersion::Windows11_21H2
static constexpr QOperatingSystemVersionBase AndroidNougat
\variable QOperatingSystemVersion::AndroidNougat
static constexpr QOperatingSystemVersionBase Android10
\variable QOperatingSystemVersion::Android10
static constexpr QOperatingSystemVersionBase MacOSSierra
\variable QOperatingSystemVersion::MacOSSierra
static constexpr QOperatingSystemVersionBase OSXYosemite
\variable QOperatingSystemVersion::OSXYosemite
static constexpr QOperatingSystemVersionBase Android12L
\variable QOperatingSystemVersion::Android12L
constexpr int majorVersion() const
Returns the major version number, that is, the first segment of the operating system's version number...
static constexpr QOperatingSystemVersionBase Windows10_1909
\variable QOperatingSystemVersion::Windows10_1909
static constexpr QOperatingSystemVersionBase AndroidKitKat
\variable QOperatingSystemVersion::AndroidKitKat
static constexpr QOperatingSystemVersionBase AndroidJellyBean
\variable QOperatingSystemVersion::AndroidJellyBean
static constexpr QOperatingSystemVersionBase AndroidOreo_MR1
\variable QOperatingSystemVersion::AndroidOreo_MR1
static constexpr QOperatingSystemVersionBase Android13
\variable QOperatingSystemVersion::Android13
static constexpr QOperatingSystemVersionBase MacOSCatalina
\variable QOperatingSystemVersion::MacOSCatalina
static constexpr QOperatingSystemVersionBase AndroidLollipop_MR1
\variable QOperatingSystemVersion::AndroidLollipop_MR1
static constexpr QOperatingSystemVersionBase Windows10
\variable QOperatingSystemVersion::Windows10
static constexpr QOperatingSystemVersionBase Windows10_1903
\variable QOperatingSystemVersion::Windows10_1903
static constexpr QOperatingSystemVersionBase Android12
\variable QOperatingSystemVersion::Android12
constexpr int minorVersion() const
Returns the minor version number, that is, the second segment of the operating system's version numbe...
static constexpr QOperatingSystemVersionBase OSXElCapitan
\variable QOperatingSystemVersion::OSXElCapitan
static constexpr QOperatingSystemVersionBase MacOSMojave
\variable QOperatingSystemVersion::MacOSMojave
static constexpr QOperatingSystemVersionBase Windows8
\variable QOperatingSystemVersion::Windows8
static constexpr QOperatingSystemVersionBase AndroidNougat_MR1
\variable QOperatingSystemVersion::AndroidNougat_MR1
QString name() const
Returns a string representation of the OS type identified by the QOperatingSystemVersion.
static constexpr QOperatingSystemVersionBase Windows10_2004
\variable QOperatingSystemVersion::Windows10_2004
static constexpr QOperatingSystemVersionBase MacOSHighSierra
\variable QOperatingSystemVersion::MacOSHighSierra
static constexpr QOperatingSystemVersionBase Windows10_21H2
\variable QOperatingSystemVersion::Windows10_21H2
static QOperatingSystemVersion current()
[0]
static constexpr QOperatingSystemVersionBase AndroidMarshmallow
\variable QOperatingSystemVersion::AndroidMarshmallow
static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR2
\variable QOperatingSystemVersion::AndroidJellyBean_MR2
static constexpr QOperatingSystemVersionBase Windows10_20H2
\variable QOperatingSystemVersion::Windows10_20H2
static constexpr QOperatingSystemVersionBase AndroidPie
\variable QOperatingSystemVersion::AndroidPie
static constexpr QOperatingSystemVersionBase AndroidOreo
\variable QOperatingSystemVersion::AndroidOreo
static constexpr QOperatingSystemVersionBase OSXMavericks
\variable QOperatingSystemVersion::OSXMavericks
static constexpr QOperatingSystemVersionBase MacOSMonterey
\variable QOperatingSystemVersion::MacOSMonterey
static constexpr QOperatingSystemVersionBase Windows7
\variable QOperatingSystemVersion::Windows7
constexpr int microVersion() const
Returns the micro version number, that is, the third segment of the operating system's version number...
static constexpr QOperatingSystemVersionBase MacOSVentura
\variable QOperatingSystemVersion::MacOSVentura
static constexpr QOperatingSystemVersionBase Windows10_1809
\variable QOperatingSystemVersion::Windows10_1809
static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR1
\variable QOperatingSystemVersion::AndroidJellyBean_MR1
static constexpr QOperatingSystemVersionBase MacOSBigSur
\variable QOperatingSystemVersion::MacOSBigSur
bool isAnyOfType(std::initializer_list< OSType > types) const
Returns whether the OS type identified by the QOperatingSystemVersion matches any of the OS types in ...
static constexpr QOperatingSystemVersionBase AndroidLollipop
\variable QOperatingSystemVersion::AndroidLollipop
static constexpr QOperatingSystemVersionBase Windows8_1
\variable QOperatingSystemVersion::Windows8_1
static constexpr QOperatingSystemVersionBase Windows10_21H1
\variable QOperatingSystemVersion::Windows10_21H1
static constexpr QOperatingSystemVersionBase Windows11_22H2
\variable QOperatingSystemVersion::Windows11_22H2
static constexpr QOperatingSystemVersionBase Windows10_22H2
\variable QOperatingSystemVersion::Windows10_22H2
static constexpr QOperatingSystemVersionBase Android11
\variable QOperatingSystemVersion::Android11
static constexpr QOperatingSystemVersionBase Windows11
\variable QOperatingSystemVersion::Windows11
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
int minorVersion() const noexcept
Returns the minor version number, that is, the second segment.
int majorVersion() const noexcept
Returns the major version number, that is, the first segment.
static Q_CORE_EXPORT QVersionNumber fromString(QAnyStringView string, qsizetype *suffixIndex=nullptr)
int microVersion() const noexcept
Returns the micro version number, that is, the third segment.
Combined button and popup list for selecting options.
Q_CORE_EXPORT jint androidSdkVersion()
GLint GLfloat GLfloat GLfloat v2
GLsizei const GLfloat * v
[13]
GLsizei GLenum GLenum * types
GLenum type
GLint GLfloat GLfloat v1
QDebug operator<<(QDebug debug, const QOperatingSystemVersion &ov)
static int compareVersionComponents(int lhs, int rhs)
#define QStringLiteral(str)
unsigned int uint
Definition qtypes.h:29
char * toString(const MyType &t)
[31]