From: tobtoht Date: Mon, 14 Mar 2022 21:36:58 +0000 (+0100) Subject: Settings: offline mode X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=54140df5976bdeaebac766f71fb0081bfe0702a3;p=gamesguru%2Ffeather.git Settings: offline mode --- diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 82f818c6..52f7b4ae 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -451,8 +451,6 @@ void MainWindow::onWalletOpened() { this->bringToFront(); this->setEnabled(true); - if (!torManager()->torConnected) - this->setStatusText("Starting Tor (may take a while)"); // receive page m_ctx->wallet->subaddress()->refresh(m_ctx->wallet->currentSubaddressAccount()); diff --git a/src/SettingsDialog.cpp b/src/SettingsDialog.cpp index 42cb3a2d..990e47a2 100644 --- a/src/SettingsDialog.cpp +++ b/src/SettingsDialog.cpp @@ -9,6 +9,7 @@ #include "Icons.h" #include "utils/WebsocketNotifier.h" +#include "utils/NetworkManager.h" Settings::Settings(QSharedPointer ctx, QWidget *parent) : QDialog(parent) @@ -48,13 +49,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->checkBox_disableWebsocket, &QCheckBox::toggled, [this](bool checked){ + config()->set(Config::disableWebsocket, checked); + this->enableWebsocket(checked); + }); + connect(ui->checkBox_offlineMode, &QCheckBox::toggled, [this](bool checked){ + config()->set(Config::offlineMode, checked); + m_ctx->wallet->setOffline(checked); + this->enableWebsocket(checked); }); connect(ui->closeButton, &QDialogButtonBox::accepted, this, &Settings::close); @@ -72,6 +74,7 @@ Settings::Settings(QSharedPointer ctx, QWidget *parent) 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()); + ui->checkBox_offlineMode->setChecked(config()->get(Config::offlineMode).toBool()); // setup comboboxes this->setupSkinCombobox(); @@ -226,4 +229,12 @@ void Settings::setupLocalMoneroFrontendCombobox() { ui->combo_localMoneroFrontend->setCurrentIndex(ui->combo_localMoneroFrontend->findData(config()->get(Config::localMoneroFrontend).toString())); } +void Settings::enableWebsocket(bool enabled) { + if (enabled && !config()->get(Config::offlineMode).toBool() && !config()->get(Config::disableWebsocket).toBool()) { + websocketNotifier()->websocketClient.restart(); + } else { + websocketNotifier()->websocketClient.stop(); + } +} + Settings::~Settings() = default; \ No newline at end of file diff --git a/src/SettingsDialog.h b/src/SettingsDialog.h index edbb77b5..97d8c99f 100644 --- a/src/SettingsDialog.h +++ b/src/SettingsDialog.h @@ -49,6 +49,7 @@ public slots: private: void setupSkinCombobox(); void setupLocalMoneroFrontendCombobox(); + void enableWebsocket(bool enabled); QScopedPointer ui; QSharedPointer m_ctx; diff --git a/src/SettingsDialog.ui b/src/SettingsDialog.ui index 6eb3635f..ee16aaec 100644 --- a/src/SettingsDialog.ui +++ b/src/SettingsDialog.ui @@ -17,7 +17,7 @@ - 0 + 1 @@ -346,6 +346,13 @@ + + + + Offline mode + + + diff --git a/src/WindowManager.cpp b/src/WindowManager.cpp index 437695a4..a76c130a 100644 --- a/src/WindowManager.cpp +++ b/src/WindowManager.cpp @@ -489,6 +489,7 @@ void WindowManager::onInitialNetworkConfigured() { if (!m_initialNetworkConfigured) { m_initialNetworkConfigured = true; appData(); + this->initTor(); this->initWS(); } @@ -522,6 +523,10 @@ void WindowManager::onTorSettingsChanged() { } void WindowManager::initWS() { + if (config()->get(Config::offlineMode).toBool()) { + return; + } + if (config()->get(Config::disableWebsocket).toBool()) { return; } diff --git a/src/dialog/DebugInfoDialog.cpp b/src/dialog/DebugInfoDialog.cpp index 2b071b20..86a6b74c 100644 --- a/src/dialog/DebugInfoDialog.cpp +++ b/src/dialog/DebugInfoDialog.cpp @@ -88,7 +88,11 @@ void DebugInfoDialog::updateInfo() { } }(); - ui->label_netType->setText(Utils::QtEnumToString(m_ctx->wallet->nettype())); + QString networkType = Utils::QtEnumToString(m_ctx->wallet->nettype()); + if (config()->get(Config::offlineMode).toBool()) { + networkType += " (offline)"; + } + ui->label_netType->setText(networkType); ui->label_seedType->setText(seedType); ui->label_deviceType->setText(deviceType); ui->label_viewOnly->setText(m_ctx->wallet->viewOnly() ? "True" : "False"); diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index 07c250da..6a60cd19 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -148,6 +148,11 @@ bool Wallet::isConnected() const return status == ConnectionStatus_Synchronizing || status == ConnectionStatus_Synchronized; } +void Wallet::setOffline(bool offline) const +{ + return m_walletImpl->setOffline(offline); +} + QString Wallet::errorString() const { return QString::fromStdString(m_walletImpl->errorString()); diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h index 19ec6855..9b08f5e8 100644 --- a/src/libwalletqt/Wallet.h +++ b/src/libwalletqt/Wallet.h @@ -127,6 +127,8 @@ public: //! return true if wallet is connected to a node bool isConnected() const; + void setOffline(bool offline) const; + //! returns last operation's error message QString errorString() const; diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index 0a07e792..499c3cf3 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -238,7 +238,11 @@ bool xdgDesktopEntryRegister() { return true; } -bool portOpen(const QString &hostname, quint16 port){ +bool portOpen(const QString &hostname, quint16 port) { // TODO: this call should be async + if (config()->get(Config::offlineMode).toBool()) { + return false; + } + QTcpSocket socket; socket.connectToHost(hostname, port); return socket.waitForConnected(600); diff --git a/src/utils/config.cpp b/src/utils/config.cpp index bba161c2..24dc35fa 100644 --- a/src/utils/config.cpp +++ b/src/utils/config.cpp @@ -69,6 +69,7 @@ static const QHash configStrings = { {Config::inactivityLockEnabled, {QS("inactivityLockEnabled"), false}}, {Config::inactivityLockTimeout, {QS("inactivityLockTimeout"), 10}}, {Config::disableWebsocket, {QS("disableWebsocket"), false}}, + {Config::offlineMode, {QS("offlineMode"), false}}, {Config::multiBroadcast, {QS("multiBroadcast"), true}}, {Config::warnOnExternalLink,{QS("warnOnExternalLink"), true}}, diff --git a/src/utils/config.h b/src/utils/config.h index 28698a5a..f0131ae9 100644 --- a/src/utils/config.h +++ b/src/utils/config.h @@ -73,6 +73,7 @@ public: inactivityLockEnabled, inactivityLockTimeout, disableWebsocket, + offlineMode, multiBroadcast, warnOnExternalLink, diff --git a/src/utils/networking.cpp b/src/utils/networking.cpp index 66e0ef78..f1893adc 100644 --- a/src/utils/networking.cpp +++ b/src/utils/networking.cpp @@ -6,16 +6,21 @@ #include "utils/Utils.h" #include "utils/networking.h" +#include "config.h" -UtilsNetworking::UtilsNetworking(QNetworkAccessManager *networkAccessManager, QObject *parent) : - QObject(parent), - m_networkAccessManager(networkAccessManager) {} +UtilsNetworking::UtilsNetworking(QNetworkAccessManager *networkAccessManager, QObject *parent) + : QObject(parent) + , m_networkAccessManager(networkAccessManager) {} void UtilsNetworking::setUserAgent(const QString &userAgent) { this->m_userAgent = userAgent; } QNetworkReply* UtilsNetworking::get(const QString &url) { + if (config()->get(Config::offlineMode).toBool()) { + return nullptr; + } + QNetworkRequest request; request.setUrl(QUrl(url)); request.setRawHeader("User-Agent", m_userAgent.toUtf8()); @@ -24,6 +29,10 @@ QNetworkReply* UtilsNetworking::get(const QString &url) { } QNetworkReply* UtilsNetworking::getJson(const QString &url) { + if (config()->get(Config::offlineMode).toBool()) { + return nullptr; + } + QNetworkRequest request; request.setUrl(QUrl(url)); request.setRawHeader("User-Agent", m_userAgent.toUtf8()); @@ -33,6 +42,10 @@ QNetworkReply* UtilsNetworking::getJson(const QString &url) { } QNetworkReply* UtilsNetworking::postJson(const QString &url, const QJsonObject &data) { + if (config()->get(Config::offlineMode).toBool()) { + return nullptr; + } + QNetworkRequest request; request.setUrl(QUrl(url)); request.setRawHeader("User-Agent", m_userAgent.toUtf8()); diff --git a/src/utils/nodes.cpp b/src/utils/nodes.cpp index 5d398dfe..09d28218 100644 --- a/src/utils/nodes.cpp +++ b/src/utils/nodes.cpp @@ -192,6 +192,10 @@ void Nodes::connectToNode(const FeatherNode &node) { if (!node.isValid()) return; + if (config()->get(Config::offlineMode).toBool()) { + return; + } + emit updateStatus(QString("Connecting to %1").arg(node.toAddress())); qInfo() << QString("Attempting to connect to %1 (%2)").arg(node.toAddress()).arg(node.custom ? "custom" : "ws");