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 int WalletModel::getNumTransactions() const
33 int numTransactions = 0;
34 CRITICAL_BLOCK(wallet->cs_mapWallet)
36 numTransactions = wallet->mapWallet.size();
38 return numTransactions;
41 void WalletModel::update()
43 // Plainly emit all signals for now. To be more efficient this should check
44 // whether the values actually changed first, although it'd be even better if these
45 // were events coming in from the bitcoin core.
46 emit balanceChanged(getBalance());
47 emit numTransactionsChanged(getNumTransactions());
49 addressTableModel->update();
52 WalletModel::StatusCode WalletModel::sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs)
57 if(!AddressToHash160(payTo.toUtf8().constData(), hash160))
59 return InvalidAddress;
67 if(payAmount > getBalance())
69 return AmountExceedsBalance;
72 if((payAmount + nTransactionFee) > getBalance())
74 return AmountWithFeeExceedsBalance;
77 CRITICAL_BLOCK(cs_main)
79 // Send to bitcoin address
82 scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
84 std::string strError = wallet->SendMoney(scriptPubKey, payAmount, wtx, true);
89 else if (strError == "ABORTED")
95 emit error(tr("Sending..."), QString::fromStdString(strError));
100 // Add addresses that we've sent to to the address book
101 std::string strAddress = payTo.toStdString();
102 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
104 if (!wallet->mapAddressBook.count(strAddress))
105 wallet->SetAddressBookName(strAddress, addToAddressBookAs.toStdString());
111 OptionsModel *WalletModel::getOptionsModel()
116 AddressTableModel *WalletModel::getAddressTableModel()
118 return addressTableModel;
121 TransactionTableModel *WalletModel::getTransactionTableModel()
123 return transactionTableModel;