aboutsummaryrefslogtreecommitdiff
path: root/src/qt/walletcontroller.h
blob: 03039dd795042b2ef5e3ab3d8967ab8b332380d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_QT_WALLETCONTROLLER_H
#define BITCOIN_QT_WALLETCONTROLLER_H

#include <qt/walletmodel.h>
#include <sync.h>

#include <map>
#include <memory>
#include <vector>

#include <QMessageBox>
#include <QMutex>
#include <QThread>

class OptionsModel;
class PlatformStyle;

namespace interfaces {
class Handler;
class Node;
} // namespace interfaces

class OpenWalletActivity;

/**
 * Controller between interfaces::Node, WalletModel instances and the GUI.
 */
class WalletController : public QObject
{
    Q_OBJECT

    WalletModel* getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet);
    void removeAndDeleteWallet(WalletModel* wallet_model);

public:
    WalletController(interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent);
    ~WalletController();

    //! Returns wallet models currently open.
    std::vector<WalletModel*> getOpenWallets() const;

    //! Returns all wallet names in the wallet dir mapped to whether the wallet
    //! is loaded.
    std::map<std::string, bool> listWalletDir() const;

    OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr);
    void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);

private Q_SLOTS:
    void addWallet(WalletModel* wallet_model);

Q_SIGNALS:
    void walletAdded(WalletModel* wallet_model);
    void walletRemoved(WalletModel* wallet_model);

    void coinsSent(WalletModel* wallet_model, SendCoinsRecipient recipient, QByteArray transaction);

private:
    QThread m_activity_thread;
    interfaces::Node& m_node;
    const PlatformStyle* const m_platform_style;
    OptionsModel* const m_options_model;
    mutable QMutex m_mutex;
    std::vector<WalletModel*> m_wallets;
    std::unique_ptr<interfaces::Handler> m_handler_load_wallet;

    friend class OpenWalletActivity;
};

class OpenWalletActivity : public QObject
{
    Q_OBJECT

public:
    OpenWalletActivity(WalletController* wallet_controller, const std::string& name);

public Q_SLOTS:
    void open();

Q_SIGNALS:
    void message(QMessageBox::Icon icon, const QString text);
    void finished();
    void opened(WalletModel* wallet_model);

private:
    WalletController* const m_wallet_controller;
    std::string const m_name;
};

#endif // BITCOIN_QT_WALLETCONTROLLER_H