Skip to content

Commit 87c29ad

Browse files
committed
qml: added onboardingmodel files to manage the onboarding process especially custom datadir setting and placeholders for optionsModel
1 parent bd57ae1 commit 87c29ad

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed

src/qml/models/onboardingmodel.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Copyright (c) 2024 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <qml/models/onboardingmodel.h>
6+
7+
#include <common/args.h>
8+
#include <common/settings.h>
9+
#include <common/settings.h>
10+
#include <common/system.h>
11+
#include <qt/guiconstants.h>
12+
#include <qt/guiutil.h>
13+
#include <util/fs.h>
14+
#include <util/fs_helpers.h>
15+
16+
#include <QDebug>
17+
#include <QDir>
18+
#include <QSettings>
19+
20+
OnboardingModel::OnboardingModel()
21+
{
22+
// This is to reset the data directory to the default
23+
gArgs.LockSettings([&](common::Settings& s) { m_previous_settings = s; });
24+
25+
// Initiate prune settings
26+
m_prune = false;
27+
}
28+
29+
void OnboardingModel::setDbcacheSizeMiB(int new_dbcache_size_mib)
30+
{
31+
if (new_dbcache_size_mib != m_dbcache_size_mib) {
32+
m_dbcache_size_mib = new_dbcache_size_mib;
33+
// // qDebug () << "Dbcache Size Set: " << new_dbcache_size_mib;
34+
Q_EMIT dbcacheSizeMiBChanged(new_dbcache_size_mib);
35+
36+
}
37+
}
38+
39+
void OnboardingModel::setListen(bool new_listen)
40+
{
41+
if (new_listen != m_listen) {
42+
m_listen = new_listen;
43+
// qDebug () << "Listen Set: " << new_listen;
44+
Q_EMIT listenChanged(new_listen);
45+
}
46+
}
47+
48+
void OnboardingModel::setNatpmp(bool new_natpmp)
49+
{
50+
if (new_natpmp != m_natpmp) {
51+
m_natpmp = new_natpmp;
52+
// qDebug () << "Natpmp Set: " << new_natpmp;
53+
Q_EMIT natpmpChanged(new_natpmp);
54+
}
55+
}
56+
57+
void OnboardingModel::setPrune(bool new_prune)
58+
{
59+
if (new_prune != m_prune) {
60+
m_prune = new_prune;
61+
// qDebug () << "Prune Set: " << new_prune;
62+
Q_EMIT pruneChanged(new_prune);
63+
}
64+
}
65+
66+
void OnboardingModel::setPruneSizeGB(int new_prune_size_gb)
67+
{
68+
if (new_prune_size_gb != m_prune_size_gb) {
69+
m_prune_size_gb = new_prune_size_gb;
70+
// qDebug () << "Prune Size Set: " << new_prune_size_gb;
71+
Q_EMIT pruneSizeGBChanged(new_prune_size_gb);
72+
}
73+
}
74+
75+
void OnboardingModel::setScriptThreads(int new_script_threads)
76+
{
77+
if (new_script_threads != m_script_threads) {
78+
m_script_threads = new_script_threads;
79+
// qDebug () << "Script Threads Set: " << new_script_threads;
80+
Q_EMIT scriptThreadsChanged(new_script_threads);
81+
}
82+
}
83+
84+
void OnboardingModel::setServer(bool new_server)
85+
{
86+
if (new_server != m_server) {
87+
m_server = new_server;
88+
// qDebug () << "Server Set: " << new_server;
89+
Q_EMIT serverChanged(new_server);
90+
}
91+
}
92+
93+
void OnboardingModel::setUpnp(bool new_upnp)
94+
{
95+
if (new_upnp != m_upnp) {
96+
m_upnp = new_upnp;
97+
// qDebug () << "Upnp Set: " << new_upnp;
98+
Q_EMIT upnpChanged(new_upnp);
99+
}
100+
}
101+
102+
void OnboardingModel::requestShutdown()
103+
{
104+
Q_EMIT requestedShutdown();
105+
}
106+
107+
QString PathToQString(const fs::path &path)
108+
{
109+
return QString::fromStdString(path.u8string());
110+
}
111+
112+
QString OnboardingModel::getDefaultDataDirString()
113+
{
114+
return PathToQString(GetDefaultDataDir());
115+
}
116+
117+
118+
QUrl OnboardingModel::getDefaultDataDirectory()
119+
{
120+
QString path = getDefaultDataDirString();
121+
return QUrl::fromLocalFile(path);
122+
}
123+
124+
void OnboardingModel::defaultReset()
125+
{
126+
QSettings settings;
127+
// Save the strDataDir setting
128+
QString dataDir = GUIUtil::getDefaultDataDirectory();
129+
130+
// Remove all entries from our QSettings object
131+
settings.clear();
132+
133+
// Set strDataDir
134+
settings.setValue("strDataDir", dataDir);
135+
136+
// Set that this was reset
137+
settings.setValue("fReset", true);
138+
139+
// qDebug() << "Resetting data directory: " << dataDir;
140+
141+
gArgs.LockSettings([&](common::Settings& s) { s = m_previous_settings; });
142+
gArgs.ClearPathCache();
143+
// for debug purposes checking the cached datadir
144+
// const fs::path& cached_dataDir = gArgs.GetDataDirNet();
145+
// qDebug() << "Cached Data directory: " << QString::fromStdString(cached_dataDir);
146+
}
147+
148+
void OnboardingModel::setCustomDataDirArgs(QString path)
149+
{
150+
if (!path.isEmpty()) {
151+
QSettings settings;
152+
path = QUrl(path).toLocalFile();
153+
try{
154+
if (TryCreateDirectories(GUIUtil::QStringToPath(path))) {
155+
// qDebug() << "Created data directory: " << path;
156+
TryCreateDirectories(GUIUtil::QStringToPath(path) / "wallets");
157+
}
158+
} catch (const std::exception& e) {
159+
qDebug() << "Error creating data directory: " << e.what();
160+
}
161+
settings.setValue("strDataDir", path);
162+
if(path != GUIUtil::getDefaultDataDirectory()) {
163+
gArgs.SoftSetArg("-datadir", fs::PathToString(GUIUtil::QStringToPath(path)));
164+
}
165+
gArgs.ClearPathCache();
166+
Q_EMIT customDataDirStringChanged(m_custom_datadir_string);
167+
}
168+
}

