Skip to content

Commit bc6d8a3

Browse files
committed
gui: Refactor OpenWalletActivity
1 parent 5e20238 commit bc6d8a3

File tree

3 files changed

+101
-59
lines changed

3 files changed

+101
-59
lines changed

src/qt/bitcoingui.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -379,31 +379,11 @@ void BitcoinGUI::createActions()
379379
continue;
380380
}
381381

382-
connect(action, &QAction::triggered, [this, name, path] {
383-
OpenWalletActivity* activity = m_wallet_controller->openWallet(path);
384-
385-
QProgressDialog* dialog = new QProgressDialog(this);
386-
dialog->setLabelText(tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
387-
dialog->setRange(0, 0);
388-
dialog->setCancelButton(nullptr);
389-
dialog->setWindowModality(Qt::ApplicationModal);
390-
dialog->show();
391-
392-
connect(activity, &OpenWalletActivity::message, this, [this] (QMessageBox::Icon icon, QString text) {
393-
QMessageBox box;
394-
box.setIcon(icon);
395-
box.setText(tr("Open Wallet Failed"));
396-
box.setInformativeText(text);
397-
box.setStandardButtons(QMessageBox::Ok);
398-
box.setDefaultButton(QMessageBox::Ok);
399-
connect(this, &QObject::destroyed, &box, &QDialog::accept);
400-
box.exec();
401-
});
382+
connect(action, &QAction::triggered, [this, path] {
383+
auto activity = new OpenWalletActivity(m_wallet_controller, this);
402384
connect(activity, &OpenWalletActivity::opened, this, &BitcoinGUI::setCurrentWallet);
403385
connect(activity, &OpenWalletActivity::finished, activity, &QObject::deleteLater);
404-
connect(activity, &OpenWalletActivity::finished, dialog, &QObject::deleteLater);
405-
bool invoked = QMetaObject::invokeMethod(activity, "open");
406-
assert(invoked);
386+
activity->open(path);
407387
});
408388
}
409389
if (m_open_wallet_menu->isEmpty()) {

src/qt/walletcontroller.cpp

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <qt/guiutil.h>
56
#include <qt/walletcontroller.h>
67

78
#include <interfaces/handler.h>
@@ -13,10 +14,13 @@
1314
#include <QMessageBox>
1415
#include <QMutexLocker>
1516
#include <QThread>
17+
#include <QTimer>
1618
#include <QWindow>
1719

1820
WalletController::WalletController(interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent)
1921
: QObject(parent)
22+
, m_activity_thread(new QThread(this))
23+
, m_activity_worker(new QObject)
2024
, m_node(node)
2125
, m_platform_style(platform_style)
2226
, m_options_model(options_model)
@@ -29,15 +33,17 @@ WalletController::WalletController(interfaces::Node& node, const PlatformStyle*
2933
getOrCreateWallet(std::move(wallet));
3034
}
3135

32-
m_activity_thread.start();
36+
m_activity_worker->moveToThread(m_activity_thread);
37+
m_activity_thread->start();
3338
}
3439

3540
// Not using the default destructor because not all member types definitions are
3641
// available in the header, just forward declared.
3742
WalletController::~WalletController()
3843
{
39-
m_activity_thread.quit();
40-
m_activity_thread.wait();
44+
m_activity_thread->quit();
45+
m_activity_thread->wait();
46+
delete m_activity_worker;
4147
}
4248

4349
std::vector<WalletModel*> WalletController::getOpenWallets() const
@@ -60,13 +66,6 @@ std::map<std::string, bool> WalletController::listWalletDir() const
6066
return wallets;
6167
}
6268

63-
OpenWalletActivity* WalletController::openWallet(const std::string& name, QWidget* parent)
64-
{
65-
OpenWalletActivity* activity = new OpenWalletActivity(this, name);
66-
activity->moveToThread(&m_activity_thread);
67-
return activity;
68-
}
69-
7069
void WalletController::closeWallet(WalletModel* wallet_model, QWidget* parent)
7170
{
7271
QMessageBox box(parent);
@@ -140,23 +139,60 @@ void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
140139
delete wallet_model;
141140
}
142141

142+
WalletControllerActivity::WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget)
143+
: QObject(wallet_controller)
144+
, m_wallet_controller(wallet_controller)
145+
, m_parent_widget(parent_widget)
146+
{
147+
}
143148

144-
OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, const std::string& name)
145-
: m_wallet_controller(wallet_controller)
146-
, m_name(name)
147-
{}
149+
WalletControllerActivity::~WalletControllerActivity()
150+
{
151+
delete m_progress_dialog;
152+
}
148153

