From 07d7dfffcbb6dd7a4a6efbd9242f08e3ab9d522a Mon Sep 17 00:00:00 2001 From: tobtoht Date: Fri, 4 Mar 2022 22:55:29 +0100 Subject: [PATCH] Settings: allow disabling websocket --- src/SettingsDialog.cpp | 10 +++++++++ src/SettingsDialog.ui | 37 ++++++++++++++++++++++++++++++++++ src/WindowManager.cpp | 4 ++++ src/dialog/DebugInfoDialog.cpp | 6 +++++- src/utils/WebsocketClient.cpp | 16 ++++++++++++++- src/utils/WebsocketClient.h | 3 +++ src/utils/config.cpp | 1 + src/utils/config.h | 1 + 8 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/SettingsDialog.cpp b/src/SettingsDialog.cpp index d8153b0..d8c77f9 100644 --- a/src/SettingsDialog.cpp +++ b/src/SettingsDialog.cpp @@ -8,6 +8,7 @@ #include #include "Icons.h" +#include "utils/WebsocketNotifier.h" Settings::Settings(QSharedPointer ctx, QWidget *parent) : QDialog(parent) @@ -43,6 +44,14 @@ Settings::Settings(QSharedPointer ctx, QWidget *parent) connect(ui->spinBox_inactivityLockTimeout, QOverload::of(&QSpinBox::valueChanged), [](int value){ config()->set(Config::inactivityLockTimeout, value); }); + connect(ui->checkBox_disableWebsocket, &QCheckBox::toggled, [this](bool toggled){ + config()->set(Config::disableWebsocket, toggled); + if (toggled) { + websocketNotifier()->websocketClient.stop(); + } else { + websocketNotifier()->websocketClient.restart(); + } + }); connect(ui->closeButton, &QDialogButtonBox::accepted, this, &Settings::close); @@ -58,6 +67,7 @@ Settings::Settings(QSharedPointer ctx, QWidget *parent) ui->checkBox_disableLogging->setChecked(config()->get(Config::disableLogging).toBool()); ui->checkBox_inactivityLockTimeout->setChecked(config()->get(Config::inactivityLockEnabled).toBool()); ui->spinBox_inactivityLockTimeout->setValue(config()->get(Config::inactivityLockTimeout).toInt()); + ui->checkBox_disableWebsocket->setChecked(config()->get(Config::disableWebsocket).toBool()); // setup comboboxes this->setupSkinCombobox(); diff --git a/src/SettingsDialog.ui b/src/SettingsDialog.ui index 75385eb..6eb3635 100644 --- a/src/SettingsDialog.ui +++ b/src/SettingsDialog.ui @@ -261,6 +261,43 @@ + + + + + + Disable websocket + + + + + + + + 0 + 0 + + + + ? + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/src/WindowManager.cpp b/src/WindowManager.cpp index 47f1866..437695a 100644 --- a/src/WindowManager.cpp +++ b/src/WindowManager.cpp @@ -522,6 +522,10 @@ void WindowManager::onTorSettingsChanged() { } void WindowManager::initWS() { + if (config()->get(Config::disableWebsocket).toBool()) { + return; + } + websocketNotifier()->websocketClient.start(); } diff --git a/src/dialog/DebugInfoDialog.cpp b/src/dialog/DebugInfoDialog.cpp index bdc957c..2b071b2 100644 --- a/src/dialog/DebugInfoDialog.cpp +++ b/src/dialog/DebugInfoDialog.cpp @@ -57,7 +57,11 @@ void DebugInfoDialog::updateInfo() { auto node = m_ctx->nodes->connection(); ui->label_remoteNode->setText(node.toAddress()); ui->label_walletStatus->setText(this->statusToString(m_ctx->wallet->connectionStatus())); - ui->label_websocketStatus->setText(Utils::QtEnumToString(websocketNotifier()->websocketClient.webSocket.state()).remove("State")); + QString websocketStatus = Utils::QtEnumToString(websocketNotifier()->websocketClient.webSocket.state()).remove("State"); + if (config()->get(Config::disableWebsocket).toBool()) { + websocketStatus = "Disabled"; + } + ui->label_websocketStatus->setText(websocketStatus); ui->label_torStatus->setText(torStatus); ui->label_torLevel->setText(config()->get(Config::torPrivacyLevel).toString()); diff --git a/src/utils/WebsocketClient.cpp b/src/utils/WebsocketClient.cpp index ff915e3..948954a 100644 --- a/src/utils/WebsocketClient.cpp +++ b/src/utils/WebsocketClient.cpp @@ -38,15 +38,29 @@ void WebsocketClient::sendMsg(const QByteArray &data) { } void WebsocketClient::start() { + if (m_stopped) { + return; + } + // connect & reconnect on errors/close qDebug() << "WebSocket connect:" << m_url.url(); - auto state = webSocket.state(); if (state != QAbstractSocket::ConnectedState && state != QAbstractSocket::ConnectingState) { webSocket.open(m_url); } } +void WebsocketClient::restart() { + m_stopped = false; + this->start(); +} + +void WebsocketClient::stop() { + m_stopped = true; + webSocket.close(); + m_connectionTimeout.stop(); +} + void WebsocketClient::onConnected() { qDebug() << "WebSocket connected"; emit connectionEstablished(); diff --git a/src/utils/WebsocketClient.h b/src/utils/WebsocketClient.h index d2479a3..ca53be6 100644 --- a/src/utils/WebsocketClient.h +++ b/src/utils/WebsocketClient.h @@ -16,6 +16,8 @@ class WebsocketClient : public QObject { public: explicit WebsocketClient(QObject *parent = nullptr); void start(); + void restart(); + void stop(); void sendMsg(const QByteArray &data); QWebSocket webSocket; @@ -39,6 +41,7 @@ private: QTimer m_connectionTimeout; int m_timeout = 10; int m_websocketUrlIndex = 0; + bool m_stopped = false; }; #endif //FEATHER_WEBSOCKETCLIENT_H diff --git a/src/utils/config.cpp b/src/utils/config.cpp index 5157ee9..3026592 100644 --- a/src/utils/config.cpp +++ b/src/utils/config.cpp @@ -67,6 +67,7 @@ static const QHash configStrings = { {Config::balanceDisplay, {QS("balanceDisplay"), Config::BalanceDisplay::spendablePlusUnconfirmed}}, {Config::inactivityLockEnabled, {QS("inactivityLockEnabled"), false}}, {Config::inactivityLockTimeout, {QS("inactivityLockTimeout"), 10}}, + {Config::disableWebsocket, {QS("disableWebsocket"), false}}, {Config::multiBroadcast, {QS("multiBroadcast"), true}}, {Config::warnOnExternalLink,{QS("warnOnExternalLink"), true}}, diff --git a/src/utils/config.h b/src/utils/config.h index 7c2a360..6327175 100644 --- a/src/utils/config.h +++ b/src/utils/config.h @@ -71,6 +71,7 @@ public: balanceDisplay, inactivityLockEnabled, inactivityLockTimeout, + disableWebsocket, multiBroadcast, warnOnExternalLink,