src/qml/models/onboardingmodel.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) 2024 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
6+
#ifndef BITCOIN_QML_MODELS_ONBOARDINGMODEL_H
7+
#define BITCOIN_QML_MODELS_ONBOARDINGMODEL_H
8+
9+
10+
#include <common/settings.h>
11+
#include <common/system.h>
12+
#include <univalue.h>
13+
#include <validation.h>
14+
15+
#include <string>
16+
17+
#include <QObject>
18+
#include <QString>
19+
#include <QUrl>
20+
21+
class OnboardingModel : public QObject
22+
{
23+
Q_OBJECT
24+
Q_PROPERTY(QString getDefaultDataDirString READ getDefaultDataDirString CONSTANT)
25+
Q_PROPERTY(QUrl getDefaultDataDirectory READ getDefaultDataDirectory CONSTANT)
26+
Q_PROPERTY(bool prune READ prune WRITE setPrune NOTIFY pruneChanged)
27+
Q_PROPERTY(int pruneSizeGB READ pruneSizeGB WRITE setPruneSizeGB NOTIFY pruneSizeGBChanged)
28+
Q_PROPERTY(int dbcacheSizeMiB READ dbcacheSizeMiB WRITE setDbcacheSizeMiB NOTIFY dbcacheSizeMiBChanged)
29+
Q_PROPERTY(bool listen READ listen WRITE setListen NOTIFY listenChanged)
30+
Q_PROPERTY(int maxDbcacheSizeMiB READ maxDbcacheSizeMiB CONSTANT)
31+
Q_PROPERTY(int minDbcacheSizeMiB READ minDbcacheSizeMiB CONSTANT)
32+
Q_PROPERTY(int maxScriptThreads READ maxScriptThreads CONSTANT)
33+
Q_PROPERTY(int minScriptThreads READ minScriptThreads CONSTANT)
34+
Q_PROPERTY(bool natpmp READ natpmp WRITE setNatpmp NOTIFY natpmpChanged)
35+
Q_PROPERTY(int scriptThreads READ scriptThreads WRITE setScriptThreads NOTIFY scriptThreadsChanged)
36+
Q_PROPERTY(bool server READ server WRITE setServer NOTIFY serverChanged)
37+
Q_PROPERTY(bool upnp READ upnp WRITE setUpnp NOTIFY upnpChanged)
38+
39+
40+
public:
41+
explicit OnboardingModel();
42+
43+
int dbcacheSizeMiB() const { return m_dbcache_size_mib; }
44+
void setDbcacheSizeMiB(int new_dbcache_size_mib);
45+
bool listen() const { return m_listen; }
46+
void setListen(bool new_listen);
47+
int maxDbcacheSizeMiB() const { return m_max_dbcache_size_mib; }
48+
int minDbcacheSizeMiB() const { return m_min_dbcache_size_mib; }
49+
int maxScriptThreads() const { return m_max_script_threads; }
50+
int minScriptThreads() const { return m_min_script_threads; }
51+
bool natpmp() const { return m_natpmp; }
52+
void setNatpmp(bool new_natpmp);
53+
bool prune() const { return m_prune; }
54+
void setPrune(bool new_prune);
55+
int pruneSizeGB() const { return m_prune_size_gb; }
56+
void setPruneSizeGB(int new_prune_size);
57+
int scriptThreads() const { return m_script_threads; }
58+
void setScriptThreads(int new_script_threads);
59+
bool server() const { return m_server; }
60+
void setServer(bool new_server);
61+
bool upnp() const { return m_upnp; }
62+
void setUpnp(bool new_upnp);
63+
QString getDefaultDataDirString();
64+
QUrl getDefaultDataDirectory();
65+
Q_INVOKABLE void defaultReset();
66+
Q_INVOKABLE void setCustomDataDirArgs(QString path);
67+
Q_INVOKABLE void requestShutdown();
68+
69+
public Q_SLOTS:
70+
void setCustomDataDirString(const QString &new_custom_datadir_string) {
71+
m_custom_datadir_string = new_custom_datadir_string;
72+
m_signalReceived = true;
73+
}
74+
75+
Q_SIGNALS:
76+
void customDataDirStringChanged(QString new_custom_datadir_string);
77+
void onboardingFinished();
78+
void requestedShutdown();
79+
void dbcacheSizeMiBChanged(int new_dbcache_size_mib);
80+
void listenChanged(bool new_listen);
81+
void natpmpChanged(bool new_natpmp);
82+
void pruneChanged(bool new_prune);
83+
void pruneSizeGBChanged(int new_prune_size_gb);
84+
void scriptThreadsChanged(int new_script_threads);
85+
void serverChanged(bool new_server);
86+
void upnpChanged(bool new_upnp);
87+
88+
private:
89+
QString m_custom_datadir_string;
90+
bool m_signalReceived = false;
91+
common::Settings m_previous_settings;
92+
int m_dbcache_size_mib;
93+
const int m_min_dbcache_size_mib{nMinDbCache};
94+
const int m_max_dbcache_size_mib{nMaxDbCache};
95+
bool m_listen;
96+
const int m_max_script_threads{MAX_SCRIPTCHECK_THREADS};
97+
const int m_min_script_threads{-GetNumCores()};
98+
bool m_natpmp;
99+
bool m_prune;
100+
int m_prune_size_gb;
101+
int m_script_threads;
102+
bool m_server;
103+
bool m_upnp;
104+
105+
common::SettingsValue pruneSetting() const;
106+
};
107+
108+
#endif // BITCOIN_QML_MODELS_ONBOARDINGMODEL_H

0 commit comments

Comments
 (0)