1 #include "clientmodel.h"
3 #include "guiconstants.h"
4 #include "optionsmodel.h"
5 #include "addresstablemodel.h"
6 #include "transactiontablemodel.h"
10 ClientModel::ClientModel(QObject *parent) :
11 QObject(parent), optionsModel(0), addressTableModel(0),
12 transactionTableModel(0)
14 // Until signal notifications is built into the bitcoin core,
15 // simply update everything after polling using a timer.
16 QTimer *timer = new QTimer(this);
17 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
18 timer->start(MODEL_UPDATE_DELAY);
20 optionsModel = new OptionsModel(this);
21 addressTableModel = new AddressTableModel(this);
22 transactionTableModel = new TransactionTableModel(this);
25 qint64 ClientModel::getBalance() const
30 int ClientModel::getNumConnections() const
35 int ClientModel::getNumBlocks() const
40 int ClientModel::getNumTransactions() const
42 int numTransactions = 0;
43 CRITICAL_BLOCK(cs_mapWallet)
45 numTransactions = mapWallet.size();
47 return numTransactions;
50 void ClientModel::update()
52 // Plainly emit all signals for now. To be more efficient this should check
53 // whether the values actually changed first, although it'd be even better if these
54 // were events coming in from the bitcoin core.
55 emit balanceChanged(getBalance());
56 emit numConnectionsChanged(getNumConnections());
57 emit numBlocksChanged(getNumBlocks());
58 emit numTransactionsChanged(getNumTransactions());
60 addressTableModel->update();
63 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
68 if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
70 return InvalidAddress;
78 if(payAmount > getBalance())
80 return AmountExceedsBalance;
83 if((payAmount + nTransactionFee) > getBalance())
85 return AmountWithFeeExceedsBalance;
88 CRITICAL_BLOCK(cs_main)
90 // Send to bitcoin address
93 scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
95 std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
100 else if (strError == "ABORTED")
106 emit error(tr("Sending..."), QString::fromStdString(strError));
111 // Add addresses that we've sent to to the address book
112 std::string strAddress = payTo.toStdString();
113 CRITICAL_BLOCK(cs_mapAddressBook)
114 if (!mapAddressBook.count(strAddress))
115 SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
120 bool ClientModel::inInitialBlockDownload() const
122 return IsInitialBlockDownload();
125 int ClientModel::getTotalBlocksEstimate() const
127 return GetTotalBlocksEstimate();
131 OptionsModel *ClientModel::getOptionsModel()
136 AddressTableModel *ClientModel::getAddressTableModel()
138 return addressTableModel;
141 TransactionTableModel *ClientModel::getTransactionTableModel()
143 return transactionTableModel;