Update license headers and add new license files
[contrib/qtwebsockets.git] / src / imports / qmlwebsockets / qqmlwebsocket.cpp
index 9d2ce7f..edd48c4 100644 (file)
@@ -1,40 +1,32 @@
 /****************************************************************************
 **
-** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the QtWebSockets module of the Qt Toolkit.
 **
-** $QT_BEGIN_LICENSE:LGPL$
+** $QT_BEGIN_LICENSE:LGPL21$
 ** 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
+** 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.
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** 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
+** 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$
 **
 ****************************************************************************/
     \instantiates QQmlWebSocket
 
     \inqmlmodule Qt.WebSockets
+    \ingroup websockets-qml
     \brief QML interface to QWebSocket.
 
-    WebSockets is a web technology providing full-duplex communications channels over a single TCP connection.
-    The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011 (see http://tools.ietf.org/html/rfc6455).
+    WebSockets is a web technology providing full-duplex communications channels over a
+    single TCP connection.
+    The WebSocket protocol was standardized by the IETF as
+    \l {http://tools.ietf.org/html/rfc6455} {RFC 6455} in 2011.
 */
 
 /*!
-  \qmlproperty url QQmlWebSocket::url
-  Server url to connect to. The url must have one of 2 schemes: {ws://} or {wss://}.
-  When not supplied, then {ws://} is used.
+  \qmlproperty QUrl WebSocket::url
+  Server url to connect to. The url must have one of 2 schemes: \e ws:// or \e wss://.
+  When not supplied, then \e ws:// is used.
+  */
+
+/*!
+  \qmlproperty Status WebSocket::status
+  Status of the WebSocket.
+
+  The status can have the following values:
+  \list
+  \li WebSockets.Connecting
+  \li WebSockets.Open
+  \li WebSockets.Closing
+  \li WebSockets.Closed
+  \li WebSockets.Error
+  \endlist
+  */
+
+/*!
+  \qmlproperty QString WebSocket::errorString
+  Contains a description of the last error that occurred. When no error occurrred,
+  this string is empty.
+  */
+
+/*!
+  \qmlproperty bool WebSocket::active
+  When set to true, a connection is made to the server with the given url.
+  When set to false, the connection is closed.
+  The default value is false.
+  */
+
+/*!
+  \qmlsignal WebSocket::textMessageReceived(QString message)
+  This signal is emitted when a text message is received.
+  */
+
+/*!
+  \qmlsignal WebSocket::statusChanged(Status status)
+  This signal is emitted when the status of the WebSocket changes.
+  the \l {WebSocket::status}{status} argument provides the current status.
+
+  \sa WebSocket::status
+  */
+
+/*!
+  \qmlmethod void WebSocket::sendTextMessage(string message)
+  Sends \c message to the server.
   */
 
 #include "qqmlwebsocket.h"
 #include <QtWebSockets/QWebSocket>
 
+QT_BEGIN_NAMESPACE
+
 QQmlWebSocket::QQmlWebSocket(QObject *parent) :
     QObject(parent),
     m_webSocket(),
@@ -70,18 +112,30 @@ QQmlWebSocket::QQmlWebSocket(QObject *parent) :
 {
 }
 
+QQmlWebSocket::QQmlWebSocket(QWebSocket *socket, QObject *parent) :
+    QObject(parent),
+    m_status(Closed),
+    m_url(socket->requestUrl()),
+    m_isActive(true),
+    m_componentCompleted(true),
+    m_errorString(socket->errorString())
+{
+    setSocket(socket);
+    onStateChanged(socket->state());
+}
+
 QQmlWebSocket::~QQmlWebSocket()
 {
 }
 
-void QQmlWebSocket::sendTextMessage(const QString &message)
+qint64 QQmlWebSocket::sendTextMessage(const QString &message)
 {
     if (m_status != Open) {
-        setErrorString(tr("Messages can only be send when the socket has Open status."));
+        setErrorString(tr("Messages can only be sent when the socket is open."));
         setStatus(Error);
-        return;
+        return 0;
     }
-    m_webSocket->write(message);
+    return m_webSocket->sendTextMessage(message);
 }
 
 QUrl QQmlWebSocket::url() const
@@ -123,16 +177,29 @@ void QQmlWebSocket::classBegin()
 
 void QQmlWebSocket::componentComplete()
 {
-    m_webSocket.reset(new QWebSocket());
-    connect(m_webSocket.data(), SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString)));
-    connect(m_webSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
-    connect(m_webSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState)));
+    setSocket(new QWebSocket);
 
     m_componentCompleted = true;
 
     open();
 }
 
+void QQmlWebSocket::setSocket(QWebSocket *socket)
+{
+    m_webSocket.reset(socket);
+    if (m_webSocket) {
+        // explicit ownership via QScopedPointer
+        m_webSocket->setParent(Q_NULLPTR);
+        connect(m_webSocket.data(), &QWebSocket::textMessageReceived,
+                this, &QQmlWebSocket::textMessageReceived);
+        typedef void (QWebSocket::* ErrorSignal)(QAbstractSocket::SocketError);
+        connect(m_webSocket.data(), static_cast<ErrorSignal>(&QWebSocket::error),
+                this, &QQmlWebSocket::onError);
+        connect(m_webSocket.data(), &QWebSocket::stateChanged,
+                this, &QQmlWebSocket::onStateChanged);
+    }
+}
+
 void QQmlWebSocket::onError(QAbstractSocket::SocketError error)
 {
     Q_UNUSED(error)
@@ -210,14 +277,14 @@ bool QQmlWebSocket::isActive() const
 
 void QQmlWebSocket::open()
 {
-    if (m_componentCompleted && m_isActive && m_url.isValid() && m_webSocket) {
+    if (m_componentCompleted && m_isActive && m_url.isValid() && Q_LIKELY(m_webSocket)) {
         m_webSocket->open(m_url);
     }
 }
 
 void QQmlWebSocket::close()
 {
-    if (m_componentCompleted && m_webSocket) {
+    if (m_componentCompleted && Q_LIKELY(m_webSocket)) {
         m_webSocket->close();
     }
 }
@@ -230,3 +297,5 @@ void QQmlWebSocket::setErrorString(QString errorString)
     m_errorString = errorString;
     Q_EMIT errorStringChanged(m_errorString);
 }
+
+QT_END_NAMESPACE