Skip to content

Commit

Permalink
qml: wiring for custom datadir using options_model
Browse files Browse the repository at this point in the history
  • Loading branch information
D33r-Gee committed Jun 20, 2024
1 parent c065a17 commit f3b8c5a
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 31 deletions.
150 changes: 121 additions & 29 deletions src/qml/models/options_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,82 @@
#include <QDir>
#include <QSettings>

OptionsQmlModel::OptionsQmlModel(interfaces::Node& node, bool is_onboarded)
OptionsQmlModel::OptionsQmlModel(interfaces::Node* node, bool is_onboarded)
: m_node{node}
, m_onboarded{is_onboarded}
{
m_dbcache_size_mib = SettingToInt(m_node.getPersistentSetting("dbcache"), nDefaultDbCache);
gArgs.LockSettings([&](common::Settings& cs) {
// Clear existing settings to ensure we're only storing new command line arguments
m_cli_settings.command_line_options.clear();

m_listen = SettingToBool(m_node.getPersistentSetting("listen"), DEFAULT_LISTEN);
// Copy only the command line arguments from the provided settings
m_cli_settings.command_line_options = cs.command_line_options;
});

m_natpmp = SettingToBool(m_node.getPersistentSetting("natpmp"), DEFAULT_NATPMP);
if (!is_onboarded) {
// added this to lock settings to default values
if (!gArgs.IsArgSet("-resetguisettings")) {
gArgs.LockSettings([&](common::Settings& s) { m_previous_settings = s; });
}
m_dbcache_size_mib = nDefaultDbCache;
m_listen = DEFAULT_LISTEN;
m_natpmp = DEFAULT_NATPMP;
int64_t prune_value = 0;
m_prune = (prune_value > 1);
m_prune_size_gb = m_prune ? PruneMiBtoGB(prune_value) : DEFAULT_PRUNE_TARGET_GB;
m_script_threads = DEFAULT_SCRIPTCHECK_THREADS;
m_server = false;
m_upnp = DEFAULT_UPNP;
}

#ifdef __ANDROID__
if (!getCustomDataDirString().isEmpty() && (m_dataDir != getDefaultDataDirString())) {
m_dataDir = getCustomDataDirString();
} else {
m_dataDir = getDefaultDataDirString();
}
#else
QSettings settings;
m_dataDir = settings.value("strDataDir", m_dataDir).toString();
#endif // __ANDROID__
}

void OptionsQmlModel::requestShutdown()
{
Q_EMIT requestedShutdown();
}

void OptionsQmlModel::setNode(interfaces::Node* node, bool is_onboarded) {
if (node != nullptr) {
m_node = node;
if (is_onboarded) {
m_dbcache_size_mib = SettingToInt(m_node->getPersistentSetting("dbcache"), nDefaultDbCache);

m_listen = SettingToBool(m_node->getPersistentSetting("listen"), DEFAULT_LISTEN);

int64_t prune_value{SettingToInt(m_node.getPersistentSetting("prune"), 0)};
m_prune = (prune_value > 1);
m_prune_size_gb = m_prune ? PruneMiBtoGB(prune_value) : DEFAULT_PRUNE_TARGET_GB;
m_natpmp = SettingToBool(m_node->getPersistentSetting("natpmp"), DEFAULT_NATPMP);

m_script_threads = SettingToInt(m_node.getPersistentSetting("par"), DEFAULT_SCRIPTCHECK_THREADS);
int64_t prune_value{SettingToInt(m_node->getPersistentSetting("prune"), 0)};
m_prune = (prune_value > 1);
m_prune_size_gb = m_prune ? PruneMiBtoGB(prune_value) : DEFAULT_PRUNE_TARGET_GB;

m_server = SettingToBool(m_node.getPersistentSetting("server"), false);
m_script_threads = SettingToInt(m_node->getPersistentSetting("par"), DEFAULT_SCRIPTCHECK_THREADS);

m_upnp = SettingToBool(m_node.getPersistentSetting("upnp"), DEFAULT_UPNP);
m_server = SettingToBool(m_node->getPersistentSetting("server"), false);

m_dataDir = getDefaultDataDirString();
m_upnp = SettingToBool(m_node->getPersistentSetting("upnp"), DEFAULT_UPNP);
}
// qDebug() << "Node Set";
return;
}
}

