File indexing completed on 2024-12-08 10:25:55

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ruqolawebsocket.h"
0008 #include "config-ruqola.h"
0009 #include "ruqola_debug.h"
0010 #include "ruqolalogger.h"
0011 
0012 #include <QWebSocket>
0013 
0014 RuqolaWebSocket::RuqolaWebSocket(RuqolaLogger *logger, QObject *parent)
0015     : AbstractWebSocket(parent)
0016     , mLogger(logger)
0017     , mWebSocket(new QWebSocket)
0018 {
0019     connect(mWebSocket, &QWebSocket::connected, this, &RuqolaWebSocket::connected);
0020     connect(mWebSocket, &QWebSocket::disconnected, this, &RuqolaWebSocket::disconnected);
0021     connect(mWebSocket, &QWebSocket::textMessageReceived, this, &RuqolaWebSocket::slotTextMessageReceived);
0022     connect(mWebSocket, &QWebSocket::sslErrors, this, &RuqolaWebSocket::sslErrors);
0023 #if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
0024     connect(mWebSocket, qOverload<QAbstractSocket::SocketError>(&QWebSocket::error), this, &RuqolaWebSocket::slotError);
0025 #else
0026     connect(mWebSocket, &QWebSocket::errorOccurred, this, &RuqolaWebSocket::slotError);
0027 #endif
0028 }
0029 
0030 RuqolaWebSocket::~RuqolaWebSocket()
0031 {
0032     delete mWebSocket;
0033 }
0034 
0035 void RuqolaWebSocket::openUrl(const QUrl &url)
0036 {
0037     QNetworkRequest request;
0038     request.setUrl(url);
0039     request.setHeader(QNetworkRequest::UserAgentHeader,
0040                       QStringLiteral("webkit/Ruqola-%1 (%2 %3) webkit/Ruqola-%1")
0041                           .arg(QStringLiteral(RUQOLA_VERSION), QSysInfo::prettyProductName(), QSysInfo::currentCpuArchitecture()));
0042     mWebSocket->open(request);
0043 }
0044 
0045 qint64 RuqolaWebSocket::sendTextMessage(const QString &message)
0046 {
0047     qCDebug(RUQOLA_LOG) << " qint64 RuqolaWebSocket::sendTextMessage(const QString &message)" << message;
0048     if (mLogger) {
0049         mLogger->dataSent(message.toUtf8());
0050     }
0051     return mWebSocket->sendTextMessage(message);
0052 }
0053 
0054 bool RuqolaWebSocket::isValid() const
0055 {
0056     return mWebSocket->isValid();
0057 }
0058 
0059 void RuqolaWebSocket::flush()
0060 {
0061     mWebSocket->flush();
0062 }
0063 
0064 void RuqolaWebSocket::close()
0065 {
0066     mWebSocket->close();
0067 }
0068 
0069 QAbstractSocket::SocketError RuqolaWebSocket::error() const
0070 {
0071     return mWebSocket->error();
0072 }
0073 
0074 QString RuqolaWebSocket::errorString() const
0075 {
0076     return mWebSocket->errorString();
0077 }
0078 
0079 QUrl RuqolaWebSocket::requestUrl() const
0080 {
0081     return mWebSocket->requestUrl();
0082 }
0083 
0084 QWebSocketProtocol::CloseCode RuqolaWebSocket::closeCode() const
0085 {
0086     return mWebSocket->closeCode();
0087 }
0088 
0089 QString RuqolaWebSocket::closeReason() const
0090 {
0091     return mWebSocket->closeReason();
0092 }
0093 
0094 qint64 RuqolaWebSocket::sendBinaryMessage(const QByteArray &data)
0095 {
0096     return mWebSocket->sendBinaryMessage(data);
0097 }
0098 
0099 void RuqolaWebSocket::ignoreSslErrors()
0100 {
0101     mWebSocket->ignoreSslErrors();
0102 }
0103 
0104 void RuqolaWebSocket::slotTextMessageReceived(const QString &msg)
0105 {
0106     if (mLogger) {
0107         mLogger->dataReceived(msg.toUtf8());
0108     }
0109     Q_EMIT textMessageReceived(msg);
0110 }
0111 
0112 void RuqolaWebSocket::slotError(QAbstractSocket::SocketError error)
0113 {
0114     Q_EMIT socketError(error, mWebSocket->errorString());
0115 }
0116 
0117 #include "moc_ruqolawebsocket.cpp"