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