QtBase  v6.3.1
qoperatingsystemversion.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
41 
42 #if !defined(Q_OS_DARWIN) && !defined(Q_OS_WIN)
44 #endif
45 
46 #if defined(Q_OS_DARWIN)
47 #include <QtCore/private/qcore_mac_p.h>
48 #endif
49 
50 #include <qversionnumber.h>
51 #include <qdebug.h>
52 
53 #ifdef Q_OS_ANDROID
54 #include <QJniObject>
55 #endif
56 
58 
154 {
156 }
157 
158 #if !defined(Q_OS_DARWIN) && !defined(Q_OS_WIN)
159 QOperatingSystemVersionBase QOperatingSystemVersionBase::current_impl()
160 {
162  version.m_os = currentType();
163 #ifdef Q_OS_ANDROID
164 #ifndef QT_BOOTSTRAPPED
165  const QVersionNumber v = QVersionNumber::fromString(QJniObject::getStaticObjectField(
166  "android/os/Build$VERSION", "RELEASE", "Ljava/lang/String;").toString());
167  if (!v.isNull()) {
168  version.m_major = v.majorVersion();
169  version.m_minor = v.minorVersion();
170  version.m_micro = v.microVersion();
171  return version;
172  }
173 #endif
174 
175  version.m_major = -1;
176  version.m_minor = -1;
177 
178  static const struct {
179  uint major : 4;
180  uint minor : 4;
181  } versions[] = {
182  { 1, 0 }, // API level 1
183  { 1, 1 }, // API level 2
184  { 1, 5 }, // API level 3
185  { 1, 6 }, // API level 4
186  { 2, 0 }, // API level 5
187  { 2, 0 }, // API level 6
188  { 2, 1 }, // API level 7
189  { 2, 2 }, // API level 8
190  { 2, 3 }, // API level 9
191  { 2, 3 }, // API level 10
192  { 3, 0 }, // API level 11
193  { 3, 1 }, // API level 12
194  { 3, 2 }, // API level 13
195  { 4, 0 }, // API level 14
196  { 4, 0 }, // API level 15
197  { 4, 1 }, // API level 16
198  { 4, 2 }, // API level 17
199  { 4, 3 }, // API level 18
200  { 4, 4 }, // API level 19
201  { 4, 4 }, // API level 20
202  { 5, 0 }, // API level 21
203  { 5, 1 }, // API level 22
204  { 6, 0 }, // API level 23
205  { 7, 0 }, // API level 24
206  { 7, 1 }, // API level 25
207  { 8, 0 }, // API level 26
208  { 8, 1 }, // API level 27
209  { 9, 0 }, // API level 28
210  { 10, 0 }, // API level 29
211  { 11, 0 }, // API level 30
212  };
213 
214  // This will give us at least the first 2 version components
215  const size_t versionIdx = size_t(QJniObject::getStaticField<jint>(
216  "android/os/Build$VERSION", "SDK_INT")) - 1;
217  if (versionIdx < sizeof(versions) / sizeof(versions[0])) {
218  version.m_major = versions[versionIdx].major;
219  version.m_minor = versions[versionIdx].minor;
220  }
221 
222  // API level 6 was exactly version 2.0.1
223  version.m_micro = versionIdx == 5 ? 1 : -1;
224 #else
225  version.m_major = -1;
226  version.m_minor = -1;
227  version.m_micro = -1;
228 #endif
229  return version;
230 }
231 
233 {
234  static const QOperatingSystemVersionBase v = current_impl();
235  return v;
236 }
237 #endif
238 
239 static inline int compareVersionComponents(int lhs, int rhs)
240 {
241  return lhs >= 0 && rhs >= 0 ? lhs - rhs : 0;
242 }
243 
246 {
247  if (v1.m_major == v2.m_major) {
248  if (v1.m_minor == v2.m_minor) {
249  return compareVersionComponents(v1.m_micro, v2.m_micro);
250  }
251  return compareVersionComponents(v1.m_minor, v2.m_minor);
252  }
253  return compareVersionComponents(v1.m_major, v2.m_major);
254 }
255 
343 {
345 }
346 
348 {
349  switch (osversion.type()) {
351  return QStringLiteral("Windows");
353  if (osversion.majorVersion() < 10)
354  return QStringLiteral("Mac OS");
355  if (osversion.majorVersion() == 10 && osversion.minorVersion() < 8)
356  return QStringLiteral("Mac OS X");
357  if (osversion.majorVersion() == 10 && osversion.minorVersion() < 12)
358  return QStringLiteral("OS X");
359  return QStringLiteral("macOS");
360  }
362  if (osversion.majorVersion() < 4)
363  return QStringLiteral("iPhone OS");
364  return QStringLiteral("iOS");
365  }
367  return QStringLiteral("tvOS");
369  return QStringLiteral("watchOS");
371  return QStringLiteral("Android");
373  default:
374  return QString();
375  }
376 }
377 
384 bool QOperatingSystemVersion::isAnyOfType(std::initializer_list<OSType> types) const
385 {
386  // ### Qt7: Remove this function
387  return std::find(types.begin(), types.end(), type()) != types.end();
388 }
389 
390 bool QOperatingSystemVersionBase::isAnyOfType(std::initializer_list<OSType> types, OSType type)
391 {
392  return std::find(types.begin(), types.end(), type) != types.end();
393 }
394 
402 
410 
418 
426 
482 
490 
498 
506 
514 
522 
530 
544 #if defined(Q_OS_DARWIN)
547  else
548 #endif
550 }();
551 
559 
567 
576 
585 
593 
601 
610 
618 
626 
635 
643 
651 
659 
667 
675 
676 #ifndef QT_NO_DEBUG_STREAM
678 {
679  QDebugStateSaver saver(debug);
680  debug.nospace();
681  debug << "QOperatingSystemVersion(" << ov.name()
682  << ", " << ov.majorVersion() << '.' << ov.minorVersion()
683  << '.' << ov.microVersion() << ')';
684  return debug;
685 }
686 #endif // !QT_NO_DEBUG_STREAM
687 
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
Convenience class for custom QDebug operators.
Definition: qdebug.h:176
static QOperatingSystemVersion buildSDK(VersionTarget target=ApplicationBinary)
static Q_CORE_EXPORT QOperatingSystemVersionBase current()
constexpr int majorVersion() const
static Q_CORE_EXPORT bool isAnyOfType(std::initializer_list< OSType > types, OSType type)
constexpr int minorVersion() const
static Q_CORE_EXPORT int compare(QOperatingSystemVersionBase v1, QOperatingSystemVersionBase v2)
static constexpr OSType currentType()
The QOperatingSystemVersion class provides information about the operating system version.
static constexpr QOperatingSystemVersionBase AndroidNougat
a version corresponding to Android Nougat (version 7.0, API level 24).
static constexpr QOperatingSystemVersionBase Android10
a version corresponding to Android 10 (version 10.0, API level 29).
static constexpr QOperatingSystemVersionBase MacOSSierra
a version corresponding to macOS Sierra (version 10.12).
static constexpr QOperatingSystemVersionBase OSXYosemite
a version corresponding to OS X Yosemite (version 10.10).
constexpr int majorVersion() const
static constexpr QOperatingSystemVersionBase AndroidKitKat
a version corresponding to Android KitKat (versions 4.4 & 4.4W, API levels 19 & 20).
static constexpr QOperatingSystemVersionBase AndroidJellyBean
a version corresponding to Android Jelly Bean (version 4.1, API level 16).
static constexpr QOperatingSystemVersionBase AndroidOreo_MR1
a version corresponding to Android Oreo_MR1 (version 8.1, API level 27).
static constexpr QOperatingSystemVersionBase MacOSCatalina
a version corresponding to macOS Catalina (version 10.15).
static constexpr QOperatingSystemVersionBase AndroidLollipop_MR1
a version corresponding to Android Lollipop, maintenance release 1 (version 5.1, API level 22).
static constexpr QOperatingSystemVersionBase Windows10
a version corresponding to Windows 10 (version 10.0).
constexpr int minorVersion() const
static constexpr QOperatingSystemVersionBase OSXElCapitan
a version corresponding to OS X El Capitan (version 10.11).
static constexpr QOperatingSystemVersionBase MacOSMojave
a version corresponding to macOS Mojave (version 10.14).
static constexpr QOperatingSystemVersionBase Windows8
a version corresponding to Windows 8 (version 6.2).
static constexpr QOperatingSystemVersionBase AndroidNougat_MR1
a version corresponding to Android Nougat, maintenance release 1 (version 7.0, API level 25).
static constexpr QOperatingSystemVersionBase MacOSHighSierra
a version corresponding to macOS High Sierra (version 10.13).
static QOperatingSystemVersion current()
[0]
static constexpr QOperatingSystemVersionBase AndroidMarshmallow
a version corresponding to Android Marshmallow (version 6.0, API level 23).
static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR2
a version corresponding to Android Jelly Bean, maintenance release 2 (version 4.3,...
static constexpr QOperatingSystemVersionBase AndroidPie
a version corresponding to Android Pie (version 9.0, API level 28).
static constexpr QOperatingSystemVersionBase AndroidOreo
a version corresponding to Android Oreo (version 8.0, API level 26).
static constexpr QOperatingSystemVersionBase OSXMavericks
a version corresponding to Windows 10 1809 (version 10.0.17763).
static constexpr QOperatingSystemVersionBase MacOSMonterey
a version corresponding to macOS Monterey (version 12).
static constexpr QOperatingSystemVersionBase Windows7
a version corresponding to Windows 7 (version 6.1).
constexpr int microVersion() const
static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR1
a version corresponding to Android Jelly Bean, maintenance release 1 (version 4.2,...
static constexpr QOperatingSystemVersionBase MacOSBigSur
a version corresponding to macOS Big Sur
bool isAnyOfType(std::initializer_list< OSType > types) const
static constexpr QOperatingSystemVersionBase AndroidLollipop
a version corresponding to Android Lollipop (version 5.0, API level 21).
static constexpr QOperatingSystemVersionBase Windows8_1
a version corresponding to Windows 8.1 (version 6.3).
static constexpr QOperatingSystemVersionBase Android11
a version corresponding to Android 11 (version 11.0, API level 30).
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QVersionNumber class contains a version number with an arbitrary number of segments.
int minorVersion() const noexcept
int majorVersion() const noexcept
static Q_CORE_EXPORT QVersionNumber fromString(const QString &string, int *suffixIndex=nullptr)
int microVersion() const noexcept
#define QString()
Definition: parse-defines.h:51
unsigned int uint
Definition: qglobal.h:334
GLenum type
Definition: qopengl.h:270
GLint GLfloat GLfloat GLfloat v2
GLsizei const GLfloat * v
[13]
GLsizei GLenum GLenum * types
GLint GLfloat GLfloat v1
#define QStringLiteral(str)
#define rhs
QDomElement find(const QString &tagName, const QDomElement &e)
Definition: main.cpp:39