Skip to content

Commit 8847cda

Browse files
committed
gui: Add OpenWalletActivity
1 parent 4c8982a commit 8847cda

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ int GuiMain(int argc, char* argv[])
456456
// IMPORTANT if it is no longer a typedef use the normal variant above
457457
qRegisterMetaType< CAmount >("CAmount");
458458
qRegisterMetaType< std::function<void()> >("std::function<void()>");
459-
459+
qRegisterMetaType<QMessageBox::Icon>("QMessageBox::Icon");
460460
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
461461
// Command-line options take precedence:
462462
node->setupServerArgs();

src/qt/bitcoingui.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ void BitcoinGUI::createActions()
371371
QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
372372
QAction* action = m_open_wallet_action->menu()->addAction(name);
373373
connect(action, &QAction::triggered, [this, path] {
374-
setCurrentWallet(m_wallet_controller->openWallet(path));
374+
OpenWalletActivity* activity = m_wallet_controller->openWallet(path);
375+
connect(activity, &OpenWalletActivity::opened, this, &BitcoinGUI::setCurrentWallet);
376+
connect(activity, &OpenWalletActivity::finished, activity, &QObject::deleteLater);
375377
});
376378
}
377379
});

src/qt/walletcontroller.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ std::vector<std::string> WalletController::getWalletsAvailableToOpen() const
5555
return wallets;
5656
}
5757

58-
WalletModel* WalletController::openWallet(const std::string& name, QWidget* parent)
58+
OpenWalletActivity* WalletController::openWallet(const std::string& name, QWidget* parent)
5959
{
60-
std::string error, warning;
61-
WalletModel* wallet_model = getOrCreateWallet(m_node.loadWallet(name, error, warning));
62-
if (!wallet_model) {
63-
QMessageBox::warning(parent, tr("Open Wallet"), QString::fromStdString(error));
64-
}
65-
if (!warning.empty()) {
66-
QMessageBox::information(parent, tr("Open Wallet"), QString::fromStdString(warning));
67-
}
68-
return wallet_model;
60+
OpenWalletActivity* activity = new OpenWalletActivity(this, name);
61+
activity->moveToThread(&m_activity_thread);
62+
QMetaObject::invokeMethod(activity, "open", Qt::QueuedConnection);
63+
return activity;
6964
}
7065

7166
WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
@@ -124,3 +119,24 @@ void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
124119
// CWallet shared pointer.
125120
delete wallet_model;
126121
}
122+
123+
124+
OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, const std::string& name)
125+
: m_wallet_controller(wallet_controller)
126+
, m_name(name)
127+
{}
128+
129+
void OpenWalletActivity::open()
130+
{
131+
std::string error, warning;
132+
std::unique_ptr<interfaces::Wallet> wallet = m_wallet_controller->m_node.loadWallet(m_name, error, warning);
133+
if (!warning.empty()) {
134+
Q_EMIT message(QMessageBox::Warning, QString::fromStdString(warning));
135+
}
136+
if (wallet) {
137+
Q_EMIT opened(m_wallet_controller->getOrCreateWallet(std::move(wallet)));
138+
} else {
139+
Q_EMIT message(QMessageBox::Critical, QString::fromStdString(error));
140+
}
141+
Q_EMIT finished();
142+
}

src/qt/walletcontroller.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <memory>
1313
#include <vector>
1414

15+
#include <QMessageBox>
1516
#include <QMutex>
1617
#include <QThread>
1718

@@ -23,6 +24,8 @@ class Handler;
2324
class Node;
2425
} // namespace interfaces
2526

27+
class OpenWalletActivity;
28+
2629
/**
2730
* Controller between interfaces::Node, WalletModel instances and the GUI.
2831
*/
@@ -40,7 +43,7 @@ class WalletController : public QObject
4043
std::vector<WalletModel*> getWallets() const;
4144
std::vector<std::string> getWalletsAvailableToOpen() const;
4245

43-
WalletModel* openWallet(const std::string& name, QWidget* parent = nullptr);
46+
OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr);
4447

4548
private Q_SLOTS:
4649
void addWallet(WalletModel* wallet_model);
@@ -59,6 +62,28 @@ private Q_SLOTS:
5962
mutable QMutex m_mutex;
6063
std::vector<WalletModel*> m_wallets;
6164
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
65+
66+
friend class OpenWalletActivity;
67+
};
68+
69+
class OpenWalletActivity : public QObject
70+
{
71+
Q_OBJECT
72+
73+
public:
74+
OpenWalletActivity(WalletController* wallet_controller, const std::string& name);
75+
76+
public Q_SLOTS:
77+
void open();
78+
79+
Q_SIGNALS:
80+
void message(QMessageBox::Icon icon, const QString text);
81+
void finished();
82+
void opened(WalletModel* wallet_model);
83+
84+
private:
85+
WalletController* const m_wallet_controller;
86+
std::string const m_name;
6287
};
6388

6489
#endif // BITCOIN_QT_WALLETCONTROLLER_H

0 commit comments

Comments
 (0)