a9a89c282b95304320aabdb100be8491897abdc6
[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
10 #include <QMessageBox>
11 #include <QLocale>
12 #include <QTextDocument>
13
14 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
15     QDialog(parent),
16     ui(new Ui::SendCoinsDialog),
17     model(0)
18 {
19     ui->setupUi(this);
20
21     addEntry();
22
23     connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
24 }
25
26 void SendCoinsDialog::setModel(WalletModel *model)
27 {
28     this->model = model;
29
30     for(int i = 0; i < ui->entries->count(); ++i)
31     {
32         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
33         if(entry)
34         {
35             entry->setModel(model);
36         }
37     }
38 }
39
40 SendCoinsDialog::~SendCoinsDialog()
41 {
42     delete ui;
43 }
44
45 void SendCoinsDialog::on_sendButton_clicked()
46 {
47     QList<SendCoinsRecipient> recipients;
48     bool valid = true;
49     for(int i = 0; i < ui->entries->count(); ++i)
50     {
51         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
52         if(entry)
53         {
54             if(entry->validate())
55             {
56                 recipients.append(entry->getValue());
57             }
58             else
59             {
60                 valid = false;
61             }
62         }
63     }
64
65     if(!valid || recipients.isEmpty())
66     {
67         return;
68     }
69
70     // Format confirmation message
71     QStringList formatted;
72     foreach(const SendCoinsRecipient &rcp, recipients)
73     {
74         formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
75     }
76
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,
80           QMessageBox::Cancel);
81
82     if(retval != QMessageBox::Yes)
83     {
84         return;
85     }
86
87     WalletModel::SendCoinsReturn sendstatus = model->sendCoins(recipients);
88     switch(sendstatus.status)
89     {
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);
94         break;
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);
99         break;
100     case WalletModel::AmountExceedsBalance:
101         QMessageBox::warning(this, tr("Send Coins"),
102             tr("Amount exceeds your balance"),
103             QMessageBox::Ok, QMessageBox::Ok);
104         break;
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);
110         break;
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);
115         break;
116     case WalletModel::TransactionCreationFailed:
117         QMessageBox::warning(this, tr("Send Coins"),
118             tr("Error: Transaction creation failed  "),
119             QMessageBox::Ok, QMessageBox::Ok);
120         break;
121         break;
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);
126         break;
127     case WalletModel::OK:
128         accept();
129         break;
130     }
131 }
132
133 void SendCoinsDialog::clear()
134 {
135     // Remove entries until only one left
136     while(ui->entries->count())
137     {
138         delete ui->entries->takeAt(0)->widget();
139     }
140     addEntry();
141
142     updateRemoveEnabled();
143
144     ui->sendButton->setDefault(true);
145 }
146
147 void SendCoinsDialog::reject()
148 {
149     clear();
150 }
151
152 void SendCoinsDialog::accept()
153 {
154     clear();
155 }
156
157 SendCoinsEntry *SendCoinsDialog::addEntry()
158 {
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*)));
163
164     updateRemoveEnabled();
165
166     // Focus the field, so that entry can start immediately
167     entry->clear();
168     return entry;
169 }
170
171 void SendCoinsDialog::updateRemoveEnabled()
172 {
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)
176     {
177         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
178         if(entry)
179         {
180             entry->setRemoveEnabled(enabled);
181         }
182     }
183     setupTabChain(0);
184 }
185
186 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
187 {
188     delete entry;
189     updateRemoveEnabled();
190 }
191
192 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
193 {
194     for(int i = 0; i < ui->entries->count(); ++i)
195     {
196         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
197         if(entry)
198         {
199             prev = entry->setupTabChain(prev);
200         }
201     }
202     QWidget::setTabOrder(prev, ui->addButton);
203     QWidget::setTabOrder(ui->addButton, ui->sendButton);
204     return ui->sendButton;
205 }
206
207 void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
208 {
209     SendCoinsEntry *entry = 0;
210     // Replace the first entry if it is still unused
211     if(ui->entries->count() == 1)
212     {
213         SendCoinsEntry *first = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
214         if(first->isClear())
215         {
216             entry = first;
217         }
218     }
219     if(!entry)
220     {
221         entry = addEntry();
222     }
223
224     entry->setValue(rv);
225 }
226
227
228 void SendCoinsDialog::handleURL(const QUrl *url)
229 {
230     SendCoinsRecipient rv;
231     if(!GUIUtil::parseBitcoinURL(url, &rv))
232     {
233         return;
234     }
235     pasteEntry(rv);
236 }