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