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