Add QQmlPlatform (exposed as Qt.platform)
authorJ-P Nurmi <jpnurmi@digia.com>
Mon, 14 Jan 2013 17:12:50 +0000 (18:12 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 14 Feb 2013 16:21:25 +0000 (17:21 +0100)
Change-Id: If34603fd8fbd907bc063b52ea31848eabdb49f61
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Reviewed-by: Alan Alpert <aalpert@rim.com>
src/qml/qml/qml.pri
src/qml/qml/qqmlengine.cpp
src/qml/qml/qqmlplatform.cpp [new file with mode: 0644]
src/qml/qml/qqmlplatform_p.h [new file with mode: 0644]
src/qml/qml/v8/qv8engine.cpp
src/qml/qml/v8/qv8engine_p.h

index 87fe477..53094db 100644 (file)
@@ -55,7 +55,8 @@ SOURCES += \
     $$PWD/qqmlmemoryprofiler.cpp \
     $$PWD/qqmlconnections.cpp \
     $$PWD/qqmltimer.cpp \
-    $$PWD/qqmlbind.cpp
+    $$PWD/qqmlbind.cpp \
+    $$PWD/qqmlplatform.cpp
 
 HEADERS += \
     $$PWD/qqmlglobal_p.h \
@@ -131,7 +132,8 @@ HEADERS += \
     $$PWD/qqmlmemoryprofiler_p.h \
     $$PWD/qqmlconnections_p.h \
     $$PWD/qqmltimer_p.h \
-    $$PWD/qqmlbind_p.h
+    $$PWD/qqmlbind_p.h \
+    $$PWD/qqmlplatform_p.h
 
 
 include(parser/parser.pri)
index 5657cac..074ea51 100644 (file)
@@ -92,6 +92,7 @@
 #include "qqmlbind_p.h"
 #include "qqmlconnections_p.h"
 #include "qqmltimer_p.h"
+#include "qqmlplatform_p.h"
 #include <private/qquickpackage_p.h>
 #include <private/qqmldelegatemodel_p.h>
 #include <private/qqmlobjectmodel_p.h>
@@ -367,6 +368,36 @@ The following functions are also on the Qt object.
 */
 
 /*!
+    \qmlproperty object Qt::platform
+    \since QtQml 2.1
+
+    The \c platform object provides info about the underlying platform.
+
+    Its properties are:
+
+    \table
+    \row
+    \li \c platform.os
+    \li
+
+    This read-only property contains the name of the operating system.
+
+    Possible values are:
+
+    \list
+        \li \c "android" - Android
+        \li \c "blackberry" - BlackBerry OS
+        \li \c "ios" - Apple iOS
+        \li \c "linux" - Linux
+        \li \c "mac" - Mac OS X
+        \li \c "unix" - Other Unix-based OS
+        \li \c "windows" - Windows
+        \li \c "wince" - Windows CE
+    \endlist
+    \endtable
+*/
+
+/*!
     \qmlproperty object Qt::application
     \since QtQuick 1.1
 
diff --git a/src/qml/qml/qqmlplatform.cpp b/src/qml/qml/qqmlplatform.cpp
new file mode 100644 (file)
index 0000000..21145be
--- /dev/null
@@ -0,0 +1,83 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQml module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qqmlplatform_p.h"
+
+QT_BEGIN_NAMESPACE
+
+/*
+    This object and its properties are documented as part of the Qt object,
+    in qqmlengine.cpp
+*/
+
+QQmlPlatform::QQmlPlatform(QObject *parent)
+    : QObject(parent)
+{
+}
+
+QQmlPlatform::~QQmlPlatform()
+{
+}
+
+QString QQmlPlatform::os()
+{
+#if defined(Q_OS_LINUX_ANDROID)
+    return QLatin1String("android");
+#elif defined(Q_OS_BLACKBERRY)
+    return QLatin1String("blackberry");
+#elif defined(Q_OS_IOS)
+    return QLatin1String("ios");
+#elif defined(Q_OS_MAC)
+    return QLatin1String("mac");
+#elif defined(Q_OS_WINCE)
+    return QLatin1String("wince");
+#elif defined(Q_OS_WIN)
+    return QLatin1String("windows");
+#elif defined(Q_OS_LINUX)
+    return QLatin1String("linux");
+#elif defined(Q_OS_UNIX)
+    return QLatin1String("unix");
+#else
+    return QLatin1String("unknown");
+#endif
+}
+
+QT_END_NAMESPACE
diff --git a/src/qml/qml/qqmlplatform_p.h b/src/qml/qml/qqmlplatform_p.h
new file mode 100644 (file)
index 0000000..a70e21f
--- /dev/null
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQml module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QQMLPLATFORM_P_H
+#define QQMLPLATFORM_P_H
+
+#include <QtCore/QObject>
+#include <qqml.h>
+#include <private/qtqmlglobal_p.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+class Q_QML_PRIVATE_EXPORT QQmlPlatform : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY(QString os READ os CONSTANT)
+
+public:
+    explicit QQmlPlatform(QObject *parent = 0);
+    virtual ~QQmlPlatform();
+
+    static QString os();
+
+private:
+    Q_DISABLE_COPY(QQmlPlatform)
+};
+
+QT_END_NAMESPACE
+
+QML_DECLARE_TYPE(QQmlPlatform)
+
+QT_END_HEADER
+
+#endif // QQMLPLATFORM_P_H
index 2619c1a..15611f3 100644 (file)
@@ -54,6 +54,7 @@
 #include <private/qqmllocale_p.h>
 #include <private/qqmlglobal_p.h>
 #include <private/qqmlmemoryprofiler_p.h>
