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 qint64 nTotal=0, nWatchOnly=0;
97 model->getBalance(nTotal, nWatchOnly);
98 setBalance(nTotal, nWatchOnly, model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
99 connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64, qint64)));
100 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
103 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(coinControlUpdateLabels()));
104 connect(model->getOptionsModel(), SIGNAL(coinControlFeaturesChanged(bool)), this, SLOT(coinControlFeatureChanged(bool)));
105 connect(model->getOptionsModel(), SIGNAL(transactionFeeChanged(qint64)), this, SLOT(coinControlUpdateLabels()));
106 ui->frameCoinControl->setVisible(model->getOptionsModel()->getCoinControlFeatures());
107 coinControlUpdateLabels();
111 SendCoinsDialog::~SendCoinsDialog()
116 void SendCoinsDialog::on_sendButton_clicked()
118 QList<SendCoinsRecipient> recipients;
124 if (ui->lineEditCoinControlChange->isEnabled())
126 if(!ui->lineEditCoinControlChange->hasAcceptableInput() ||
127 (model && !model->validateAddress(ui->lineEditCoinControlChange->text())))
129 CoinControlDialog::coinControl->destChange = CNoDestination();
130 ui->lineEditCoinControlChange->setValid(false);
134 CoinControlDialog::coinControl->destChange = CBitcoinAddress(ui->lineEditCoinControlChange->text().toStdString()).Get();
137 for(int i = 0; i < ui->entries->count(); ++i)
139 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
142 if(entry->validate())
144 recipients.append(entry->getValue());
153 if(!valid || recipients.isEmpty())
158 // Format confirmation message
159 QStringList formatted;
160 foreach(const SendCoinsRecipient &rcp, recipients)
162 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
165 fNewRecipientAllowed = false;
167 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
168 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
169 QMessageBox::Yes|QMessageBox::Cancel,
170 QMessageBox::Cancel);
172 if(retval != QMessageBox::Yes)
174 fNewRecipientAllowed = true;
178 WalletModel::UnlockContext ctx(model->requestUnlock());
181 // Unlock wallet was cancelled
182 fNewRecipientAllowed = true;
186 WalletModel::SendCoinsReturn sendstatus;
188 if (!model->getOptionsModel() || !model->getOptionsModel()->getCoinControlFeatures())
189 sendstatus = model->sendCoins(recipients);
191 sendstatus = model->sendCoins(recipients, CoinControlDialog::coinControl);
193 switch(sendstatus.status)
195 case WalletModel::InvalidAddress:
196 QMessageBox::warning(this, tr("Send Coins"),
197 tr("The recipient address is not valid, please recheck."),
198 QMessageBox::Ok, QMessageBox::Ok);
200 case WalletModel::InvalidAmount:
201 QMessageBox::warning(this, tr("Send Coins"),
202 tr("The amount to pay must be larger than 0."),
203 QMessageBox::Ok, QMessageBox::Ok);
205 case WalletModel::AmountExceedsBalance:
206 QMessageBox::warning(this, tr("Send Coins"),
207 tr("The amount exceeds your balance."),
208 QMessageBox::Ok, QMessageBox::Ok);
210 case WalletModel::AmountWithFeeExceedsBalance:
211 QMessageBox::warning(this, tr("Send Coins"),
212 tr("The total exceeds your balance when the %1 transaction fee is included.").
213 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
214 QMessageBox::Ok, QMessageBox::Ok);
216 case WalletModel::DuplicateAddress:
217 QMessageBox::warning(this, tr("Send Coins"),
218 tr("Duplicate address found, can only send to each address once per send operation."),
219 QMessageBox::Ok, QMessageBox::Ok);
221 case WalletModel::TransactionCreationFailed:
222 QMessageBox::warning(this, tr("Send Coins"),
223 tr("Error: Transaction creation failed."),
224 QMessageBox::Ok, QMessageBox::Ok);
226 case WalletModel::TransactionCommitFailed:
227 QMessageBox::warning(this, tr("Send Coins"),
228 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."),
229 QMessageBox::Ok, QMessageBox::Ok);
231 case WalletModel::Aborted: // User aborted, nothing to do
233 case WalletModel::OK:
235 CoinControlDialog::coinControl->UnSelectAll();
236 coinControlUpdateLabels();
239 fNewRecipientAllowed = true;
242 void SendCoinsDialog::clear()
244 // Remove entries until only one left
245 while(ui->entries->count())
247 delete ui->entries->takeAt(0)->widget();
251 updateRemoveEnabled();
253 ui->sendButton->setDefault(true);
256 void SendCoinsDialog::reject()
261 void SendCoinsDialog::accept()
266 SendCoinsEntry *SendCoinsDialog::addEntry()
268 SendCoinsEntry *entry = new SendCoinsEntry(this);
269 entry->setModel(model);
270 ui->entries->addWidget(entry);
271 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
272 connect(entry, SIGNAL(payAmountChanged()), this, SLOT(coinControlUpdateLabels()));
274 updateRemoveEnabled();
276 // Focus the field, so that entry can start immediately
279 ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
280 QCoreApplication::instance()->processEvents();
281 QScrollBar* bar = ui->scrollArea->verticalScrollBar();
283 bar->setSliderPosition(bar->maximum());
287 void SendCoinsDialog::updateRemoveEnabled()
289 // Remove buttons are enabled as soon as there is more than one send-entry
290 bool enabled = (ui->entries->count() > 1);
291 for(int i = 0; i < ui->entries->count(); ++i)
293 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
296 entry->setRemoveEnabled(enabled);
300 coinControlUpdateLabels();
303 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
306 updateRemoveEnabled();
309 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
311 for(int i = 0; i < ui->entries->count(); ++i)
313 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
316 prev = entry->setupTabChain(prev);
319 QWidget::setTabOrder(prev, ui->addButton);
320 QWidget::setTabOrder(ui->addButton, ui->sendButton);
321 return ui->sendButton;
324 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
326 if(!fNewRecipientAllowed)
329 SendCoinsEntry *entry = 0;
330 // Replace the first entry if it is still unused
331 if(ui->entries->count() == 1)
333 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
347 bool SendCoinsDialog::handleURI(const QString &uri)
349 SendCoinsRecipient rv;
350 // URI has to be valid
351 if (GUIUtil::parseBitcoinURI(uri, &rv))
353 CBitcoinAddress address(rv.address.toStdString());
354 if (!address.IsValid())
363 void SendCoinsDialog::setBalance(qint64 total, qint64 watchOnly, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
366 Q_UNUSED(unconfirmedBalance);
367 Q_UNUSED(immatureBalance);
368 if(!model || !model->getOptionsModel())
371 int unit = model->getOptionsModel()->getDisplayUnit();
372 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, total - watchOnly));
375 void SendCoinsDialog::updateDisplayUnit()
377 if(model && model->getOptionsModel())
379 // Update labelBalance with the current balance and the current unit
380 qint64 total=0, watchOnly=0;
381 model->getBalance(total, watchOnly);
382 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), total - watchOnly));
386 // Coin Control: copy label "Quantity" to clipboard
387 void SendCoinsDialog::coinControlClipboardQuantity()
389 QApplication::clipboard()->setText(ui->labelCoinControlQuantity->text());
392 // Coin Control: copy label "Amount" to clipboard
393 void SendCoinsDialog::coinControlClipboardAmount()
395 QApplication::clipboard()->setText(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
398 // Coin Control: copy label "Fee" to clipboard
399 void SendCoinsDialog::coinControlClipboardFee()
401 QApplication::clipboard()->setText(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")));
404 // Coin Control: copy label "After fee" to clipboard
405 void SendCoinsDialog::coinControlClipboardAfterFee()
407 QApplication::clipboard()->setText(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")));
410 // Coin Control: copy label "Bytes" to clipboard
411 void SendCoinsDialog::coinControlClipboardBytes()
413 QApplication::clipboard()->setText(ui->labelCoinControlBytes->text());
416 // Coin Control: copy label "Priority" to clipboard
417 void SendCoinsDialog::coinControlClipboardPriority()
419 QApplication::clipboard()->setText(ui->labelCoinControlPriority->text());
422 // Coin Control: copy label "Low output" to clipboard
423 void SendCoinsDialog::coinControlClipboardLowOutput()
425 QApplication::clipboard()->setText(ui->labelCoinControlLowOutput->text());
428 // Coin Control: copy label "Change" to clipboard
429 void SendCoinsDialog::coinControlClipboardChange()
431 QApplication::clipboard()->setText(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")));
434 // Coin Control: settings menu - coin control enabled/disabled by user
435 void SendCoinsDialog::coinControlFeatureChanged(bool checked)
437 ui->frameCoinControl->setVisible(checked);
439 if (!checked && model) // coin control features disabled
440 CoinControlDialog::coinControl->SetNull();
443 // Coin Control: button inputs -> show actual coin control dialog
444 void SendCoinsDialog::coinControlButtonClicked()
446 CoinControlDialog dlg;
449 coinControlUpdateLabels();
452 // Coin Control: checkbox custom change address
453 void SendCoinsDialog::coinControlChangeChecked(int state)
457 if (state == Qt::Checked)
458 CoinControlDialog::coinControl->destChange = CBitcoinAddress(ui->lineEditCoinControlChange->text().toStdString()).Get();
460 CoinControlDialog::coinControl->destChange = CNoDestination();
463 ui->lineEditCoinControlChange->setEnabled((state == Qt::Checked));
464 // ui->labelCoinControlChangeLabel->setEnabled((state == Qt::Checked));
465 ui->addressBookButton->setEnabled((state == Qt::Checked));
466 ui->pasteButton->setEnabled((state == Qt::Checked));
469 void SendCoinsDialog::on_pasteButton_clicked()
471 // Paste text from clipboard into recipient field
472 ui->lineEditCoinControlChange->setText(QApplication::clipboard()->text());
475 void SendCoinsDialog::on_addressBookButton_clicked()
479 AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
480 dlg.setModel(model->getAddressTableModel());
483 ui->lineEditCoinControlChange->setText(dlg.getReturnValue());
487 // Coin Control: update labels
488 void SendCoinsDialog::coinControlUpdateLabels()
490 if (!model || !model->getOptionsModel() || !model->getOptionsModel()->getCoinControlFeatures())
494 CoinControlDialog::payAmounts.clear();
495 for(int i = 0; i < ui->entries->count(); ++i)
497 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
499 CoinControlDialog::payAmounts.append(entry->getValue().amount);
502 if (CoinControlDialog::coinControl->HasSelected())
504 // actual coin control calculation
505 CoinControlDialog::updateLabels(model, this);
507 // show coin control stats
508 ui->labelCoinControlAutomaticallySelected->hide();
509 ui->widgetCoinControl->show();
513 // hide coin control stats
514 ui->labelCoinControlAutomaticallySelected->show();
515 ui->widgetCoinControl->hide();
516 ui->labelCoinControlInsuffFunds->hide();