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