Fix balanceChanged event error
[novacoin.git] / src / qt / walletmodel.h
1 #ifndef WALLETMODEL_H
2 #define WALLETMODEL_H
3
4 #include <QObject>
5
6 #include "allocators.h" /* for SecureString */
7
8 class OptionsModel;
9 class AddressTableModel;
10 class TransactionTableModel;
11 class CWallet;
12
13 QT_BEGIN_NAMESPACE
14 class QTimer;
15 QT_END_NAMESPACE
16
17 class SendCoinsRecipient
18 {
19 public:
20     QString address;
21     QString label;
22     qint64 amount;
23 };
24
25 /** Interface to Bitcoin wallet from Qt view code. */
26 class WalletModel : public QObject
27 {
28     Q_OBJECT
29
30 public:
31     explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
32     ~WalletModel();
33
34     enum StatusCode // Returned by sendCoins
35     {
36         OK,
37         InvalidAmount,
38         InvalidAddress,
39         AmountExceedsBalance,
40         AmountWithFeeExceedsBalance,
41         DuplicateAddress,
42         TransactionCreationFailed, // Error returned when wallet is still locked
43         TransactionCommitFailed,
44         Aborted
45     };
46
47     enum EncryptionStatus
48     {
49         Unencrypted,  // !wallet->IsCrypted()
50         Locked,       // wallet->IsCrypted() && wallet->IsLocked()
51         Unlocked      // wallet->IsCrypted() && !wallet->IsLocked()
52     };
53
54     OptionsModel *getOptionsModel();
55     AddressTableModel *getAddressTableModel();
56     TransactionTableModel *getTransactionTableModel();
57
58     qint64 getBalance() const;
59     qint64 getStake() const;
60     qint64 getUnconfirmedBalance() const;
61     qint64 getImmatureBalance() const;
62     int getNumTransactions() const;
63     EncryptionStatus getEncryptionStatus() const;
64
65     // Check address for validity
66     bool validateAddress(const QString &address);
67
68     // Return status record for SendCoins, contains error id + information
69     struct SendCoinsReturn
70     {
71         SendCoinsReturn(StatusCode status,
72                          qint64 fee=0,
73                          QString hex=QString()):
74             status(status), fee(fee), hex(hex) {}
75         StatusCode status;
76         qint64 fee; // is used in case status is "AmountWithFeeExceedsBalance"
77         QString hex; // is filled with the transaction hash if status is "OK"
78     };
79
80     // Send coins to a list of recipients
81     SendCoinsReturn sendCoins(const QList<SendCoinsRecipient> &recipients);
82
83     // Wallet encryption
84     bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
85     // Passphrase only needed when unlocking
86     bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
87     bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
88     // Wallet backup
89     bool backupWallet(const QString &filename);
90
91     // RAI object for unlocking wallet, returned by requestUnlock()
92     class UnlockContext
93     {
94     public:
95         UnlockContext(WalletModel *wallet, bool valid, bool relock);
96         ~UnlockContext();
97
98         bool isValid() const { return valid; }
99
100         // Copy operator and constructor transfer the context
101         UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
102         UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
103     private:
104         WalletModel *wallet;
105         bool valid;
106         mutable bool relock; // mutable, as it can be set to false by copying
107
108         void CopyFrom(const UnlockContext& rhs);
109     };
110
111     UnlockContext requestUnlock();
112
113 private:
114     CWallet *wallet;
115
116     // Wallet has an options model for wallet-specific options
117     // (transaction fee, for example)
118     OptionsModel *optionsModel;
119
120     AddressTableModel *addressTableModel;
121     TransactionTableModel *transactionTableModel;
122
123     // Cache some values to be able to detect changes
124     qint64 cachedBalance;
125     qint64 cachedStake;
126     qint64 cachedUnconfirmedBalance;
127     qint64 cachedImmatureBalance;
128     qint64 cachedNumTransactions;
129     EncryptionStatus cachedEncryptionStatus;
130     int cachedNumBlocks;
131
132     QTimer *pollTimer;
133
134     void subscribeToCoreSignals();
135     void unsubscribeFromCoreSignals();
136     void checkBalanceChanged();
137
138
139 public slots:
140     /* Wallet status might have changed */
141     void updateStatus();
142     /* New transaction, or transaction changed status */
143     void updateTransaction(const QString &hash, int status);
144     /* New, updated or removed address book entry */
145     void updateAddressBook(const QString &address, const QString &label, bool isMine, int status);
146     /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
147     void pollBalanceChanged();
148
149 signals:
150     // Signal that balance in wallet changed
151     void balanceChanged(qint64 balance, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance);
152
153     // Number of transactions in wallet changed
154     void numTransactionsChanged(int count);
155
156     // Encryption status of wallet changed
157     void encryptionStatusChanged(int status);
158
159     // Signal emitted when wallet needs to be unlocked
160     // It is valid behaviour for listeners to keep the wallet locked after this signal;
161     // this means that the unlocking failed or was cancelled.
162     void requireUnlock();
163
164     // Asynchronous error notification
165     void error(const QString &title, const QString &message, bool modal);
166 };
167
168
169 #endif // WALLETMODEL_H