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