Skip to content

Commit

Permalink
qml, proxy: Persist proxy settings
Browse files Browse the repository at this point in the history
Addding the logic to persist the ip address and port values of both
the default proxy and the tor proxy configuration into settings.js.
  • Loading branch information
pablomartin4btc committed Nov 14, 2024
1 parent 4dea7c9 commit 0c9722a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/qml/components/ProxySettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ ColumnLayout {
} else {
defaultProxy.state = "FILLED"
}
optionsModel.setIsProxySet(checked)
}
checked: optionsModel.isProxySet
}
onClicked: {
loadedItem.toggle()
Expand All @@ -52,10 +54,12 @@ ColumnLayout {
showErrorText: !defaultProxy.loadedItem.validInput && defaultProxyEnable.loadedItem.checked
actionItem: IPAddressValueInput {
parentState: defaultProxy.state
description: nodeModel.defaultProxyAddress()
text: optionsModel.proxyAddress
activeFocusOnTab: true
onTextChanged: {
validInput = nodeModel.validateProxyAddress(text);
if (validInput = nodeModel.validateProxyAddress(text)) {
optionsModel.setProxyAddress(text);
}
}
}
onClicked: {
Expand Down Expand Up @@ -86,7 +90,9 @@ ColumnLayout {
} else {
torProxy.state = "FILLED"
}
optionsModel.setIsTorProxySet(checked)
}
checked: optionsModel.isTorProxySet
}
onClicked: {
loadedItem.toggle()
Expand All @@ -103,10 +109,12 @@ ColumnLayout {
showErrorText: !torProxy.loadedItem.validInput && torProxyEnable.loadedItem.checked
actionItem: IPAddressValueInput {
parentState: torProxy.state
description: nodeModel.defaultProxyAddress()
text: optionsModel.torProxyAddress
activeFocusOnTab: true
onTextChanged: {
validInput = nodeModel.validateProxyAddress(text);
if (validInput = nodeModel.validateProxyAddress(text)) {
optionsModel.setTorProxyAddress(text);
}
}
}
onClicked: {
Expand Down
98 changes: 98 additions & 0 deletions src/qml/models/options_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ OptionsQmlModel::OptionsQmlModel(interfaces::Node& node, bool is_onboarded)
m_upnp = SettingToBool(m_node.getPersistentSetting("upnp"), DEFAULT_UPNP);

m_dataDir = getDefaultDataDirString();

m_proxy_address = QString::fromStdString(SettingToString(m_node.getPersistentSetting("proxy"), ""));

m_is_proxy_set = evaluateIfProxyIsSet();

m_tor_proxy_address = QString::fromStdString(SettingToString(m_node.getPersistentSetting("onion"), ""));

m_is_tor_proxy_set = evaluateIfTorProxyIsSet();
}

void OptionsQmlModel::setDbcacheSizeMiB(int new_dbcache_size_mib)
Expand Down Expand Up @@ -225,5 +233,95 @@ void OptionsQmlModel::onboard()
if (m_upnp) {
m_node.updateRwSetting("upnp", m_upnp);
}
if (m_is_proxy_set && !m_proxy_address.isEmpty() && m_node.validateProxyAddress(m_proxy_address.toStdString())) {
m_node.updateRwSetting("proxy", m_proxy_address.toStdString());
}
if (m_is_tor_proxy_set && !m_tor_proxy_address.isEmpty() && m_node.validateProxyAddress(m_tor_proxy_address.toStdString())) {
m_node.updateRwSetting("onion", m_tor_proxy_address.toStdString());
}
m_onboarded = true;
}

void OptionsQmlModel::setProxyAddress(QString new_proxy_address)
{
if (new_proxy_address != m_proxy_address) {
m_proxy_address = new_proxy_address;
if (m_onboarded && m_node.validateProxyAddress(new_proxy_address.toStdString())) {
m_node.updateRwSetting("proxy", new_proxy_address.toStdString());
}
Q_EMIT proxyAddressChanged(new_proxy_address);
}
}

void OptionsQmlModel::setIsProxySet(bool is_set)
{
if (is_set != m_is_proxy_set) {
m_is_proxy_set = is_set;
if (m_onboarded && m_is_proxy_set && m_node.validateProxyAddress(m_proxy_address.toStdString())) {
m_node.updateRwSetting("proxy", m_proxy_address.toStdString());
}
if (!m_is_proxy_set) {
m_node.updateRwSetting("proxy", {});
}
Q_EMIT isProxySetChanged(is_set);
}
}

bool OptionsQmlModel::evaluateIfProxyIsSet()
{
bool proxyHasBeenSet;

if (!m_proxy_address.isEmpty()) {
if (!m_node.validateProxyAddress(m_proxy_address.toStdString())) {
m_proxy_address = "";
}
}

proxyHasBeenSet = !m_proxy_address.isEmpty();
if (!proxyHasBeenSet) {
m_proxy_address = QString::fromStdString(m_node.defaultProxyAddress());
}
return proxyHasBeenSet;
}

void OptionsQmlModel::setTorProxyAddress(QString new_proxy_address)
{
if (new_proxy_address != m_tor_proxy_address) {
m_tor_proxy_address = new_proxy_address;
if (m_onboarded && m_node.validateProxyAddress(new_proxy_address.toStdString())) {
m_node.updateRwSetting("onion", new_proxy_address.toStdString());
}
Q_EMIT torProxyAddressChanged(new_proxy_address);
}
}

void OptionsQmlModel::setIsTorProxySet(bool is_set)
{
if (is_set != m_is_tor_proxy_set) {
m_is_tor_proxy_set = is_set;
if (m_onboarded && m_is_proxy_set && m_node.validateProxyAddress(m_tor_proxy_address.toStdString())) {
m_node.updateRwSetting("onion", m_tor_proxy_address.toStdString());
}
if (!m_is_proxy_set) {
m_node.updateRwSetting("onion", {});
}
Q_EMIT isTorProxySetChanged(is_set);
}
}

bool OptionsQmlModel::evaluateIfTorProxyIsSet()
{
bool proxyHasBeenSet;

if (!m_tor_proxy_address.isEmpty()) {
if (!m_node.validateProxyAddress(m_tor_proxy_address.toStdString())) {
m_tor_proxy_address = "";
}
}

proxyHasBeenSet = !m_tor_proxy_address.isEmpty();
if (!proxyHasBeenSet) {
m_tor_proxy_address = QString::fromStdString(m_node.defaultProxyAddress());
}
return proxyHasBeenSet;
}
24 changes: 24 additions & 0 deletions src/qml/models/options_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class OptionsQmlModel : public QObject
Q_PROPERTY(QString dataDir READ dataDir WRITE setDataDir NOTIFY dataDirChanged)
Q_PROPERTY(QString getDefaultDataDirString READ getDefaultDataDirString CONSTANT)
Q_PROPERTY(QUrl getDefaultDataDirectory READ getDefaultDataDirectory CONSTANT)
Q_PROPERTY(QString proxyAddress READ proxyAddress WRITE setProxyAddress NOTIFY proxyAddressChanged)
Q_PROPERTY(bool isProxySet READ isProxySet WRITE setIsProxySet NOTIFY isProxySetChanged)
Q_PROPERTY(QString torProxyAddress READ torProxyAddress WRITE setTorProxyAddress NOTIFY proxyAddressChanged)
Q_PROPERTY(bool isTorProxySet READ isTorProxySet WRITE setIsTorProxySet NOTIFY isTorProxySetChanged)

protected:
bool evaluateIfProxyIsSet();
bool evaluateIfTorProxyIsSet();

public:
explicit OptionsQmlModel(interfaces::Node& node, bool is_onboarded);
Expand Down Expand Up @@ -67,6 +75,14 @@ class OptionsQmlModel : public QObject
QUrl getDefaultDataDirectory();
Q_INVOKABLE bool setCustomDataDirArgs(QString path);
Q_INVOKABLE QString getCustomDataDirString();
QString proxyAddress() const { return m_proxy_address; }
Q_INVOKABLE void setProxyAddress(QString new_proxy_address);
bool isProxySet() const { return m_is_proxy_set; }
Q_INVOKABLE void setIsProxySet(bool is_set);
QString torProxyAddress() const { return m_tor_proxy_address; }
Q_INVOKABLE void setTorProxyAddress(QString new_proxy_address);
bool isTorProxySet() const { return m_is_tor_proxy_set; }
Q_INVOKABLE void setIsTorProxySet(bool is_set);

public Q_SLOTS:
void setCustomDataDirString(const QString &new_custom_datadir_string) {
Expand All @@ -85,6 +101,10 @@ public Q_SLOTS:
void upnpChanged(bool new_upnp);
void customDataDirStringChanged(QString new_custom_datadir_string);
void dataDirChanged(QString new_data_dir);
void proxyAddressChanged(QString new_proxy_address);
void isProxySetChanged(bool is_set);
void torProxyAddressChanged(QString new_proxy_address);
void isTorProxySetChanged(bool is_set);

private:
interfaces::Node& m_node;
Expand All @@ -105,6 +125,10 @@ public Q_SLOTS:
bool m_upnp;
QString m_custom_datadir_string;
QString m_dataDir;
QString m_proxy_address;
bool m_is_proxy_set;
QString m_tor_proxy_address;
bool m_is_tor_proxy_set;

common::SettingsValue pruneSetting() const;
};
Expand Down

0 comments on commit 0c9722a

Please sign in to comment.