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()));
27 void SendCoinsDialog::setModel(WalletModel *model)
31 for(int i = 0; i < ui->entries->count(); ++i)
33 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
36 entry->setModel(model);
40 setBalance(model->getBalance(), model->getUnconfirmedBalance());
41 connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
44 SendCoinsDialog::~SendCoinsDialog()
49 void SendCoinsDialog::on_sendButton_clicked()
51 QList<SendCoinsRecipient> recipients;
53 for(int i = 0; i < ui->entries->count(); ++i)
55 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
60 recipients.append(entry->getValue());
69 if(!valid || recipients.isEmpty())
74 // Format confirmation message
75 QStringList formatted;
76 foreach(const SendCoinsRecipient &rcp, recipients)
78 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
81 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
82 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
83 QMessageBox::Yes|QMessageBox::Cancel,
86 if(retval != QMessageBox::Yes)
91 WalletModel::UnlockContext ctx(model->requestUnlock());
94 // Unlock wallet was cancelled
98 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
99 switch(sendstatus.status)
101 case WalletModel::InvalidAddress:
102 QMessageBox::warning(this, tr("Send Coins"),
103 tr("The recepient address is not valid, please recheck."),
104 QMessageBox::Ok, QMessageBox::Ok);
106 case WalletModel::InvalidAmount:
107 QMessageBox::warning(this, tr("Send Coins"),
108 tr("The amount to pay must be larger than 0."),
109 QMessageBox::Ok, QMessageBox::Ok);
111 case WalletModel::AmountExceedsBalance:
112 QMessageBox::warning(this, tr("Send Coins"),
113 tr("Amount exceeds your balance"),
114 QMessageBox::Ok, QMessageBox::Ok);
116 case WalletModel::AmountWithFeeExceedsBalance:
117 QMessageBox::warning(this, tr("Send Coins"),
118 tr("Total exceeds your balance when the %1 transaction fee is included").
119 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
120 QMessageBox::Ok, QMessageBox::Ok);
122 case WalletModel::DuplicateAddress:
123 QMessageBox::warning(this, tr("Send Coins"),
124 tr("Duplicate address found, can only send to each address once in one send operation"),
125 QMessageBox::Ok, QMessageBox::Ok);
127 case WalletModel::TransactionCreationFailed:
128 QMessageBox::warning(this, tr("Send Coins"),
129 tr("Error: Transaction creation failed "),
130 QMessageBox::Ok, QMessageBox::Ok);
132 case WalletModel::TransactionCommitFailed:
133 QMessageBox::warning(this, tr("Send Coins"),
134 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."),
135 QMessageBox::Ok, QMessageBox::Ok);
137 case WalletModel::OK:
143 void SendCoinsDialog::clear()
145 // Remove entries until only one left
146 while(ui->entries->count())
148 delete ui->entries->takeAt(0)->widget();
152 updateRemoveEnabled();
154 ui->sendButton->setDefault(true);
157 void SendCoinsDialog::reject()
162 void SendCoinsDialog::accept()
167 SendCoinsEntry *SendCoinsDialog::addEntry()
169 SendCoinsEntry *entry = new SendCoinsEntry(this);
170 entry->setModel(model);
171 ui->entries->addWidget(entry);
172 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
174 updateRemoveEnabled();
176 // Focus the field, so that entry can start immediately
181 void SendCoinsDialog::updateRemoveEnabled()
183 // Remove buttons are enabled as soon as there is more than one send-entry
184 bool enabled = (ui->entries->count() > 1);
185 for(int i = 0; i < ui->entries->count(); ++i)
187 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
190 entry->setRemoveEnabled(enabled);
196 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
199 updateRemoveEnabled();
202 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
204 for(int i = 0; i < ui->entries->count(); ++i)
206 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
209 prev = entry->setupTabChain(prev);
212 QWidget::setTabOrder(prev, ui->addButton);
213 QWidget::setTabOrder(ui->addButton, ui->sendButton);
214 return ui->sendButton;
217 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
219 SendCoinsEntry *entry = 0;
220 // Replace the first entry if it is still unused
221 if(ui->entries->count() == 1)
223 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
238 void SendCoinsDialog::handleURL(const QUrl *url)
240 SendCoinsRecipient rv;
241 if(!GUIUtil::parseBitcoinURL(url, &rv))
248 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
250 Q_UNUSED(unconfirmedBalance);
251 int unit = model->getOptionsModel()->getDisplayUnit();
252 ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));