Changeset View
Changeset View
Standalone View
Standalone View
src/qt/walletmodel.cpp
Show All 25 Lines | |||||
#include <QTimer> | #include <QTimer> | ||||
#include <cstdint> | #include <cstdint> | ||||
WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, | WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, | ||||
ClientModel &client_model, | ClientModel &client_model, | ||||
const PlatformStyle *platformStyle, QObject *parent) | const PlatformStyle *platformStyle, QObject *parent) | ||||
: QObject(parent), m_wallet(std::move(wallet)), | : QObject(parent), m_wallet(std::move(wallet)), | ||||
m_client_model(client_model), m_node(client_model.node()), | m_client_model(&client_model), m_node(client_model.node()), | ||||
optionsModel(client_model.getOptionsModel()), addressTableModel(nullptr), | optionsModel(client_model.getOptionsModel()), addressTableModel(nullptr), | ||||
transactionTableModel(nullptr), recentRequestsTableModel(nullptr), | transactionTableModel(nullptr), recentRequestsTableModel(nullptr), | ||||
cachedEncryptionStatus(Unencrypted), cachedNumBlocks(0) { | cachedEncryptionStatus(Unencrypted), cachedNumBlocks(0), | ||||
timer(new QTimer(this)) { | |||||
fHaveWatchOnly = m_wallet->haveWatchOnly(); | fHaveWatchOnly = m_wallet->haveWatchOnly(); | ||||
addressTableModel = new AddressTableModel(this); | addressTableModel = new AddressTableModel(this); | ||||
transactionTableModel = new TransactionTableModel(platformStyle, this); | transactionTableModel = new TransactionTableModel(platformStyle, this); | ||||
recentRequestsTableModel = new RecentRequestsTableModel(this); | recentRequestsTableModel = new RecentRequestsTableModel(this); | ||||
subscribeToCoreSignals(); | subscribeToCoreSignals(); | ||||
} | } | ||||
WalletModel::~WalletModel() { | WalletModel::~WalletModel() { | ||||
unsubscribeFromCoreSignals(); | unsubscribeFromCoreSignals(); | ||||
} | } | ||||
void WalletModel::startPollBalance() { | void WalletModel::startPollBalance() { | ||||
// This timer will be fired repeatedly to update the balance | // This timer will be fired repeatedly to update the balance | ||||
QTimer *timer = new QTimer(this); | |||||
connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged); | connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged); | ||||
timer->start(MODEL_UPDATE_DELAY); | timer->start(MODEL_UPDATE_DELAY); | ||||
} | } | ||||
void WalletModel::setClientModel(ClientModel *client_model) { | |||||
m_client_model = client_model; | |||||
if (!m_client_model) { | |||||
timer->stop(); | |||||
} | |||||
} | |||||
void WalletModel::updateStatus() { | void WalletModel::updateStatus() { | ||||
EncryptionStatus newEncryptionStatus = getEncryptionStatus(); | EncryptionStatus newEncryptionStatus = getEncryptionStatus(); | ||||
if (cachedEncryptionStatus != newEncryptionStatus) { | if (cachedEncryptionStatus != newEncryptionStatus) { | ||||
Q_EMIT encryptionStatusChanged(); | Q_EMIT encryptionStatusChanged(); | ||||
} | } | ||||
} | } | ||||
void WalletModel::pollBalanceChanged() { | void WalletModel::pollBalanceChanged() { | ||||
// Avoid recomputing wallet balances unless a TransactionChanged or | |||||
// BlockTip notification was received. | |||||
if (!fForceCheckBalanceChanged && | |||||
cachedNumBlocks == m_client_model->getNumBlocks()) { | |||||
return; | |||||
} | |||||
// Try to get balances and return early if locks can't be acquired. This | // Try to get balances and return early if locks can't be acquired. This | ||||
// avoids the GUI from getting stuck on periodical polls if the core is | // avoids the GUI from getting stuck on periodical polls if the core is | ||||
// holding the locks for a longer time - for example, during a wallet | // holding the locks for a longer time - for example, during a wallet | ||||
// rescan. | // rescan. | ||||
interfaces::WalletBalances new_balances; | interfaces::WalletBalances new_balances; | ||||
int numBlocks = -1; | int numBlocks = -1; | ||||
if (!m_wallet->tryGetBalances(new_balances, numBlocks, | if (!m_wallet->tryGetBalances(new_balances, numBlocks)) { | ||||
fForceCheckBalanceChanged, cachedNumBlocks)) { | |||||
return; | return; | ||||
} | } | ||||
if (fForceCheckBalanceChanged || numBlocks != cachedNumBlocks) { | |||||
fForceCheckBalanceChanged = false; | fForceCheckBalanceChanged = false; | ||||
// Balance and number of transactions might have changed | // Balance and number of transactions might have changed | ||||
cachedNumBlocks = numBlocks; | cachedNumBlocks = numBlocks; | ||||
checkBalanceChanged(new_balances); | checkBalanceChanged(new_balances); | ||||
if (transactionTableModel) { | if (transactionTableModel) { | ||||
transactionTableModel->updateConfirmations(); | transactionTableModel->updateConfirmations(); | ||||
} | } | ||||
} | } | ||||
} | |||||
void WalletModel::checkBalanceChanged( | void WalletModel::checkBalanceChanged( | ||||
const interfaces::WalletBalances &new_balances) { | const interfaces::WalletBalances &new_balances) { | ||||
if (new_balances.balanceChanged(m_cached_balances)) { | if (new_balances.balanceChanged(m_cached_balances)) { | ||||
m_cached_balances = new_balances; | m_cached_balances = new_balances; | ||||
Q_EMIT balanceChanged(new_balances); | Q_EMIT balanceChanged(new_balances); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 425 Lines • Show Last 20 Lines |