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)));
52 connect(ui->lineEditCoinControlChange, SIGNAL(textEdited(const QString &)), this, SLOT(coinControlChangeEdited(const QString &)));
54 // Coin Control: clipboard actions
55 QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
56 QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
57 QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
58 QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
59 QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
60 QAction *clipboardPriorityAction = new QAction(tr("Copy priority"), this);
61 QAction *clipboardLowOutputAction = new QAction(tr("Copy low output"), this);
62 QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
63 connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardQuantity()));
64 connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardAmount()));
65 connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardFee()));
66 connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardAfterFee()));
67 connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardBytes()));
68 connect(clipboardPriorityAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardPriority()));
69 connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardLowOutput()));
70 connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(coinControlClipboardChange()));
71 ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
72 ui->labelCoinControlAmount->addAction(clipboardAmountAction);
73 ui->labelCoinControlFee->addAction(clipboardFeeAction);
74 ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
75 ui->labelCoinControlBytes->addAction(clipboardBytesAction);
76 ui->labelCoinControlPriority->addAction(clipboardPriorityAction);
77 ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
78 ui->labelCoinControlChange->addAction(clipboardChangeAction);
80 fNewRecipientAllowed = true;
83 void SendCoinsDialog::setModel(WalletModel *model)
87 for(int i = 0; i < ui->entries->count(); ++i)
89 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
92 entry->setModel(model);
95 if(model && model->getOptionsModel())
97 setBalance(model->getBalance(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
98 connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64)));
99 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
102 connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(coinControlUpdateLabels()));
103 connect(model->getOptionsModel(), SIGNAL(coinControlFeaturesChanged(bool)), this, SLOT(coinControlFeatureChanged(bool)));
104 connect(model->getOptionsModel(), SIGNAL(transactionFeeChanged(qint64)), this, SLOT(coinControlUpdateLabels()));
105 ui->frameCoinControl->setVisible(model->getOptionsModel()->getCoinControlFeatures());
106 coinControlUpdateLabels();
110 SendCoinsDialog::~SendCoinsDialog()
115 void SendCoinsDialog::on_sendButton_clicked()
117 QList<SendCoinsRecipient> recipients;
123 for(int i = 0; i < ui->entries->count(); ++i)
125 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
128 if(entry->validate())
130 recipients.append(entry->getValue());
139 if(!valid || recipients.isEmpty())
144 // Format confirmation message
145 QStringList formatted;
146 foreach(const SendCoinsRecipient &rcp, recipients)
148 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
151 fNewRecipientAllowed = false;
153 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
154 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
155 QMessageBox::Yes|QMessageBox::Cancel,
156 QMessageBox::Cancel);
158 if(retval != QMessageBox::Yes)
160 fNewRecipientAllowed = true;
164 WalletModel::UnlockContext ctx(model->requestUnlock());
167 // Unlock wallet was cancelled
168 fNewRecipientAllowed = true;
172 WalletModel::SendCoinsReturn sendstatus;
174 if (!model->getOptionsModel() || !model->getOptionsModel()->getCoinControlFeatures())
175 sendstatus = model->sendCoins(recipients);
177 sendstatus = model->sendCoins(recipients, CoinControlDialog::coinControl);
179 switch(sendstatus.status)
181 case WalletModel::InvalidAddress:
182 QMessageBox::warning(this, tr("Send Coins"),
183 tr("The recipient address is not valid, please recheck."),
184 QMessageBox::Ok, QMessageBox::Ok);
186 case WalletModel::InvalidAmount:
187 QMessageBox::warning(this, tr("Send Coins"),
188 tr("The amount to pay must be larger than 0."),
189 QMessageBox::Ok, QMessageBox::Ok);
191 case WalletModel::AmountExceedsBalance:
192 QMessageBox::warning(this, tr("Send Coins"),
193 tr("The amount exceeds your balance."),
194 QMessageBox::Ok, QMessageBox::Ok);
196 case WalletModel::AmountWithFeeExceedsBalance:
197 QMessageBox::warning(this, tr("Send Coins"),
198 tr("The total exceeds your balance when the %1 transaction fee is included.").
199 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
200 QMessageBox::Ok, QMessageBox::Ok);
202 case WalletModel::DuplicateAddress:
203 QMessageBox::warning(this, tr("Send Coins"),
204 tr("Duplicate address found, can only send to each address once per send operation."),
205 QMessageBox::Ok, QMessageBox::Ok);
207 case WalletModel::TransactionCreationFailed:
208 QMessageBox::warning(this, tr("Send Coins"),
209 tr("Error: Transaction creation failed."),
210 QMessageBox::Ok, QMessageBox::Ok);
212 case WalletModel::TransactionCommitFailed:
213 QMessageBox::warning(this, tr("Send Coins"),
214 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."),
215 QMessageBox::Ok, QMessageBox::Ok);
217 case WalletModel::Aborted: // User aborted, nothing to do
219 case WalletModel::OK:
221 CoinControlDialog::coinControl->UnSelectAll();
222 coinControlUpdateLabels();
225 fNewRecipientAllowed = true;
228 void SendCoinsDialog::clear()
230 // Remove entries until only one left
231 while(ui->entries->count())
233 delete ui->entries->takeAt(0)->widget();
237 updateRemoveEnabled();
239 ui->sendButton->setDefault(true);
242 void SendCoinsDialog::reject()
247 void SendCoinsDialog::accept()
252 SendCoinsEntry *SendCoinsDialog::addEntry()
254 SendCoinsEntry *entry = new SendCoinsEntry(this);
255 entry->setModel(model);
256 ui->entries->addWidget(entry);
257 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
258 connect(entry, SIGNAL(payAmountChanged()), this, SLOT(coinControlUpdateLabels()));
260 updateRemoveEnabled();
262 // Focus the field, so that entry can start immediately
265 ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
266 QCoreApplication::instance()->processEvents();
267 QScrollBar* bar = ui->scrollArea->verticalScrollBar();
269 bar->setSliderPosition(bar->maximum());
273 void SendCoinsDialog::updateRemoveEnabled()
275 // Remove buttons are enabled as soon as there is more than one send-entry
276 bool enabled = (ui->entries->count() > 1);
277 for(int i = 0; i < ui->entries->count(); ++i)
279 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
282 entry->setRemoveEnabled(enabled);
286 coinControlUpdateLabels();
289 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
292 updateRemoveEnabled();
295 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
297 for(int i = 0; i < ui->entries->count(); ++i)
299 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
302 prev = entry->setupTabChain(prev);
305 QWidget::setTabOrder(prev, ui->addButton);
306 QWidget::setTabOrder(ui->addButton, ui->sendButton);
307 return ui->sendButton;
310 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
312 if(!fNewRecipientAllowed)
315 SendCoinsEntry *entry = 0;
316 // Replace the first entry if it is still unused
317 if(ui->entries->count() == 1)
319 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
333 bool SendCoinsDialog::handleURI(const QString &uri)
335 SendCoinsRecipient rv;
336 // URI has to be valid
337 if (GUIUtil::parseBitcoinURI(uri, &rv))
339 CBitcoinAddress address(rv.address.toStdString());
340 if (!address.IsValid())
349 void SendCoinsDialog::setBalance(qint64 balance, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
352 Q_UNUSED(unconfirmedBalance);
353 Q_UNUSED(immatureBalance);
354 if(!model || !model->getOptionsModel())
357 int unit = model->getOptionsModel()->getDisplayUnit();
358 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
361 void SendCoinsDialog::updateDisplayUnit()
363 if(model && model->getOptionsModel())
365 // Update labelBalance with the current balance and the current unit
366 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), model->getBalance()));
370 // Coin Control: copy label "Quantity" to clipboard
371 void SendCoinsDialog::coinControlClipboardQuantity()
373 QApplication::clipboard()->setText(ui->labelCoinControlQuantity->text());
376 // Coin Control: copy label "Amount" to clipboard
377 void SendCoinsDialog::coinControlClipboardAmount()
379 QApplication::clipboard()->setText(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
382 // Coin Control: copy label "Fee" to clipboard
383 void SendCoinsDialog::coinControlClipboardFee()
385 QApplication::clipboard()->setText(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")));
388 // Coin Control: copy label "After fee" to clipboard
389 void SendCoinsDialog::coinControlClipboardAfterFee()
391 QApplication::clipboard()->setText(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")));
394 // Coin Control: copy label "Bytes" to clipboard
395 void SendCoinsDialog::coinControlClipboardBytes()
397 QApplication::clipboard()->setText(ui->labelCoinControlBytes->text());
400 // Coin Control: copy label "Priority" to clipboard
401 void SendCoinsDialog::coinControlClipboardPriority()
403 QApplication::clipboard()->setText(ui->labelCoinControlPriority->text());
406 // Coin Control: copy label "Low output" to clipboard
407 void SendCoinsDialog::coinControlClipboardLowOutput()
409 QApplication::clipboard()->setText(ui->labelCoinControlLowOutput->text());
412 // Coin Control: copy label "Change" to clipboard
413 void SendCoinsDialog::coinControlClipboardChange()
415 QApplication::clipboard()->setText(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")));
418 // Coin Control: settings menu - coin control enabled/disabled by user
419 void SendCoinsDialog::coinControlFeatureChanged(bool checked)
421 ui->frameCoinControl->setVisible(checked);
423 if (!checked && model) // coin control features disabled
424 CoinControlDialog::coinControl->SetNull();
427 // Coin Control: button inputs -> show actual coin control dialog
428 void SendCoinsDialog::coinControlButtonClicked()
430 CoinControlDialog dlg;
433 coinControlUpdateLabels();
436 // Coin Control: checkbox custom change address
437 void SendCoinsDialog::coinControlChangeChecked(int state)
441 if (state == Qt::Checked)
442 CoinControlDialog::coinControl->destChange = CBitcoinAddress(ui->lineEditCoinControlChange->text().toStdString()).Get();
444 CoinControlDialog::coinControl->destChange = CNoDestination();
447 ui->lineEditCoinControlChange->setEnabled((state == Qt::Checked));
448 ui->labelCoinControlChangeLabel->setEnabled((state == Qt::Checked));
451 // Coin Control: custom change address changed
452 void SendCoinsDialog::coinControlChangeEdited(const QString & text)
456 CoinControlDialog::coinControl->destChange = CBitcoinAddress(text.toStdString()).Get();
458 // label for the change address
459 ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:black;}");
461 ui->labelCoinControlChangeLabel->setText("");
462 else if (!CBitcoinAddress(text.toStdString()).IsValid())
464 ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:red;}");
465 ui->labelCoinControlChangeLabel->setText(tr("WARNING: Invalid Bitcoin address"));
469 QString associatedLabel = model->getAddressTableModel()->labelForAddress(text);
470 if (!associatedLabel.isEmpty())
471 ui->labelCoinControlChangeLabel->setText(associatedLabel);
476 CBitcoinAddress(text.toStdString()).GetKeyID(keyid);
477 if (model->getPubKey(keyid, pubkey))
478 ui->labelCoinControlChangeLabel->setText(tr("(no label)"));
481 ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:red;}");
482 ui->labelCoinControlChangeLabel->setText(tr("WARNING: unknown change address"));
489 // Coin Control: update labels
490 void SendCoinsDialog::coinControlUpdateLabels()
492 if (!model || !model->getOptionsModel() || !model->getOptionsModel()->getCoinControlFeatures())
496 CoinControlDialog::payAmounts.clear();
497 for(int i = 0; i < ui->entries->count(); ++i)
499 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
501 CoinControlDialog::payAmounts.append(entry->getValue().amount);
504 if (CoinControlDialog::coinControl->HasSelected())
506 // actual coin control calculation
507 CoinControlDialog::updateLabels(model, this);
509 // show coin control stats
510 ui->labelCoinControlAutomaticallySelected->hide();
511 ui->widgetCoinControl->show();
515 // hide coin control stats
516 ui->labelCoinControlAutomaticallySelected->show();
517 ui->widgetCoinControl->hide();
518 ui->labelCoinControlInsuffFunds->hide();