Skip to content

Commit 0cc3492

Browse files
committed
gui: Replace interface::Node references with pointers
No change in behavior. Replacing references with pointers allows Node interface creation to be delayed until later during gui startup next commit to support implementing -ipcconnect option
1 parent 9139f5b commit 0cc3492

File tree

9 files changed

+74
-41
lines changed

9 files changed

+74
-41
lines changed

src/qt/bitcoin.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,9 @@ void BitcoinCore::shutdown()
193193
static int qt_argc = 1;
194194
static const char* qt_argv = "bitcoin-qt";
195195

196-
BitcoinApplication::BitcoinApplication(interfaces::Node& node):
196+
BitcoinApplication::BitcoinApplication():
197197
QApplication(qt_argc, const_cast<char **>(&qt_argv)),
198198
coreThread(nullptr),
199-
m_node(node),
200199
optionsModel(nullptr),
201200
clientModel(nullptr),
202201
window(nullptr),
@@ -246,38 +245,47 @@ void BitcoinApplication::createPaymentServer()
246245

247246
void BitcoinApplication::createOptionsModel(bool resetSettings)
248247
{
249-
optionsModel = new OptionsModel(m_node, this, resetSettings);
248+
optionsModel = new OptionsModel(this, resetSettings);
249+
optionsModel->setNode(node());
250250
}
251251

252252
void BitcoinApplication::createWindow(const NetworkStyle *networkStyle)
253253
{
254-
window = new BitcoinGUI(m_node, platformStyle, networkStyle, nullptr);
254+
window = new BitcoinGUI(node(), platformStyle, networkStyle, nullptr);
255255

256256
pollShutdownTimer = new QTimer(window);
257257
connect(pollShutdownTimer, &QTimer::timeout, window, &BitcoinGUI::detectShutdown);
258258
}
259259

260260
void BitcoinApplication::createSplashScreen(const NetworkStyle *networkStyle)
261261
{
262-
SplashScreen *splash = new SplashScreen(m_node, nullptr, networkStyle);
262+
assert(!m_splash);
263+
m_splash = new SplashScreen(nullptr, networkStyle);
264+
m_splash->setNode(node());
263265
// We don't hold a direct pointer to the splash screen after creation, but the splash
264266
// screen will take care of deleting itself when finish() happens.
265-
splash->show();
266-
connect(this, &BitcoinApplication::splashFinished, splash, &SplashScreen::finish);
267-
connect(this, &BitcoinApplication::requestedShutdown, splash, &QWidget::close);
267+
m_splash->show();
268+
connect(this, &BitcoinApplication::splashFinished, m_splash, &SplashScreen::finish);
269+
connect(this, &BitcoinApplication::requestedShutdown, m_splash, &QWidget::close);
270+
}
271+
272+
void BitcoinApplication::setNode(interfaces::Node& node)
273+
{
274+
assert(!m_node);
275+
m_node = &node;
268276
}
269277

270278
bool BitcoinApplication::baseInitialize()
271279
{
272-
return m_node.baseInitialize();
280+
return node().baseInitialize();
273281
}
274282

275283
void BitcoinApplication::startThread()
276284
{
277285
if(coreThread)
278286
return;
279287
coreThread = new QThread(this);
280-
BitcoinCore *executor = new BitcoinCore(m_node);
288+
BitcoinCore *executor = new BitcoinCore(node());
281289
executor->moveToThread(coreThread);
282290

283291
/* communication to and from thread */
@@ -331,7 +339,7 @@ void BitcoinApplication::requestShutdown()
331339
window->unsubscribeFromCoreSignals();
332340
// Request node shutdown, which can interrupt long operations, like
333341
// rescanning a wallet.
334-
m_node.startShutdown();
342+
node().startShutdown();
335343
// Unsetting the client model can cause the current thread to wait for node
336344
// to complete an operation, like wait for a RPC execution to complete.
337345
window->setClientModel(nullptr);
@@ -353,7 +361,7 @@ void BitcoinApplication::initializeResult(bool success)
353361
{
354362
// Log this only after AppInitMain finishes, as then logging setup is guaranteed complete
355363
qInfo() << "Platform customization:" << platformStyle->getName();
356-
clientModel = new ClientModel(m_node, optionsModel);
364+
clientModel = new ClientModel(node(), optionsModel);
357365
window->setClientModel(clientModel);
358366
#ifdef ENABLE_WALLET
359367
if (WalletModel::isWalletEnabled()) {
@@ -452,7 +460,8 @@ int GuiMain(int argc, char* argv[])
452460
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
453461
#endif
454462

455-
BitcoinApplication app(*node);
463+
BitcoinApplication app;
464+
app.setNode(*node);
456465

457466
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
458467
// Command-line options take precedence:
@@ -601,10 +610,10 @@ int GuiMain(int argc, char* argv[])
601610
}
602611
} catch (const std::exception& e) {
603612
PrintExceptionContinue(&e, "Runaway exception");
604-
app.handleRunawayException(QString::fromStdString(node->getWarnings().translated));
613+
app.handleRunawayException(QString::fromStdString(app.node().getWarnings().translated));
605614
} catch (...) {
606615
PrintExceptionContinue(nullptr, "Runaway exception");
607-
app.handleRunawayException(QString::fromStdString(node->getWarnings().translated));
616+
app.handleRunawayException(QString::fromStdString(app.node().getWarnings().translated));
608617
}
609618
return rv;
610619
}

