Browse Source

Add robustness against null models

miguelfreitas
Wladimir J. van der Laan 13 years ago
parent
commit
dead0ff8cd
  1. 3
      src/qt/aboutdialog.cpp
  2. 25
      src/qt/addressbookpage.cpp
  3. 2
      src/qt/addressbookpage.h
  4. 2
      src/qt/askpassphrasedialog.cpp
  5. 21
      src/qt/bitcoingui.cpp
  6. 6
      src/qt/csvmodelwriter.cpp
  7. 4
      src/qt/editaddressdialog.cpp
  8. 6
      src/qt/overviewpage.cpp
  9. 11
      src/qt/sendcoinsdialog.cpp
  10. 6
      src/qt/sendcoinsentry.cpp
  11. 25
      src/qt/transactionview.cpp

3
src/qt/aboutdialog.cpp

@ -11,9 +11,12 @@ AboutDialog::AboutDialog(QWidget *parent) :
} }
void AboutDialog::setModel(ClientModel *model) void AboutDialog::setModel(ClientModel *model)
{
if(model)
{ {
ui->versionLabel->setText(model->formatFullVersion()); ui->versionLabel->setText(model->formatFullVersion());
} }
}
AboutDialog::~AboutDialog() AboutDialog::~AboutDialog()
{ {

25
src/qt/addressbookpage.cpp

@ -57,6 +57,8 @@ AddressBookPage::~AddressBookPage()
void AddressBookPage::setModel(AddressTableModel *model) void AddressBookPage::setModel(AddressTableModel *model)
{ {
this->model = model; this->model = model;
if(!model)
return;
// Refresh list from core // Refresh list from core
model->updateList(); model->updateList();
@ -96,16 +98,13 @@ void AddressBookPage::setModel(AddressTableModel *model)
selectionChanged(); selectionChanged();
} }
QTableView *AddressBookPage::getCurrentTable()
{
return ui->tableView;
}
void AddressBookPage::on_copyToClipboard_clicked() void AddressBookPage::on_copyToClipboard_clicked()
{ {
// Copy currently selected address to clipboard // Copy currently selected address to clipboard
// (or nothing, if nothing selected) // (or nothing, if nothing selected)
QTableView *table = getCurrentTable(); QTableView *table = ui->tableView;
if(!table->selectionModel())
return;
QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address); QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
foreach (QModelIndex index, indexes) foreach (QModelIndex index, indexes)
@ -117,6 +116,8 @@ void AddressBookPage::on_copyToClipboard_clicked()
void AddressBookPage::on_newAddressButton_clicked() void AddressBookPage::on_newAddressButton_clicked()
{ {
if(!model)
return;
EditAddressDialog dlg( EditAddressDialog dlg(
tab == SendingTab ? tab == SendingTab ?
EditAddressDialog::NewSendingAddress : EditAddressDialog::NewSendingAddress :
@ -139,7 +140,9 @@ void AddressBookPage::on_newAddressButton_clicked()
void AddressBookPage::on_deleteButton_clicked() void AddressBookPage::on_deleteButton_clicked()
{ {
QTableView *table = getCurrentTable(); QTableView *table = ui->tableView;
if(!table->selectionModel())
return;
QModelIndexList indexes = table->selectionModel()->selectedRows(); QModelIndexList indexes = table->selectionModel()->selectedRows();
if(!indexes.isEmpty()) if(!indexes.isEmpty())
{ {
@ -150,7 +153,9 @@ void AddressBookPage::on_deleteButton_clicked()
void AddressBookPage::selectionChanged() void AddressBookPage::selectionChanged()
{ {
// Set button states based on selected tab and selection // Set button states based on selected tab and selection
QTableView *table = getCurrentTable(); QTableView *table = ui->tableView;
if(!table->selectionModel())
return;
if(table->selectionModel()->hasSelection()) if(table->selectionModel()->hasSelection())
{ {
@ -174,12 +179,14 @@ void AddressBookPage::selectionChanged()
void AddressBookPage::done(int retval) void AddressBookPage::done(int retval)
{ {
QTableView *table = ui->tableView;
if(!table->selectionModel() || !table->model())
return;
// When this is a tab/widget and not a model dialog, ignore "done" // When this is a tab/widget and not a model dialog, ignore "done"
if(mode == ForEditing) if(mode == ForEditing)
return; return;
// Figure out which address was selected, and return it // Figure out which address was selected, and return it
QTableView *table = getCurrentTable();
QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address); QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
foreach (QModelIndex index, indexes) foreach (QModelIndex index, indexes)

2
src/qt/addressbookpage.h

@ -47,8 +47,6 @@ private:
QString returnValue; QString returnValue;
QSortFilterProxyModel *proxyModel; QSortFilterProxyModel *proxyModel;
QTableView *getCurrentTable();
private slots: private slots:
void on_deleteButton_clicked(); void on_deleteButton_clicked();
void on_newAddressButton_clicked(); void on_newAddressButton_clicked();

2
src/qt/askpassphrasedialog.cpp

@ -72,6 +72,8 @@ void AskPassphraseDialog::setModel(WalletModel *model)
void AskPassphraseDialog::accept() void AskPassphraseDialog::accept()
{ {
std::string oldpass, newpass1, newpass2; std::string oldpass, newpass1, newpass2;
if(!model)
return;
// TODO: mlock memory / munlock on return so they will not be swapped out, really need "mlockedstring" wrapper class to do this safely // TODO: mlock memory / munlock on return so they will not be swapped out, really need "mlockedstring" wrapper class to do this safely
oldpass.reserve(MAX_PASSPHRASE_SIZE); oldpass.reserve(MAX_PASSPHRASE_SIZE);
newpass1.reserve(MAX_PASSPHRASE_SIZE); newpass1.reserve(MAX_PASSPHRASE_SIZE);

21
src/qt/bitcoingui.cpp

@ -267,7 +267,8 @@ void BitcoinGUI::createToolBars()
void BitcoinGUI::setClientModel(ClientModel *clientModel) void BitcoinGUI::setClientModel(ClientModel *clientModel)
{ {
this->clientModel = clientModel; this->clientModel = clientModel;
if(clientModel)
{
if(clientModel->isTestNet()) if(clientModel->isTestNet())
{ {
QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]"); QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
@ -294,11 +295,13 @@ void BitcoinGUI::setClientModel(ClientModel *clientModel)
// Report errors from network/worker thread // Report errors from network/worker thread
connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString))); connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
} }
}
void BitcoinGUI::setWalletModel(WalletModel *walletModel) void BitcoinGUI::setWalletModel(WalletModel *walletModel)
{ {
this->walletModel = walletModel; this->walletModel = walletModel;
if(walletModel)
{
// Report errors from wallet thread // Report errors from wallet thread
connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString))); connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
@ -320,6 +323,7 @@ void BitcoinGUI::setWalletModel(WalletModel *walletModel)
// Ask for passphrase if needed // Ask for passphrase if needed
connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet())); connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
} }
}
void BitcoinGUI::createTrayIcon() void BitcoinGUI::createTrayIcon()
{ {
@ -369,6 +373,8 @@ void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
void BitcoinGUI::optionsClicked() void BitcoinGUI::optionsClicked()
{ {
if(!clientModel || !clientModel->getOptionsModel())
return;
OptionsDialog dlg; OptionsDialog dlg;
dlg.setModel(clientModel->getOptionsModel()); dlg.setModel(clientModel->getOptionsModel());
dlg.exec(); dlg.exec();
@ -398,6 +404,8 @@ void BitcoinGUI::setNumConnections(int count)
void BitcoinGUI::setNumBlocks(int count) void BitcoinGUI::setNumBlocks(int count)
{ {
if(!clientModel)
return;
int initTotal = clientModel->getNumBlocksAtStartup(); int initTotal = clientModel->getNumBlocksAtStartup();
int total = clientModel->getNumBlocksOfPeers(); int total = clientModel->getNumBlocksOfPeers();
QString tooltip; QString tooltip;
@ -491,6 +499,8 @@ void BitcoinGUI::changeEvent(QEvent *e)
} }
void BitcoinGUI::closeEvent(QCloseEvent *event) void BitcoinGUI::closeEvent(QCloseEvent *event)
{
if(clientModel)
{ {
#ifndef Q_WS_MAC // Ignored on Mac #ifndef Q_WS_MAC // Ignored on Mac
if(!clientModel->getOptionsModel()->getMinimizeToTray() && if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
@ -499,6 +509,7 @@ void BitcoinGUI::closeEvent(QCloseEvent *event)
qApp->quit(); qApp->quit();
} }
#endif #endif
}
QMainWindow::closeEvent(event); QMainWindow::closeEvent(event);
} }
@ -517,6 +528,8 @@ void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end) void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
{ {
if(!walletModel || !clientModel)
return;
TransactionTableModel *ttm = walletModel->getTransactionTableModel(); TransactionTableModel *ttm = walletModel->getTransactionTableModel();
qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent) qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
.data(Qt::EditRole).toULongLong(); .data(Qt::EditRole).toULongLong();
@ -654,6 +667,8 @@ void BitcoinGUI::setEncryptionStatus(int status)
void BitcoinGUI::encryptWallet(bool status) void BitcoinGUI::encryptWallet(bool status)
{ {
if(!walletModel)
return;
AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt: AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
AskPassphraseDialog::Decrypt, this); AskPassphraseDialog::Decrypt, this);
dlg.setModel(walletModel); dlg.setModel(walletModel);
@ -671,6 +686,8 @@ void BitcoinGUI::changePassphrase()
void BitcoinGUI::unlockWallet() void BitcoinGUI::unlockWallet()
{ {
if(!walletModel)
return;
// Unlock wallet when requested by wallet model // Unlock wallet when requested by wallet model
if(walletModel->getEncryptionStatus() == WalletModel::Locked) if(walletModel->getEncryptionStatus() == WalletModel::Locked)
{ {

6
src/qt/csvmodelwriter.cpp

@ -48,7 +48,11 @@ bool CSVModelWriter::write()
return false; return false;
QTextStream out(&file); QTextStream out(&file);
int numRows = model->rowCount(); int numRows = 0;
if(model)
{
numRows = model->rowCount();
}
// Header row // Header row
for(int i=0; i<columns.size(); ++i) for(int i=0; i<columns.size(); ++i)

4
src/qt/editaddressdialog.cpp

@ -56,6 +56,8 @@ void EditAddressDialog::loadRow(int row)
bool EditAddressDialog::saveCurrentRow() bool EditAddressDialog::saveCurrentRow()
{ {
if(!model)
return false;
switch(mode) switch(mode)
{ {
case NewReceivingAddress: case NewReceivingAddress:
@ -78,6 +80,8 @@ bool EditAddressDialog::saveCurrentRow()
void EditAddressDialog::accept() void EditAddressDialog::accept()
{ {
if(!model)
return;
if(!saveCurrentRow()) if(!saveCurrentRow())
{ {
switch(model->getEditStatus()) switch(model->getEditStatus())

6
src/qt/overviewpage.cpp

@ -143,7 +143,8 @@ void OverviewPage::setNumTransactions(int count)
void OverviewPage::setModel(WalletModel *model) void OverviewPage::setModel(WalletModel *model)
{ {
this->model = model; this->model = model;
if(model)
{
// Set up transaction list // Set up transaction list
TransactionFilterProxy *filter = new TransactionFilterProxy(); TransactionFilterProxy *filter = new TransactionFilterProxy();
filter->setSourceModel(model->getTransactionTableModel()); filter->setSourceModel(model->getTransactionTableModel());
@ -164,9 +165,12 @@ void OverviewPage::setModel(WalletModel *model)
connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(displayUnitChanged())); connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(displayUnitChanged()));
} }
}
void OverviewPage::displayUnitChanged() void OverviewPage::displayUnitChanged()
{ {
if(!model || !model->getOptionsModel())
return;
if(currentBalance != -1) if(currentBalance != -1)
setBalance(currentBalance, currentUnconfirmedBalance); setBalance(currentBalance, currentUnconfirmedBalance);

11
src/qt/sendcoinsdialog.cpp

@ -43,10 +43,12 @@ void SendCoinsDialog::setModel(WalletModel *model)
entry->setModel(model); entry->setModel(model);
} }
} }
if(model)
{
setBalance(model->getBalance(), model->getUnconfirmedBalance()); setBalance(model->getBalance(), model->getUnconfirmedBalance());
connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64))); connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
} }
}
SendCoinsDialog::~SendCoinsDialog() SendCoinsDialog::~SendCoinsDialog()
{ {
@ -57,6 +59,10 @@ void SendCoinsDialog::on_sendButton_clicked()
{ {
QList<SendCoinsRecipient> recipients; QList<SendCoinsRecipient> recipients;
bool valid = true; bool valid = true;
if(!model)
return;
for(int i = 0; i < ui->entries->count(); ++i) for(int i = 0; i < ui->entries->count(); ++i)
{ {
SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget()); SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
@ -255,6 +261,9 @@ void SendCoinsDialog::handleURL(const QUrl *url)
void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance) void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
{ {
Q_UNUSED(unconfirmedBalance); Q_UNUSED(unconfirmedBalance);
if(!model || !model->getOptionsModel())
return;
int unit = model->getOptionsModel()->getDisplayUnit(); int unit = model->getOptionsModel()->getDisplayUnit();
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance)); ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
} }

6
src/qt/sendcoinsentry.cpp

@ -44,6 +44,8 @@ void SendCoinsEntry::on_pasteButton_clicked()
void SendCoinsEntry::on_addressBookButton_clicked() void SendCoinsEntry::on_addressBookButton_clicked()
{ {
if(!model)
return;
AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this); AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
dlg.setModel(model->getAddressTableModel()); dlg.setModel(model->getAddressTableModel());
if(dlg.exec()) if(dlg.exec())
@ -55,6 +57,8 @@ void SendCoinsEntry::on_addressBookButton_clicked()
void SendCoinsEntry::on_payTo_textChanged(const QString &address) void SendCoinsEntry::on_payTo_textChanged(const QString &address)
{ {
if(!model)
return;
ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address)); ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address));
} }
@ -74,7 +78,7 @@ void SendCoinsEntry::clear()
ui->addAsLabel->clear(); ui->addAsLabel->clear();
ui->payAmount->clear(); ui->payAmount->clear();
ui->payTo->setFocus(); ui->payTo->setFocus();
if(model) if(model && model->getOptionsModel())
{ {
ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit()); ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
} }

25
src/qt/transactionview.cpp

@ -155,7 +155,8 @@ TransactionView::TransactionView(QWidget *parent) :
void TransactionView::setModel(WalletModel *model) void TransactionView::setModel(WalletModel *model)
{ {
this->model = model; this->model = model;
if(model)
{
transactionProxyModel = new TransactionFilterProxy(this); transactionProxyModel = new TransactionFilterProxy(this);
transactionProxyModel->setSourceModel(model->getTransactionTableModel()); transactionProxyModel->setSourceModel(model->getTransactionTableModel());
transactionProxyModel->setDynamicSortFilter(true); transactionProxyModel->setDynamicSortFilter(true);
@ -180,11 +181,13 @@ void TransactionView::setModel(WalletModel *model)
TransactionTableModel::ToAddress, QHeaderView::Stretch); TransactionTableModel::ToAddress, QHeaderView::Stretch);
transactionView->horizontalHeader()->resizeSection( transactionView->horizontalHeader()->resizeSection(
TransactionTableModel::Amount, 100); TransactionTableModel::Amount, 100);
}
} }
void TransactionView::chooseDate(int idx) void TransactionView::chooseDate(int idx)
{ {
if(!transactionProxyModel)
return;
QDate current = QDate::currentDate(); QDate current = QDate::currentDate();
dateRangeWidget->setVisible(false); dateRangeWidget->setVisible(false);
switch(dateWidget->itemData(idx).toInt()) switch(dateWidget->itemData(idx).toInt())
@ -231,17 +234,23 @@ void TransactionView::chooseDate(int idx)
void TransactionView::chooseType(int idx) void TransactionView::chooseType(int idx)
{ {
if(!transactionProxyModel)
return;
transactionProxyModel->setTypeFilter( transactionProxyModel->setTypeFilter(
typeWidget->itemData(idx).toInt()); typeWidget->itemData(idx).toInt());
} }
void TransactionView::changedPrefix(const QString &prefix) void TransactionView::changedPrefix(const QString &prefix)
{ {
if(!transactionProxyModel)
return;
transactionProxyModel->setAddressPrefix(prefix); transactionProxyModel->setAddressPrefix(prefix);
} }
void TransactionView::changedAmount(const QString &amount) void TransactionView::changedAmount(const QString &amount)
{ {
if(!transactionProxyModel)
return;
qint64 amount_parsed = 0; qint64 amount_parsed = 0;
if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed)) if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
{ {
@ -294,6 +303,8 @@ void TransactionView::contextualMenu(const QPoint &point)
void TransactionView::copyAddress() void TransactionView::copyAddress()
{ {
if(!transactionView->selectionModel())
return;
QModelIndexList selection = transactionView->selectionModel()->selectedRows(); QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty()) if(!selection.isEmpty())
{ {
@ -303,6 +314,8 @@ void TransactionView::copyAddress()
void TransactionView::copyLabel() void TransactionView::copyLabel()
{ {
if(!transactionView->selectionModel())
return;
QModelIndexList selection = transactionView->selectionModel()->selectedRows(); QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty()) if(!selection.isEmpty())
{ {
@ -312,10 +325,14 @@ void TransactionView::copyLabel()
void TransactionView::editLabel() void TransactionView::editLabel()
{ {
if(!transactionView->selectionModel() ||!model)
return;
QModelIndexList selection = transactionView->selectionModel()->selectedRows(); QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty()) if(!selection.isEmpty())
{ {
AddressTableModel *addressBook = model->getAddressTableModel(); AddressTableModel *addressBook = model->getAddressTableModel();
if(!addressBook)
return;
QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString(); QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
if(address.isEmpty()) if(address.isEmpty())
{ {
@ -354,6 +371,8 @@ void TransactionView::editLabel()
void TransactionView::showDetails() void TransactionView::showDetails()
{ {
if(!transactionView->selectionModel())
return;
QModelIndexList selection = transactionView->selectionModel()->selectedRows(); QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty()) if(!selection.isEmpty())
{ {
@ -400,6 +419,8 @@ QWidget *TransactionView::createDateRangeWidget()
void TransactionView::dateRangeChanged() void TransactionView::dateRangeChanged()
{ {
if(!transactionProxyModel)
return;
transactionProxyModel->setDateRange( transactionProxyModel->setDateRange(
QDateTime(dateFrom->date()), QDateTime(dateFrom->date()),
QDateTime(dateTo->date()).addDays(1)); QDateTime(dateTo->date()).addDays(1));

Loading…
Cancel
Save