From 6c83a8419b3dd6aee2900803e2ee6e61781a16f8 Mon Sep 17 00:00:00 2001 From: Scott Ellis Date: Fri, 6 Jul 2012 01:43:28 +1000 Subject: Add a timer to check for changes in immature or unconfirmed balances, when these are non-zero. Fixed a minor mem leak. --- src/qt/clientmodel.cpp | 2 +- src/qt/walletmodel.cpp | 56 ++++++++++++++++++++++++++++++++++++++------------ src/qt/walletmodel.h | 11 ++++++++++ 3 files changed, 55 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index cabbd5d240..8fcc4e650e 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -18,7 +18,7 @@ ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) : { numBlocksAtStartup = -1; - pollTimer = new QTimer(); + pollTimer = new QTimer(this); pollTimer->setInterval(MODEL_UPDATE_DELAY); pollTimer->start(); connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer())); diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 9245f774a4..0111e0cd91 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -10,17 +10,27 @@ #include "base58.h" #include +#include WalletModel::WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent) : QObject(parent), wallet(wallet), optionsModel(optionsModel), addressTableModel(0), transactionTableModel(0), cachedBalance(0), cachedUnconfirmedBalance(0), cachedImmatureBalance(0), cachedNumTransactions(0), - cachedEncryptionStatus(Unencrypted) + cachedEncryptionStatus(Unencrypted), + cachedNumBlocks(0) { addressTableModel = new AddressTableModel(wallet, this); transactionTableModel = new TransactionTableModel(wallet, this); + // This single-shot timer will be fired from the 'checkBalancedChanged' + // method repeatedly while either of the unconfirmed or immature balances + // are non-zero + pollTimer = new QTimer(this); + pollTimer->setInterval(MODEL_UPDATE_DELAY); + pollTimer->setSingleShot(true); + connect(pollTimer, SIGNAL(timeout()), this, SLOT(pollBalanceChanged())); + subscribeToCoreSignals(); } @@ -62,27 +72,47 @@ void WalletModel::updateStatus() emit encryptionStatusChanged(newEncryptionStatus); } -void WalletModel::updateTransaction(const QString &hash, int status) +void WalletModel::pollBalanceChanged() { - if(transactionTableModel) - transactionTableModel->updateTransaction(hash, status); + if(nBestHeight != cachedNumBlocks) { + cachedNumBlocks = nBestHeight; + checkBalanceChanged(); + } - // Balance and number of transactions might have changed + if(cachedUnconfirmedBalance || cachedImmatureBalance) + pollTimer->start(); +} + +void WalletModel::checkBalanceChanged() +{ qint64 newBalance = getBalance(); qint64 newUnconfirmedBalance = getUnconfirmedBalance(); qint64 newImmatureBalance = getImmatureBalance(); - int newNumTransactions = getNumTransactions(); - if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance || cachedImmatureBalance != newImmatureBalance) + if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance || cachedImmatureBalance != newImmatureBalance) { + cachedBalance = newBalance; + cachedUnconfirmedBalance = newUnconfirmedBalance; + cachedImmatureBalance = newImmatureBalance; emit balanceChanged(newBalance, newUnconfirmedBalance, newImmatureBalance); + } +} - if(cachedNumTransactions != newNumTransactions) - emit numTransactionsChanged(newNumTransactions); +void WalletModel::updateTransaction(const QString &hash, int status) +{ + if(transactionTableModel) + transactionTableModel->updateTransaction(hash, status); - cachedBalance = newBalance; - cachedUnconfirmedBalance = newUnconfirmedBalance; - cachedImmatureBalance = newImmatureBalance; - cachedNumTransactions = newNumTransactions; + // Balance and number of transactions might have changed + checkBalanceChanged(); + + if(cachedUnconfirmedBalance || cachedImmatureBalance) + pollTimer->start(); + + int newNumTransactions = getNumTransactions(); + if(cachedNumTransactions != newNumTransactions) { + emit numTransactionsChanged(newNumTransactions); + cachedNumTransactions = newNumTransactions; + } } void WalletModel::updateAddressBook(const QString &address, const QString &label, bool isMine, int status) diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h index c973c5cf53..62558a49df 100644 --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -10,6 +10,10 @@ class AddressTableModel; class TransactionTableModel; class CWallet; +QT_BEGIN_NAMESPACE +class QTimer; +QT_END_NAMESPACE + class SendCoinsRecipient { public: @@ -120,9 +124,14 @@ private: qint64 cachedImmatureBalance; qint64 cachedNumTransactions; EncryptionStatus cachedEncryptionStatus; + int cachedNumBlocks; + + QTimer *pollTimer; void subscribeToCoreSignals(); void unsubscribeFromCoreSignals(); + void checkBalanceChanged(); + signals: // Signal that balance in wallet changed void balanceChanged(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance); @@ -148,6 +157,8 @@ public slots: void updateTransaction(const QString &hash, int status); /* New, updated or removed address book entry */ void updateAddressBook(const QString &address, const QString &label, bool isMine, int status); + /* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */ + void pollBalanceChanged(); }; -- cgit v1.2.3