]> Git Repo - VerusCoin.git/blob - src/qt/walletmodel.h
Merge pull request #2950 from pstratem/walletload
[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     SendCoinsRecipient() : amount(0) { }
25
26     QString address;
27     QString label;
28     qint64 amount;
29
30     // If from a payment request, paymentRequest.IsInitialized() will be true
31     PaymentRequestPlus paymentRequest;
32     QString authenticatedMerchant; // Empty if no authentication or invalid signature/cert/etc.
33 };
34
35 /** Interface to Bitcoin wallet from Qt view code. */
36 class WalletModel : public QObject
37 {
38     Q_OBJECT
39
40 public:
41     explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
42     ~WalletModel();
43
44     enum StatusCode // Returned by sendCoins
45     {
46         OK,
47         InvalidAmount,
48         InvalidAddress,
49         AmountExceedsBalance,
50         AmountWithFeeExceedsBalance,
51         DuplicateAddress,
52         TransactionCreationFailed, // Error returned when wallet is still locked
53         TransactionCommitFailed,
54         Aborted
55     };
56
57     enum EncryptionStatus
58     {
59         Unencrypted,  // !wallet->IsCrypted()
60         Locked,       // wallet->IsCrypted() && wallet->IsLocked()
61         Unlocked      // wallet->IsCrypted() && !wallet->IsLocked()
62     };
63
64     OptionsModel *getOptionsModel();
65     AddressTableModel *getAddressTableModel();
66     TransactionTableModel *getTransactionTableModel();
67
68     qint64 getBalance() const;
69     qint64 getUnconfirmedBalance() const;
70     qint64 getImmatureBalance() const;
71     int getNumTransactions() const;
72     EncryptionStatus getEncryptionStatus() const;
73
74     // Check address for validity
75     bool validateAddress(const QString &address);
76
77     // Return status record for SendCoins, contains error id + information
78     struct SendCoinsReturn
79     {
80         SendCoinsReturn(StatusCode status):
81             status(status) {}
82         StatusCode status;
83     };
84
85     // prepare transaction for getting txfee before sending coins
86     SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction);
87
88     // Send coins to a list of recipients
89     SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
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     // RAI object for unlocking wallet, returned by requestUnlock()
100     class UnlockContext
101     {
102     public:
103         UnlockContext(WalletModel *wallet, bool valid, bool relock);
104         ~UnlockContext();
105
106         bool isValid() const { return valid; }
107
108         // Copy operator and constructor transfer the context
109         UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
110         UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
111     private:
112         WalletModel *wallet;
113         bool valid;
114         mutable bool relock; // mutable, as it can be set to false by copying
115
116         void CopyFrom(const UnlockContext& rhs);
117     };
118
119     UnlockContext requestUnlock();
120
121 private:
122     CWallet *wallet;
123
124     // Wallet has an options model for wallet-specific options
125     // (transaction fee, for example)
126     OptionsModel *optionsModel;
127
128     AddressTableModel *addressTableModel;
129     TransactionTableModel *transactionTableModel;
130
131     // Cache some values to be able to detect changes
132     qint64 cachedBalance;
133     qint64 cachedUnconfirmedBalance;
134     qint64 cachedImmatureBalance;
135     qint64 cachedNumTransactions;
136     EncryptionStatus cachedEncryptionStatus;
137     int cachedNumBlocks;
138
139     QTimer *pollTimer;
140
141     void subscribeToCoreSignals();
142     void unsubscribeFromCoreSignals();
143     void checkBalanceChanged();
144
145 signals:
146     // Signal that balance in wallet changed
147     void balanceChanged(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance);
148
149     // Number of transactions in wallet changed
150     void numTransactionsChanged(int count);
151
152     // Encryption status of wallet changed
153     void encryptionStatusChanged(int status);
154
155     // Signal emitted when wallet needs to be unlocked
156     // It is valid behaviour for listeners to keep the wallet locked after this signal;
157     // this means that the unlocking failed or was cancelled.
158     void requireUnlock();
159
160     // Asynchronous message notification
161     void message(const QString &title, const QString &message, unsigned int style);
162
163     // Coins sent: from wallet, to recipient, in (serialized) transaction:
164     void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
165
166 public slots:
167     /* Wallet status might have changed */
168     void updateStatus();
169     /* New transaction, or transaction changed status */
170     void updateTransaction(const QString &hash, int status);
171     /* New, updated or removed address book entry */
172     void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
173     /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
174     void pollBalanceChanged();
175 };
176
177 #endif // WALLETMODEL_H
This page took 0.033517 seconds and 4 git commands to generate.