Improve documentation for UI classes
[novacoin.git] / src / qt / walletmodel.h
1 #ifndef WALLETMODEL_H
2 #define WALLETMODEL_H
3
4 #include <QObject>
5 #include <string>
6
7 class OptionsModel;
8 class AddressTableModel;
9 class TransactionTableModel;
10 class CWallet;
11
12 struct SendCoinsRecipient
13 {
14     QString address;
15     QString label;
16     qint64 amount;
17 };
18
19 /** Interface to Bitcoin wallet from Qt view code. */
20 class WalletModel : public QObject
21 {
22     Q_OBJECT
23 public:
24     explicit WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent = 0);
25
26     enum StatusCode // Returned by sendCoins
27     {
28         OK,
29         InvalidAmount,
30         InvalidAddress,
31         AmountExceedsBalance,
32         AmountWithFeeExceedsBalance,
33         DuplicateAddress,
34         TransactionCreationFailed, // Error returned when wallet is still locked
35         TransactionCommitFailed,
36         Aborted,
37         MiscError
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 std::string &passphrase);
76     // Passphrase only needed when unlocking
77     bool setWalletLocked(bool locked, const std::string &passPhrase=std::string());
78     bool changePassphrase(const std::string &oldPass, const std::string &newPass);
79
80     // RAI object for unlocking wallet, returned by requestUnlock()
81     class UnlockContext
82     {
83     public:
84         UnlockContext(WalletModel *wallet, bool valid, bool relock);
85         ~UnlockContext();
86
87         bool isValid() const { return valid; }
88
89         // Copy operator and constructor transfer the context
90         UnlockContext(const UnlockContext& obj) { CopyFrom(obj); }
91         UnlockContext& operator=(const UnlockContext& rhs) { CopyFrom(rhs); return *this; }
92     private:
93         WalletModel *wallet;
94         bool valid;
95         mutable bool relock; // mutable, as it can be set to false by copying
96
97         void CopyFrom(const UnlockContext& rhs);
98     };
99
100     UnlockContext requestUnlock();
101
102 private:
103     CWallet *wallet;
104
105     // Wallet has an options model for wallet-specific options
106     // (transaction fee, for example)
107     OptionsModel *optionsModel;
108
109     AddressTableModel *addressTableModel;
110     TransactionTableModel *transactionTableModel;
111
112     // Cache some values to be able to detect changes
113     qint64 cachedBalance;
114     qint64 cachedUnconfirmedBalance;
115     qint64 cachedNumTransactions;
116     EncryptionStatus cachedEncryptionStatus;
117
118 signals:
119     // Signal that balance in wallet changed
120     void balanceChanged(qint64 balance, qint64 unconfirmedBalance);
121
122     // Number of transactions in wallet changed
123     void numTransactionsChanged(int count);
124
125     // Encryption status of wallet changed
126     void encryptionStatusChanged(int status);
127
128     // Signal emitted when wallet needs to be unlocked
129     // It is valid behaviour for listeners to keep the wallet locked after this signal;
130     // this means that the unlocking failed or was cancelled.
131     void requireUnlock();
132
133     // Asynchronous error notification
134     void error(const QString &title, const QString &message);
135
136 public slots:
137
138 private slots:
139     void update();
140 };
141
142
143 #endif // WALLETMODEL_H