1 #include "sendcoinsdialog.h"
2 #include "ui_sendcoinsdialog.h"
5 #include "walletmodel.h"
6 #include "addresstablemodel.h"
7 #include "addressbookpage.h"
9 #include "bitcoinunits.h"
10 #include "addressbookpage.h"
11 #include "optionsmodel.h"
12 #include "sendcoinsentry.h"
14 #include "askpassphrasedialog.h"
16 #include "coincontrol.h"
17 #include "coincontroldialog.h"
19 #include <QMessageBox>
21 #include <QTextDocument>
25 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
27 ui(new Ui::SendCoinsDialog),
32 #ifdef Q_OS_MAC // Icons on push buttons are very uncommon on Mac
33 ui->addButton->setIcon(QIcon());
34 ui->clearButton->setIcon(QIcon());
35 ui->sendButton->setIcon(QIcon());
38 #if QT_VERSION >= 0x040700
39 /* Do not move this to the XML file, Qt before 4.7 will choke on it */
40 ui->lineEditCoinControlChange->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
45 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
46 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
49 ui->lineEditCoinControlChange->setFont(GUIUtil::bitcoinAddressFont());
50 connect(ui->pushButtonCoinControl, SIGNAL(clicked()), this, SLOT(coinControlButtonClicked()));
51 connect(ui->checkBoxCoinControlChange, SIGNAL(stateChanged(int)), this, SLOT(coinControlChangeChecked(int)));
53 // Coin Control: clipboard actions
54 QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
55 QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
56 QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
57 QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
58 QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
59 QAction *clipboardPriorityAction = new QAction(tr("Copy priority"), this);
60 QAction *clipboardLowOutputAction = new QAction(tr("Copy low output"), this);
61 QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
62 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardQuantity()));
63 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardAmount()));
64 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardFee()));
65 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardAfterFee()));
66 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardBytes()));
67 connect(clipboardPriorityAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardPriority()));
68 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardLowOutput()));
69 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardChange()));
70 ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
71 ui->labelCoinControlAmount->addAction(clipboardAmountAction);
72 ui->labelCoinControlFee->addAction(clipboardFeeAction);
73 ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
74 ui->labelCoinControlBytes->addAction(clipboardBytesAction);
75 ui->labelCoinControlPriority->addAction(clipboardPriorityAction);
76 ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
77 ui->labelCoinControlChange->addAction(clipboardChangeAction);
79 fNewRecipientAllowed = true;
82 void SendCoinsDialog::setModel(WalletModel *model)
86 for(int i = 0; i < ui->entries->count(); ++i)
88 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
91 entry->setModel(model);
94 if(model && model->getOptionsModel())
96 setBalance(model->getBalance(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
97 connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64)));
98 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
101 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(coinControlUpdateLabels()));
102 connect(model->getOptionsModel(), SIGNAL(coinControlFeaturesChanged(bool)), this, SLOT(coinControlFeatureChanged(bool)));
103 connect(model->getOptionsModel(), SIGNAL(transactionFeeChanged(qint64)), this, SLOT(coinControlUpdateLabels()));
104 ui->frameCoinControl->setVisible(model->getOptionsModel()->getCoinControlFeatures());
105 coinControlUpdateLabels();
109 SendCoinsDialog::~SendCoinsDialog()
114 void SendCoinsDialog::on_sendButton_clicked()
116 QList<SendCoinsRecipient> recipients;
122 if (ui->lineEditCoinControlChange->isEnabled())
124 if(!ui->lineEditCoinControlChange->hasAcceptableInput() ||
125 (model && !model->validateAddress(ui->lineEditCoinControlChange->text())))
127 ui->lineEditCoinControlChange->setValid(false);
132 for(int i = 0; i < ui->entries->count(); ++i)
134 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
137 if(entry->validate())
139 recipients.append(entry->getValue());
148 if(!valid || recipients.isEmpty())
153 // Format confirmation message
154 QStringList formatted;
155 foreach(const SendCoinsRecipient &rcp, recipients)
157 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
160 fNewRecipientAllowed = false;
162 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
163 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
164 QMessageBox::Yes|QMessageBox::Cancel,
165 QMessageBox::Cancel);
167 if(retval != QMessageBox::Yes)
169 fNewRecipientAllowed = true;
173 WalletModel::UnlockContext ctx(model->requestUnlock());
176 // Unlock wallet was cancelled
177 fNewRecipientAllowed = true;
181 WalletModel::SendCoinsReturn sendstatus;
183 if (!model->getOptionsModel() || !model->getOptionsModel()->getCoinControlFeatures())
184 sendstatus = model->sendCoins(recipients);
186 sendstatus = model->sendCoins(recipients, CoinControlDialog::coinControl);
188 switch(sendstatus.status)
190 case WalletModel::InvalidAddress:
191 QMessageBox::warning(this, tr("Send Coins"),
192 tr("The recipient address is not valid, please recheck."),
193 QMessageBox::Ok, QMessageBox::Ok);
195 case WalletModel::InvalidAmount:
196 QMessageBox::warning(this, tr("Send Coins"),
197 tr("The amount to pay must be larger than 0."),
198 QMessageBox::Ok, QMessageBox::Ok);
200 case WalletModel::AmountExceedsBalance:
201 QMessageBox::warning(this, tr("Send Coins"),
202 tr("The amount exceeds your balance."),
203 QMessageBox::Ok, QMessageBox::Ok);
205 case WalletModel::AmountWithFeeExceedsBalance:
206 QMessageBox::warning(this, tr("Send Coins"),
207 tr("The total exceeds your balance when the %1 transaction fee is included.").
208 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
209 QMessageBox::Ok, QMessageBox::Ok);
211 case WalletModel::DuplicateAddress:
212 QMessageBox::warning(this, tr("Send Coins"),
213 tr("Duplicate address found, can only send to each address once per send operation."),
214 QMessageBox::Ok, QMessageBox::Ok);
216 case WalletModel::TransactionCreationFailed:
217 QMessageBox::warning(this, tr("Send Coins"),
218 tr("Error: Transaction creation failed."),
219 QMessageBox::Ok, QMessageBox::Ok);
221 case WalletModel::TransactionCommitFailed:
222 QMessageBox::warning(this, tr("Send Coins"),
223 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."),
224 QMessageBox::Ok, QMessageBox::Ok);
226 case WalletModel::Aborted: // User aborted, nothing to do
228 case WalletModel::OK:
230 CoinControlDialog::coinControl->UnSelectAll();
231 coinControlUpdateLabels();
234 fNewRecipientAllowed = true;
237 void SendCoinsDialog::clear()
239 // Remove entries until only one left
240 while(ui->entries->count())
242 delete ui->entries->takeAt(0)->widget();
246 updateRemoveEnabled();
248 ui->sendButton->setDefault(true);
251 void SendCoinsDialog::reject()
256 void SendCoinsDialog::accept()
261 SendCoinsEntry *SendCoinsDialog::addEntry()
263 SendCoinsEntry *entry = new SendCoinsEntry(this);
264 entry->setModel(model);
265 ui->entries->addWidget(entry);
266 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
267 connect(entry, SIGNAL(payAmountChanged()), this, SLOT(coinControlUpdateLabels()));
269 updateRemoveEnabled();
271 // Focus the field, so that entry can start immediately
274 ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
275 QCoreApplication::instance()->processEvents();
276 QScrollBar* bar = ui->scrollArea->verticalScrollBar();
278 bar->setSliderPosition(bar->maximum());
282 void SendCoinsDialog::updateRemoveEnabled()
284 // Remove buttons are enabled as soon as there is more than one send-entry
285 bool enabled = (ui->entries->count() > 1);
286 for(int i = 0; i < ui->entries->count(); ++i)
288 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
291 entry->setRemoveEnabled(enabled);
295 coinControlUpdateLabels();
298 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
301 updateRemoveEnabled();
304 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
306 for(int i = 0; i < ui->entries->count(); ++i)
308 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
311 prev = entry->setupTabChain(prev);
314 QWidget::setTabOrder(prev, ui->addButton);
315 QWidget::setTabOrder(ui->addButton, ui->sendButton);
316 return ui->sendButton;
319 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
321 if(!fNewRecipientAllowed)
324 SendCoinsEntry *entry = 0;
325 // Replace the first entry if it is still unused
326 if(ui->entries->count() == 1)
328 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
342 bool SendCoinsDialog::handleURI(const QString &uri)
344 SendCoinsRecipient rv;
345 // URI has to be valid
346 if (GUIUtil::parseBitcoinURI(uri, &rv))
348 CBitcoinAddress address(rv.address.toStdString());
349 if (!address.IsValid())
358 void SendCoinsDialog::setBalance(qint64 balance, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
361 Q_UNUSED(unconfirmedBalance);
362 Q_UNUSED(immatureBalance);
363 if(!model || !model->getOptionsModel())
366 int unit = model->getOptionsModel()->getDisplayUnit();
367 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
370 void SendCoinsDialog::updateDisplayUnit()
372 if(model && model->getOptionsModel())
374 // Update labelBalance with the current balance and the current unit
375 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), model->getBalance()));
379 // Coin Control: copy label "Quantity" to clipboard
380 void SendCoinsDialog::coinControlClipboardQuantity()
382 QApplication::clipboard()->setText(ui->labelCoinControlQuantity->text());
385 // Coin Control: copy label "Amount" to clipboard
386 void SendCoinsDialog::coinControlClipboardAmount()
388 QApplication::clipboard()->setText(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
391 // Coin Control: copy label "Fee" to clipboard
392 void SendCoinsDialog::coinControlClipboardFee()
394 QApplication::clipboard()->setText(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")));
397 // Coin Control: copy label "After fee" to clipboard
398 void SendCoinsDialog::coinControlClipboardAfterFee()
400 QApplication::clipboard()->setText(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")));
403 // Coin Control: copy label "Bytes" to clipboard
404 void SendCoinsDialog::coinControlClipboardBytes()
406 QApplication::clipboard()->setText(ui->labelCoinControlBytes->text());
409 // Coin Control: copy label "Priority" to clipboard
410 void SendCoinsDialog::coinControlClipboardPriority()
412 QApplication::clipboard()->setText(ui->labelCoinControlPriority->text());
415 // Coin Control: copy label "Low output" to clipboard
416 void SendCoinsDialog::coinControlClipboardLowOutput()
418 QApplication::clipboard()->setText(ui->labelCoinControlLowOutput->text());
421 // Coin Control: copy label "Change" to clipboard
422 void SendCoinsDialog::coinControlClipboardChange()
424 QApplication::clipboard()->setText(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")));
427 // Coin Control: settings menu - coin control enabled/disabled by user
428 void SendCoinsDialog::coinControlFeatureChanged(bool checked)
430 ui->frameCoinControl->setVisible(checked);
432 if (!checked && model) // coin control features disabled
433 CoinControlDialog::coinControl->SetNull();
436 // Coin Control: button inputs -> show actual coin control dialog
437 void SendCoinsDialog::coinControlButtonClicked()
439 CoinControlDialog dlg;
442 coinControlUpdateLabels();
445 // Coin Control: checkbox custom change address
446 void SendCoinsDialog::coinControlChangeChecked(int state)
450 if (state == Qt::Checked)
451 CoinControlDialog::coinControl->destChange = CBitcoinAddress(ui->lineEditCoinControlChange->text().toStdString()).Get();
453 CoinControlDialog::coinControl->destChange = CNoDestination();
456 ui->lineEditCoinControlChange->setEnabled((state == Qt::Checked));
457 // ui->labelCoinControlChangeLabel->setEnabled((state == Qt::Checked));
458 ui->addressBookButton->setEnabled((state == Qt::Checked));
459 ui->pasteButton->setEnabled((state == Qt::Checked));
462 void SendCoinsDialog::on_pasteButton_clicked()
464 // Paste text from clipboard into recipient field
465 ui->lineEditCoinControlChange->setText(QApplication::clipboard()->text());
468 void SendCoinsDialog::on_addressBookButton_clicked()
472 AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
473 dlg.setModel(model->getAddressTableModel());
476 ui->lineEditCoinControlChange->setText(dlg.getReturnValue());
480 // Coin Control: update labels
481 void SendCoinsDialog::coinControlUpdateLabels()
483 if (!model || !model->getOptionsModel() || !model->getOptionsModel()->getCoinControlFeatures())
487 CoinControlDialog::payAmounts.clear();
488 for(int i = 0; i < ui->entries->count(); ++i)
490 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
492 CoinControlDialog::payAmounts.append(entry->getValue().amount);
495 if (CoinControlDialog::coinControl->HasSelected())
497 // actual coin control calculation
498 CoinControlDialog::updateLabels(model, this);
500 // show coin control stats
501 ui->labelCoinControlAutomaticallySelected->hide();
502 ui->widgetCoinControl->show();
506 // hide coin control stats
507 ui->labelCoinControlAutomaticallySelected->show();
508 ui->widgetCoinControl->hide();
509 ui->labelCoinControlInsuffFunds->hide();