Update license headers and add new license files
[contrib/qtwebsockets.git] / src / imports / qmlwebsockets / qqmlwebsocket.cpp
index d2f1016..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$
 **
 ****************************************************************************/
     \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 QUrl WebSocket::url
-  Server url to connect to. The url must have one of 2 schemes: {ws://} or {wss://}.
-  When not supplied, then {ws://} is used.
+  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.
   */
 
 /*!
   \sa WebSocket::status
   */
 
+/*!
+  \qmlmethod void WebSocket::sendTextMessage(string message)
+  Sends \c message to the server.
+  */
+
 #include "qqmlwebsocket.h"
 #include <QtWebSockets/QWebSocket>
 
@@ -113,6 +112,18 @@ 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()
 {
 }
@@ -120,11 +131,11 @@ QQmlWebSocket::~QQmlWebSocket()
 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 0;
     }
-    return m_webSocket->write(message);
+    return m_webSocket->sendTextMessage(message);
 }
 
 QUrl QQmlWebSocket::url() const
@@ -166,8 +177,19 @@ void QQmlWebSocket::classBegin()
 
 void QQmlWebSocket::componentComplete()
 {
-    m_webSocket.reset(new (std::nothrow) QWebSocket());
-    if (Q_LIKELY(m_webSocket)) {
+    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);
@@ -175,10 +197,6 @@ void QQmlWebSocket::componentComplete()
                 this, &QQmlWebSocket::onError);
         connect(m_webSocket.data(), &QWebSocket::stateChanged,
                 this, &QQmlWebSocket::onStateChanged);
-
-        m_componentCompleted = true;
-
-        open();
     }
 }