89e8cdd2a0fc130dd3c21729d6b8922e00c8329b
[novacoin.git] / src / qt / walletmodel.h
1 #ifndef WALLETMODEL_H
2 #define WALLETMODEL_H
3
4 #include <QObject>
5
6 #include "util.h"
7
8 class OptionsModel;
9 class AddressTableModel;
10 class TransactionTableModel;
11 class CWallet;
12
13 struct SendCoinsRecipient
14 {
15     QString address;
16     QString label;
17     qint64 amount;
18 };
19
20 /** Interface to Bitcoin wallet from Qt view code. */
21 class WalletModel : public QObject
22 {
23     Q_OBJECT
24 public:
25     explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
26
27     enum StatusCode // Returned by sendCoins
28     {
29         OK,
30         InvalidAmount,
31         InvalidAddress,
32         AmountExceedsBalance,
33         AmountWithFeeExceedsBalance,
34         DuplicateAddress,
35         TransactionCreationFailed, // Error returned when wallet is still locked
36         TransactionCommitFailed,
37         Aborted,
38         MiscError
39     };
40
41     enum EncryptionStatus
42     {
43         Unencrypted,  // !wallet->IsCrypted()
44         Locked,       // wallet->IsCrypted() && wallet->IsLocked()
45         Unlocked      // wallet->IsCrypted() && !wallet->IsLocked()
46     };
47
48     OptionsModel *getOptionsModel();
49     AddressTableModel *getAddressTableModel();
50     TransactionTableModel *getTransactionTableModel();
51
52     qint64 getBalance() const;
53     qint64 getUnconfirmedBalance() const;
54     int getNumTransactions() const;
55     EncryptionStatus getEncryptionStatus() const;
56
57     // Check address for validity
58     bool validateAddress(const QString &address);
59
60     // Return status record for SendCoins, contains error id + information
61     struct SendCoinsReturn
62     {
63         SendCoinsReturn(StatusCode status,
64                          qint64 fee=0,
65                          QString hex=QString()):
66             status(status), fee(fee), hex(hex) {}
67         StatusCode status;
68         qint64 fee; // is used in case status is "AmountWithFeeExceedsBalance"
69         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     bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
79     bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
80
81     // RAI object for unlocking wallet, returned by requestUnlock()
82     class UnlockContext
83     {
84     public:
85         UnlockContext(WalletModel *wallet, bool valid, bool relock);
86         ~UnlockContext();
87
88         bool isValid() const { return valid; }
89
90         // Copy operator and constructor transfer the context
91         UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
92         UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
93     private:
94         WalletModel *wallet;
95         bool valid;
96         mutable bool relock; // mutable, as it can be set to false by copying
97
98         void CopyFrom(const UnlockContext& rhs);
99     };
100
101     UnlockContext requestUnlock();
102
103 private:
104     CWallet *wallet;
105
106     // Wallet has an options model for wallet-specific options
107     // (transaction fee, for example)
108     OptionsModel *optionsModel;
109
110     AddressTableModel *addressTableModel;
111     TransactionTableModel *transactionTableModel;
112
113     // Cache some values to be able to detect changes
114     qint64 cachedBalance;
115     qint64 cachedUnconfirmedBalance;
116     qint64 cachedNumTransactions;
117     EncryptionStatus cachedEncryptionStatus;
118
119 signals:
120     // Signal that balance in wallet changed
121     void balanceChanged(qint64 balance, qint64 unconfirmedBalance);
122
123     // Number of transactions in wallet changed
124     void numTransactionsChanged(int count);
125
126     // Encryption status of wallet changed
127     void encryptionStatusChanged(int status);
128
129     // Signal emitted when wallet needs to be unlocked
130     // It is valid behaviour for listeners to keep the wallet locked after this signal;
131     // this means that the unlocking failed or was cancelled.
132     void requireUnlock();
133
134     // Asynchronous error notification
135     void error(const QString &title, const QString &message);
136
137 public slots:
138
139 private slots:
140     void update();
141 };
142
143
144 #endif // WALLETMODEL_H