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