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