2
2
// Distributed under the MIT software license, see the accompanying
3
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
4
5
+ #include < qt/guiutil.h>
5
6
#include < qt/walletcontroller.h>
6
7
7
8
#include < interfaces/handler.h>
13
14
#include < QMessageBox>
14
15
#include < QMutexLocker>
15
16
#include < QThread>
17
+ #include < QTimer>
16
18
#include < QWindow>
17
19
18
20
WalletController::WalletController (interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent)
19
21
: QObject(parent)
22
+ , m_activity_thread(new QThread(this ))
23
+ , m_activity_worker(new QObject)
20
24
, m_node(node)
21
25
, m_platform_style(platform_style)
22
26
, m_options_model(options_model)
@@ -29,15 +33,17 @@ WalletController::WalletController(interfaces::Node& node, const PlatformStyle*
29
33
getOrCreateWallet (std::move (wallet));
30
34
}
31
35
32
- m_activity_thread.start ();
36
+ m_activity_worker->moveToThread (m_activity_thread);
37
+ m_activity_thread->start ();
33
38
}
34
39
35
40
// Not using the default destructor because not all member types definitions are
36
41
// available in the header, just forward declared.
37
42
WalletController::~WalletController ()
38
43
{
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;
41
47
}
42
48
43
49
std::vector<WalletModel*> WalletController::getOpenWallets () const
@@ -60,13 +66,6 @@ std::map<std::string, bool> WalletController::listWalletDir() const
60
66
return wallets;
61
67
}
62
68
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
-
70
69
void WalletController::closeWallet (WalletModel* wallet_model, QWidget* parent)
71
70
{
72
71
QMessageBox box (parent);
@@ -140,23 +139,60 @@ void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
140
139
delete wallet_model;
141
140
}
142
141
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
+ }
143
148
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
+ }
148
153
149
- void OpenWalletActivity::open ( )
154
+ void WalletControllerActivity::showProgressDialog ( const QString& label_text )
150
155
{
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));
160
178
}
179
+
180
+ if (m_wallet_model) Q_EMIT opened (m_wallet_model);
181
+
161
182
Q_EMIT finished ();
162
183
}
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