99648d1f62277b7a182c35a9fe0aacf3305d86d3
[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 getStakeStats(float &nKernelsRate, float &nCoinDaysRate);
106     void getStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& 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     void clearOrphans();
138     CWallet* getWallet();
139
140 private:
141     CWallet *wallet;
142
143     // Wallet has an options model for wallet-specific options
144     // (transaction fee, for example)
145     OptionsModel *optionsModel;
146
147     AddressTableModel *addressTableModel;
148     MintingTableModel *mintingTableModel;
149     TransactionTableModel *transactionTableModel;
150
151     // Cache some values to be able to detect changes
152     qint64 cachedBalance;
153     qint64 cachedStake;
154     qint64 cachedUnconfirmedBalance;
155     qint64 cachedImmatureBalance;
156     qint64 cachedNumTransactions;
157     EncryptionStatus cachedEncryptionStatus;
158     int cachedNumBlocks;
159
160     QTimer *pollTimer;
161
162     void subscribeToCoreSignals();
163     void unsubscribeFromCoreSignals();
164     void checkBalanceChanged();
165
166
167 public slots:
168     /* Wallet status might have changed */
169     void updateStatus();
170     /* New transaction, or transaction changed status */
171     void updateTransaction(const QString &hash, int status);
172     /* New, updated or removed address book entry */
173     void updateAddressBook(const QString &address, const QString &label, bool isMine, int status);
174     /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
175     void pollBalanceChanged();
176
177 signals:
178     // Signal that balance in wallet changed
179     void balanceChanged(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance);
180
181     // Number of transactions in wallet changed
182     void numTransactionsChanged(int count);
183
184     // Encryption status of wallet changed
185     void encryptionStatusChanged(int status);
186
187     // Signal emitted when wallet needs to be unlocked
188     // It is valid behaviour for listeners to keep the wallet locked after this signal;
189     // this means that the unlocking failed or was cancelled.
190     void requireUnlock();
191
192     // Asynchronous error notification
193     void error(const QString &title, const QString &message, bool modal);
194 };
195
196
197 #endif // WALLETMODEL_H