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"
10 #include <QMessageBox>
12 #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::SendCoinsReturn sendstatus = model->sendCoins(recipients);
89 switch(sendstatus.status)
91 case WalletModel::InvalidAddress:
92 QMessageBox::warning(this, tr("Send Coins"),
93 tr("The recepient address is not valid, please recheck."),
94 QMessageBox::Ok, QMessageBox::Ok);
96 case WalletModel::InvalidAmount:
97 QMessageBox::warning(this, tr("Send Coins"),
98 tr("The amount to pay must be larger than 0."),
99 QMessageBox::Ok, QMessageBox::Ok);
101 case WalletModel::AmountExceedsBalance:
102 QMessageBox::warning(this, tr("Send Coins"),
103 tr("Amount exceeds your balance"),
104 QMessageBox::Ok, QMessageBox::Ok);
106 case WalletModel::AmountWithFeeExceedsBalance:
107 QMessageBox::warning(this, tr("Send Coins"),
108 tr("Total exceeds your balance when the %1 transaction fee is included").
109 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
110 QMessageBox::Ok, QMessageBox::Ok);
112 case WalletModel::DuplicateAddress:
113 QMessageBox::warning(this, tr("Send Coins"),
114 tr("Duplicate address found, can only send to each address once in one send operation"),
115 QMessageBox::Ok, QMessageBox::Ok);
117 case WalletModel::TransactionCreationFailed:
118 QMessageBox::warning(this, tr("Send Coins"),
119 tr("Error: Transaction creation failed "),
120 QMessageBox::Ok, QMessageBox::Ok);
123 case WalletModel::TransactionCommitFailed:
124 QMessageBox::warning(this, tr("Send Coins"),
125 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."),
126 QMessageBox::Ok, QMessageBox::Ok);
128 case WalletModel::OK:
134 void SendCoinsDialog::clear()
136 // Remove entries until only one left
137 while(ui->entries->count())
139 delete ui->entries->takeAt(0)->widget();
143 updateRemoveEnabled();
145 ui->sendButton->setDefault(true);
148 void SendCoinsDialog::reject()
153 void SendCoinsDialog::accept()
158 SendCoinsEntry *SendCoinsDialog::addEntry()
160 SendCoinsEntry *entry = new SendCoinsEntry(this);
161 entry->setModel(model);
162 ui->entries->addWidget(entry);
163 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
165 updateRemoveEnabled();
167 // Focus the field, so that entry can start immediately
172 void SendCoinsDialog::updateRemoveEnabled()
174 // Remove buttons are enabled as soon as there is more than one send-entry
175 bool enabled = (ui->entries->count() > 1);
176 for(int i = 0; i < ui->entries->count(); ++i)
178 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
181 entry->setRemoveEnabled(enabled);
187 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
190 updateRemoveEnabled();
193 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
195 for(int i = 0; i < ui->entries->count(); ++i)
197 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
200 prev = entry->setupTabChain(prev);
203 QWidget::setTabOrder(prev, ui->addButton);
204 QWidget::setTabOrder(ui->addButton, ui->sendButton);
205 return ui->sendButton;
208 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
210 SendCoinsEntry *entry = 0;
211 // Replace the first entry if it is still unused
212 if(ui->entries->count() == 1)
214 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
229 void SendCoinsDialog::handleURL(const QUrl *url)
231 SendCoinsRecipient rv;
232 if(!GUIUtil::parseBitcoinURL(url, &rv))