src/qt/bitcoin.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111

1212
#include <QApplication>
13+
#include <assert.h>
1314
#include <memory>
1415

1516
class BitcoinGUI;
@@ -18,6 +19,7 @@ class NetworkStyle;
1819
class OptionsModel;
1920
class PaymentServer;
2021
class PlatformStyle;
22+
class SplashScreen;
2123
class WalletController;
2224
class WalletModel;
2325

@@ -56,7 +58,7 @@ class BitcoinApplication: public QApplication
5658
{
5759
Q_OBJECT
5860
public:
59-
explicit BitcoinApplication(interfaces::Node& node);
61+
explicit BitcoinApplication();
6062
~BitcoinApplication();
6163

6264
#ifdef ENABLE_WALLET
@@ -90,6 +92,9 @@ class BitcoinApplication: public QApplication
9092
/// Setup platform style
9193
void setupPlatformStyle();
9294

95+
interfaces::Node& node() const { assert(m_node); return *m_node; }
96+
void setNode(interfaces::Node& node);
97+
9398
public Q_SLOTS:
9499
void initializeResult(bool success);
95100
void shutdownResult();
@@ -104,7 +109,6 @@ public Q_SLOTS:
104109

105110
private:
106111
QThread *coreThread;
107-
interfaces::Node& m_node;
108112
OptionsModel *optionsModel;
109113
ClientModel *clientModel;
110114
BitcoinGUI *window;
@@ -116,6 +120,8 @@ public Q_SLOTS:
116120
int returnValue;
117121
const PlatformStyle *platformStyle;
118122
std::unique_ptr<QWidget> shutdownWindow;
123+
SplashScreen* m_splash = nullptr;
124+
interfaces::Node* m_node = nullptr;
119125

120126
void startThread();
121127
};

src/qt/optionsmodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1";
2727

2828
static const QString GetDefaultProxyAddress();
2929

30-
OptionsModel::OptionsModel(interfaces::Node& node, QObject *parent, bool resetSettings) :
31-
QAbstractListModel(parent), m_node(node)
30+
OptionsModel::OptionsModel(QObject *parent, bool resetSettings) :
31+
QAbstractListModel(parent)
3232
{
3333
Init(resetSettings);
3434
}
@@ -353,7 +353,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
353353
break;
354354
case MapPortUPnP: // core option - can be changed on-the-fly
355355
settings.setValue("fUseUPnP", value.toBool());
356-
m_node.mapPort(value.toBool());
356+
node().mapPort(value.toBool());
357357
break;
358358
case MinimizeOnClose:
359359
fMinimizeOnClose = value.toBool();

src/qt/optionsmodel.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <QAbstractListModel>
1313

14+
#include <assert.h>
15+
1416
namespace interfaces {
1517
class Node;
1618
}
@@ -39,7 +41,7 @@ class OptionsModel : public QAbstractListModel
3941
Q_OBJECT
4042

4143
public:
42-
explicit OptionsModel(interfaces::Node& node, QObject *parent = nullptr, bool resetSettings = false);
44+
explicit OptionsModel(QObject *parent = nullptr, bool resetSettings = false);
4345

4446
enum OptionID {
4547
StartAtStartup, // bool
@@ -92,10 +94,11 @@ class OptionsModel : public QAbstractListModel
9294
void setRestartRequired(bool fRequired);
9395
bool isRestartRequired() const;
9496

95-
interfaces::Node& node() const { return m_node; }
97+
interfaces::Node& node() const { assert(m_node); return *m_node; }
98+
void setNode(interfaces::Node& node) { assert(!m_node); m_node = &node; }
9699

97100
private:
98-
interfaces::Node& m_node;
101+
interfaces::Node* m_node = nullptr;
99102
/* Qt-only settings */
100103
bool fHideTrayIcon;
101104
bool fMinimizeToTray;

src/qt/splashscreen.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include <QScreen>
2525

2626

27-
SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) :
28-
QWidget(nullptr, f), curAlignment(0), m_node(node)
27+
SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
28+
QWidget(nullptr, f), curAlignment(0)
2929
{
3030
// set reference point, paddings
3131
int paddingRight = 50;
@@ -124,22 +124,33 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw
124124
setFixedSize(r.size());
125125
move(QGuiApplication::primaryScreen()->geometry().center() - r.center());
126126

127-
subscribeToCoreSignals();
128127
installEventFilter(this);
129128

130129
GUIUtil::handleCloseWindowShortcut(this);
131130
}
132131

133132
SplashScreen::~SplashScreen()
134133
{
135-
unsubscribeFromCoreSignals();
134+
if (m_node) unsubscribeFromCoreSignals();
135+
}
136+
137+
void SplashScreen::setNode(interfaces::Node& node)
138+
{
139+
assert(!m_node);
140+
m_node = &node;
141+
subscribeToCoreSignals();
142+
}
143+
144+
void SplashScreen::shutdown()
145+
{
146+
if (m_node) m_node->startShutdown();
136147
}
137148

