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.
17 QTimer *timer = new QTimer(this);
18 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
19 timer->start(MODEL_UPDATE_DELAY);
21 optionsModel = new OptionsModel(this);
22 addressTableModel = new AddressTableModel(this);
23 transactionTableModel = new TransactionTableModel(this);
26 qint64 ClientModel::getBalance()
31 QString ClientModel::getAddress()
33 std::vector<unsigned char> vchPubKey;
34 if (CWalletDB("r").ReadDefaultKey(vchPubKey))
36 return QString::fromStdString(PubKeyToAddress(vchPubKey));
44 int ClientModel::getNumConnections()
49 int ClientModel::getNumBlocks()
54 int ClientModel::getNumTransactions()
56 int numTransactions = 0;
57 CRITICAL_BLOCK(cs_mapWallet)
59 numTransactions = mapWallet.size();
61 return numTransactions;
64 void ClientModel::update()
66 /* Plainly emit all signals for now. To be precise this should check
67 wether the values actually changed first.
69 emit balanceChanged(getBalance());
70 emit addressChanged(getAddress());
71 emit numConnectionsChanged(getNumConnections());
72 emit numBlocksChanged(getNumBlocks());
73 emit numTransactionsChanged(getNumTransactions());
76 void ClientModel::setAddress(const QString &defaultAddress)
79 std::string strAddress = defaultAddress.toStdString();
80 if (!AddressToHash160(strAddress, hash160))
82 if (!mapPubKeys.count(hash160))
84 CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
87 ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)
92 if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
94 return InvalidAddress;
102 if(payAmount > getBalance())
104 return AmountExceedsBalance;
107 if((payAmount + nTransactionFee) > getBalance())
109 return AmountWithFeeExceedsBalance;
112 CRITICAL_BLOCK(cs_main)
114 // Send to bitcoin address
116 CScript scriptPubKey;
117 scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
119 std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
124 else if (strError == "ABORTED")
130 emit error(tr("Sending..."), QString::fromStdString(strError));
134 // Add addresses that we've sent to to the address book
135 std::string strAddress = payTo.toStdString();
136 CRITICAL_BLOCK(cs_mapAddressBook)
137 if (!mapAddressBook.count(strAddress))
138 SetAddressBookName(strAddress, "");
143 OptionsModel *ClientModel::getOptionsModel()
148 AddressTableModel *ClientModel::getAddressTableModel()
150 return addressTableModel;
153 TransactionTableModel *ClientModel::getTransactionTableModel()
155 return transactionTableModel;