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>
89#include < interfaces/node.h>
910
1011#include < QApplication>
1112#include < QMutexLocker>
13+ #include < QTimer>
1214#include < QWindow>
1315
1416#include < algorithm>
1517
1618WalletController::WalletController (interfaces::Node &node,
1719 const PlatformStyle *platform_style,
1820 OptionsModel *options_model, QObject *parent)
19- : QObject(parent), m_node(node), m_platform_style(platform_style),
20- m_options_model(options_model) {
21+ : QObject(parent), m_activity_thread(new QThread(this )),
22+ m_activity_worker(new QObject), m_node(node),
23+ m_platform_style(platform_style), m_options_model(options_model) {
2124 m_handler_load_wallet = m_node.handleLoadWallet (
2225 [this ](std::unique_ptr<interfaces::Wallet> wallet) {
2326 getOrCreateWallet (std::move (wallet));
@@ -27,14 +30,16 @@ WalletController::WalletController(interfaces::Node &node,
2730 getOrCreateWallet (std::move (wallet));
2831 }
2932
30- m_activity_thread.start ();
33+ m_activity_worker->moveToThread (m_activity_thread);
34+ m_activity_thread->start ();
3135}
3236
3337// Not using the default destructor because not all member types definitions are
3438// available in the header, just forward declared.
3539WalletController::~WalletController () {
36- m_activity_thread.quit ();
37- m_activity_thread.wait ();
40+ m_activity_thread->quit ();
41+ m_activity_thread->wait ();
42+ delete m_activity_worker;
3843}
3944
4045std::vector<WalletModel *> WalletController::getOpenWallets () const {
@@ -57,13 +62,6 @@ std::map<std::string, bool> WalletController::listWalletDir() const {
5762 return wallets;
5863}
5964
60- OpenWalletActivity *WalletController::openWallet (const CChainParams ¶ms,
61- const std::string &name,
62- QWidget *parent) {
63- OpenWalletActivity *activity = new OpenWalletActivity (this , name, params);
64- activity->moveToThread (&m_activity_thread);
65- return activity;
66- }
6765void WalletController::closeWallet (WalletModel *wallet_model, QWidget *parent) {
6866 QMessageBox box (parent);
6967 box.setWindowTitle (tr (" Close wallet" ));
@@ -150,25 +148,65 @@ void WalletController::removeAndDeleteWallet(WalletModel *wallet_model) {
150148 delete wallet_model;
151149}
152150
151+ WalletControllerActivity::WalletControllerActivity (
152+ WalletController *wallet_controller, QWidget *parent_widget,
153+ const CChainParams &chainparams)
154+ : QObject(wallet_controller), m_wallet_controller(wallet_controller),
155+ m_parent_widget(parent_widget), m_chainparams(chainparams) {}
156+
157+ WalletControllerActivity::~WalletControllerActivity () {
158+ delete m_progress_dialog;
159+ }
160+
161+ void WalletControllerActivity::showProgressDialog (const QString &label_text) {
162+ m_progress_dialog = new QProgressDialog (m_parent_widget);
163+
164+ m_progress_dialog->setLabelText (label_text);
165+ m_progress_dialog->setRange (0 , 0 );
166+ m_progress_dialog->setCancelButton (nullptr );
167+ m_progress_dialog->setWindowModality (Qt::ApplicationModal);
168+ GUIUtil::PolishProgressDialog (m_progress_dialog);
169+ }
170+
153171OpenWalletActivity::OpenWalletActivity (WalletController *wallet_controller,
154- const std::string &name,
155- const CChainParams ¶ms)
156- : m_wallet_controller(wallet_controller), m_name(name),
157- m_chain_params(params) {}
158-
159- void OpenWalletActivity::open () {
160- std::string error, warning;
161- std::unique_ptr<interfaces::Wallet> wallet =
162- m_wallet_controller->m_node .loadWallet (m_chain_params, m_name, error,
163- warning);
164- if (!warning.empty ()) {
165- Q_EMIT message (QMessageBox::Warning, QString::fromStdString (warning));
172+ QWidget *parent_widget,
173+ const CChainParams &chainparams)
174+ : WalletControllerActivity(wallet_controller, parent_widget, chainparams) {}
175+
176+ void OpenWalletActivity::finish () {
177+ m_progress_dialog->hide ();
178+
179+ if (!m_error_message.empty ()) {
180+ QMessageBox::critical (m_parent_widget, tr (" Open wallet failed" ),
181+ QString::fromStdString (m_error_message));
182+ } else if (!m_warning_message.empty ()) {
183+ QMessageBox::warning (m_parent_widget, tr (" Open wallet warning" ),
184+ QString::fromStdString (m_warning_message));
166185 }
167- if (wallet) {
168- Q_EMIT opened (
169- m_wallet_controller->getOrCreateWallet (std::move (wallet)));
170- } else {
171- Q_EMIT message (QMessageBox::Critical, QString::fromStdString (error));
186+
187+ if (m_wallet_model) {
188+ Q_EMIT opened (m_wallet_model);
172189 }
190+
173191 Q_EMIT finished ();
174192}
193+
194+ void OpenWalletActivity::open (const std::string &path) {
195+ QString name = path.empty () ? QString (" [" + tr (" default wallet" ) + " ]" )
196+ : QString::fromStdString (path);
197+
198+ showProgressDialog (
199+ tr (" Opening Wallet <b>%1</b>..." ).arg (name.toHtmlEscaped ()));
200+
201+ QTimer::singleShot (0 , worker (), [this , path] {
202+ std::unique_ptr<interfaces::Wallet> wallet = node ().loadWallet (
203+ this ->m_chainparams , path, m_error_message, m_warning_message);
204+
205+ if (wallet) {
206+ m_wallet_model =
207+ m_wallet_controller->getOrCreateWallet (std::move (wallet));
208+ }
209+
210+ QTimer::singleShot (0 , this , &OpenWalletActivity::finish);
211+ });
212+ }
0 commit comments