1 #include "clientmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
11 ClientModel::ClientModel(CWallet *wallet, QObject *parent) :
12 QObject(parent), wallet(wallet), optionsModel(0), addressTableModel(0),
13 transactionTableModel(0)
15 // Until signal notifications is built into the bitcoin core,
16 // simply update everything after polling using a timer.
17 QTimer *timer = new QTimer(this);
18 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
19 timer->start(MODEL_UPDATE_DELAY);
21 optionsModel = new OptionsModel(wallet, this);
22 addressTableModel = new AddressTableModel(wallet, this);
23 transactionTableModel = new TransactionTableModel(wallet, this);
26 qint64 ClientModel::getBalance() const
28 return wallet->GetBalance();
31 int ClientModel::getNumConnections() const
36 int ClientModel::getNumBlocks() const
41 int ClientModel::getNumTransactions() const
43 int numTransactions = 0;
44 CRITICAL_BLOCK(wallet->cs_mapWallet)
46 numTransactions = wallet->mapWallet.size();
48 return numTransactions;
51 void ClientModel::update()
53 // Plainly emit all signals for now. To be more efficient this should check
54 // whether the values actually changed first, although it'd be even better if these
55 // were events coming in from the bitcoin core.
56 emit balanceChanged(getBalance());
57 emit numConnectionsChanged(getNumConnections());
58 emit numBlocksChanged(getNumBlocks());
59 emit numTransactionsChanged(getNumTransactions());
61 addressTableModel->update();
64 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
69 if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
71 return InvalidAddress;
79 if(payAmount > getBalance())
81 return AmountExceedsBalance;
84 if((payAmount + nTransactionFee) > getBalance())
86 return AmountWithFeeExceedsBalance;
89 CRITICAL_BLOCK(cs_main)
91 // Send to bitcoin address
94 scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
96 std::string strError = wallet->SendMoney(scriptPubKey, payAmount, wtx, true);
101 else if (strError == "ABORTED")
107 emit error(tr("Sending..."), QString::fromStdString(strError));
112 // Add addresses that we've sent to to the address book
113 std::string strAddress = payTo.toStdString();
114 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
116 if (!wallet->mapAddressBook.count(strAddress))
117 wallet->SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
123 bool ClientModel::inInitialBlockDownload() const
125 return IsInitialBlockDownload();
128 int ClientModel::getTotalBlocksEstimate() const
130 return GetTotalBlocksEstimate();
134 OptionsModel *ClientModel::getOptionsModel()
139 AddressTableModel *ClientModel::getAddressTableModel()
141 return addressTableModel;
144 TransactionTableModel *ClientModel::getTransactionTableModel()
146 return transactionTableModel;