149-
void OpenWalletActivity::open()
154+
void WalletControllerActivity::showProgressDialog(const QString& label_text)
150155
{
151-
std::string error, warning;
152-
std::unique_ptr<interfaces::Wallet> wallet = m_wallet_controller->m_node.loadWallet(m_name, error, warning);
153-
if (!warning.empty()) {
154-
Q_EMIT message(QMessageBox::Warning, QString::fromStdString(warning));
155-
}
156-
if (wallet) {
157-
Q_EMIT opened(m_wallet_controller->getOrCreateWallet(std::move(wallet)));
158-
} else {
159-
Q_EMIT message(QMessageBox::Critical, QString::fromStdString(error));
156+
m_progress_dialog = new QProgressDialog(m_parent_widget);
157+
158+
m_progress_dialog->setLabelText(label_text);
159+
m_progress_dialog->setRange(0, 0);
160+
m_progress_dialog->setCancelButton(nullptr);
161+
m_progress_dialog->setWindowModality(Qt::ApplicationModal);
162+
GUIUtil::PolishProgressDialog(m_progress_dialog);
163+
}
164+
165+
OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
166+
: WalletControllerActivity(wallet_controller, parent_widget)
167+
{
168+
}
169+
170+
void OpenWalletActivity::finish()
171+
{
172+
m_progress_dialog->hide();
173+
174+
if (!m_error_message.empty()) {
175+
QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message));
176+
} else if (!m_warning_message.empty()) {
177+
QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(m_warning_message));
160178
}
179+
180+
if (m_wallet_model) Q_EMIT opened(m_wallet_model);
181+
161182
Q_EMIT finished();
162183
}
184+
185+
void OpenWalletActivity::open(const std::string& path)
186+
{
187+
QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
188+
189+
showProgressDialog(tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
190+
191+
QTimer::singleShot(0, worker(), [this, path] {
192+
std::unique_ptr<interfaces::Wallet> wallet = node().loadWallet(path, m_error_message, m_warning_message);
193+
194+
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
195+
196+
QTimer::singleShot(0, this, &OpenWalletActivity::finish);
197+
});
198+
}

src/qt/walletcontroller.h

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
#include <map>
1212
#include <memory>
13+
#include <string>
1314
#include <vector>
1415

1516
#include <QMessageBox>
1617
#include <QMutex>
18+
#include <QProgressDialog>
19+
#include <QString>
1720
#include <QThread>
1821

1922
class OptionsModel;
@@ -25,6 +28,7 @@ class Node;
2528
} // namespace interfaces
2629

2730
class OpenWalletActivity;
31+
class WalletControllerActivity;
2832

2933
/**
3034
* Controller between interfaces::Node, WalletModel instances and the GUI.
@@ -33,7 +37,6 @@ class WalletController : public QObject
3337
{
3438
Q_OBJECT
3539

36-
WalletModel* getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet);
3740
void removeAndDeleteWallet(WalletModel* wallet_model);
3841

3942
public:
@@ -43,11 +46,12 @@ class WalletController : public QObject
4346
//! Returns wallet models currently open.
4447
std::vector<WalletModel*> getOpenWallets() const;
4548

49+
WalletModel* getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet);
50+
4651
//! Returns all wallet names in the wallet dir mapped to whether the wallet
4752
//! is loaded.
4853
std::map<std::string, bool> listWalletDir() const;
4954

50-
OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr);
5155
void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);
5256

5357
Q_SIGNALS:
@@ -57,35 +61,57 @@ class WalletController : public QObject
5761
void coinsSent(WalletModel* wallet_model, SendCoinsRecipient recipient, QByteArray transaction);
5862

5963
private:
60-
QThread m_activity_thread;
64+
QThread* const m_activity_thread;
65+
QObject* const m_activity_worker;
6166
interfaces::Node& m_node;
6267
const PlatformStyle* const m_platform_style;
6368
OptionsModel* const m_options_model;
6469
mutable QMutex m_mutex;
6570
std::vector<WalletModel*> m_wallets;
6671
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
6772

68-
friend class OpenWalletActivity;
73+
friend class WalletControllerActivity;
6974
};
7075

71-
class OpenWalletActivity : public QObject
76+
class WalletControllerActivity : public QObject
7277
{
7378
Q_OBJECT
7479

7580
public:
76-
OpenWalletActivity(WalletController* wallet_controller, const std::string& name);
77-
78-
public Q_SLOTS:
79-
void open();
81+
WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget);
82+
virtual ~WalletControllerActivity();
8083

8184
Q_SIGNALS:
82-
void message(QMessageBox::Icon icon, const QString text);
8385
void finished();
86+
87+
protected:
88+
interfaces::Node& node() const { return m_wallet_controller->m_node; }
89+
QObject* worker() const { return m_wallet_controller->m_activity_worker; }
90+
91+
void showProgressDialog(const QString& label_text);
92+
93+
WalletController* const m_wallet_controller;
94+
QWidget* const m_parent_widget;
95+
QProgressDialog* m_progress_dialog{nullptr};
96+
WalletModel* m_wallet_model{nullptr};
97+
std::string m_error_message;
98+
std::string m_warning_message;
99+
};
100+
101+
class OpenWalletActivity : public WalletControllerActivity
102+
{
103+
Q_OBJECT
104+
105+
public:
106+
OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget);
107+
108+
void open(const std::string& path);
109+
110+
Q_SIGNALS:
84111
void opened(WalletModel* wallet_model);
85112

86113
private:
87-
WalletController* const m_wallet_controller;
88-
std::string const m_name;
114+
void finish();
89115
};
90116

91117
#endif // BITCOIN_QT_WALLETCONTROLLER_H

0 commit comments

Comments
 (0)