void OptionsQmlModel::setDbcacheSizeMiB(int new_dbcache_size_mib)
{
if (new_dbcache_size_mib != m_dbcache_size_mib) {
m_dbcache_size_mib = new_dbcache_size_mib;
if (m_onboarded) {
m_node.updateRwSetting("dbcache", new_dbcache_size_mib);
m_node->updateRwSetting("dbcache", new_dbcache_size_mib);
}
Q_EMIT dbcacheSizeMiBChanged(new_dbcache_size_mib);
}
Expand All @@ -63,7 +110,7 @@ void OptionsQmlModel::setListen(bool new_listen)
if (new_listen != m_listen) {
m_listen = new_listen;
if (m_onboarded) {
m_node.updateRwSetting("listen", new_listen);
m_node->updateRwSetting("listen", new_listen);
}
Q_EMIT listenChanged(new_listen);
}
Expand All @@ -74,7 +121,7 @@ void OptionsQmlModel::setNatpmp(bool new_natpmp)
if (new_natpmp != m_natpmp) {
m_natpmp = new_natpmp;
if (m_onboarded) {
m_node.updateRwSetting("natpmp", new_natpmp);
m_node->updateRwSetting("natpmp", new_natpmp);
}
Q_EMIT natpmpChanged(new_natpmp);
}
Expand All @@ -85,7 +132,7 @@ void OptionsQmlModel::setPrune(bool new_prune)
if (new_prune != m_prune) {
m_prune = new_prune;
if (m_onboarded) {
m_node.updateRwSetting("prune", pruneSetting());
m_node->updateRwSetting("prune", pruneSetting());
}
Q_EMIT pruneChanged(new_prune);
}
Expand All @@ -96,7 +143,7 @@ void OptionsQmlModel::setPruneSizeGB(int new_prune_size_gb)
if (new_prune_size_gb != m_prune_size_gb) {
m_prune_size_gb = new_prune_size_gb;
if (m_onboarded) {
m_node.updateRwSetting("prune", pruneSetting());
m_node->updateRwSetting("prune", pruneSetting());
}
Q_EMIT pruneSizeGBChanged(new_prune_size_gb);
}
Expand All @@ -107,7 +154,7 @@ void OptionsQmlModel::setScriptThreads(int new_script_threads)
if (new_script_threads != m_script_threads) {
m_script_threads = new_script_threads;
if (m_onboarded) {
m_node.updateRwSetting("par", new_script_threads);
m_node->updateRwSetting("par", new_script_threads);
}
Q_EMIT scriptThreadsChanged(new_script_threads);
}
Expand All @@ -118,7 +165,7 @@ void OptionsQmlModel::setServer(bool new_server)
if (new_server != m_server) {
m_server = new_server;
if (m_onboarded) {
m_node.updateRwSetting("server", new_server);
m_node->updateRwSetting("server", new_server);
}
Q_EMIT serverChanged(new_server);
}
Expand All @@ -129,7 +176,7 @@ void OptionsQmlModel::setUpnp(bool new_upnp)
if (new_upnp != m_upnp) {
m_upnp = new_upnp;
if (m_onboarded) {
m_node.updateRwSetting("upnp", new_upnp);
m_node->updateRwSetting("upnp", new_upnp);
}
Q_EMIT upnpChanged(new_upnp);
}
Expand Down Expand Up @@ -158,6 +205,33 @@ QUrl OptionsQmlModel::getDefaultDataDirectory()
return QUrl::fromLocalFile(path);
}

void OptionsQmlModel::defaultReset()
{
QSettings settings;
// Save the strDataDir setting
QString path = GUIUtil::getDefaultDataDirectory();

setDataDir(path);

// Remove all entries from our QSettings object
settings.clear();

// Set strDataDir
settings.setValue("strDataDir", path);

// Set that this was reset
settings.setValue("fReset", true);

// Clear the settings
gArgs.LockSettings([&](common::Settings& s) { s = m_previous_settings; });

// reinstate command line arguments
gArgs.LockSettings([&](common::Settings& cs) { cs = m_cli_settings; });
gArgs.SoftSetBoolArg("-printtoconsole", false);

gArgs.ClearPathCache();
}

