Add removeaddress RPC call
[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     bool haveWatchOnly() const;
69     qint64 getBalance() const;
70     qint64 getBalanceWatchOnly() const;
71     qint64 getStake() const;
72     qint64 getUnconfirmedBalance() const;
73     qint64 getImmatureBalance() const;
74     int getNumTransactions() const;
75     EncryptionStatus getEncryptionStatus() const;
76
77     // Check address for validity
78     bool validateAddress(const QString &address);
79
80     // Return status record for SendCoins, contains error id + information
81     struct SendCoinsReturn
82     {
83         SendCoinsReturn(StatusCode status=Aborted,
84                          qint64 fee=0,
85                          QString hex=QString()):
86             status(status), fee(fee), hex(hex) {}
87         StatusCode status;
88         qint64 fee; // is used in case status is "AmountWithFeeExceedsBalance"
89         QString hex; // is filled with the transaction hash if status is "OK"
90     };
91
92     // Send coins to a list of recipients
93     SendCoinsReturn sendCoins(const QList<SendCoinsRecipient> &recipients, const CCoinControl *coinControl=NULL);
94
95     // Wallet encryption
96     bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
97     // Passphrase only needed when unlocking
98     bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
99     bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
100     // Wallet backup
101     bool backupWallet(const QString &filename);
102
103     bool dumpWallet(const QString &filename);
104     bool importWallet(const QString &filename);
105
106     void getStakeStats(float &nKernelsRate, float &nCoinDaysRate);
107     void getStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
108
109     // RAI object for unlocking wallet, returned by requestUnlock()
110     class UnlockContext
111     {
112     public:
113         UnlockContext(WalletModel *wallet, bool valid, bool relock);
114         ~UnlockContext();
115
116         bool isValid() const { return valid; }
117
118         // Copy operator and constructor transfer the context
119         UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
120         UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
121     private:
122         WalletModel *wallet;
123         bool valid;
124         mutable bool relock; // mutable, as it can be set to false by copying
125
126         void CopyFrom(const UnlockContext& rhs);
127     };
128
129     UnlockContext requestUnlock();
130
131     bool getPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
132     void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
133     void listCoins(std::map<QString, std::vector<COutput> >& mapCoins) const;
134     bool isLockedCoin(uint256 hash, unsigned int n) const;
135     void lockCoin(COutPoint& output);
136     void unlockCoin(COutPoint& output);
137     void listLockedCoins(std::vector<COutPoint>& vOutpts);
138     void clearOrphans();
139     CWallet* getWallet();
140
141 private:
142     CWallet *wallet;
143     bool fHaveWatchOnly;
144
145     // Wallet has an options model for wallet-specific options
146     // (transaction fee, for example)
147     OptionsModel *optionsModel;
148
149     AddressTableModel *addressTableModel;
150     MintingTableModel *mintingTableModel;
151     TransactionTableModel *transactionTableModel;
152
153     // Cache some values to be able to detect changes
154     qint64 cachedBalance;
155     qint64 cachedStake;
156     qint64 cachedUnconfirmedBalance;
157     qint64 cachedImmatureBalance;
158     qint64 cachedNumTransactions;
159     EncryptionStatus cachedEncryptionStatus;
160     int cachedNumBlocks;
161
162     QTimer *pollTimer;
163
164     void subscribeToCoreSignals();
165     void unsubscribeFromCoreSignals();
166     void checkBalanceChanged();
167
168 public slots:
169     /* Wallet status might have changed */
170     void updateStatus();
171     /* New transaction, or transaction changed status */
172     void updateTransaction(const QString &hash, int status);
173     /* New, updated or removed address book entry */
174     void updateAddressBook(const QString &address, const QString &label, bool isMine, int status);
175     /* Watchonly added */
176     void updateWatchOnlyFlag(bool fHaveWatchonly);
177     /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
178     void pollBalanceChanged();
179
180 signals:
181     // Signal that balance in wallet changed
182     void balanceChanged(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance);
183
184     // Number of transactions in wallet changed
185     void numTransactionsChanged(int count);
186
187     // Encryption status of wallet changed
188     void encryptionStatusChanged(int status);
189
190     // Signal emitted when wallet needs to be unlocked
191     // It is valid behaviour for listeners to keep the wallet locked after this signal;
192     // this means that the unlocking failed or was cancelled.
193     void requireUnlock();
194
195     // Asynchronous error notification
196     void error(const QString &title, const QString &message, bool modal);
197
198     // Watch-only address added
199     void notifyWatchonlyChanged(bool fHaveWatchonly);
200 };
201
202
203 #endif // WALLETMODEL_H