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>
16 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
18 ui(new Ui::SendCoinsDialog),
23 #ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
24 ui->addButton->setIcon(QIcon());
25 ui->clearButton->setIcon(QIcon());
26 ui->sendButton->setIcon(QIcon());
31 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
32 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
34 fNewRecipientAllowed = true;
37 void SendCoinsDialog::setModel(WalletModel *model)
41 for(int i = 0; i < ui->entries->count(); ++i)
43 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
46 entry->setModel(model);
51 setBalance(model->getBalance(), model->getUnconfirmedBalance());
52 connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
56 SendCoinsDialog::~SendCoinsDialog()
61 void SendCoinsDialog::on_sendButton_clicked()
63 QList<SendCoinsRecipient> recipients;
69 for(int i = 0; i < ui->entries->count(); ++i)
71 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
76 recipients.append(entry->getValue());
85 if(!valid || recipients.isEmpty())
90 // Format confirmation message
91 QStringList formatted;
92 foreach(const SendCoinsRecipient &rcp, recipients)
94 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
97 fNewRecipientAllowed = false;
99 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
100 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
101 QMessageBox::Yes|QMessageBox::Cancel,
102 QMessageBox::Cancel);
104 if(retval != QMessageBox::Yes)
106 fNewRecipientAllowed = true;
110 WalletModel::UnlockContext ctx(model->requestUnlock());
113 // Unlock wallet was cancelled
114 fNewRecipientAllowed = true;
118 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
119 switch(sendstatus.status)
121 case WalletModel::InvalidAddress:
122 QMessageBox::warning(this, tr("Send Coins"),
123 tr("The recepient address is not valid, please recheck."),
124 QMessageBox::Ok, QMessageBox::Ok);
126 case WalletModel::InvalidAmount:
127 QMessageBox::warning(this, tr("Send Coins"),
128 tr("The amount to pay must be larger than 0."),
129 QMessageBox::Ok, QMessageBox::Ok);
131 case WalletModel::AmountExceedsBalance:
132 QMessageBox::warning(this, tr("Send Coins"),
133 tr("Amount exceeds your balance"),
134 QMessageBox::Ok, QMessageBox::Ok);
136 case WalletModel::AmountWithFeeExceedsBalance:
137 QMessageBox::warning(this, tr("Send Coins"),
138 tr("Total exceeds your balance when the %1 transaction fee is included").
139 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
140 QMessageBox::Ok, QMessageBox::Ok);
142 case WalletModel::DuplicateAddress:
143 QMessageBox::warning(this, tr("Send Coins"),
144 tr("Duplicate address found, can only send to each address once in one send operation"),
145 QMessageBox::Ok, QMessageBox::Ok);
147 case WalletModel::TransactionCreationFailed:
148 QMessageBox::warning(this, tr("Send Coins"),
149 tr("Error: Transaction creation failed "),
150 QMessageBox::Ok, QMessageBox::Ok);
152 case WalletModel::TransactionCommitFailed:
153 QMessageBox::warning(this, tr("Send Coins"),
154 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."),
155 QMessageBox::Ok, QMessageBox::Ok);
157 case WalletModel::Aborted: // User aborted, nothing to do
159 case WalletModel::OK:
163 fNewRecipientAllowed = true;
166 void SendCoinsDialog::clear()
168 // Remove entries until only one left
169 while(ui->entries->count())
171 delete ui->entries->takeAt(0)->widget();
175 updateRemoveEnabled();
177 ui->sendButton->setDefault(true);
180 void SendCoinsDialog::reject()
185 void SendCoinsDialog::accept()
190 SendCoinsEntry *SendCoinsDialog::addEntry()
192 SendCoinsEntry *entry = new SendCoinsEntry(this);
193 entry->setModel(model);
194 ui->entries->addWidget(entry);
195 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
197 updateRemoveEnabled();
199 // Focus the field, so that entry can start immediately
202 ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
203 QCoreApplication::instance()->processEvents();
204 QScrollBar* bar = ui->scrollArea->verticalScrollBar();
206 bar->setSliderPosition(bar->maximum());
210 void SendCoinsDialog::updateRemoveEnabled()
212 // Remove buttons are enabled as soon as there is more than one send-entry
213 bool enabled = (ui->entries->count() > 1);
214 for(int i = 0; i < ui->entries->count(); ++i)
216 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
219 entry->setRemoveEnabled(enabled);
225 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
228 updateRemoveEnabled();
231 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
233 for(int i = 0; i < ui->entries->count(); ++i)
235 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
238 prev = entry->setupTabChain(prev);
241 QWidget::setTabOrder(prev, ui->addButton);
242 QWidget::setTabOrder(ui->addButton, ui->sendButton);
243 return ui->sendButton;
246 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
248 if (!fNewRecipientAllowed)
251 SendCoinsEntry *entry = 0;
252 // Replace the first entry if it is still unused
253 if(ui->entries->count() == 1)
255 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
270 void SendCoinsDialog::handleURI(const QString &uri)
272 SendCoinsRecipient rv;
273 if(!GUIUtil::parseBitcoinURI(uri, &rv))
280 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
282 Q_UNUSED(unconfirmedBalance);
283 if(!model || !model->getOptionsModel())
286 int unit = model->getOptionsModel()->getDisplayUnit();
287 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));