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>
15 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
17 ui(new Ui::SendCoinsDialog),
24 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
25 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
28 void SendCoinsDialog::setModel(WalletModel *model)
32 for(int i = 0; i < ui->entries->count(); ++i)
34 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
37 entry->setModel(model);
41 setBalance(model->getBalance(), model->getUnconfirmedBalance());
42 connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
45 SendCoinsDialog::~SendCoinsDialog()
50 void SendCoinsDialog::on_sendButton_clicked()
52 QList<SendCoinsRecipient> recipients;
54 for(int i = 0; i < ui->entries->count(); ++i)
56 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
61 recipients.append(entry->getValue());
70 if(!valid || recipients.isEmpty())
75 // Format confirmation message
76 QStringList formatted;
77 foreach(const SendCoinsRecipient &rcp, recipients)
79 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
82 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
83 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
84 QMessageBox::Yes|QMessageBox::Cancel,
87 if(retval != QMessageBox::Yes)
92 WalletModel::UnlockContext ctx(model->requestUnlock());
95 // Unlock wallet was cancelled
99 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
100 switch(sendstatus.status)
102 case WalletModel::InvalidAddress:
103 QMessageBox::warning(this, tr("Send Coins"),
104 tr("The recepient address is not valid, please recheck."),
105 QMessageBox::Ok, QMessageBox::Ok);
107 case WalletModel::InvalidAmount:
108 QMessageBox::warning(this, tr("Send Coins"),
109 tr("The amount to pay must be larger than 0."),
110 QMessageBox::Ok, QMessageBox::Ok);
112 case WalletModel::AmountExceedsBalance:
113 QMessageBox::warning(this, tr("Send Coins"),
114 tr("Amount exceeds your balance"),
115 QMessageBox::Ok, QMessageBox::Ok);
117 case WalletModel::AmountWithFeeExceedsBalance:
118 QMessageBox::warning(this, tr("Send Coins"),
119 tr("Total exceeds your balance when the %1 transaction fee is included").
120 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
121 QMessageBox::Ok, QMessageBox::Ok);
123 case WalletModel::DuplicateAddress:
124 QMessageBox::warning(this, tr("Send Coins"),
125 tr("Duplicate address found, can only send to each address once in one send operation"),
126 QMessageBox::Ok, QMessageBox::Ok);
128 case WalletModel::TransactionCreationFailed:
129 QMessageBox::warning(this, tr("Send Coins"),
130 tr("Error: Transaction creation failed "),
131 QMessageBox::Ok, QMessageBox::Ok);
133 case WalletModel::TransactionCommitFailed:
134 QMessageBox::warning(this, tr("Send Coins"),
135 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."),
136 QMessageBox::Ok, QMessageBox::Ok);
138 case WalletModel::OK:
144 void SendCoinsDialog::clear()
146 // Remove entries until only one left
147 while(ui->entries->count())
149 delete ui->entries->takeAt(0)->widget();
153 updateRemoveEnabled();
155 ui->sendButton->setDefault(true);
158 void SendCoinsDialog::reject()
163 void SendCoinsDialog::accept()
168 SendCoinsEntry *SendCoinsDialog::addEntry()
170 SendCoinsEntry *entry = new SendCoinsEntry(this);
171 entry->setModel(model);
172 ui->entries->addWidget(entry);
173 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
175 updateRemoveEnabled();
177 // Focus the field, so that entry can start immediately
182 void SendCoinsDialog::updateRemoveEnabled()
184 // Remove buttons are enabled as soon as there is more than one send-entry
185 bool enabled = (ui->entries->count() > 1);
186 for(int i = 0; i < ui->entries->count(); ++i)
188 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
191 entry->setRemoveEnabled(enabled);
197 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
200 updateRemoveEnabled();
203 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
205 for(int i = 0; i < ui->entries->count(); ++i)
207 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
210 prev = entry->setupTabChain(prev);
213 QWidget::setTabOrder(prev, ui->addButton);
214 QWidget::setTabOrder(ui->addButton, ui->sendButton);
215 return ui->sendButton;
218 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
220 SendCoinsEntry *entry = 0;
221 // Replace the first entry if it is still unused
222 if(ui->entries->count() == 1)
224 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
239 void SendCoinsDialog::handleURL(const QUrl *url)
241 SendCoinsRecipient rv;
242 if(!GUIUtil::parseBitcoinURL(url, &rv))
249 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
251 Q_UNUSED(unconfirmedBalance);
252 int unit = model->getOptionsModel()->getDisplayUnit();
253 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));