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 QString ClientModel::getAddress() const
32 std::vector<unsigned char> vchPubKey;
33 if (CWalletDB("r").ReadDefaultKey(vchPubKey))
35 return QString::fromStdString(PubKeyToAddress(vchPubKey));
43 int ClientModel::getNumConnections() const
48 int ClientModel::getNumBlocks() const
53 int ClientModel::getNumTransactions() const
55 int numTransactions = 0;
56 CRITICAL_BLOCK(cs_mapWallet)
58 numTransactions = mapWallet.size();
60 return numTransactions;
63 void ClientModel::update()
65 // Plainly emit all signals for now. To be more efficient this should check
66 // whether the values actually changed first.
67 emit balanceChanged(getBalance());
68 emit addressChanged(getAddress());
69 emit numConnectionsChanged(getNumConnections());
70 emit numBlocksChanged(getNumBlocks());
71 emit numTransactionsChanged(getNumTransactions());
74 void ClientModel::setAddress(const QString &defaultAddress)
77 std::string strAddress = defaultAddress.toStdString();
78 if (!AddressToHash160(strAddress, hash160))
80 if (!mapPubKeys.count(hash160))
82 CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
85 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)
90 if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
92 return InvalidAddress;
100 if(payAmount > getBalance())
102 return AmountExceedsBalance;
105 if((payAmount + nTransactionFee) > getBalance())
107 return AmountWithFeeExceedsBalance;
110 CRITICAL_BLOCK(cs_main)
112 // Send to bitcoin address
114 CScript scriptPubKey;
115 scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
117 std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
122 else if (strError == "ABORTED")
128 emit error(tr("Sending..."), QString::fromStdString(strError));
132 // Add addresses that we've sent to to the address book
133 std::string strAddress = payTo.toStdString();
134 CRITICAL_BLOCK(cs_mapAddressBook)
135 if (!mapAddressBook.count(strAddress))
136 SetAddressBookName(strAddress, "");
141 bool ClientModel::inInitialBlockDownload() const
143 return IsInitialBlockDownload();
146 int ClientModel::getTotalBlocksEstimate() const
148 return GetTotalBlocksEstimate();
152 OptionsModel *ClientModel::getOptionsModel()
157 AddressTableModel *ClientModel::getAddressTableModel()
159 return addressTableModel;
162 TransactionTableModel *ClientModel::getTransactionTableModel()
164 return transactionTableModel;