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