138149
bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
139150
if (ev->type() == QEvent::KeyPress) {
140151
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
141152
if (keyEvent->key() == Qt::Key_Q) {
142-
m_node.startShutdown();
153+
shutdown();
143154
}
144155
}
145156
return QObject::eventFilter(obj, ev);
@@ -183,10 +194,10 @@ void SplashScreen::ConnectWallet(std::unique_ptr<interfaces::Wallet> wallet)
183194
void SplashScreen::subscribeToCoreSignals()
184195
{
185196
// Connect signals to client
186-
m_handler_init_message = m_node.handleInitMessage(std::bind(InitMessage, this, std::placeholders::_1));
187-
m_handler_show_progress = m_node.handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
197+
m_handler_init_message = m_node->handleInitMessage(std::bind(InitMessage, this, std::placeholders::_1));
198+
m_handler_show_progress = m_node->handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
188199
#ifdef ENABLE_WALLET
189-
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) { ConnectWallet(std::move(wallet)); });
200+
m_handler_load_wallet = m_node->handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) { ConnectWallet(std::move(wallet)); });
190201
#endif
191202
}
192203

@@ -221,6 +232,6 @@ void SplashScreen::paintEvent(QPaintEvent *event)
221232

222233
void SplashScreen::closeEvent(QCloseEvent *event)
223234
{
224-
m_node.startShutdown(); // allows an "emergency" shutdown during startup
235+
shutdown(); // allows an "emergency" shutdown during startup
225236
event->ignore();
226237
}

src/qt/splashscreen.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ class SplashScreen : public QWidget
2828
Q_OBJECT
2929

3030
public:
31-
explicit SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle);
31+
explicit SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle);
3232
~SplashScreen();
33+
void setNode(interfaces::Node& node);
3334

3435
protected:
3536
void paintEvent(QPaintEvent *event) override;
@@ -50,6 +51,8 @@ public Q_SLOTS:
5051
void subscribeToCoreSignals();
5152
/** Disconnect core signals to splash screen */
5253
void unsubscribeFromCoreSignals();
54+
/** Initiate shutdown */
55+
void shutdown();
5356
/** Connect wallet signals to splash screen */
5457
void ConnectWallet(std::unique_ptr<interfaces::Wallet> wallet);
5558

@@ -58,7 +61,7 @@ public Q_SLOTS:
5861
QColor curColor;
5962
int curAlignment;
6063

61-
interfaces::Node& m_node;
64+
interfaces::Node* m_node = nullptr;
6265
std::unique_ptr<interfaces::Handler> m_handler_init_message;
6366
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
6467
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
106106

107107
// Initialize relevant QT models.
108108
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
109-
OptionsModel optionsModel(node);
109+
OptionsModel optionsModel;
110110
ClientModel clientModel(node, &optionsModel);
111111
AddWallet(wallet);
112112
WalletModel walletModel(interfaces::MakeWallet(wallet), clientModel, platformStyle.get());

src/qt/test/test_main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ int main(int argc, char* argv[])
6767

6868
// Don't remove this, it's needed to access
6969
// QApplication:: and QCoreApplication:: in the tests
70-
BitcoinApplication app(*node);
70+
BitcoinApplication app;
71+
app.setNode(*node);
7172
app.setApplicationName("Bitcoin-Qt-test");
7273

73-
node->context()->args = &gArgs; // Make gArgs available in the NodeContext
74+
app.node().context()->args = &gArgs; // Make gArgs available in the NodeContext
7475
AppTests app_tests(app);
7576
if (QTest::qExec(&app_tests) != 0) {
7677
fInvalid = true;
@@ -79,7 +80,7 @@ int main(int argc, char* argv[])
7980
if (QTest::qExec(&test1) != 0) {
8081
fInvalid = true;
8182
}
82-
RPCNestedTests test3(*node);
83+
RPCNestedTests test3(app.node());
8384
if (QTest::qExec(&test3) != 0) {
8485
fInvalid = true;
8586
}
@@ -88,11 +89,11 @@ int main(int argc, char* argv[])
8889
fInvalid = true;
8990
}
9091
#ifdef ENABLE_WALLET
91-
WalletTests test5(*node);
92+
WalletTests test5(app.node());
9293
if (QTest::qExec(&test5) != 0) {
9394
fInvalid = true;
9495
}
95-
AddressBookTests test6(*node);
96+
AddressBookTests test6(app.node());
9697
if (QTest::qExec(&test6) != 0) {
9798
fInvalid = true;
9899
}

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void TestGUI(interfaces::Node& node)
164164
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
165165
SendCoinsDialog sendCoinsDialog(platformStyle.get());
166166
TransactionView transactionView(platformStyle.get());
167-
OptionsModel optionsModel(node);
167+
OptionsModel optionsModel;
168168
ClientModel clientModel(node, &optionsModel);
169169
AddWallet(wallet);
170170
WalletModel walletModel(interfaces::MakeWallet(wallet), clientModel, platformStyle.get());

0 commit comments

Comments
 (0)