Merge remote branch 'upstream/master'
[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
9 #include <QTimer>
10 #include <QSet>
11
12 WalletModel::WalletModel(CWallet *wallet, QObject *parent) :
13     QObject(parent), wallet(wallet), optionsModel(0), addressTableModel(0),
14     transactionTableModel(0),
15     cachedBalance(0), cachedUnconfirmedBalance(0), cachedNumTransactions(0)
16 {
17     // Until signal notifications is built into the bitcoin core,
18     //  simply update everything after polling using a timer.
19     QTimer *timer = new QTimer(this);
20     connect(timer, SIGNAL(timeout()), this, SLOT(update()));
21     timer->start(MODEL_UPDATE_DELAY);
22
23     optionsModel = new OptionsModel(wallet, this);
24     addressTableModel = new AddressTableModel(wallet, this);
25     transactionTableModel = new TransactionTableModel(wallet, this);
26 }
27
28 qint64 WalletModel::getBalance() const
29 {
30     return wallet->GetBalance();
31 }
32
33 qint64 WalletModel::getUnconfirmedBalance() const
34 {
35     return wallet->GetUnconfirmedBalance();
36 }
37
38 int WalletModel::getNumTransactions() const
39 {
40     int numTransactions = 0;
41     CRITICAL_BLOCK(wallet->cs_mapWallet)
42     {
43         numTransactions = wallet->mapWallet.size();
44     }
45     return numTransactions;
46 }
47
48 void WalletModel::update()
49 {
50     qint64 newBalance = getBalance();
51     qint64 newUnconfirmedBalance = getUnconfirmedBalance();
52     int newNumTransactions = getNumTransactions();
53
54     if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
55         emit balanceChanged(newBalance, newUnconfirmedBalance);
56
57     if(cachedNumTransactions != newNumTransactions)
58         emit numTransactionsChanged(newNumTransactions);
59
60     cachedBalance = newBalance;
61     cachedUnconfirmedBalance = newUnconfirmedBalance;
62     cachedNumTransactions = newNumTransactions;
63
64     addressTableModel->update();
65 }
66
67 bool WalletModel::validateAddress(const QString &address)
68 {
69     CBitcoinAddress addressParsed(address.toStdString());
70     return addressParsed.IsValid();
71 }
72
73 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
74 {
75     qint64 total = 0;
76     QSet<QString> setAddress;
77     QString hex;
78
79     if(recipients.empty())
80     {
81         return OK;
82     }
83
84     // Pre-check input data for validity
85     foreach(const SendCoinsRecipient &rcp, recipients)
86     {
87         uint160 hash160 = 0;
88
89         if(!validateAddress(rcp.address))
90         {
91             return InvalidAddress;
92         }
93         setAddress.insert(rcp.address);
94
95         if(rcp.amount <= 0)
96         {
97             return InvalidAmount;
98         }
99         total += rcp.amount;
100     }
101
102     if(recipients.size() > setAddress.size())
103     {
104         return DuplicateAddress;
105     }
106
107     if(total > getBalance())
108     {
109         return AmountExceedsBalance;
110     }
111
112     if((total + nTransactionFee) > getBalance())
113     {
114         return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
115     }
116
117     CRITICAL_BLOCK(cs_main)
118     CRITICAL_BLOCK(wallet->cs_mapWallet)
119     {
120         // Sendmany
121         std::vector<std::pair<CScript, int64> > vecSend;
122         foreach(const SendCoinsRecipient &rcp, recipients)
123         {
124             CScript scriptPubKey;
125             scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
126             vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
127         }
128
129         CWalletTx wtx;
130         CReserveKey keyChange(wallet);
131         int64 nFeeRequired = 0;
132         bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
133
134         if(!fCreated)
135         {
136             if((total + nFeeRequired) > wallet->GetBalance())
137             {
138                 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
139             }
140             return TransactionCreationFailed;
141         }
142         if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString(), NULL))
143         {
144             return Aborted;
145         }
146         if(!wallet->CommitTransaction(wtx, keyChange))
147         {
148             return TransactionCommitFailed;
149         }
150         hex = QString::fromStdString(wtx.GetHash().GetHex());
151     }
152
153     // Add addresses that we've sent to to the address book
154     foreach(const SendCoinsRecipient &rcp, recipients)
155     {
156         std::string strAddress = rcp.address.toStdString();
157         CRITICAL_BLOCK(wallet->cs_mapAddressBook)
158         {
159             if (!wallet->mapAddressBook.count(strAddress))
160                 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
161         }
162     }
163
164     return SendCoinsReturn(OK, 0, hex);
165 }
166
167 OptionsModel *WalletModel::getOptionsModel()
168 {
169     return optionsModel;
170 }
171
172 AddressTableModel *WalletModel::getAddressTableModel()
173 {
174     return addressTableModel;
175 }
176
177 TransactionTableModel *WalletModel::getTransactionTableModel()
178 {
179     return transactionTableModel;
180 }
181
182