54cae21a1b28a184de5323a9b9dfef3c190ca08f
[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
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() > 1)
137     {
138         delete ui->entries->takeAt(0)->widget();
139     }
140
141     // Reset the entry that is left to empty
142     SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
143     if(entry)
144     {
145         entry->clear();
146     }
147
148     updateRemoveEnabled();
149
150     ui->sendButton->setDefault(true);
151 }
152
153 void SendCoinsDialog::reject()
154 {
155     clear();
156 }
157
158 void SendCoinsDialog::accept()
159 {
160     clear();
161 }
162
163 void SendCoinsDialog::addEntry()
164 {
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*)));
169
170     updateRemoveEnabled();
171
172     // Focus the field, so that entry can start immediately
173     entry->clear();
174 }
175
176 void SendCoinsDialog::updateRemoveEnabled()
177 {
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)
181     {
182         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
183         if(entry)
184         {
185             entry->setRemoveEnabled(enabled);
186         }
187     }
188     setupTabChain(0);
189 }
190
191 void SendCoinsDialog::removeEntry(SendCoinsEntry* entry)
192 {
193     delete entry;
194     updateRemoveEnabled();
195 }
196
197 QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
198 {
199     for(int i = 0; i < ui->entries->count(); ++i)
200     {
201         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
202         if(entry)
203         {
204             prev = entry->setupTabChain(prev);
205         }
206     }
207     QWidget::setTabOrder(prev, ui->addButton);
208     QWidget::setTabOrder(ui->addButton, ui->sendButton);
209     return ui->sendButton;
210 }