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