]> Git Repo - VerusCoin.git/blob - src/qt/walletmodel.h
Merge pull request #3076 from lano1106/uint256_util
[VerusCoin.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 #include "wallet.h"
8 #include "walletmodeltransaction.h"
9 #include "paymentrequestplus.h"
10
11 class OptionsModel;
12 class AddressTableModel;
13 class TransactionTableModel;
14 class CWallet;
15 class WalletModelTransaction;
16
17 QT_BEGIN_NAMESPACE
18 class QTimer;
19 QT_END_NAMESPACE
20
21 class SendCoinsRecipient
22 {
23 public:
24     explicit SendCoinsRecipient() : amount(0) { }
25     explicit SendCoinsRecipient(const QString &addr, const QString &label, quint64 amount, const QString &message):
26         address(addr), label(label), amount(amount), message(message) {}
27
28     QString address;
29     QString label;
30     qint64 amount;
31     QString message;
32
33     // If from a payment request, paymentRequest.IsInitialized() will be true
34     PaymentRequestPlus paymentRequest;
35     QString authenticatedMerchant; // Empty if no authentication or invalid signature/cert/etc.
36 };
37
38 /** Interface to Bitcoin wallet from Qt view code. */
39 class WalletModel : public QObject
40 {
41     Q_OBJECT
42
43 public:
44     explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
45     ~WalletModel();
46
47     enum StatusCode // Returned by sendCoins
48     {
49         OK,
50         InvalidAmount,
51         InvalidAddress,
52         AmountExceedsBalance,
53         AmountWithFeeExceedsBalance,
54         DuplicateAddress,
55         TransactionCreationFailed, // Error returned when wallet is still locked
56         TransactionCommitFailed,
57         Aborted
58     };
59
60     enum EncryptionStatus
61     {
62         Unencrypted,  // !wallet->IsCrypted()
63         Locked,       // wallet->IsCrypted() && wallet->IsLocked()
64         Unlocked      // wallet->IsCrypted() && !wallet->IsLocked()
65     };
66
67     OptionsModel *getOptionsModel();
68     AddressTableModel *getAddressTableModel();
69     TransactionTableModel *getTransactionTableModel();
70
71     qint64 getBalance() 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):
84             status(status) {}
85         StatusCode status;
86     };
87
88     // prepare transaction for getting txfee before sending coins
89     SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction);
90
91     // Send coins to a list of recipients
92     SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
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     // 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 private:
125     CWallet *wallet;
126
127     // Wallet has an options model for wallet-specific options
128     // (transaction fee, for example)
129     OptionsModel *optionsModel;
130
131     AddressTableModel *addressTableModel;
132     TransactionTableModel *transactionTableModel;
133
134     // Cache some values to be able to detect changes
135     qint64 cachedBalance;
136     qint64 cachedUnconfirmedBalance;
137     qint64 cachedImmatureBalance;
138     qint64 cachedNumTransactions;
139     EncryptionStatus cachedEncryptionStatus;
140     int cachedNumBlocks;
141
142     QTimer *pollTimer;
143
144     void subscribeToCoreSignals();
145     void unsubscribeFromCoreSignals();
146     void checkBalanceChanged();
147
148 signals:
149     // Signal that balance in wallet changed
150     void balanceChanged(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance);
151
152     // Number of transactions in wallet changed
153     void numTransactionsChanged(int count);
154
155     // Encryption status of wallet changed
156     void encryptionStatusChanged(int status);
157
158     // Signal emitted when wallet needs to be unlocked
159     // It is valid behaviour for listeners to keep the wallet locked after this signal;
160     // this means that the unlocking failed or was cancelled.
161     void requireUnlock();
162
163     // Asynchronous message notification
164     void message(const QString &title, const QString &message, unsigned int style);
165
166     // Coins sent: from wallet, to recipient, in (serialized) transaction:
167     void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
168
169 public slots:
170     /* Wallet status might have changed */
171     void updateStatus();
172     /* New transaction, or transaction changed status */
173     void updateTransaction(const QString &hash, int status);
174     /* New, updated or removed address book entry */
175     void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
176     /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
177     void pollBalanceChanged();
178 };
179
180 #endif // WALLETMODEL_H
This page took 0.032751 seconds and 4 git commands to generate.