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),
22 #ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
23 ui->addButton->setIcon(QIcon());
24 ui->clearButton->setIcon(QIcon());
25 ui->sendButton->setIcon(QIcon());
30 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
31 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
34 void SendCoinsDialog::setModel(WalletModel *model)
38 for(int i = 0; i < ui->entries->count(); ++i)
40 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
43 entry->setModel(model);
47 setBalance(model->getBalance(), model->getUnconfirmedBalance());
48 connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
51 SendCoinsDialog::~SendCoinsDialog()
56 void SendCoinsDialog::on_sendButton_clicked()
58 QList<SendCoinsRecipient> recipients;
60 for(int i = 0; i < ui->entries->count(); ++i)
62 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
67 recipients.append(entry->getValue());
76 if(!valid || recipients.isEmpty())
81 // Format confirmation message
82 QStringList formatted;
83 foreach(const SendCoinsRecipient &rcp, recipients)
85 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
88 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
89 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
90 QMessageBox::Yes|QMessageBox::Cancel,
93 if(retval != QMessageBox::Yes)
98 WalletModel::UnlockContext ctx(model->requestUnlock());
101 // Unlock wallet was cancelled
105 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
106 switch(sendstatus.status)
108 case WalletModel::InvalidAddress:
109 QMessageBox::warning(this, tr("Send Coins"),
110 tr("The recepient address is not valid, please recheck."),
111 QMessageBox::Ok, QMessageBox::Ok);
113 case WalletModel::InvalidAmount:
114 QMessageBox::warning(this, tr("Send Coins"),
115 tr("The amount to pay must be larger than 0."),
116 QMessageBox::Ok, QMessageBox::Ok);
118 case WalletModel::AmountExceedsBalance:
119 QMessageBox::warning(this, tr("Send Coins"),
120 tr("Amount exceeds your balance"),
121 QMessageBox::Ok, QMessageBox::Ok);
123 case WalletModel::AmountWithFeeExceedsBalance:
124 QMessageBox::warning(this, tr("Send Coins"),
125 tr("Total exceeds your balance when the %1 transaction fee is included").
126 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
127 QMessageBox::Ok, QMessageBox::Ok);
129 case WalletModel::DuplicateAddress:
130 QMessageBox::warning(this, tr("Send Coins"),
131 tr("Duplicate address found, can only send to each address once in one send operation"),
132 QMessageBox::Ok, QMessageBox::Ok);
134 case WalletModel::TransactionCreationFailed:
135 QMessageBox::warning(this, tr("Send Coins"),
136 tr("Error: Transaction creation failed "),
137 QMessageBox::Ok, QMessageBox::Ok);
139 case WalletModel::TransactionCommitFailed:
140 QMessageBox::warning(this, tr("Send Coins"),
141 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."),
142 QMessageBox::Ok, QMessageBox::Ok);
144 case WalletModel::OK:
150 void SendCoinsDialog::clear()
152 // Remove entries until only one left
153 while(ui->entries->count())
155 delete ui->entries->takeAt(0)->widget();
159 updateRemoveEnabled();
161 ui->sendButton->setDefault(true);
164 void SendCoinsDialog::reject()
169 void SendCoinsDialog::accept()
174 SendCoinsEntry *SendCoinsDialog::addEntry()
176 SendCoinsEntry *entry = new SendCoinsEntry(this);
177 entry->setModel(model);
178 ui->entries->addWidget(entry);
179 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
181 updateRemoveEnabled();
183 // Focus the field, so that entry can start immediately
188 void SendCoinsDialog::updateRemoveEnabled()
190 // Remove buttons are enabled as soon as there is more than one send-entry
191 bool enabled = (ui->entries->count() > 1);
192 for(int i = 0; i < ui->entries->count(); ++i)
194 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
197 entry->setRemoveEnabled(enabled);
203 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
206 updateRemoveEnabled();
209 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
211 for(int i = 0; i < ui->entries->count(); ++i)
213 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
216 prev = entry->setupTabChain(prev);
219 QWidget::setTabOrder(prev, ui->addButton);
220 QWidget::setTabOrder(ui->addButton, ui->sendButton);
221 return ui->sendButton;
224 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
226 SendCoinsEntry *entry = 0;
227 // Replace the first entry if it is still unused
228 if(ui->entries->count() == 1)
230 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
245 void SendCoinsDialog::handleURL(const QUrl *url)
247 SendCoinsRecipient rv;
248 if(!GUIUtil::parseBitcoinURL(url, &rv))
255 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
257 Q_UNUSED(unconfirmedBalance);
258 int unit = model->getOptionsModel()->getDisplayUnit();
259 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));