+#include <private/qqmlplatform_p.h>
 
 #include "qscript_impl_p.h"
 #include "qv8domerrors_p.h"
@@ -125,6 +126,7 @@ QV8Engine::QV8Engine(QJSEngine* qq, ContextOwnership ownership)
     , m_ownsV8Context(ownership == CreateNewContext)
     , m_xmlHttpRequestData(0)
     , m_listModelData(0)
+    , m_platform(0)
     , m_application(0)
 {
     QML_MEMORY_SCOPE_STRING("QV8Engine::QV8Engine");
@@ -629,6 +631,7 @@ void QV8Engine::initializeGlobal(v8::Handle<v8::Object> global)
     qt->Set(v8::String::New("binding"), V8FUNCTION(binding, this));
 
     if (m_engine) {
+        qt->SetAccessor(v8::String::New("platform"), getPlatform, 0, v8::External::New(this));
         qt->SetAccessor(v8::String::New("application"), getApplication, 0, v8::External::New(this));
 #ifndef QT_NO_IM
         qt->SetAccessor(v8::String::New("inputMethod"), getInputMethod, 0, v8::External::New(this));
@@ -1446,6 +1449,16 @@ int QV8Engine::consoleCountHelper(const QString &file, quint16 line, quint16 col
     return number;
 }
 
+v8::Handle<v8::Value> QV8Engine::getPlatform(v8::Local<v8::String>, const v8::AccessorInfo &info)
+{
+    QV8Engine *engine = reinterpret_cast<QV8Engine*>(v8::External::Unwrap(info.Data()));
+    if (!engine->m_platform) {
+        // Only allocate a platform object once
+        engine->m_platform = new QQmlPlatform(engine->m_engine);
+    }
+    return engine->newQObject(engine->m_platform);
+}
+
 v8::Handle<v8::Value> QV8Engine::getApplication(v8::Local<v8::String>, const v8::AccessorInfo &info)
 {
     QV8Engine *engine = reinterpret_cast<QV8Engine*>(v8::External::Unwrap(info.Data()));
index 43c978c..9c3eb22 100644 (file)
@@ -427,6 +427,7 @@ public:
     void addRelationshipForGC(QObject *object, v8::Persistent<v8::Value> handle);
     void addRelationshipForGC(QObject *object, QObject *other);
 
+    static v8::Handle<v8::Value> getPlatform(v8::Local<v8::String> property, const v8::AccessorInfo &info);
     static v8::Handle<v8::Value> getApplication(v8::Local<v8::String> property, const v8::AccessorInfo &info);
 #ifndef QT_NO_IM
     static v8::Handle<v8::Value> getInputMethod(v8::Local<v8::String> property, const v8::AccessorInfo &info);
@@ -480,6 +481,7 @@ protected:
 
     QHash<QString, quint32> m_consoleCount;
 
+    QObject *m_platform;
     QObject *m_application;
 
     QVariant toBasicVariant(v8::Handle<v8::Value>);