walletmodel.h 4.52 KB
Newer Older
1
2
3
4
#ifndef WALLETMODEL_H
#define WALLETMODEL_H

#include <QObject>
5

6
#include "allocators.h" /* for SecureString */
7
8
9
10
11
12

class OptionsModel;
class AddressTableModel;
class TransactionTableModel;
class CWallet;

13
class SendCoinsRecipient
14
{
15
public:
16
17
18
19
20
    QString address;
    QString label;
    qint64 amount;
};

21
/** Interface to Bitcoin wallet from Qt view code. */
22
23
24
25
class WalletModel : public QObject
{
    Q_OBJECT
public:
26
    explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
27

28
    enum StatusCode // Returned by sendCoins
29
30
31
32
33
34
    {
        OK,
        InvalidAmount,
        InvalidAddress,
        AmountExceedsBalance,
        AmountWithFeeExceedsBalance,
35
        DuplicateAddress,
36
        TransactionCreationFailed, // Error returned when wallet is still locked
37
        TransactionCommitFailed,
38
        Aborted
39
40
    };

41
42
43
44
45
46
47
    enum EncryptionStatus
    {
        Unencrypted,  // !wallet->IsCrypted()
        Locked,       // wallet->IsCrypted() && wallet->IsLocked()
        Unlocked      // wallet->IsCrypted() && !wallet->IsLocked()
    };

48
49
50
51
52
    OptionsModel *getOptionsModel();
    AddressTableModel *getAddressTableModel();
    TransactionTableModel *getTransactionTableModel();

    qint64 getBalance() const;
53
    qint64 getUnconfirmedBalance() const;
54
    int getNumTransactions() const;
55
56
    EncryptionStatus getEncryptionStatus() const;

57
58
59
    // Check address for validity
    bool validateAddress(const QString &address);

60
    // Return status record for SendCoins, contains error id + information
61
62
63
64
65
66
67
    struct SendCoinsReturn
    {
        SendCoinsReturn(StatusCode status,
                         qint64 fee=0,
                         QString hex=QString()):
            status(status), fee(fee), hex(hex) {}
        StatusCode status;
68
69
        qint64 fee; // is used in case status is "AmountWithFeeExceedsBalance"
        QString hex; // is filled with the transaction hash if status is "OK"
70
71
    };

72
    // Send coins to a list of recipients
73
    SendCoinsReturn sendCoins(const QList<SendCoinsRecipient> &recipients);
74
75

    // Wallet encryption
76
    bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
77
    // Passphrase only needed when unlocking
78
79
    bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
    bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
sje397's avatar
sje397 committed
80
81
    // Wallet backup
    bool backupWallet(const QString &filename);
82
83
84
85
86
87
88
89
90
91

    // RAI object for unlocking wallet, returned by requestUnlock()
    class UnlockContext
    {
    public:
        UnlockContext(WalletModel *wallet, bool valid, bool relock);
        ~UnlockContext();

        bool isValid() const { return valid; }

92
93
94
        // Copy operator and constructor transfer the context
        UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
        UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
95
96
97
98
99
100
101
102
103
104
    private:
        WalletModel *wallet;
        bool valid;
        mutable bool relock; // mutable, as it can be set to false by copying

        void CopyFrom(const UnlockContext& rhs);
    };

    UnlockContext requestUnlock();

105
106
107
108
109
110
111
112
113
114
private:
    CWallet *wallet;

    // Wallet has an options model for wallet-specific options
    // (transaction fee, for example)
    OptionsModel *optionsModel;

    AddressTableModel *addressTableModel;
    TransactionTableModel *transactionTableModel;

115
    // Cache some values to be able to detect changes
116
117
118
    qint64 cachedBalance;
    qint64 cachedUnconfirmedBalance;
    qint64 cachedNumTransactions;
119
    EncryptionStatus cachedEncryptionStatus;
120

121
signals:
122
    // Signal that balance in wallet changed
123
    void balanceChanged(qint64 balance, qint64 unconfirmedBalance);
124
125

    // Number of transactions in wallet changed
126
    void numTransactionsChanged(int count);
127
128

    // Encryption status of wallet changed
129
    void encryptionStatusChanged(int status);
130
131
132
133

    // Signal emitted when wallet needs to be unlocked
    // It is valid behaviour for listeners to keep the wallet locked after this signal;
    // this means that the unlocking failed or was cancelled.
134
    void requireUnlock();
135
136

    // Asynchronous error notification
137
    void error(const QString &title, const QString &message, bool modal);
138
139

public slots:
140
141
142
143
144
145
    /* Wallet status might have changed */
    void updateStatus();
    /* New transaction, or transaction changed status */
    void updateTransaction(const QString &hash, int status);
    /* New, updated or removed address book entry */
    void updateAddressBook(const QString &address, const QString &label, int status);
146
147
148
149
};


#endif // WALLETMODEL_H