bool OptionsQmlModel::setCustomDataDirArgs(QString path)
{
if (!path.isEmpty()) {
Expand All @@ -168,12 +242,29 @@ bool OptionsQmlModel::setCustomDataDirArgs(QString path)
QString newPrefix = "/storage/self/primary/";
QString path = uri.replace(originalPrefix, newPrefix);
#else
QSettings settings;
path = QUrl(path).toLocalFile();
#endif // __ANDROID__
qDebug() << "PlaceHolder: Created data directory: " << path;
// Delet before merge
// qDebug() << "PlaceHolder: Created data directory: " << path;
try{
if (TryCreateDirectories(GUIUtil::QStringToPath(path))) {
// qDebug() << "Created data directory: " << path;
TryCreateDirectories(GUIUtil::QStringToPath(path) / "wallets");
}
} catch (const std::exception& e) {
qDebug() << "Error creating data directory: " << e.what();
}
#ifndef __ANDROID__
settings.setValue("strDataDir", path);
#endif // __ANDROID__
if(path != GUIUtil::getDefaultDataDirectory()) {
gArgs.SoftSetArg("-datadir", fs::PathToString(GUIUtil::QStringToPath(path)));
}
gArgs.ClearPathCache();
Q_EMIT customDataDirStringChanged(m_custom_datadir_string);

m_custom_datadir_string = path;
Q_EMIT customDataDirStringChanged(path);
setDataDir(path);
return true;
}
Expand Down Expand Up @@ -203,27 +294,28 @@ void OptionsQmlModel::setDataDir(QString new_data_dir)

void OptionsQmlModel::onboard()
{
m_node.resetSettings();
m_node->resetSettings();
if (m_dbcache_size_mib != nDefaultDbCache) {
m_node.updateRwSetting("dbcache", m_dbcache_size_mib);
m_node->updateRwSetting("dbcache", m_dbcache_size_mib);
}
if (m_listen) {
m_node.updateRwSetting("listen", m_listen);
m_node->updateRwSetting("listen", m_listen);
}
if (m_natpmp) {
m_node.updateRwSetting("natpmp", m_natpmp);
m_node->updateRwSetting("natpmp", m_natpmp);
}
if (m_prune) {
m_node.updateRwSetting("prune", pruneSetting());
m_node->updateRwSetting("prune", pruneSetting());
}
if (m_script_threads != DEFAULT_SCRIPTCHECK_THREADS) {
m_node.updateRwSetting("par", m_script_threads);
m_node->updateRwSetting("par", m_script_threads);
}
if (m_server) {
m_node.updateRwSetting("server", m_server);
m_node->updateRwSetting("server", m_server);
}
if (m_upnp) {
m_node.updateRwSetting("upnp", m_upnp);
m_node->updateRwSetting("upnp", m_upnp);
}

m_onboarded = true;
}
22 changes: 20 additions & 2 deletions src/qml/models/options_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
#define BITCOIN_QML_MODELS_OPTIONS_MODEL_H

#include <txdb.h>
#include <chainparams.h>
#include <clientversion.h>
#include <common/settings.h>
#include <common/system.h>
#include <interfaces/chain.h>
#include <validation.h>

#include <QObject>
Expand Down Expand Up @@ -37,10 +40,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 fullClientVersion READ fullClientVersion CONSTANT)
Q_PROPERTY(quint64 assumedBlockchainSize READ assumedBlockchainSize CONSTANT)
Q_PROPERTY(quint64 assumedChainstateSize READ assumedChainstateSize CONSTANT)

public:
explicit OptionsQmlModel(interfaces::Node& node, bool is_onboarded);
explicit OptionsQmlModel(interfaces::Node* node, bool is_onboarded);

void setNode(interfaces::Node* node, bool is_onboarded);
int dbcacheSizeMiB() const { return m_dbcache_size_mib; }
void setDbcacheSizeMiB(int new_dbcache_size_mib);
bool listen() const { return m_listen; }
Expand All @@ -67,12 +74,17 @@ class OptionsQmlModel : public QObject
QUrl getDefaultDataDirectory();
Q_INVOKABLE bool setCustomDataDirArgs(QString path);
Q_INVOKABLE QString getCustomDataDirString();
Q_INVOKABLE void defaultReset();
QString fullClientVersion() const { return QString::fromStdString(FormatFullVersion()); }
quint64 assumedBlockchainSize() const { return m_assumed_blockchain_size; };
quint64 assumedChainstateSize() const { return m_assumed_chainstate_size; };

public Q_SLOTS:
void setCustomDataDirString(const QString &new_custom_datadir_string) {
m_custom_datadir_string = new_custom_datadir_string;
}
Q_INVOKABLE void onboard();
Q_INVOKABLE void requestShutdown();

Q_SIGNALS:
void dbcacheSizeMiBChanged(int new_dbcache_size_mib);
Expand All @@ -83,11 +95,13 @@ public Q_SLOTS:
void scriptThreadsChanged(int new_script_threads);
void serverChanged(bool new_server);
void upnpChanged(bool new_upnp);
void onboardingFinished();
void requestedShutdown();
void customDataDirStringChanged(QString new_custom_datadir_string);
void dataDirChanged(QString new_data_dir);

private:
interfaces::Node& m_node;
interfaces::Node* m_node;
bool m_onboarded;

// Properties that are exposed to QML.
Expand All @@ -105,6 +119,10 @@ public Q_SLOTS:
bool m_upnp;
QString m_custom_datadir_string;
QString m_dataDir;
common::Settings m_previous_settings;
common::Settings m_cli_settings;
quint64 m_assumed_blockchain_size{ Params().AssumedBlockchainSize() };
quint64 m_assumed_chainstate_size{ Params().AssumedChainStateSize() };

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

0 comments on commit f3b8c5a

Please sign in to comment.