58eb5c21f79085c58147fdc1f1cb724016acd7a9
[novacoin.git] / src / qt / sendcoinsdialog.cpp
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"
8 #include "guiutil.h"
9 #include "askpassphrasedialog.h"
10
11 #include <QMessageBox>
12 #include <QLocale>
13 #include <QTextDocument>
14
15 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
16     QDialog(parent),
17     ui(new Ui::SendCoinsDialog),
18     model(0)
19 {
20     ui->setupUi(this);
21
22     addEntry();
23
24     connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
25     connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
26 }
27
28 void SendCoinsDialog::setModel(WalletModel *model)
29 {
30     this->model = model;
31
32     for(int i = 0; i < ui->entries->count(); ++i)
33     {
34         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
35         if(entry)
36         {
37             entry->setModel(model);
38         }
39     }
40
41     setBalance(model->getBalance(), model->getUnconfirmedBalance());
42     connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
43 }
44
45 SendCoinsDialog::~SendCoinsDialog()
46 {
47     delete ui;
48 }
49
50 void SendCoinsDialog::on_sendButton_clicked()
51 {
52     QList<SendCoinsRecipient> recipients;
53     bool valid = true;
54     for(int i = 0; i < ui->entries->count(); ++i)
55     {
56         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
57         if(entry)
58         {
59             if(entry->validate())
60             {
61                 recipients.append(entry->getValue());
62             }
63             else
64             {
65                 valid = false;
66             }
67         }
68     }
69
70     if(!valid || recipients.isEmpty())
71     {
72         return;
73     }
74
75     // Format confirmation message
76     QStringList formatted;
77     foreach(const SendCoinsRecipient &rcp, recipients)
78     {
79         formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
80     }
81
82     QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
83                           tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
84           QMessageBox::Yes|QMessageBox::Cancel,
85           QMessageBox::Cancel);
86
87     if(retval != QMessageBox::Yes)
88     {
89         return;
90     }
91
92     WalletModel::UnlockContext ctx(model->requestUnlock());
93     if(!ctx.isValid())
94     {
95         // Unlock wallet was cancelled
96         return;
97     }
98
99     WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
100     switch(sendstatus.status)
101     {
102     case WalletModel::InvalidAddress:
103         QMessageBox::warning(this, tr("Send Coins"),
104             tr("The recepient address is not valid, please recheck."),
105             QMessageBox::Ok, QMessageBox::Ok);
106         break;
107     case WalletModel::InvalidAmount:
108         QMessageBox::warning(this, tr("Send Coins"),
109             tr("The amount to pay must be larger than 0."),
110             QMessageBox::Ok, QMessageBox::Ok);
111         break;
112     case WalletModel::AmountExceedsBalance:
113         QMessageBox::warning(this, tr("Send Coins"),
114             tr("Amount exceeds your balance"),
115             QMessageBox::Ok, QMessageBox::Ok);
116         break;
117     case WalletModel::AmountWithFeeExceedsBalance:
118         QMessageBox::warning(this, tr("Send Coins"),
119             tr("Total exceeds your balance when the %1 transaction fee is included").
120             arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
121             QMessageBox::Ok, QMessageBox::Ok);
122         break;
123     case WalletModel::DuplicateAddress:
124         QMessageBox::warning(this, tr("Send Coins"),
125             tr("Duplicate address found, can only send to each address once in one send operation"),
126             QMessageBox::Ok, QMessageBox::Ok);
127         break;
128     case WalletModel::TransactionCreationFailed:
129         QMessageBox::warning(this, tr("Send Coins"),
130             tr("Error: Transaction creation failed  "),
131             QMessageBox::Ok, QMessageBox::Ok);
132         break;
133     case WalletModel::TransactionCommitFailed:
134         QMessageBox::warning(this, tr("Send Coins"),
135             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."),
136             QMessageBox::Ok, QMessageBox::Ok);
137         break;
138     case WalletModel::OK:
139         accept();
140         break;
141     }
142 }
143
144 void SendCoinsDialog::clear()
145 {
146     // Remove entries until only one left
147     while(ui->entries->count())
148     {
149         delete ui->entries->takeAt(0)->widget();
150     }
151     addEntry();
152
153     updateRemoveEnabled();
154
155     ui->sendButton->setDefault(true);
156 }
157
158 void SendCoinsDialog::reject()
159 {
160     clear();
161 }
162
163 void SendCoinsDialog::accept()
164 {
165     clear();
166 }
167
168 SendCoinsEntry *SendCoinsDialog::addEntry()
169 {
170     SendCoinsEntry *entry = new SendCoinsEntry(this);
171     entry->setModel(model);
172     ui->entries->addWidget(entry);
173     connect(entry, SIGNAL(removeEntry(SendCoinsEntry*)), this, SLOT(removeEntry(SendCoinsEntry*)));
174
175     updateRemoveEnabled();
176
177     // Focus the field, so that entry can start immediately
178     entry->clear();
179     return entry;
180 }
181
182 void SendCoinsDialog::updateRemoveEnabled()
183 {
184     // Remove buttons are enabled as soon as there is more than one send-entry
185     bool enabled = (ui->entries->count() > 1);
186     for(int i = 0; i < ui->entries->count(); ++i)
187     {
188         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
189         if(entry)
190         {
191             entry->setRemoveEnabled(enabled);
192         }
193     }
194     setupTabChain(0);
195 }
196
197 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
198 {
199     delete entry;
200     updateRemoveEnabled();
201 }
202
203 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
204 {
205     for(int i = 0; i < ui->entries->count(); ++i)
206     {
207         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
208         if(entry)
209         {
210             prev = entry->setupTabChain(prev);
211         }
212     }
213     QWidget::setTabOrder(prev, ui->addButton);
214     QWidget::setTabOrder(ui->addButton, ui->sendButton);
215     return ui->sendButton;
216 }
217
218 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
219 {
220     SendCoinsEntry *entry = 0;
221     // Replace the first entry if it is still unused
222     if(ui->entries->count() == 1)
223     {
224         SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
225         if(first->isClear())
226         {
227             entry = first;
228         }
229     }
230     if(!entry)
231     {
232         entry = addEntry();
233     }
234
235     entry->setValue(rv);
236 }
237
238
239 void SendCoinsDialog::handleURL(const QUrl *url)
240 {
241     SendCoinsRecipient rv;
242     if(!GUIUtil::parseBitcoinURL(url, &rv))
243     {
244         return;
245     }
246     pasteEntry(rv);
247 }
248
249 void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
250 {
251     Q_UNUSED(unconfirmedBalance);
252     int unit = model->getOptionsModel()->getDisplayUnit();
253     ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
254 }