2011-05-22 17:19:43 +02:00
|
|
|
#include "clientmodel.h"
|
2011-05-28 20:32:19 +02:00
|
|
|
#include "guiconstants.h"
|
2011-05-31 22:24:53 +02:00
|
|
|
#include "optionsmodel.h"
|
2011-06-03 21:03:20 +02:00
|
|
|
#include "addresstablemodel.h"
|
2011-06-05 16:03:29 +02:00
|
|
|
#include "transactiontablemodel.h"
|
2011-05-22 17:19:43 +02:00
|
|
|
|
2011-06-26 19:23:24 +02:00
|
|
|
#include "headers.h"
|
|
|
|
|
2011-05-22 17:19:43 +02:00
|
|
|
#include <QTimer>
|
|
|
|
|
2011-06-26 19:23:24 +02:00
|
|
|
ClientModel::ClientModel(CWallet *wallet, QObject *parent) :
|
|
|
|
QObject(parent), wallet(wallet), optionsModel(0), addressTableModel(0),
|
2011-06-05 16:03:29 +02:00
|
|
|
transactionTableModel(0)
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
2011-06-18 11:53:25 +02:00
|
|
|
// Until signal notifications is built into the bitcoin core,
|
|
|
|
// simply update everything after polling using a timer.
|
2011-05-22 17:19:43 +02:00
|
|
|
QTimer *timer = new QTimer(this);
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
|
|
|
|
timer->start(MODEL_UPDATE_DELAY);
|
2011-05-31 22:24:53 +02:00
|
|
|
|
2011-06-26 19:23:24 +02:00
|
|
|
optionsModel = new OptionsModel(wallet, this);
|
|
|
|
addressTableModel = new AddressTableModel(wallet, this);
|
|
|
|
transactionTableModel = new TransactionTableModel(wallet, this);
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 13:13:48 +02:00
|
|
|
qint64 ClientModel::getBalance() const
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
return wallet->GetBalance();
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 13:13:48 +02:00
|
|
|
int ClientModel::getNumConnections() const
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
|
|
|
return vNodes.size();
|
|
|
|
}
|
|
|
|
|
2011-06-18 13:13:48 +02:00
|
|
|
int ClientModel::getNumBlocks() const
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
|
|
|
return nBestHeight;
|
|
|
|
}
|
|
|
|
|
2011-06-18 13:13:48 +02:00
|
|
|
int ClientModel::getNumTransactions() const
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
2011-05-27 21:24:17 +02:00
|
|
|
int numTransactions = 0;
|
2011-06-26 19:23:24 +02:00
|
|
|
CRITICAL_BLOCK(wallet->cs_mapWallet)
|
2011-05-27 21:24:17 +02:00
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
numTransactions = wallet->mapWallet.size();
|
2011-05-27 21:24:17 +02:00
|
|
|
}
|
|
|
|
return numTransactions;
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientModel::update()
|
|
|
|
{
|
2011-06-18 11:53:25 +02:00
|
|
|
// Plainly emit all signals for now. To be more efficient this should check
|
2011-06-21 20:34:43 +02:00
|
|
|
// whether the values actually changed first, although it'd be even better if these
|
|
|
|
// were events coming in from the bitcoin core.
|
2011-05-22 17:19:43 +02:00
|
|
|
emit balanceChanged(getBalance());
|
|
|
|
emit numConnectionsChanged(getNumConnections());
|
|
|
|
emit numBlocksChanged(getNumBlocks());
|
|
|
|
emit numTransactionsChanged(getNumTransactions());
|
2011-05-30 20:20:12 +02:00
|
|
|
|
2011-06-21 20:34:43 +02:00
|
|
|
addressTableModel->update();
|
2011-06-03 20:48:03 +02:00
|
|
|
}
|
|
|
|
|
2011-06-25 19:32:36 +02:00
|
|
|
ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
|
2011-05-30 20:20:12 +02:00
|
|
|
{
|
|
|
|
uint160 hash160 = 0;
|
|
|
|
bool valid = false;
|
|
|
|
|
|
|
|
if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
|
|
|
|
{
|
|
|
|
return InvalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(payAmount <= 0)
|
|
|
|
{
|
|
|
|
return InvalidAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(payAmount > getBalance())
|
|
|
|
{
|
|
|
|
return AmountExceedsBalance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((payAmount + nTransactionFee) > getBalance())
|
|
|
|
{
|
|
|
|
return AmountWithFeeExceedsBalance;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRITICAL_BLOCK(cs_main)
|
|
|
|
{
|
|
|
|
// Send to bitcoin address
|
|
|
|
CWalletTx wtx;
|
|
|
|
CScript scriptPubKey;
|
|
|
|
scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
|
|
|
|
|
2011-06-26 19:23:24 +02:00
|
|
|
std::string strError = wallet->SendMoney(scriptPubKey, payAmount, wtx, true);
|
2011-05-30 20:20:12 +02:00
|
|
|
if (strError == "")
|
2011-06-07 18:59:01 +02:00
|
|
|
{
|
2011-06-25 19:32:36 +02:00
|
|
|
// OK
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
2011-05-30 20:20:12 +02:00
|
|
|
else if (strError == "ABORTED")
|
2011-06-07 18:59:01 +02:00
|
|
|
{
|
2011-05-30 20:20:12 +02:00
|
|
|
return Aborted;
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
2011-05-30 20:20:12 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
emit error(tr("Sending..."), QString::fromStdString(strError));
|
|
|
|
return MiscError;
|
|
|
|
}
|
|
|
|
}
|
2011-06-25 19:32:36 +02:00
|
|
|
|
2011-06-01 15:33:51 +02:00
|
|
|
// Add addresses that we've sent to to the address book
|
|
|
|
std::string strAddress = payTo.toStdString();
|
2011-06-26 19:23:24 +02:00
|
|
|
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
|
|
|
|
{
|
|
|
|
if (!wallet->mapAddressBook.count(strAddress))
|
|
|
|
wallet->SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
|
|
|
|
}
|
2011-05-30 20:20:12 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-06-18 13:13:48 +02:00
|
|
|
bool ClientModel::inInitialBlockDownload() const
|
|
|
|
{
|
|
|
|
return IsInitialBlockDownload();
|
|
|
|
}
|
|
|
|
|
2011-06-18 21:25:38 +02:00
|
|
|
int ClientModel::getTotalBlocksEstimate() const
|
|
|
|
{
|
|
|
|
return GetTotalBlocksEstimate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-31 22:24:53 +02:00
|
|
|
OptionsModel *ClientModel::getOptionsModel()
|
|
|
|
{
|
2011-06-03 21:03:20 +02:00
|
|
|
return optionsModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddressTableModel *ClientModel::getAddressTableModel()
|
|
|
|
{
|
|
|
|
return addressTableModel;
|
2011-05-31 22:24:53 +02:00
|
|
|
}
|
2011-06-05 16:03:29 +02:00
|
|
|
|
|
|
|
TransactionTableModel *ClientModel::getTransactionTableModel()
|
|
|
|
{
|
|
|
|
return transactionTableModel;
|
|
|
|
}
|