walletcontroller.h raw
1 // Copyright (c) 2019-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #ifndef BITCOIN_QT_WALLETCONTROLLER_H
6 #define BITCOIN_QT_WALLETCONTROLLER_H
7
8 #include <qt/sendcoinsrecipient.h>
9 #include <support/allocators/secure.h>
10 #include <sync.h>
11 #include <util/translation.h>
12
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <vector>
17
18 #include <QMessageBox>
19 #include <QMutex>
20 #include <QProgressDialog>
21 #include <QThread>
22 #include <QTimer>
23 #include <QString>
24
25 class ClientModel;
26 class OptionsModel;
27 class PlatformStyle;
28 class WalletModel;
29
30 namespace interfaces {
31 class Handler;
32 class Node;
33 class Wallet;
34 } // namespace interfaces
35
36 namespace fs {
37 class path;
38 }
39
40 class AskPassphraseDialog;
41 class CreateWalletActivity;
42 class CreateWalletDialog;
43 class MigrateWalletActivity;
44 class OpenWalletActivity;
45 class WalletControllerActivity;
46
47 /**
48 * Controller between interfaces::Node, WalletModel instances and the GUI.
49 */
50 class WalletController : public QObject
51 {
52 Q_OBJECT
53
54 void removeAndDeleteWallet(WalletModel* wallet_model);
55
56 public:
57 WalletController(ClientModel& client_model, const PlatformStyle* platform_style, QObject* parent);
58 ~WalletController();
59
60 WalletModel* getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet);
61
62 //! Returns all wallet names in the wallet dir mapped to whether the wallet
63 //! is loaded.
64 std::map<std::string, std::pair<bool, std::string>> listWalletDir() const;
65
66 void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);
67 void closeAllWallets(QWidget* parent = nullptr);
68
69 Q_SIGNALS:
70 void walletAdded(WalletModel* wallet_model);
71 void walletRemoved(WalletModel* wallet_model);
72
73 void coinsSent(WalletModel* wallet_model, SendCoinsRecipient recipient, QByteArray transaction);
74
75 private:
76 QThread* const m_activity_thread;
77 QObject* const m_activity_worker;
78 ClientModel& m_client_model;
79 interfaces::Node& m_node;
80 const PlatformStyle* const m_platform_style;
81 OptionsModel* const m_options_model;
82 mutable QMutex m_mutex;
83 std::vector<WalletModel*> m_wallets;
84 std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
85
86 friend class WalletControllerActivity;
87 friend class MigrateWalletActivity;
88
89 //! Starts the wallet closure procedure
90 void removeWallet(WalletModel* wallet_model);
91 };
92
93 class WalletControllerActivity : public QObject
94 {
95 Q_OBJECT
96
97 public:
98 WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget);
99 virtual ~WalletControllerActivity() = default;
100
101 Q_SIGNALS:
102 void finished();
103
104 protected:
105 interfaces::Node& node() const { return m_wallet_controller->m_node; }
106 QObject* worker() const { return m_wallet_controller->m_activity_worker; }
107
108 void showProgressDialog(const QString& title_text, const QString& label_text, bool show_minimized=false);
109
110 WalletController* const m_wallet_controller;
111 QWidget* const m_parent_widget;
112 WalletModel* m_wallet_model{nullptr};
113 bilingual_str m_error_message;
114 std::vector<bilingual_str> m_warning_message;
115 };
116
117
118 class CreateWalletActivity : public WalletControllerActivity
119 {
120 Q_OBJECT
121
122 public:
123 CreateWalletActivity(WalletController* wallet_controller, QWidget* parent_widget);
124 virtual ~CreateWalletActivity();
125
126 void create();
127
128 Q_SIGNALS:
129 void created(WalletModel* wallet_model);
130
131 private:
132 void askPassphrase();
133 void createWallet();
134 void finish();
135
136 SecureString m_passphrase;
137 CreateWalletDialog* m_create_wallet_dialog{nullptr};
138 AskPassphraseDialog* m_passphrase_dialog{nullptr};
139 };
140
141 class OpenWalletActivity : public WalletControllerActivity
142 {
143 Q_OBJECT
144
145 public:
146 OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget);
147
148 void open(const std::string& path);
149
150 Q_SIGNALS:
151 void opened(WalletModel* wallet_model);
152
153 private:
154 void finish();
155 };
156
157 class LoadWalletsActivity : public WalletControllerActivity
158 {
159 Q_OBJECT
160
161 public:
162 LoadWalletsActivity(WalletController* wallet_controller, QWidget* parent_widget);
163
164 void load(bool show_loading_minimized);
165 };
166
167 class RestoreWalletActivity : public WalletControllerActivity
168 {
169 Q_OBJECT
170
171 public:
172 RestoreWalletActivity(WalletController* wallet_controller, QWidget* parent_widget);
173
174 void restore(const fs::path& backup_file, const std::string& wallet_name);
175
176 Q_SIGNALS:
177 void restored(WalletModel* wallet_model);
178
179 private:
180 void finish();
181 };
182
183 class MigrateWalletActivity : public WalletControllerActivity
184 {
185 Q_OBJECT
186
187 public:
188 MigrateWalletActivity(WalletController* wallet_controller, QWidget* parent) : WalletControllerActivity(wallet_controller, parent) {}
189
190 void restore_and_migrate(const fs::path& path, const std::string& wallet_name);
191 void migrate(const std::string& path);
192
193 Q_SIGNALS:
194 void migrated(WalletModel* wallet_model);
195
196 private:
197 QString m_success_message;
198
199 void do_migrate(const std::string& name);
200 void finish();
201 };
202
203 #endif // BITCOIN_QT_WALLETCONTROLLER_H
204