1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
11 WalletModel::WalletModel(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 WalletModel::getBalance() const
28 return wallet->GetBalance();
31 qint64 WalletModel::getUnconfirmedBalance() const
33 return wallet->GetUnconfirmedBalance();
36 int WalletModel::getNumTransactions() const
38 int numTransactions = 0;
39 CRITICAL_BLOCK(wallet->cs_mapWallet)
41 numTransactions = wallet->mapWallet.size();
43 return numTransactions;
46 void WalletModel::update()
48 // Plainly emit all signals for now. To be more efficient this should check
49 // whether the values actually changed first, although it'd be even better if these
50 // were events coming in from the bitcoin core.
51 emit balanceChanged(getBalance());
52 emit numTransactionsChanged(getNumTransactions());
54 addressTableModel->update();
57 WalletModel::StatusCode WalletModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
62 if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
64 return InvalidAddress;
72 if(payAmount > getBalance())
74 return AmountExceedsBalance;
77 if((payAmount + nTransactionFee) > getBalance())
79 return AmountWithFeeExceedsBalance;
82 CRITICAL_BLOCK(cs_main)
84 // Send to bitcoin address
87 scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
89 std::string strError = wallet->SendMoney(scriptPubKey, payAmount, wtx, true);
94 else if (strError == "ABORTED")
100 emit error(tr("Sending..."), QString::fromStdString(strError));
105 // Add addresses that we've sent to to the address book
106 std::string strAddress = payTo.toStdString();
107 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
109 if (!wallet->mapAddressBook.count(strAddress))
110 wallet->SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
116 OptionsModel *WalletModel::getOptionsModel()
121 AddressTableModel *WalletModel::getAddressTableModel()
123 return addressTableModel;
126 TransactionTableModel *WalletModel::getTransactionTableModel()
128 return transactionTableModel;