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