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>
14 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
16 ui(new Ui::SendCoinsDialog),
23 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
26 void SendCoinsDialog::setModel(WalletModel *model)
30 for(int i = 0; i < ui->entries->count(); ++i)
32 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
35 entry->setModel(model);
40 SendCoinsDialog::~SendCoinsDialog()
45 void SendCoinsDialog::on_sendButton_clicked()
47 QList<SendCoinsRecipient> recipients;
49 for(int i = 0; i < ui->entries->count(); ++i)
51 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
56 recipients.append(entry->getValue());
65 if(!valid || recipients.isEmpty())
70 // Format confirmation message
71 QStringList formatted;
72 foreach(const SendCoinsRecipient &rcp, recipients)
74 formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
77 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
78 tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
79 QMessageBox::Yes|QMessageBox::Cancel,
82 if(retval != QMessageBox::Yes)
87 WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
88 switch(sendstatus.status)
90 case WalletModel::InvalidAddress:
91 QMessageBox::warning(this, tr("Send Coins"),
92 tr("The recepient address is not valid, please recheck."),
93 QMessageBox::Ok, QMessageBox::Ok);
95 case WalletModel::InvalidAmount:
96 QMessageBox::warning(this, tr("Send Coins"),
97 tr("The amount to pay must be larger than 0."),
98 QMessageBox::Ok, QMessageBox::Ok);
100 case WalletModel::AmountExceedsBalance:
101 QMessageBox::warning(this, tr("Send Coins"),
102 tr("Amount exceeds your balance"),
103 QMessageBox::Ok, QMessageBox::Ok);
105 case WalletModel::AmountWithFeeExceedsBalance:
106 QMessageBox::warning(this, tr("Send Coins"),
107 tr("Total exceeds your balance when the %1 transaction fee is included").
108 arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
109 QMessageBox::Ok, QMessageBox::Ok);
111 case WalletModel::DuplicateAddress:
112 QMessageBox::warning(this, tr("Send Coins"),
113 tr("Duplicate address found, can only send to each address once in one send operation"),
114 QMessageBox::Ok, QMessageBox::Ok);
116 case WalletModel::TransactionCreationFailed:
117 QMessageBox::warning(this, tr("Send Coins"),
118 tr("Error: Transaction creation failed "),
119 QMessageBox::Ok, QMessageBox::Ok);
122 case WalletModel::TransactionCommitFailed:
123 QMessageBox::warning(this, tr("Send Coins"),
124 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."),
125 QMessageBox::Ok, QMessageBox::Ok);
127 case WalletModel::OK:
133 void SendCoinsDialog::clear()
135 // Remove entries until only one left
136 while(ui->entries->count())
138 delete ui->entries->takeAt(0)->widget();
142 updateRemoveEnabled();
144 ui->sendButton->setDefault(true);
147 void SendCoinsDialog::reject()
152 void SendCoinsDialog::accept()
157 SendCoinsEntry *SendCoinsDialog::addEntry()
159 SendCoinsEntry *entry = new SendCoinsEntry(this);
160 entry->setModel(model);
161 ui->entries->addWidget(entry);
162 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
164 updateRemoveEnabled();
166 // Focus the field, so that entry can start immediately
171 void SendCoinsDialog::updateRemoveEnabled()
173 // Remove buttons are enabled as soon as there is more than one send-entry
174 bool enabled = (ui->entries->count() > 1);
175 for(int i = 0; i < ui->entries->count(); ++i)
177 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
180 entry->setRemoveEnabled(enabled);
186 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
189 updateRemoveEnabled();
192 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
194 for(int i = 0; i < ui->entries->count(); ++i)
196 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
199 prev = entry->setupTabChain(prev);
202 QWidget::setTabOrder(prev, ui->addButton);
203 QWidget::setTabOrder(ui->addButton, ui->sendButton);
204 return ui->sendButton;
207 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
209 SendCoinsEntry *entry = 0;
210 // Replace the first entry if it is still unused
211 if(ui->entries->count() == 1)
213 SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
228 void SendCoinsDialog::handleURL(const QUrl *url)
230 SendCoinsRecipient rv;
231 if(!GUIUtil::parseBitcoinURL(url, &rv))