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>
1314#include < QMessageBox>
1415#include < QMutexLocker>
1516#include < QThread>
17+ #include < QTimer>
1618#include < QWindow>
1719
1820WalletController::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.
3742WalletController::~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
4349std::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-
7069void 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+ }
0 commit comments