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() > 1)
138 delete ui->entries->takeAt(0)->widget();
141 // Reset the entry that is left to empty
142 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
148 updateRemoveEnabled();
150 ui->sendButton->setDefault(true);
153 void SendCoinsDialog::reject()
158 void SendCoinsDialog::accept()
163 void SendCoinsDialog::addEntry()
165 SendCoinsEntry *entry = new SendCoinsEntry(this);
166 entry->setModel(model);
167 ui->entries->addWidget(entry);
168 connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
170 updateRemoveEnabled();
172 // Focus the field, so that entry can start immediately
176 void SendCoinsDialog::updateRemoveEnabled()
178 // Remove buttons are enabled as soon as there is more than one send-entry
179 bool enabled = (ui->entries->count() > 1);
180 for(int i = 0; i < ui->entries->count(); ++i)
182 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
185 entry->setRemoveEnabled(enabled);
191 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
194 updateRemoveEnabled();
197 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
199 for(int i = 0; i < ui->entries->count(); ++i)
201 SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
204 prev = entry->setupTabChain(prev);
207 QWidget::setTabOrder(prev, ui->addButton);
208 QWidget::setTabOrder(ui->addButton, ui->sendButton);
209 return ui->sendButton;