From 003596fad52690127afca0d7025b62bad7fd013e Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Thu, 3 Jul 2014 19:17:02 +0200 Subject: Make the QWebChannel QML API publically accessible. This is required for proper QtWebKit/QtWebEngine integration, as otherwise these modules would have to redo a lot of the QtWebChannel QML API. Furthermore, without this, we could not use the WebChannel.id attached property everywhere, independent of the web browser technology. Change-Id: I032a9326841d505c2f77959a240bbfc71e94b6e8 Reviewed-by: Sean Harmer --- src/imports/webchannel/plugin.cpp | 5 +- src/imports/webchannel/qmlwebchannel.cpp | 199 --------------------- src/imports/webchannel/qmlwebchannel.h | 102 ----------- src/imports/webchannel/qmlwebchannelattached.cpp | 68 -------- src/imports/webchannel/qmlwebchannelattached.h | 70 -------- src/imports/webchannel/webchannel.pro | 8 +- src/webchannel/qmetaobjectpublisher.cpp | 2 +- src/webchannel/qmetaobjectpublisher_p.h | 2 +- src/webchannel/qqmlwebchannel.cpp | 211 +++++++++++++++++++++++ src/webchannel/qqmlwebchannel.h | 97 +++++++++++ src/webchannel/qqmlwebchannelattached.cpp | 68 ++++++++ src/webchannel/qqmlwebchannelattached_p.h | 72 ++++++++ src/webchannel/qwebchannel.cpp | 32 +++- src/webchannel/qwebchannel.h | 14 +- src/webchannel/qwebchannel_p.h | 7 +- src/webchannel/webchannel.pro | 12 +- tests/auto/cmake/CMakeLists.txt | 4 + tests/auto/qml/Client.qml | 2 +- tests/auto/qml/qml.cpp | 2 + tests/auto/qml/qml.pro | 8 +- tests/auto/qml/testwebchannel.cpp | 65 +++++++ tests/auto/qml/testwebchannel.h | 62 +++++++ tests/auto/qml/tst_bench.qml | 3 +- tests/auto/qml/tst_metaobjectpublisher.qml | 3 +- tests/auto/qml/tst_multiclient.qml | 3 +- tests/auto/qml/tst_webchannel.qml | 3 +- tests/auto/webchannel/tst_webchannel.cpp | 20 +-- tests/auto/webchannel/webchannel.pro | 2 +- 28 files changed, 664 insertions(+), 482 deletions(-) delete mode 100644 src/imports/webchannel/qmlwebchannel.cpp delete mode 100644 src/imports/webchannel/qmlwebchannel.h delete mode 100644 src/imports/webchannel/qmlwebchannelattached.cpp delete mode 100644 src/imports/webchannel/qmlwebchannelattached.h create mode 100644 src/webchannel/qqmlwebchannel.cpp create mode 100644 src/webchannel/qqmlwebchannel.h create mode 100644 src/webchannel/qqmlwebchannelattached.cpp create mode 100644 src/webchannel/qqmlwebchannelattached_p.h create mode 100644 tests/auto/qml/testwebchannel.cpp create mode 100644 tests/auto/qml/testwebchannel.h diff --git a/src/imports/webchannel/plugin.cpp b/src/imports/webchannel/plugin.cpp index 6507112..235f542 100644 --- a/src/imports/webchannel/plugin.cpp +++ b/src/imports/webchannel/plugin.cpp @@ -42,7 +42,8 @@ #include #include -#include "qmlwebchannel.h" +#include +#include QT_BEGIN_NAMESPACE @@ -59,7 +60,7 @@ void QWebChannelPlugin::registerTypes(const char *uri) { int major = 1; int minor = 0; - qmlRegisterType(uri, major, minor, "WebChannel"); + qmlRegisterType(uri, major, minor, "WebChannel"); } QT_END_NAMESPACE diff --git a/src/imports/webchannel/qmlwebchannel.cpp b/src/imports/webchannel/qmlwebchannel.cpp deleted file mode 100644 index e61e9c8..0000000 --- a/src/imports/webchannel/qmlwebchannel.cpp +++ /dev/null @@ -1,199 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel 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 "qmlwebchannel.h" - -#include "qwebchannel_p.h" -#include "qmetaobjectpublisher_p.h" -#include "qwebchannelabstracttransport.h" - -#include - -QT_USE_NAMESPACE - -QmlWebChannel::QmlWebChannel(QObject *parent) - : QWebChannel(parent) -{ -} - -QmlWebChannel::~QmlWebChannel() -{ - -} - -void QmlWebChannel::registerObjects(const QVariantMap &objects) -{ - QMap::const_iterator it = objects.constBegin(); - for (; it != objects.constEnd(); ++it) { - QObject *object = it.value().value(); - if (!object) { - qWarning("Invalid QObject given to register under name %s", qPrintable(it.key())); - continue; - } - d->publisher->registerObject(it.key(), object); - } -} - -bool QmlWebChannel::test_clientIsIdle() const -{ - return d->publisher->clientIsIdle; -} - -void QmlWebChannel::objectIdChanged(const QString &newId) -{ - const QmlWebChannelAttached *const attached = qobject_cast(sender()); - Q_ASSERT(attached); - Q_ASSERT(attached->parent()); - Q_ASSERT(m_registeredObjects.contains(attached->parent())); - - QObject *const object = attached->parent(); - const QString &oldId = d->publisher->registeredObjectIds.value(object); - - if (!oldId.isEmpty()) { - deregisterObject(object); - } - - registerObject(newId, object); -} - -QmlWebChannelAttached *QmlWebChannel::qmlAttachedProperties(QObject *obj) -{ - return new QmlWebChannelAttached(obj); -} - -void QmlWebChannel::connectTo(QObject *transport) -{ - if (QWebChannelAbstractTransport *realTransport = qobject_cast(transport)) { - QWebChannel::connectTo(realTransport); - } else { - qWarning() << "Cannot connect to transport" << transport << " - it is not a QWebChannelAbstractTransport."; - } -} - -void QmlWebChannel::disconnectFrom(QObject *transport) -{ - if (QWebChannelAbstractTransport *realTransport = qobject_cast(transport)) { - QWebChannel::disconnectFrom(realTransport); - } else { - qWarning() << "Cannot disconnect from transport" << transport << " - it is not a QWebChannelAbstractTransport."; - } -} - -QQmlListProperty QmlWebChannel::registeredObjects() -{ - return QQmlListProperty(this, 0, - registeredObjects_append, - registeredObjects_count, - registeredObjects_at, - registeredObjects_clear); -} - -void QmlWebChannel::registeredObjects_append(QQmlListProperty *prop, QObject *object) -{ - const QmlWebChannelAttached *const attached = qobject_cast( - qmlAttachedPropertiesObject(object, false /* don't create */)); - if (!attached) { - const QQmlContext *const context = qmlContext(object); - qWarning() << "Cannot register object" << context->nameForObject(object) << '(' << object << ") without attached WebChannel.id property. Did you forget to set it?"; - return; - } - QmlWebChannel *channel = static_cast(prop->object); - if (!attached->id().isEmpty()) { - // TODO: warning in such cases? - channel->registerObject(attached->id(), object); - } - channel->m_registeredObjects.append(object); - connect(attached, SIGNAL(idChanged(QString)), channel, SLOT(objectIdChanged(QString))); -} - -int QmlWebChannel::registeredObjects_count(QQmlListProperty *prop) -{ - return static_cast(prop->object)->m_registeredObjects.size(); -} - -QObject *QmlWebChannel::registeredObjects_at(QQmlListProperty *prop, int index) -{ - return static_cast(prop->object)->m_registeredObjects.at(index); -} - -void QmlWebChannel::registeredObjects_clear(QQmlListProperty *prop) -{ - QmlWebChannel *channel = static_cast(prop->object); - foreach (QObject *object, channel->m_registeredObjects) { - channel->deregisterObject(object); - } - return channel->m_registeredObjects.clear(); -} - -QQmlListProperty QmlWebChannel::transports() -{ - return QQmlListProperty(this, 0, - transports_append, - transports_count, - transports_at, - transports_clear); -} - -void QmlWebChannel::transports_append(QQmlListProperty *prop, QObject *transport) -{ - QmlWebChannel *channel = static_cast(prop->object); - channel->connectTo(transport); -} - -int QmlWebChannel::transports_count(QQmlListProperty *prop) -{ - return static_cast(prop->object)->d->transports.size(); -} - -QObject *QmlWebChannel::transports_at(QQmlListProperty *prop, int index) -{ - QmlWebChannel *channel = static_cast(prop->object); - return dynamic_cast(channel->d->transports.at(index)); -} - -void QmlWebChannel::transports_clear(QQmlListProperty *prop) -{ - QWebChannel *channel = static_cast(prop->object); - foreach (QWebChannelAbstractTransport *transport, channel->d->transports) { - channel->disconnectFrom(transport); - } - Q_ASSERT(channel->d->transports.isEmpty()); -} diff --git a/src/imports/webchannel/qmlwebchannel.h b/src/imports/webchannel/qmlwebchannel.h deleted file mode 100644 index 6bc7127..0000000 --- a/src/imports/webchannel/qmlwebchannel.h +++ /dev/null @@ -1,102 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel 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 QMLWEBCHANNEL_H -#define QMLWEBCHANNEL_H - -#include - -#include "qmlwebchannelattached.h" - -#include - -#include -#include - -QT_BEGIN_NAMESPACE - -class QmlWebChannel : public QWebChannel -{ - Q_OBJECT - - Q_PROPERTY( QQmlListProperty transports READ transports ); - Q_PROPERTY( QQmlListProperty registeredObjects READ registeredObjects ) - -public: - explicit QmlWebChannel(QObject *parent = 0); - virtual ~QmlWebChannel(); - - Q_INVOKABLE void registerObjects(const QVariantMap &objects); - QQmlListProperty registeredObjects(); - - QQmlListProperty transports(); - - // TODO: remove this by replacing QML with C++ tests - Q_INVOKABLE bool test_clientIsIdle() const; - - static QmlWebChannelAttached *qmlAttachedProperties(QObject *obj); - - Q_INVOKABLE void connectTo(QObject *transport); - Q_INVOKABLE void disconnectFrom(QObject *transport); - -private Q_SLOTS: - void objectIdChanged(const QString &newId); - -private: - static void registeredObjects_append(QQmlListProperty *prop, QObject *item); - static int registeredObjects_count(QQmlListProperty *prop); - static QObject *registeredObjects_at(QQmlListProperty *prop, int index); - static void registeredObjects_clear(QQmlListProperty *prop); - - static void transports_append(QQmlListProperty *prop, QObject *item); - static int transports_count(QQmlListProperty *prop); - static QObject *transports_at(QQmlListProperty *prop, int index); - static void transports_clear(QQmlListProperty *prop); - - QVector m_registeredObjects; -}; - -QML_DECLARE_TYPE( QmlWebChannel ) -QML_DECLARE_TYPEINFO( QmlWebChannel, QML_HAS_ATTACHED_PROPERTIES ) - -QT_END_NAMESPACE - -#endif // QMLWEBCHANNEL_H diff --git a/src/imports/webchannel/qmlwebchannelattached.cpp b/src/imports/webchannel/qmlwebchannelattached.cpp deleted file mode 100644 index 637feb2..0000000 --- a/src/imports/webchannel/qmlwebchannelattached.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel 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 "qmlwebchannelattached.h" - -QT_USE_NAMESPACE - -QmlWebChannelAttached::QmlWebChannelAttached(QObject *parent) - : QObject(parent) -{ - -} - -QmlWebChannelAttached::~QmlWebChannelAttached() -{ - -} - -QString QmlWebChannelAttached::id() const -{ - return m_id; -} - -void QmlWebChannelAttached::setId(const QString &id) -{ - if (id != m_id) { - m_id = id; - emit idChanged(id); - } -} diff --git a/src/imports/webchannel/qmlwebchannelattached.h b/src/imports/webchannel/qmlwebchannelattached.h deleted file mode 100644 index 3d60122..0000000 --- a/src/imports/webchannel/qmlwebchannelattached.h +++ /dev/null @@ -1,70 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel 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 QMLWEBCHANNELATTACHED_H -#define QMLWEBCHANNELATTACHED_H - -#include - -QT_BEGIN_NAMESPACE - -class QmlWebChannelAttached : public QObject -{ - Q_OBJECT - - Q_PROPERTY( QString id READ id WRITE setId NOTIFY idChanged FINAL ) -public: - explicit QmlWebChannelAttached(QObject *parent = 0); - virtual ~QmlWebChannelAttached(); - - QString id() const; - void setId(const QString &id); - -Q_SIGNALS: - void idChanged(const QString &id); - -private: - QString m_id; -}; - -QT_END_NAMESPACE - -#endif // QMLWEBCHANNELATTACHED_H diff --git a/src/imports/webchannel/webchannel.pro b/src/imports/webchannel/webchannel.pro index c1959f5..8ae6ef5 100644 --- a/src/imports/webchannel/webchannel.pro +++ b/src/imports/webchannel/webchannel.pro @@ -4,12 +4,6 @@ INCLUDEPATH += ../../webchannel VPATH += ../../webchannel SOURCES += \ - plugin.cpp \ - qmlwebchannel.cpp \ - qmlwebchannelattached.cpp - -HEADERS += \ - qmlwebchannel.h \ - qmlwebchannelattached.h + plugin.cpp load(qml_plugin) diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index 096bf7b..646398d 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -395,7 +395,7 @@ QByteArray QMetaObjectPublisher::invokeMethod(QObject *const object, const int m void QMetaObjectPublisher::signalEmitted(const QObject *object, const int signalIndex, const QVariantList &arguments) { - if (!webChannel || webChannel->d->transports.isEmpty()) { + if (!webChannel || webChannel->d_func()->transports.isEmpty()) { return; } if (!signalToPropertyMap.value(object).contains(signalIndex)) { diff --git a/src/webchannel/qmetaobjectpublisher_p.h b/src/webchannel/qmetaobjectpublisher_p.h index dda18ae..4da0c26 100644 --- a/src/webchannel/qmetaobjectpublisher_p.h +++ b/src/webchannel/qmetaobjectpublisher_p.h @@ -172,7 +172,7 @@ protected: void timerEvent(QTimerEvent *) Q_DECL_OVERRIDE; private: - friend class QmlWebChannel; + friend class QQmlWebChannelPrivate; friend class QWebChannel; friend class TestWebChannel; diff --git a/src/webchannel/qqmlwebchannel.cpp b/src/webchannel/qqmlwebchannel.cpp new file mode 100644 index 0000000..ca99fe5 --- /dev/null +++ b/src/webchannel/qqmlwebchannel.cpp @@ -0,0 +1,211 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebChannel 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 "qqmlwebchannel.h" + +#include "qwebchannel_p.h" +#include "qmetaobjectpublisher_p.h" +#include "qwebchannelabstracttransport.h" + +#include + +#include "qqmlwebchannelattached_p.h" + +QT_BEGIN_NAMESPACE + +class QQmlWebChannelPrivate : public QWebChannelPrivate +{ + Q_DECLARE_PUBLIC(QQmlWebChannel) +public: + QVector registeredObjects; + + void _q_objectIdChanged(const QString &newId); +}; + +void QQmlWebChannelPrivate::_q_objectIdChanged(const QString &newId) +{ + Q_Q(QQmlWebChannel); + const QQmlWebChannelAttached *const attached = qobject_cast(q->sender()); + Q_ASSERT(attached); + Q_ASSERT(attached->parent()); + Q_ASSERT(registeredObjects.contains(attached->parent())); + + QObject *const object = attached->parent(); + const QString &oldId = publisher->registeredObjectIds.value(object); + + if (!oldId.isEmpty()) { + q->deregisterObject(object); + } + + q->registerObject(newId, object); +} + +QQmlWebChannel::QQmlWebChannel(QObject *parent) + : QWebChannel(*(new QQmlWebChannelPrivate), parent) +{ +} + +QQmlWebChannel::~QQmlWebChannel() +{ + +} + +void QQmlWebChannel::registerObjects(const QVariantMap &objects) +{ + Q_D(QQmlWebChannel); + QMap::const_iterator it = objects.constBegin(); + for (; it != objects.constEnd(); ++it) { + QObject *object = it.value().value(); + if (!object) { + qWarning("Invalid QObject given to register under name %s", qPrintable(it.key())); + continue; + } + d->publisher->registerObject(it.key(), object); + } +} + +QQmlWebChannelAttached *QQmlWebChannel::qmlAttachedProperties(QObject *obj) +{ + return new QQmlWebChannelAttached(obj); +} + +void QQmlWebChannel::connectTo(QObject *transport) +{ + if (QWebChannelAbstractTransport *realTransport = qobject_cast(transport)) { + QWebChannel::connectTo(realTransport); + } else { + qWarning() << "Cannot connect to transport" << transport << " - it is not a QWebChannelAbstractTransport."; + } +} + +void QQmlWebChannel::disconnectFrom(QObject *transport) +{ + if (QWebChannelAbstractTransport *realTransport = qobject_cast(transport)) { + QWebChannel::disconnectFrom(realTransport); + } else { + qWarning() << "Cannot disconnect from transport" << transport << " - it is not a QWebChannelAbstractTransport."; + } +} + +QQmlListProperty QQmlWebChannel::registeredObjects() +{ + return QQmlListProperty(this, 0, + registeredObjects_append, + registeredObjects_count, + registeredObjects_at, + registeredObjects_clear); +} + +void QQmlWebChannel::registeredObjects_append(QQmlListProperty *prop, QObject *object) +{ + const QQmlWebChannelAttached *const attached = qobject_cast( + qmlAttachedPropertiesObject(object, false /* don't create */)); + if (!attached) { + const QQmlContext *const context = qmlContext(object); + qWarning() << "Cannot register object" << context->nameForObject(object) << '(' << object << ") without attached WebChannel.id property. Did you forget to set it?"; + return; + } + QQmlWebChannel *channel = static_cast(prop->object); + if (!attached->id().isEmpty()) { + // TODO: warning in such cases? + channel->registerObject(attached->id(), object); + } + channel->d_func()->registeredObjects.append(object); + connect(attached, SIGNAL(idChanged(QString)), channel, SLOT(_q_objectIdChanged(QString))); +} + +int QQmlWebChannel::registeredObjects_count(QQmlListProperty *prop) +{ + return static_cast(prop->object)->d_func()->registeredObjects.size(); +} + +QObject *QQmlWebChannel::registeredObjects_at(QQmlListProperty *prop, int index) +{ + return static_cast(prop->object)->d_func()->registeredObjects.at(index); +} + +void QQmlWebChannel::registeredObjects_clear(QQmlListProperty *prop) +{ + QQmlWebChannel *channel = static_cast(prop->object); + foreach (QObject *object, channel->d_func()->registeredObjects) { + channel->deregisterObject(object); + } + return channel->d_func()->registeredObjects.clear(); +} + +QQmlListProperty QQmlWebChannel::transports() +{ + return QQmlListProperty(this, 0, + transports_append, + transports_count, + transports_at, + transports_clear); +} + +void QQmlWebChannel::transports_append(QQmlListProperty *prop, QObject *transport) +{ + QQmlWebChannel *channel = static_cast(prop->object); + channel->connectTo(transport); +} + +int QQmlWebChannel::transports_count(QQmlListProperty *prop) +{ + return static_cast(prop->object)->d_func()->transports.size(); +} + +QObject *QQmlWebChannel::transports_at(QQmlListProperty *prop, int index) +{ + QQmlWebChannel *channel = static_cast(prop->object); + return channel->d_func()->transports.at(index); +} + +void QQmlWebChannel::transports_clear(QQmlListProperty *prop) +{ + QWebChannel *channel = static_cast(prop->object); + foreach (QWebChannelAbstractTransport *transport, channel->d_func()->transports) { + channel->disconnectFrom(transport); + } + Q_ASSERT(channel->d_func()->transports.isEmpty()); +} + +QT_END_NAMESPACE + +#include "moc_qqmlwebchannel.cpp" diff --git a/src/webchannel/qqmlwebchannel.h b/src/webchannel/qqmlwebchannel.h new file mode 100644 index 0000000..3bbd2a0 --- /dev/null +++ b/src/webchannel/qqmlwebchannel.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebChannel 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 QQMLWEBCHANNEL_H +#define QQMLWEBCHANNEL_H + +#include +#include + +#include +#include + +QT_BEGIN_NAMESPACE + +class QQmlWebChannelPrivate; +class QQmlWebChannelAttached; +class Q_WEBCHANNEL_EXPORT QQmlWebChannel : public QWebChannel +{ + Q_OBJECT + Q_DISABLE_COPY(QQmlWebChannel) + + Q_PROPERTY( QQmlListProperty transports READ transports ); + Q_PROPERTY( QQmlListProperty registeredObjects READ registeredObjects ) + +public: + explicit QQmlWebChannel(QObject *parent = 0); + virtual ~QQmlWebChannel(); + + Q_INVOKABLE void registerObjects(const QVariantMap &objects); + QQmlListProperty registeredObjects(); + + QQmlListProperty transports(); + + static QQmlWebChannelAttached *qmlAttachedProperties(QObject *obj); + + Q_INVOKABLE void connectTo(QObject *transport); + Q_INVOKABLE void disconnectFrom(QObject *transport); + +private: + Q_DECLARE_PRIVATE(QQmlWebChannel) + Q_PRIVATE_SLOT(d_func(), void _q_objectIdChanged(const QString &newId)); + + static void registeredObjects_append(QQmlListProperty *prop, QObject *item); + static int registeredObjects_count(QQmlListProperty *prop); + static QObject *registeredObjects_at(QQmlListProperty *prop, int index); + static void registeredObjects_clear(QQmlListProperty *prop); + + static void transports_append(QQmlListProperty *prop, QObject *item); + static int transports_count(QQmlListProperty *prop); + static QObject *transports_at(QQmlListProperty *prop, int index); + static void transports_clear(QQmlListProperty *prop); +}; + +QML_DECLARE_TYPE( QQmlWebChannel ) +QML_DECLARE_TYPEINFO( QQmlWebChannel, QML_HAS_ATTACHED_PROPERTIES ) + +QT_END_NAMESPACE + +#endif // QQMLWEBCHANNEL_H diff --git a/src/webchannel/qqmlwebchannelattached.cpp b/src/webchannel/qqmlwebchannelattached.cpp new file mode 100644 index 0000000..cfe03f8 --- /dev/null +++ b/src/webchannel/qqmlwebchannelattached.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebChannel 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 "qqmlwebchannelattached_p.h" + +QT_USE_NAMESPACE + +QQmlWebChannelAttached::QQmlWebChannelAttached(QObject *parent) + : QObject(parent) +{ + +} + +QQmlWebChannelAttached::~QQmlWebChannelAttached() +{ + +} + +QString QQmlWebChannelAttached::id() const +{ + return m_id; +} + +void QQmlWebChannelAttached::setId(const QString &id) +{ + if (id != m_id) { + m_id = id; + emit idChanged(id); + } +} diff --git a/src/webchannel/qqmlwebchannelattached_p.h b/src/webchannel/qqmlwebchannelattached_p.h new file mode 100644 index 0000000..09d18d3 --- /dev/null +++ b/src/webchannel/qqmlwebchannelattached_p.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebChannel 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 QQMLWEBCHANNELATTACHED_H +#define QQMLWEBCHANNELATTACHED_H + +#include + +#include "qwebchannelglobal.h" + +QT_BEGIN_NAMESPACE + +class Q_WEBCHANNEL_EXPORT QQmlWebChannelAttached : public QObject +{ + Q_OBJECT + + Q_PROPERTY( QString id READ id WRITE setId NOTIFY idChanged FINAL ) +public: + explicit QQmlWebChannelAttached(QObject *parent = 0); + virtual ~QQmlWebChannelAttached(); + + QString id() const; + void setId(const QString &id); + +Q_SIGNALS: + void idChanged(const QString &id); + +private: + QString m_id; +}; + +QT_END_NAMESPACE + +#endif // QQMLWEBCHANNELATTACHED_H diff --git a/src/webchannel/qwebchannel.cpp b/src/webchannel/qwebchannel.cpp index 651de58..d0b968e 100644 --- a/src/webchannel/qwebchannel.cpp +++ b/src/webchannel/qwebchannel.cpp @@ -72,13 +72,26 @@ void QWebChannelPrivate::_q_transportDestroyed(QObject *object) } } +void QWebChannelPrivate::init() +{ + Q_Q(QWebChannel); + publisher = new QMetaObjectPublisher(q); + QObject::connect(publisher, SIGNAL(blockUpdatesChanged(bool)), + q, SIGNAL(blockUpdatesChanged(bool))); +} + QWebChannel::QWebChannel(QObject *parent) -: QObject(parent) -, d(new QWebChannelPrivate) +: QObject(*(new QWebChannelPrivate), parent) +{ + Q_D(QWebChannel); + d->init(); +} + +QWebChannel::QWebChannel(QWebChannelPrivate &dd, QObject *parent) +: QObject(dd, parent) { - d->publisher = new QMetaObjectPublisher(this); - connect(d->publisher, SIGNAL(blockUpdatesChanged(bool)), - SIGNAL(blockUpdatesChanged(bool))); + Q_D(QWebChannel); + d->init(); } QWebChannel::~QWebChannel() @@ -87,6 +100,7 @@ QWebChannel::~QWebChannel() void QWebChannel::registerObjects(const QHash< QString, QObject * > &objects) { + Q_D(QWebChannel); const QHash::const_iterator end = objects.constEnd(); for (QHash::const_iterator it = objects.constBegin(); it != end; ++it) { d->publisher->registerObject(it.key(), it.value()); @@ -95,32 +109,38 @@ void QWebChannel::registerObjects(const QHash< QString, QObject * > &objects) QHash QWebChannel::registeredObjects() const { + Q_D(const QWebChannel); return d->publisher->registeredObjects; } void QWebChannel::registerObject(const QString &id, QObject *object) { + Q_D(QWebChannel); d->publisher->registerObject(id, object); } void QWebChannel::deregisterObject(QObject *object) { + Q_D(QWebChannel); // handling of deregistration is analogously to handling of a destroyed signal d->publisher->signalEmitted(object, s_destroyedSignalIndex, QVariantList() << QVariant::fromValue(object)); } bool QWebChannel::blockUpdates() const { + Q_D(const QWebChannel); return d->publisher->blockUpdates; } void QWebChannel::setBlockUpdates(bool block) { + Q_D(QWebChannel); d->publisher->setBlockUpdates(block); } void QWebChannel::connectTo(QWebChannelAbstractTransport *transport) { + Q_D(QWebChannel); Q_ASSERT(transport); if (!d->transports.contains(transport)) { d->transports << transport; @@ -134,6 +154,7 @@ void QWebChannel::connectTo(QWebChannelAbstractTransport *transport) void QWebChannel::disconnectFrom(QWebChannelAbstractTransport *transport) { + Q_D(QWebChannel); const int idx = d->transports.indexOf(transport); if (idx != -1) { disconnect(transport, 0, this, 0); @@ -143,6 +164,7 @@ void QWebChannel::disconnectFrom(QWebChannelAbstractTransport *transport) void QWebChannel::sendMessage(const QJsonValue &id, const QJsonValue &data) const { + Q_D(const QWebChannel); if (d->transports.isEmpty()) { qWarning("QWebChannel is not connected to any transports, cannot send messages."); return; diff --git a/src/webchannel/qwebchannel.h b/src/webchannel/qwebchannel.h index 3e6f7a1..a4f34b4 100644 --- a/src/webchannel/qwebchannel.h +++ b/src/webchannel/qwebchannel.h @@ -90,20 +90,22 @@ public: */ void setBlockUpdates(bool block); - void connectTo(QWebChannelAbstractTransport *transport); - void disconnectFrom(QWebChannelAbstractTransport *transport); - Q_SIGNALS: void blockUpdatesChanged(bool block); public Q_SLOTS: + void connectTo(QWebChannelAbstractTransport *transport); + void disconnectFrom(QWebChannelAbstractTransport *transport); + void sendMessage(const QJsonValue &id, const QJsonValue &data = QJsonValue()) const; private: - QScopedPointer d; - Q_PRIVATE_SLOT(d, void _q_transportDestroyed(QObject*)); + Q_DECLARE_PRIVATE(QWebChannel) + QWebChannel(QWebChannelPrivate &dd, QObject *parent = 0); + Q_PRIVATE_SLOT(d_func(), void _q_transportDestroyed(QObject*)); + friend class QMetaObjectPublisher; - friend class QmlWebChannel; + friend class QQmlWebChannel; friend class TestWebChannel; }; diff --git a/src/webchannel/qwebchannel_p.h b/src/webchannel/qwebchannel_p.h index fbdf2ef..62b2cd5 100644 --- a/src/webchannel/qwebchannel_p.h +++ b/src/webchannel/qwebchannel_p.h @@ -44,6 +44,7 @@ #include "qwebchannelglobal.h" +#include #include QT_BEGIN_NAMESPACE @@ -54,11 +55,15 @@ class QMetaObjectPublisher; Q_WEBCHANNEL_EXPORT QByteArray generateJSONMessage(const QJsonValue &id, const QJsonValue &data, bool response); -struct Q_WEBCHANNEL_EXPORT QWebChannelPrivate +class Q_WEBCHANNEL_EXPORT QWebChannelPrivate : public QObjectPrivate { + Q_DECLARE_PUBLIC(QWebChannel) +public: QVector transports; QMetaObjectPublisher *publisher; + void init(); + void _q_transportDestroyed(QObject* object); }; diff --git a/src/webchannel/webchannel.pro b/src/webchannel/webchannel.pro index 82bd79f..d368237 100644 --- a/src/webchannel/webchannel.pro +++ b/src/webchannel/webchannel.pro @@ -1,5 +1,5 @@ TARGET = QtWebChannel -QT = core +QT = core-private CONFIG += warn_on strict_flags load(qt_module) @@ -28,6 +28,16 @@ SOURCES += \ qtHaveModule(qml) { QT += qml DEFINES += HAVE_QML=1 + + SOURCES += \ + qqmlwebchannel.cpp \ + qqmlwebchannelattached.cpp + + PUBLIC_HEADERS += \ + qqmlwebchannel.h + + PRIVATE_HEADERS += \ + qqmlwebchannelattached_p.h } HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS diff --git a/tests/auto/cmake/CMakeLists.txt b/tests/auto/cmake/CMakeLists.txt index 1d588ac..f912bf0 100644 --- a/tests/auto/cmake/CMakeLists.txt +++ b/tests/auto/cmake/CMakeLists.txt @@ -9,6 +9,10 @@ find_package(Qt5Core REQUIRED) include("${_Qt5CTestMacros}") +# Qt5Qml is an optional dependency of QWebChannel headers. Make sure we find it +# in the test. +set(Qt5_MODULE_TEST_DEPENDS Qml) + test_module_includes( WebChannel QWebChannel ) diff --git a/tests/auto/qml/Client.qml b/tests/auto/qml/Client.qml index 609fbac..2a47395 100644 --- a/tests/auto/qml/Client.qml +++ b/tests/auto/qml/Client.qml @@ -149,7 +149,7 @@ Item { verify(msg); verify(msg.data); compare(msg.data.type, Client.QWebChannelMessageTypes.idle); - verify(webChannel.test_clientIsIdle()) + verify(webChannel.clientIsIdle()) } function awaitMessageSkipIdle() diff --git a/tests/auto/qml/qml.cpp b/tests/auto/qml/qml.cpp index 0612bdb..9e3989c 100644 --- a/tests/auto/qml/qml.cpp +++ b/tests/auto/qml/qml.cpp @@ -47,10 +47,12 @@ #endif #include "testtransport.h" +#include "testwebchannel.h" int main(int argc, char **argv) { qmlRegisterType("QtWebChannel.Tests", 1, 0, "TestTransport"); + qmlRegisterType("QtWebChannel.Tests", 1, 0, "TestWebChannel"); return quick_test_main(argc, argv, "qml", QUICK_TEST_SOURCE_DIR); } diff --git a/tests/auto/qml/qml.pro b/tests/auto/qml/qml.pro index ddef2cb..b0c52b5 100644 --- a/tests/auto/qml/qml.pro +++ b/tests/auto/qml/qml.pro @@ -1,4 +1,4 @@ -QT += testlib webchannel +QT += testlib core-private webchannel-private TEMPLATE = app TARGET = qml @@ -9,10 +9,12 @@ IMPORTPATH += $$OUT_PWD/../../../qml $$PWD SOURCES += \ qml.cpp \ - testtransport.cpp + testtransport.cpp \ + testwebchannel.cpp HEADERS += \ - testtransport.h + testtransport.h \ + testwebchannel.h OTHER_FILES += \ WebChannelTest.qml \ diff --git a/tests/auto/qml/testwebchannel.cpp b/tests/auto/qml/testwebchannel.cpp new file mode 100644 index 0000000..2d31b87 --- /dev/null +++ b/tests/auto/qml/testwebchannel.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebChannel 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 "testwebchannel.h" + +#include +#include + +QT_BEGIN_NAMESPACE + +TestWebChannel::TestWebChannel(QObject *parent) + : QQmlWebChannel(parent) +{ + +} + +TestWebChannel::~TestWebChannel() +{ + +} + +bool TestWebChannel::clientIsIdle() const +{ + return QWebChannel::d_func()->publisher->clientIsIdle; +} + +QT_END_NAMESPACE diff --git a/tests/auto/qml/testwebchannel.h b/tests/auto/qml/testwebchannel.h new file mode 100644 index 0000000..a172493 --- /dev/null +++ b/tests/auto/qml/testwebchannel.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebChannel 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 TESTWEBCHANNEL_H +#define TESTWEBCHANNEL_H + +#include + +QT_BEGIN_NAMESPACE + +class TestWebChannel : public QQmlWebChannel +{ + Q_OBJECT + +public: + explicit TestWebChannel(QObject *parent = 0); + virtual ~TestWebChannel(); + + Q_INVOKABLE bool clientIsIdle() const; +}; + +QT_END_NAMESPACE + +#endif // TESTWEBCHANNEL_H diff --git a/tests/auto/qml/tst_bench.qml b/tests/auto/qml/tst_bench.qml index 833720e..e1354d7 100644 --- a/tests/auto/qml/tst_bench.qml +++ b/tests/auto/qml/tst_bench.qml @@ -43,6 +43,7 @@ import QtQuick 2.0 import QtTest 1.0 import QtWebChannel 1.0 +import QtWebChannel.Tests 1.0 import "qrc:///qwebchannel/qwebchannel.js" as Client TestCase { @@ -53,7 +54,7 @@ TestCase { id: client } - WebChannel { + TestWebChannel { id: webChannel transports: [client.serverTransport] } diff --git a/tests/auto/qml/tst_metaobjectpublisher.qml b/tests/auto/qml/tst_metaobjectpublisher.qml index 880b30d..62b9866 100644 --- a/tests/auto/qml/tst_metaobjectpublisher.qml +++ b/tests/auto/qml/tst_metaobjectpublisher.qml @@ -43,6 +43,7 @@ import QtQuick 2.0 import QtTest 1.0 import QtWebChannel 1.0 +import QtWebChannel.Tests 1.0 import "qrc:///qwebchannel/qwebchannel.js" as Client TestCase { @@ -95,7 +96,7 @@ TestCase { } } - WebChannel { + TestWebChannel { id: webChannel transports: [client.serverTransport] registeredObjects: [myObj, myOtherObj, myFactory] diff --git a/tests/auto/qml/tst_multiclient.qml b/tests/auto/qml/tst_multiclient.qml index 6c4b9b7..739ace7 100644 --- a/tests/auto/qml/tst_multiclient.qml +++ b/tests/auto/qml/tst_multiclient.qml @@ -43,6 +43,7 @@ import QtQuick 2.0 import QtTest 1.0 import QtWebChannel 1.0 +import QtWebChannel.Tests 1.0 import "qrc:///qwebchannel/qwebchannel.js" as Client TestCase { @@ -70,7 +71,7 @@ TestCase { WebChannel.id: "foo" } - WebChannel { + TestWebChannel { id: webChannel transports: [client1.serverTransport, client2.serverTransport] registeredObjects: [foo] diff --git a/tests/auto/qml/tst_webchannel.qml b/tests/auto/qml/tst_webchannel.qml index 3c404d3..0443802 100644 --- a/tests/auto/qml/tst_webchannel.qml +++ b/tests/auto/qml/tst_webchannel.qml @@ -43,6 +43,7 @@ import QtQuick 2.0 import QtTest 1.0 import QtWebChannel 1.0 +import QtWebChannel.Tests 1.0 import "qrc:///qwebchannel/qwebchannel.js" as Client TestCase { @@ -52,7 +53,7 @@ TestCase { id: client } - WebChannel { + TestWebChannel { id: webChannel transports: [client.serverTransport] } diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index 10baaa3..632d055 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -83,7 +83,7 @@ void TestWebChannel::testRegisterObjects() QHash objects; objects[QStringLiteral("plain")] = &plain; objects[QStringLiteral("channel")] = &channel; - objects[QStringLiteral("publisher")] = channel.d->publisher; + objects[QStringLiteral("publisher")] = channel.d_func()->publisher; objects[QStringLiteral("test")] = this; channel.registerObjects(objects); @@ -95,7 +95,7 @@ void TestWebChannel::testInfoForObject() obj.setObjectName("myTestObject"); QWebChannel channel; - const QJsonObject info = channel.d->publisher->classInfoForObject(&obj); + const QJsonObject info = channel.d_func()->publisher->classInfoForObject(&obj); QCOMPARE(info.keys(), QStringList() << "enums" << "methods" << "properties" << "signals"); @@ -227,19 +227,19 @@ void TestWebChannel::testInvokeMethodConversion() { int method = metaObject()->indexOfMethod("setInt(int)"); QVERIFY(method != -1); - QVERIFY(!channel.d->publisher->invokeMethod(this, method, args, QJsonValue()).isEmpty()); + QVERIFY(!channel.d_func()->publisher->invokeMethod(this, method, args, QJsonValue()).isEmpty()); QCOMPARE(m_lastInt, args.at(0).toInt()); } { int method = metaObject()->indexOfMethod("setDouble(double)"); QVERIFY(method != -1); - QVERIFY(!channel.d->publisher->invokeMethod(this, method, args, QJsonValue()).isEmpty()); + QVERIFY(!channel.d_func()->publisher->invokeMethod(this, method, args, QJsonValue()).isEmpty()); QCOMPARE(m_lastDouble, args.at(0).toDouble()); } { int method = metaObject()->indexOfMethod("setVariant(QVariant)"); QVERIFY(method != -1); - QVERIFY(!channel.d->publisher->invokeMethod(this, method, args, QJsonValue()).isEmpty()); + QVERIFY(!channel.d_func()->publisher->invokeMethod(this, method, args, QJsonValue()).isEmpty()); QCOMPARE(m_lastVariant, args.at(0).toVariant()); } } @@ -265,7 +265,7 @@ void TestWebChannel::benchClassInfo() QBENCHMARK { foreach (const QObject *object, objects) { - channel.d->publisher->classInfoForObject(object); + channel.d_func()->publisher->classInfoForObject(object); } } } @@ -278,7 +278,7 @@ void TestWebChannel::benchInitializeClients() QObject parent; channel.registerObjects(createObjects(&parent)); - QMetaObjectPublisher *publisher = channel.d->publisher; + QMetaObjectPublisher *publisher = channel.d_func()->publisher; QBENCHMARK { publisher->initializeClients(); @@ -302,15 +302,15 @@ void TestWebChannel::benchPropertyUpdates() } channel.registerObjects(objects); - channel.d->publisher->initializeClients(); + channel.d_func()->publisher->initializeClients(); QBENCHMARK { foreach (BenchObject *obj, objectList) { obj->change(); } - channel.d->publisher->clientIsIdle = true; - channel.d->publisher->sendPendingPropertyUpdates(); + channel.d_func()->publisher->clientIsIdle = true; + channel.d_func()->publisher->sendPendingPropertyUpdates(); } } diff --git a/tests/auto/webchannel/webchannel.pro b/tests/auto/webchannel/webchannel.pro index 71036dd..40b324e 100644 --- a/tests/auto/webchannel/webchannel.pro +++ b/tests/auto/webchannel/webchannel.pro @@ -1,4 +1,4 @@ -QT = core webchannel-private testlib +QT = core-private webchannel-private testlib CONFIG += testcase strict_flags warn_on -- cgit v1.2.1