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::Aborted: // User aborted, nothing to do
153 case WalletModel::OK:
159 void SendCoinsDialog::clear()
161 // Remove entries until only one left
162 while(ui->entries->count())
164 delete ui->entries->takeAt(0)->widget();
168 updateRemoveEnabled();
170 ui->sendButton->setDefault(true);
173 void SendCoinsDialog::reject()
178 void SendCoinsDialog::accept()
183 SendCoinsEntry *SendCoinsDialog::addEntry()
185 SendCoinsEntry *entry = new SendCoinsEntry(this);
186 entry->setModel(model);
187 ui->entries->addWidget(entry);
188 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
190 updateRemoveEnabled();
192 // Focus the field, so that entry can start immediately
195 ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
196 QCoreApplication::instance()->processEvents();
197 QScrollBar* bar = ui->scrollArea->verticalScrollBar();
199 bar->setSliderPosition(bar->maximum());
203 void SendCoinsDialog::updateRemoveEnabled()
205 // Remove buttons are enabled as soon as there is more than one send-entry
206 bool enabled = (ui->entries->count() > 1);
207 for(int i = 0; i < ui->entries->count(); ++i)
209 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
212 entry->setRemoveEnabled(enabled);
218 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
221 updateRemoveEnabled();
224 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
226 for(int i = 0; i < ui->entries->count(); ++i)
228 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
231 prev = entry->setupTabChain(prev);
234 QWidget::setTabOrder(prev, ui->addButton);
235 QWidget::setTabOrder(ui->addButton, ui->sendButton);
236 return ui->sendButton;
239 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
241 SendCoinsEntry *entry = 0;
242 // Replace the first entry if it is still unused
243 if(ui->entries->count() == 1)
245 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
260 void SendCoinsDialog::handleURI(const QUrl *uri)
262 SendCoinsRecipient rv;
263 if(!GUIUtil::parseBitcoinURI(uri, &rv))
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));