dash/src/qt/sendcoinsdialog.cpp

352 lines
9.9 KiB
C++
Raw Normal View History

2011-05-12 14:49:42 +02:00
#include "sendcoinsdialog.h"
2011-05-12 14:44:52 +02:00
#include "ui_sendcoinsdialog.h"
#include "walletmodel.h"
#include "bitcoinunits.h"
2011-07-07 17:33:15 +02:00
#include "addressbookpage.h"
2011-06-01 09:34:12 +02:00
#include "optionsmodel.h"
2011-07-16 19:01:05 +02:00
#include "sendcoinsentry.h"
#include "guiutil.h"
#include "askpassphrasedialog.h"
#include "base58.h"
2011-05-12 17:55:24 +02:00
#include <QMessageBox>
2011-07-31 12:56:46 +02:00
#include <QTextDocument>
#include <QScrollBar>
2011-05-12 20:16:42 +02:00
2011-07-16 19:01:05 +02:00
SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
2011-05-12 14:44:52 +02:00
QDialog(parent),
2011-05-30 20:20:12 +02:00
ui(new Ui::SendCoinsDialog),
model(0)
2011-05-12 14:44:52 +02:00
{
ui->setupUi(this);
2011-07-08 19:25:35 +02:00
#ifdef Q_OS_MAC // Icons on push buttons are very uncommon on Mac
ui->addButton->setIcon(QIcon());
ui->clearButton->setIcon(QIcon());
ui->sendButton->setIcon(QIcon());
#endif
2011-07-16 19:01:05 +02:00
addEntry();
2011-07-16 19:01:05 +02:00
connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
fNewRecipientAllowed = true;
2011-05-12 14:44:52 +02:00
}
void SendCoinsDialog::setModel(WalletModel *model)
2011-05-30 20:20:12 +02:00
{
this->model = model;
2011-07-16 19:01:05 +02:00
if(model && model->getOptionsModel())
2011-07-16 19:01:05 +02:00
{
for(int i = 0; i < ui->entries->count(); ++i)
2011-07-16 19:01:05 +02:00
{
SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
if(entry)
{
entry->setModel(model);
}
2011-07-16 19:01:05 +02:00
}
setBalance(model->getBalance(), model->getUnconfirmedBalance(), model->getImmatureBalance());
connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64)));
connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
2011-11-08 21:18:36 +01:00
}
2011-05-30 20:20:12 +02:00
}
2011-05-12 14:44:52 +02:00
SendCoinsDialog::~SendCoinsDialog()
{
delete ui;
}
2011-05-12 17:55:24 +02:00
void SendCoinsDialog::on_sendButton_clicked()
{
if(!model || !model->getOptionsModel())
return;
2011-07-16 19:01:05 +02:00
QList<SendCoinsRecipient> recipients;
bool valid = true;
2011-11-08 21:18:36 +01:00
2011-07-16 19:01:05 +02:00
for(int i = 0; i < ui->entries->count(); ++i)
{
SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
if(entry)
{
if(entry->validate())
{
recipients.append(entry->getValue());
}
else
{
valid = false;
}
}
}
2011-05-30 20:20:12 +02:00
2011-07-16 19:01:05 +02:00
if(!valid || recipients.isEmpty())
2011-05-14 17:25:05 +02:00
{
return;
2011-05-14 17:25:05 +02:00
}
2011-07-16 19:01:05 +02:00
// Format confirmation message
QStringList formatted;
foreach(const SendCoinsRecipient &rcp, recipients)
{
QString amount = BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), rcp.amount);
if (rcp.authenticatedMerchant.isEmpty())
{
QString address = rcp.address;
#if QT_VERSION < 0x050000
QString to = Qt::escape(rcp.label);
#else
QString to = rcp.label.toHtmlEscaped();
#endif
formatted.append(tr("<b>%1</b> to %2 (%3)").arg(amount, to, address));
}
else
{
#if QT_VERSION < 0x050000
QString merchant = Qt::escape(rcp.authenticatedMerchant);
#else
QString merchant = rcp.authenticatedMerchant.toHtmlEscaped();
#endif
formatted.append(tr("<b>%1</b> to %2").arg(amount, merchant));
}
2011-07-16 19:01:05 +02:00
}
fNewRecipientAllowed = false;
2011-07-07 17:33:15 +02:00
QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
2011-07-16 19:01:05 +02:00
tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
2011-07-07 17:33:15 +02:00
QMessageBox::Yes|QMessageBox::Cancel,
QMessageBox::Cancel);
if(retval != QMessageBox::Yes)
{
fNewRecipientAllowed = true;
2011-07-07 17:33:15 +02:00
return;
}
WalletModel::UnlockContext ctx(model->requestUnlock());
if(!ctx.isValid())
{
// Unlock wallet was cancelled
fNewRecipientAllowed = true;
return;
}
2011-07-16 19:01:05 +02:00
WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
switch(sendstatus.status)
2011-05-14 17:25:05 +02:00
{
case WalletModel::InvalidAddress:
2011-05-30 20:20:12 +02:00
QMessageBox::warning(this, tr("Send Coins"),
2012-07-26 04:07:43 +02:00
tr("The recipient address is not valid, please recheck."),
2011-05-30 20:20:12 +02:00
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::InvalidAmount:
2011-05-30 20:20:12 +02:00
QMessageBox::warning(this, tr("Send Coins"),
tr("The amount to pay must be larger than 0."),
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::AmountExceedsBalance:
2011-05-30 20:20:12 +02:00
QMessageBox::warning(this, tr("Send Coins"),
tr("The amount exceeds your balance."),
2011-05-30 20:20:12 +02:00
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::AmountWithFeeExceedsBalance:
2011-05-30 20:20:12 +02:00
QMessageBox::warning(this, tr("Send Coins"),
tr("The total exceeds your balance when the %1 transaction fee is included.").
arg(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), sendstatus.fee)),
2011-07-16 19:01:05 +02:00
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::DuplicateAddress:
QMessageBox::warning(this, tr("Send Coins"),
tr("Duplicate address found, can only send to each address once per send operation."),
2011-07-16 19:01:05 +02:00
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::TransactionCreationFailed:
QMessageBox::warning(this, tr("Send Coins"),
tr("Error: Transaction creation failed!"),
2011-07-16 19:01:05 +02:00
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::TransactionCommitFailed:
QMessageBox::warning(this, tr("Send Coins"),
tr("Error: The transaction was rejected. This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here."),
2011-05-30 20:20:12 +02:00
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::Aborted: // User aborted, nothing to do
break;
case WalletModel::OK:
accept();
break;
2011-05-14 17:25:05 +02:00
}
fNewRecipientAllowed = true;
2011-05-12 17:55:24 +02:00
}
2011-07-16 19:01:05 +02:00
void SendCoinsDialog::clear()
2011-05-12 17:55:24 +02:00
{
2011-07-16 19:01:05 +02:00
// Remove entries until only one left
while(ui->entries->count())
2011-07-16 19:01:05 +02:00
{
delete ui->entries->takeAt(0)->widget();
}
addEntry();
2011-05-13 22:00:27 +02:00
2011-07-16 19:01:05 +02:00
updateRemoveEnabled();
2011-07-07 17:33:15 +02:00
ui->sendButton->setDefault(true);
2011-07-07 17:33:15 +02:00
}
void SendCoinsDialog::reject()
{
clear();
}
void SendCoinsDialog::accept()
{
clear();
}
2011-07-16 19:01:05 +02:00
SendCoinsEntry *SendCoinsDialog::addEntry()
2011-07-16 19:01:05 +02:00
{
SendCoinsEntry *entry = new SendCoinsEntry(this);
entry->setModel(model);
ui->entries->addWidget(entry);
connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
updateRemoveEnabled();
// Focus the field, so that entry can start immediately
entry->clear();
entry->setFocus();
ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
qApp->processEvents();
QScrollBar* bar = ui->scrollArea->verticalScrollBar();
if(bar)
bar->setSliderPosition(bar->maximum());
return entry;
2011-07-16 19:01:05 +02:00
}
void SendCoinsDialog::updateRemoveEnabled()
{
// Remove buttons are enabled as soon as there is more than one send-entry
bool enabled = (ui->entries->count() > 1);
for(int i = 0; i < ui->entries->count(); ++i)
{
SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
if(entry)
{
entry->setRemoveEnabled(enabled);
}
}
setupTabChain(0);
}
void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
{
delete entry;
updateRemoveEnabled();
}
QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
{
for(int i = 0; i < ui->entries->count(); ++i)
{
SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
if(entry)
{
prev = entry->setupTabChain(prev);
}
}
QWidget::setTabOrder(prev, ui->addButton);
QWidget::setTabOrder(ui->addButton, ui->sendButton);
return ui->sendButton;
}
void SendCoinsDialog::setAddress(const QString &address)
{
SendCoinsEntry *entry = 0;
// Replace the first entry if it is still unused
if(ui->entries->count() == 1)
{
SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
if(first->isClear())
{
entry = first;
}
}
if(!entry)
{
entry = addEntry();
}
entry->setAddress(address);
}
void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
{
if(!fNewRecipientAllowed)
return;
SendCoinsEntry *entry = 0;
// Replace the first entry if it is still unused
if(ui->entries->count() == 1)
{
SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
if(first->isClear())
{
entry = first;
}
}
if(!entry)
{
entry = addEntry();
}
entry->setValue(rv);
}
bool SendCoinsDialog::handlePaymentRequest(const SendCoinsRecipient &rv)
{
if (!rv.authenticatedMerchant.isEmpty()) {
// Expired payment request?
const payments::PaymentDetails& details = rv.paymentRequest.getDetails();
if (details.has_expires() && (int64)details.expires() < GetTime())
{
QMessageBox::warning(this, tr("Send Coins"),
tr("Payment request expired"));
return false;
}
}
else {
CBitcoinAddress address(rv.address.toStdString());
if (!address.IsValid()) {
QString strAddress(address.ToString().c_str());
QMessageBox::warning(this, tr("Send Coins"),
tr("Invalid payment address %1").arg(strAddress));
return false;
}
}
pasteEntry(rv);
return true;
}
void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance, qint64 immatureBalance)
{
Q_UNUSED(unconfirmedBalance);
Q_UNUSED(immatureBalance);
2011-11-08 21:18:36 +01:00
if(model && model->getOptionsModel())
{
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), balance));
}
}
void SendCoinsDialog::updateDisplayUnit()
{
setBalance(model->getBalance(), 0, 0);
}