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()));
35 void SendCoinsDialog::setModel(WalletModel *model)
39 for(int i = 0; i < ui->entries->count(); ++i)
41 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
44 entry->setModel(model);
49 setBalance(model->getBalance(), model->getUnconfirmedBalance());
50 connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
54 SendCoinsDialog::~SendCoinsDialog()
59 void SendCoinsDialog::on_sendButton_clicked()
61 QList<SendCoinsRecipient> recipients;
67 for(int i = 0; i < ui->entries->count(); ++i)
69 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
74 recipients.append(entry->getValue());
83 if(!valid || recipients.isEmpty())
88 // Format confirmation message
89 QStringList formatted;
90 foreach(const SendCoinsRecipient &rcp, recipients)
92 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
95 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
96 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
97 QMessageBox::Yes|QMessageBox::Cancel,
100 if(retval != QMessageBox::Yes)
105 WalletModel::UnlockContext ctx(model->requestUnlock());
108 // Unlock wallet was cancelled
112 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
113 switch(sendstatus.status)
115 case WalletModel::InvalidAddress:
116 QMessageBox::warning(this, tr("Send Coins"),
117 tr("The recepient address is not valid, please recheck."),
118 QMessageBox::Ok, QMessageBox::Ok);
120 case WalletModel::InvalidAmount:
121 QMessageBox::warning(this, tr("Send Coins"),
122 tr("The amount to pay must be larger than 0."),
123 QMessageBox::Ok, QMessageBox::Ok);
125 case WalletModel::AmountExceedsBalance:
126 QMessageBox::warning(this, tr("Send Coins"),
127 tr("Amount exceeds your balance"),
128 QMessageBox::Ok, QMessageBox::Ok);
130 case WalletModel::AmountWithFeeExceedsBalance:
131 QMessageBox::warning(this, tr("Send Coins"),
132 tr("Total exceeds your balance when the %1 transaction fee is included").
133 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
134 QMessageBox::Ok, QMessageBox::Ok);
136 case WalletModel::DuplicateAddress:
137 QMessageBox::warning(this, tr("Send Coins"),
138 tr("Duplicate address found, can only send to each address once in one send operation"),
139 QMessageBox::Ok, QMessageBox::Ok);
141 case WalletModel::TransactionCreationFailed:
142 QMessageBox::warning(this, tr("Send Coins"),
143 tr("Error: Transaction creation failed "),
144 QMessageBox::Ok, QMessageBox::Ok);
146 case WalletModel::TransactionCommitFailed:
147 QMessageBox::warning(this, tr("Send Coins"),
148 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."),
149 QMessageBox::Ok, QMessageBox::Ok);
151 case WalletModel::OK:
157 void SendCoinsDialog::clear()
159 // Remove entries until only one left
160 while(ui->entries->count())
162 delete ui->entries->takeAt(0)->widget();
166 updateRemoveEnabled();
168 ui->sendButton->setDefault(true);
171 void SendCoinsDialog::reject()
176 void SendCoinsDialog::accept()
181 SendCoinsEntry *SendCoinsDialog::addEntry()
183 SendCoinsEntry *entry = new SendCoinsEntry(this);
184 entry->setModel(model);
185 ui->entries->addWidget(entry);
186 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
188 updateRemoveEnabled();
190 // Focus the field, so that entry can start immediately
193 ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
194 QCoreApplication::instance()->processEvents();
195 QScrollBar* bar = ui->scrollArea->verticalScrollBar();
197 bar->setSliderPosition(bar->maximum());
201 void SendCoinsDialog::updateRemoveEnabled()
203 // Remove buttons are enabled as soon as there is more than one send-entry
204 bool enabled = (ui->entries->count() > 1);
205 for(int i = 0; i < ui->entries->count(); ++i)
207 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
210 entry->setRemoveEnabled(enabled);
216 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
219 updateRemoveEnabled();
222 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
224 for(int i = 0; i < ui->entries->count(); ++i)
226 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
229 prev = entry->setupTabChain(prev);
232 QWidget::setTabOrder(prev, ui->addButton);
233 QWidget::setTabOrder(ui->addButton, ui->sendButton);
234 return ui->sendButton;
237 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
239 SendCoinsEntry *entry = 0;
240 // Replace the first entry if it is still unused
241 if(ui->entries->count() == 1)
243 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
258 void SendCoinsDialog::handleURI(const QUrl *uri)
260 SendCoinsRecipient rv;
261 if(!GUIUtil::parseBitcoinURI(uri, &rv))
268 void SendCoinsDialog::handleURI(const QString &uri)
270 SendCoinsRecipient rv;
271 if(!GUIUtil::parseBitcoinURI(uri, &rv))
278 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
280 Q_UNUSED(unconfirmedBalance);
281 if(!model || !model->getOptionsModel())
284 int unit = model->getOptionsModel()->getDisplayUnit();
285 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));