Wallet encryption part 2: ask passphrase when needed, add menu options
[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
27     {
28         OK,
29         InvalidAmount,
30         InvalidAddress,
31         AmountExceedsBalance,
32         AmountWithFeeExceedsBalance,
33         DuplicateAddress,
34         TransactionCreationFailed,
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         UnlockContext(const UnlockContext& obj)
90         { CopyFrom(obj); }
91     private:
92         UnlockContext& operator=(const UnlockContext& rhs)
93         { CopyFrom(rhs); return *this; }
94
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     qint64 cachedBalance;
116     qint64 cachedUnconfirmedBalance;
117     qint64 cachedNumTransactions;
118     EncryptionStatus cachedEncryptionStatus;
119
120 signals:
121     void balanceChanged(qint64 balance, qint64 unconfirmedBalance);
122     void numTransactionsChanged(int count);
123     void encryptionStatusChanged(int status);
124     void requireUnlock();
125
126     // Asynchronous error notification
127     void error(const QString &title, const QString &message);
128
129 public slots:
130
131 private slots:
132     void update();
133 };
134
135
136 #endif // WALLETMODEL_H