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);
41 SendCoinsDialog::~SendCoinsDialog()
46 void SendCoinsDialog::on_sendButton_clicked()
48 QList<SendCoinsRecipient> recipients;
50 for(int i = 0; i < ui->entries->count(); ++i)
52 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
57 recipients.append(entry->getValue());
66 if(!valid || recipients.isEmpty())
71 // Format confirmation message
72 QStringList formatted;
73 foreach(const SendCoinsRecipient &rcp, recipients)
75 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
78 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
79 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
80 QMessageBox::Yes|QMessageBox::Cancel,
83 if(retval != QMessageBox::Yes)
88 WalletModel::UnlockContext ctx(model->requestUnlock());
91 // Unlock wallet was cancelled
95 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
96 switch(sendstatus.status)
98 case WalletModel::InvalidAddress:
99 QMessageBox::warning(this, tr("Send Coins"),
100 tr("The recepient address is not valid, please recheck."),
101 QMessageBox::Ok, QMessageBox::Ok);
103 case WalletModel::InvalidAmount:
104 QMessageBox::warning(this, tr("Send Coins"),
105 tr("The amount to pay must be larger than 0."),
106 QMessageBox::Ok, QMessageBox::Ok);
108 case WalletModel::AmountExceedsBalance:
109 QMessageBox::warning(this, tr("Send Coins"),
110 tr("Amount exceeds your balance"),
111 QMessageBox::Ok, QMessageBox::Ok);
113 case WalletModel::AmountWithFeeExceedsBalance:
114 QMessageBox::warning(this, tr("Send Coins"),
115 tr("Total exceeds your balance when the %1 transaction fee is included").
116 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
117 QMessageBox::Ok, QMessageBox::Ok);
119 case WalletModel::DuplicateAddress:
120 QMessageBox::warning(this, tr("Send Coins"),
121 tr("Duplicate address found, can only send to each address once in one send operation"),
122 QMessageBox::Ok, QMessageBox::Ok);
124 case WalletModel::TransactionCreationFailed:
125 QMessageBox::warning(this, tr("Send Coins"),
126 tr("Error: Transaction creation failed "),
127 QMessageBox::Ok, QMessageBox::Ok);
129 case WalletModel::TransactionCommitFailed:
130 QMessageBox::warning(this, tr("Send Coins"),
131 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."),
132 QMessageBox::Ok, QMessageBox::Ok);
134 case WalletModel::OK:
140 void SendCoinsDialog::clear()
142 // Remove entries until only one left
143 while(ui->entries->count())
145 delete ui->entries->takeAt(0)->widget();
149 updateRemoveEnabled();
151 ui->sendButton->setDefault(true);
154 void SendCoinsDialog::reject()
159 void SendCoinsDialog::accept()
164 SendCoinsEntry *SendCoinsDialog::addEntry()
166 SendCoinsEntry *entry = new SendCoinsEntry(this);
167 entry->setModel(model);
168 ui->entries->addWidget(entry);
169 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
171 updateRemoveEnabled();
173 // Focus the field, so that entry can start immediately
178 void SendCoinsDialog::updateRemoveEnabled()
180 // Remove buttons are enabled as soon as there is more than one send-entry
181 bool enabled = (ui->entries->count() > 1);
182 for(int i = 0; i < ui->entries->count(); ++i)
184 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
187 entry->setRemoveEnabled(enabled);
193 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
196 updateRemoveEnabled();
199 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
201 for(int i = 0; i < ui->entries->count(); ++i)
203 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
206 prev = entry->setupTabChain(prev);
209 QWidget::setTabOrder(prev, ui->addButton);
210 QWidget::setTabOrder(ui->addButton, ui->sendButton);
211 return ui->sendButton;
214 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
216 SendCoinsEntry *entry = 0;
217 // Replace the first entry if it is still unused
218 if(ui->entries->count() == 1)
220 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
235 void SendCoinsDialog::handleURL(const QUrl *url)
237 SendCoinsRecipient rv;
238 if(!GUIUtil::parseBitcoinURL(url, &rv))