#include "sendcoinsdialog.h"
#include "ui_sendcoinsdialog.h"
-#include "clientmodel.h"
-#include "guiutil.h"
-
-#include "addressbookdialog.h"
+#include "walletmodel.h"
+#include "bitcoinunits.h"
+#include "addressbookpage.h"
#include "optionsmodel.h"
+#include "sendcoinsentry.h"
+#include "guiutil.h"
+#include "askpassphrasedialog.h"
-#include <QApplication>
-#include <QClipboard>
#include <QMessageBox>
#include <QLocale>
-#include <QDebug>
+#include <QTextDocument>
+#include <QScrollBar>
-#include "util.h"
-#include "base58.h"
-
-SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
+SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SendCoinsDialog),
model(0)
{
ui->setupUi(this);
- GUIUtil::setupAddressWidget(ui->payTo, this);
- GUIUtil::setupAmountWidget(ui->payAmount, this);
+#ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
+ ui->addButton->setIcon(QIcon());
+ ui->clearButton->setIcon(QIcon());
+ ui->sendButton->setIcon(QIcon());
+#endif
- /* Set initial address if provided */
- if(!address.isEmpty())
- {
- ui->payTo->setText(address);
- ui->payAmount->setFocus();
- }
+ addEntry();
+
+ connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
+ connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
}
-void SendCoinsDialog::setModel(ClientModel *model)
+void SendCoinsDialog::setModel(WalletModel *model)
{
this->model = model;
+
+ for(int i = 0; i < ui->entries->count(); ++i)
+ {
+ SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
+ if(entry)
+ {
+ entry->setModel(model);
+ }
+ }
+ if(model)
+ {
+ setBalance(model->getBalance(), model->getUnconfirmedBalance());
+ connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
+ }
}
SendCoinsDialog::~SendCoinsDialog()
void SendCoinsDialog::on_sendButton_clicked()
{
- bool valid;
- QString payAmount = ui->payAmount->text();
- qint64 payAmountParsed;
+ QList<SendCoinsRecipient> recipients;
+ bool valid = true;
- valid = ParseMoney(payAmount.toStdString(), payAmountParsed);
+ if(!model)
+ return;
- if(!valid)
+ 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;
+ }
+ }
+ }
+
+ if(!valid || recipients.isEmpty())
+ {
+ return;
+ }
+
+ // Format confirmation message
+ QStringList formatted;
+ foreach(const SendCoinsRecipient &rcp, recipients)
+ {
+ formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
+ }
+
+ QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
+ tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
+ QMessageBox::Yes|QMessageBox::Cancel,
+ QMessageBox::Cancel);
+
+ if(retval != QMessageBox::Yes)
{
- QMessageBox::warning(this, tr("Send Coins"),
- tr("The amount to pay must be a valid number."),
- QMessageBox::Ok, QMessageBox::Ok);
return;
}
- switch(model->sendCoins(ui->payTo->text(), payAmountParsed))
+ WalletModel::UnlockContext ctx(model->requestUnlock());
+ if(!ctx.isValid())
{
- case ClientModel::InvalidAddress:
+ // Unlock wallet was cancelled
+ return;
+ }
+
+ WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
+ switch(sendstatus.status)
+ {
+ case WalletModel::InvalidAddress:
QMessageBox::warning(this, tr("Send Coins"),
tr("The recepient address is not valid, please recheck."),
QMessageBox::Ok, QMessageBox::Ok);
- ui->payTo->setFocus();
break;
- case ClientModel::InvalidAmount:
+ case WalletModel::InvalidAmount:
QMessageBox::warning(this, tr("Send Coins"),
tr("The amount to pay must be larger than 0."),
QMessageBox::Ok, QMessageBox::Ok);
- ui->payAmount->setFocus();
break;
- case ClientModel::AmountExceedsBalance:
+ case WalletModel::AmountExceedsBalance:
QMessageBox::warning(this, tr("Send Coins"),
tr("Amount exceeds your balance"),
QMessageBox::Ok, QMessageBox::Ok);
- ui->payAmount->setFocus();
break;
- case ClientModel::AmountWithFeeExceedsBalance:
+ case WalletModel::AmountWithFeeExceedsBalance:
QMessageBox::warning(this, tr("Send Coins"),
tr("Total exceeds your balance when the %1 transaction fee is included").
- arg(QString::fromStdString(FormatMoney(model->getOptionsModel()->getTransactionFee()))),
+ arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
+ 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 in one send operation"),
QMessageBox::Ok, QMessageBox::Ok);
- ui->payAmount->setFocus();
break;
- case ClientModel::OK:
+ case WalletModel::TransactionCreationFailed:
+ QMessageBox::warning(this, tr("Send Coins"),
+ tr("Error: Transaction creation failed "),
+ 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."),
+ QMessageBox::Ok, QMessageBox::Ok);
+ break;
+ case WalletModel::OK:
accept();
break;
}
}
-void SendCoinsDialog::on_pasteButton_clicked()
+void SendCoinsDialog::clear()
{
- /* Paste text from clipboard into recipient field */
- ui->payTo->setText(QApplication::clipboard()->text());
+ // Remove entries until only one left
+ while(ui->entries->count())
+ {
+ delete ui->entries->takeAt(0)->widget();
+ }
+ addEntry();
+
+ updateRemoveEnabled();
+
+ ui->sendButton->setDefault(true);
}
-void SendCoinsDialog::on_addressBookButton_clicked()
+void SendCoinsDialog::reject()
{
- AddressBookDialog dlg;
- dlg.setModel(model->getAddressTableModel());
- dlg.setTab(AddressBookDialog::SendingTab);
- dlg.exec();
- ui->payTo->setText(dlg.getReturnValue());
+ clear();
}
-void SendCoinsDialog::on_buttonBox_rejected()
+void SendCoinsDialog::accept()
{
- reject();
+ clear();
+}
+
+SendCoinsEntry *SendCoinsDialog::addEntry()
+{
+ 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());
+ QCoreApplication::instance()->processEvents();
+ QScrollBar* bar = ui->scrollArea->verticalScrollBar();
+ if (bar)
+ bar->setSliderPosition(bar->maximum());
+ return entry;
+}
+
+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::pasteEntry(const SendCoinsRecipient &rv)
+{
+ 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);
+}
+
+
+void SendCoinsDialog::handleURI(const QUrl *uri)
+{
+ SendCoinsRecipient rv;
+ if(!GUIUtil::parseBitcoinURI(uri, &rv))
+ {
+ return;
+ }
+ pasteEntry(rv);
+}
+
+void SendCoinsDialog::handleURI(const QString &uri)
+{
+ SendCoinsRecipient rv;
+ if(!GUIUtil::parseBitcoinURI(uri, &rv))
+ {
+ return;
+ }
+ pasteEntry(rv);
+}
+
+void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
+{
+ Q_UNUSED(unconfirmedBalance);
+ if(!model || !model->getOptionsModel())
+ return;
+
+ int unit = model->getOptionsModel()->getDisplayUnit();
+ ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
}