Further reduce header dependencies
[novacoin.git] / src / qt / walletmodel.cpp
1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
6
7 #include "ui_interface.h"
8 #include "wallet.h"
9 #include "walletdb.h" // for BackupWallet
10
11 #include <QSet>
12
13 WalletModel::WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent) :
14     QObject(parent), wallet(wallet), optionsModel(optionsModel), addressTableModel(0),
15     transactionTableModel(0),
16     cachedBalance(0), cachedUnconfirmedBalance(0), cachedNumTransactions(0),
17     cachedEncryptionStatus(Unencrypted)
18 {
19     addressTableModel = new AddressTableModel(wallet, this);
20     transactionTableModel = new TransactionTableModel(wallet, this);
21 }
22
23 qint64 WalletModel::getBalance() const
24 {
25     return wallet->GetBalance();
26 }
27
28 qint64 WalletModel::getUnconfirmedBalance() const
29 {
30     return wallet->GetUnconfirmedBalance();
31 }
32
33 int WalletModel::getNumTransactions() const
34 {
35     int numTransactions = 0;
36     {
37         LOCK(wallet->cs_wallet);
38         numTransactions = wallet->mapWallet.size();
39     }
40     return numTransactions;
41 }
42
43 void WalletModel::update()
44 {
45     qint64 newBalance = getBalance();
46     qint64 newUnconfirmedBalance = getUnconfirmedBalance();
47     int newNumTransactions = getNumTransactions();
48     EncryptionStatus newEncryptionStatus = getEncryptionStatus();
49
50     if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
51         emit balanceChanged(newBalance, newUnconfirmedBalance);
52
53     if(cachedNumTransactions != newNumTransactions)
54         emit numTransactionsChanged(newNumTransactions);
55
56     if(cachedEncryptionStatus != newEncryptionStatus)
57         emit encryptionStatusChanged(newEncryptionStatus);
58
59     cachedBalance = newBalance;
60     cachedUnconfirmedBalance = newUnconfirmedBalance;
61     cachedNumTransactions = newNumTransactions;
62
63     addressTableModel->update();
64 }
65
66 void WalletModel::updateAddressList()
67 {
68     addressTableModel->update();
69 }
70
71 bool WalletModel::validateAddress(const QString &address)
72 {
73     CBitcoinAddress addressParsed(address.toStdString());
74     return addressParsed.IsValid();
75 }
76
77 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
78 {
79     qint64 total = 0;
80     QSet<QString> setAddress;
81     QString hex;
82
83     if(recipients.empty())
84     {
85         return OK;
86     }
87
88     // Pre-check input data for validity
89     foreach(const SendCoinsRecipient &rcp, recipients)
90     {
91         if(!validateAddress(rcp.address))
92         {
93             return InvalidAddress;
94         }
95         setAddress.insert(rcp.address);
96
97         if(rcp.amount <= 0)
98         {
99             return InvalidAmount;
100         }
101         total += rcp.amount;
102     }
103
104     if(recipients.size() > setAddress.size())
105     {
106         return DuplicateAddress;
107     }
108
109     if(total > getBalance())
110     {
111         return AmountExceedsBalance;
112     }
113
114     if((total + nTransactionFee) > getBalance())
115     {
116         return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
117     }
118
119     {
120         LOCK2(cs_main, wallet->cs_wallet);
121
122         // Sendmany
123         std::vector<std::pair<CScript, int64> > vecSend;
124         foreach(const SendCoinsRecipient &rcp, recipients)
125         {
126             CScript scriptPubKey;
127             scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
128             vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
129         }
130
131         CWalletTx wtx;
132         CReserveKey keyChange(wallet);
133         int64 nFeeRequired = 0;
134         bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
135
136         if(!fCreated)
137         {
138             if((total + nFeeRequired) > wallet->GetBalance())
139             {
140                 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
141             }
142             return TransactionCreationFailed;
143         }
144         if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString()))
145         {
146             return Aborted;
147         }
148         if(!wallet->CommitTransaction(wtx, keyChange))
149         {
150             return TransactionCommitFailed;
151         }
152         hex = QString::fromStdString(wtx.GetHash().GetHex());
153     }
154
155     // Add addresses that we've sent to to the address book
156     foreach(const SendCoinsRecipient &rcp, recipients)
157     {
158         std::string strAddress = rcp.address.toStdString();
159         {
160             LOCK(wallet->cs_wallet);
161             if (!wallet->mapAddressBook.count(strAddress))
162                 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
163         }
164     }
165
166     return SendCoinsReturn(OK, 0, hex);
167 }
168
169 OptionsModel *WalletModel::getOptionsModel()
170 {
171     return optionsModel;
172 }
173
174 AddressTableModel *WalletModel::getAddressTableModel()
175 {
176     return addressTableModel;
177 }
178
179 TransactionTableModel *WalletModel::getTransactionTableModel()
180 {
181     return transactionTableModel;
182 }
183
184 WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
185 {
186     if(!wallet->IsCrypted())
187     {
188         return Unencrypted;
189     }
190     else if(wallet->IsLocked())
191     {
192         return Locked;
193     }
194     else
195     {
196         return Unlocked;
197     }
198 }
199
200 bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
201 {
202     if(encrypted)
203     {
204         // Encrypt
205         return wallet->EncryptWallet(passphrase);
206     }
207     else
208     {
209         // Decrypt -- TODO; not supported yet
210         return false;
211     }
212 }
213
214 bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
215 {
216     if(locked)
217     {
218         // Lock
219         return wallet->Lock();
220     }
221     else
222     {
223         // Unlock
224         return wallet->Unlock(passPhrase);
225     }
226 }
227
228 bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
229 {
230     bool retval;
231     {
232         LOCK(wallet->cs_wallet);
233         wallet->Lock(); // Make sure wallet is locked before attempting pass change
234         retval = wallet->ChangeWalletPassphrase(oldPass, newPass);
235     }
236     return retval;
237 }
238
239 bool WalletModel::backupWallet(const QString &filename)
240 {
241     return BackupWallet(*wallet, filename.toLocal8Bit().data());
242 }
243
244 // WalletModel::UnlockContext implementation
245 WalletModel::UnlockContext WalletModel::requestUnlock()
246 {
247     bool was_locked = getEncryptionStatus() == Locked;
248     if(was_locked)
249     {
250         // Request UI to unlock wallet
251         emit requireUnlock();
252     }
253     // If wallet is still locked, unlock was failed or cancelled, mark context as invalid
254     bool valid = getEncryptionStatus() != Locked;
255
256     return UnlockContext(this, valid, was_locked);
257 }
258
259 WalletModel::UnlockContext::UnlockContext(WalletModel *wallet, bool valid, bool relock):
260         wallet(wallet),
261         valid(valid),
262         relock(relock)
263 {
264 }
265
266 WalletModel::UnlockContext::~UnlockContext()
267 {
268     if(valid && relock)
269     {
270         wallet->setWalletLocked(true);
271     }
272 }
273
274 void WalletModel::UnlockContext::CopyFrom(const UnlockContext& rhs)
275 {
276     // Transfer context; old object no longer relocks wallet
277     *this = rhs;
278     rhs.relock = false;
279 }