1 #include "sendcoinsdialog.h"
2 #include "ui_sendcoinsdialog.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
5 #include "addressbookpage.h"
6 #include "optionsmodel.h"
7 #include "sendcoinsentry.h"
9 #include "askpassphrasedialog.h"
11 #include <QMessageBox>
13 #include <QTextDocument>
15 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
17 ui(new Ui::SendCoinsDialog),
22 #ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
23 ui->addButton->setIcon(QIcon());
24 ui->clearButton->setIcon(QIcon());
25 ui->sendButton->setIcon(QIcon());
30 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
31 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
34 void SendCoinsDialog::setModel(WalletModel *model)
38 for(int i = 0; i < ui->entries->count(); ++i)
40 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
43 entry->setModel(model);
48 setBalance(model->getBalance(), model->getUnconfirmedBalance());
49 connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
53 SendCoinsDialog::~SendCoinsDialog()
58 void SendCoinsDialog::on_sendButton_clicked()
60 QList<SendCoinsRecipient> recipients;
66 for(int i = 0; i < ui->entries->count(); ++i)
68 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
73 recipients.append(entry->getValue());
82 if(!valid || recipients.isEmpty())
87 // Format confirmation message
88 QStringList formatted;
89 foreach(const SendCoinsRecipient &rcp, recipients)
91 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
94 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
95 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
96 QMessageBox::Yes|QMessageBox::Cancel,
99 if(retval != QMessageBox::Yes)
104 WalletModel::UnlockContext ctx(model->requestUnlock());
107 // Unlock wallet was cancelled
111 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
112 switch(sendstatus.status)
114 case WalletModel::InvalidAddress:
115 QMessageBox::warning(this, tr("Send Coins"),
116 tr("The recepient address is not valid, please recheck."),
117 QMessageBox::Ok, QMessageBox::Ok);
119 case WalletModel::InvalidAmount:
120 QMessageBox::warning(this, tr("Send Coins"),
121 tr("The amount to pay must be larger than 0."),
122 QMessageBox::Ok, QMessageBox::Ok);
124 case WalletModel::AmountExceedsBalance:
125 QMessageBox::warning(this, tr("Send Coins"),
126 tr("Amount exceeds your balance"),
127 QMessageBox::Ok, QMessageBox::Ok);
129 case WalletModel::AmountWithFeeExceedsBalance:
130 QMessageBox::warning(this, tr("Send Coins"),
131 tr("Total exceeds your balance when the %1 transaction fee is included").
132 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
133 QMessageBox::Ok, QMessageBox::Ok);
135 case WalletModel::DuplicateAddress:
136 QMessageBox::warning(this, tr("Send Coins"),
137 tr("Duplicate address found, can only send to each address once in one send operation"),
138 QMessageBox::Ok, QMessageBox::Ok);
140 case WalletModel::TransactionCreationFailed:
141 QMessageBox::warning(this, tr("Send Coins"),
142 tr("Error: Transaction creation failed "),
143 QMessageBox::Ok, QMessageBox::Ok);
145 case WalletModel::TransactionCommitFailed:
146 QMessageBox::warning(this, tr("Send Coins"),
147 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."),
148 QMessageBox::Ok, QMessageBox::Ok);
150 case WalletModel::OK:
156 void SendCoinsDialog::clear()
158 // Remove entries until only one left
159 while(ui->entries->count())
161 delete ui->entries->takeAt(0)->widget();
165 updateRemoveEnabled();
167 ui->sendButton->setDefault(true);
170 void SendCoinsDialog::reject()
175 void SendCoinsDialog::accept()
180 SendCoinsEntry *SendCoinsDialog::addEntry()
182 SendCoinsEntry *entry = new SendCoinsEntry(this);
183 entry->setModel(model);
184 ui->entries->addWidget(entry);
185 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
187 updateRemoveEnabled();
189 // Focus the field, so that entry can start immediately
194 void SendCoinsDialog::updateRemoveEnabled()
196 // Remove buttons are enabled as soon as there is more than one send-entry
197 bool enabled = (ui->entries->count() > 1);
198 for(int i = 0; i < ui->entries->count(); ++i)
200 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
203 entry->setRemoveEnabled(enabled);
209 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
212 updateRemoveEnabled();
215 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
217 for(int i = 0; i < ui->entries->count(); ++i)
219 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
222 prev = entry->setupTabChain(prev);
225 QWidget::setTabOrder(prev, ui->addButton);
226 QWidget::setTabOrder(ui->addButton, ui->sendButton);
227 return ui->sendButton;
230 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
232 SendCoinsEntry *entry = 0;
233 // Replace the first entry if it is still unused
234 if(ui->entries->count() == 1)
236 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
251 void SendCoinsDialog::handleURL(const QUrl *url)
253 SendCoinsRecipient rv;
254 if(!GUIUtil::parseBitcoinURL(url, &rv))
261 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
263 Q_UNUSED(unconfirmedBalance);
264 if(!model || !model->getOptionsModel())
267 int unit = model->getOptionsModel()->getDisplayUnit();
268 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));