2011-05-22 17:19:43 +02:00
|
|
|
#include "clientmodel.h"
|
|
|
|
#include "main.h"
|
2011-05-28 20:32:19 +02:00
|
|
|
#include "guiconstants.h"
|
2011-05-31 22:24:53 +02:00
|
|
|
#include "optionsmodel.h"
|
2011-05-22 17:19:43 +02:00
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
ClientModel::ClientModel(QObject *parent) :
|
2011-05-31 22:24:53 +02:00
|
|
|
QObject(parent), options_model(0)
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
|
|
|
/* Until we build signal notifications into the bitcoin core,
|
|
|
|
simply update everything using a timer.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
options_model = new OptionsModel(this);
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 21:24:17 +02:00
|
|
|
qint64 ClientModel::getBalance()
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
|
|
|
return GetBalance();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ClientModel::getAddress()
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> vchPubKey;
|
|
|
|
if (CWalletDB("r").ReadDefaultKey(vchPubKey))
|
|
|
|
{
|
|
|
|
return QString::fromStdString(PubKeyToAddress(vchPubKey));
|
|
|
|
} else {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ClientModel::getNumConnections()
|
|
|
|
{
|
|
|
|
return vNodes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ClientModel::getNumBlocks()
|
|
|
|
{
|
|
|
|
return nBestHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ClientModel::getNumTransactions()
|
|
|
|
{
|
2011-05-27 21:24:17 +02:00
|
|
|
int numTransactions = 0;
|
|
|
|
CRITICAL_BLOCK(cs_mapWallet)
|
|
|
|
{
|
|
|
|
numTransactions = mapWallet.size();
|
|
|
|
}
|
|
|
|
return numTransactions;
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientModel::update()
|
|
|
|
{
|
|
|
|
emit balanceChanged(getBalance());
|
|
|
|
emit addressChanged(getAddress());
|
|
|
|
emit numConnectionsChanged(getNumConnections());
|
|
|
|
emit numBlocksChanged(getNumBlocks());
|
|
|
|
emit numTransactionsChanged(getNumTransactions());
|
|
|
|
}
|
2011-05-30 20:20:12 +02:00
|
|
|
|
2011-06-03 20:48:03 +02:00
|
|
|
void ClientModel::setAddress(const QString &defaultAddress)
|
|
|
|
{
|
|
|
|
uint160 hash160;
|
|
|
|
std::string strAddress = defaultAddress.toStdString();
|
|
|
|
if (!AddressToHash160(strAddress, hash160))
|
|
|
|
return;
|
|
|
|
if (!mapPubKeys.count(hash160))
|
|
|
|
return;
|
|
|
|
CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
|
|
|
|
}
|
|
|
|
|
2011-05-30 20:20:12 +02:00
|
|
|
ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
std::string strError = SendMoney(scriptPubKey, payAmount, wtx, true);
|
|
|
|
if (strError == "")
|
|
|
|
return OK;
|
|
|
|
else if (strError == "ABORTED")
|
|
|
|
return Aborted;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit error(tr("Sending..."), QString::fromStdString(strError));
|
|
|
|
return MiscError;
|
|
|
|
}
|
|
|
|
}
|
2011-06-01 15:33:51 +02:00
|
|
|
// Add addresses that we've sent to to the address book
|
|
|
|
std::string strAddress = payTo.toStdString();
|
|
|
|
CRITICAL_BLOCK(cs_mapAddressBook)
|
|
|
|
if (!mapAddressBook.count(strAddress))
|
|
|
|
SetAddressBookName(strAddress, "");
|
2011-05-30 20:20:12 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-05-31 22:24:53 +02:00
|
|
|
OptionsModel *ClientModel::getOptionsModel()
|
|
|
|
{
|
|
|
|
return options_model;
|
|
|
|
}
|