9b7cc632e0c6164b2929ea7caa048ab9b5456566
[novacoin.git] / src / qt / sendcoinsdialog.cpp
1 #include "sendcoinsdialog.h"
2 #include "ui_sendcoinsdialog.h"
3 #include "walletmodel.h"
4 #include "guiutil.h"
5
6 #include "addressbookpage.h"
7 #include "optionsmodel.h"
8
9 #include <QApplication>
10 #include <QClipboard>
11 #include <QMessageBox>
12 #include <QLocale>
13 #include <QDebug>
14 #include <QMessageBox>
15
16 SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
17     QDialog(parent),
18     ui(new Ui::SendCoinsDialog),
19     model(0)
20 {
21     ui->setupUi(this);
22 #if QT_VERSION >= 0x040700
23     ui->payTo->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
24     ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
25 #endif
26
27     GUIUtil::setupAddressWidget(ui->payTo, this);
28
29     // Set initial send-to address if provided
30     if(!address.isEmpty())
31     {
32         ui->payTo->setText(address);
33         ui->payAmount->setFocus();
34     }
35 }
36
37 void SendCoinsDialog::setModel(WalletModel *model)
38 {
39     this->model = model;
40 }
41
42 SendCoinsDialog::~SendCoinsDialog()
43 {
44     delete ui;
45 }
46
47 void SendCoinsDialog::on_sendButton_clicked()
48 {
49     bool valid;
50     QString payAmount = ui->payAmount->text();
51     QString label;
52     qint64 payAmountParsed;
53
54     valid = GUIUtil::parseMoney(payAmount, &payAmountParsed);
55
56     if(!valid || payAmount.isEmpty())
57     {
58         QMessageBox::warning(this, tr("Send Coins"),
59             tr("Must fill in an amount to pay."),
60             QMessageBox::Ok, QMessageBox::Ok);
61         return;
62     }
63
64     // Add address to address book under label, if specified
65     label = ui->addAsLabel->text();
66
67     QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
68                           tr("Are you sure you want to send %1 BTC to %2 (%3)?").arg(GUIUtil::formatMoney(payAmountParsed), label, ui->payTo->text()),
69           QMessageBox::Yes|QMessageBox::Cancel,
70           QMessageBox::Cancel);
71
72     if(retval != QMessageBox::Yes)
73     {
74         return;
75     }
76
77     switch(model->sendCoins(ui->payTo->text(), payAmountParsed, label))
78     {
79     case WalletModel::InvalidAddress:
80         QMessageBox::warning(this, tr("Send Coins"),
81             tr("The recepient address is not valid, please recheck."),
82             QMessageBox::Ok, QMessageBox::Ok);
83         ui->payTo->setFocus();
84         break;
85     case WalletModel::InvalidAmount:
86         QMessageBox::warning(this, tr("Send Coins"),
87             tr("The amount to pay must be larger than 0."),
88             QMessageBox::Ok, QMessageBox::Ok);
89         ui->payAmount->setFocus();
90         break;
91     case WalletModel::AmountExceedsBalance:
92         QMessageBox::warning(this, tr("Send Coins"),
93             tr("Amount exceeds your balance"),
94             QMessageBox::Ok, QMessageBox::Ok);
95         ui->payAmount->setFocus();
96         break;
97     case WalletModel::AmountWithFeeExceedsBalance:
98         QMessageBox::warning(this, tr("Send Coins"),
99             tr("Total exceeds your balance when the %1 transaction fee is included").
100             arg(GUIUtil::formatMoney(model->getOptionsModel()->getTransactionFee())),
101             QMessageBox::Ok, QMessageBox::Ok);
102         ui->payAmount->setFocus();
103         break;
104     case WalletModel::OK:
105         accept();
106         break;
107     }
108 }
109
110 void SendCoinsDialog::on_pasteButton_clicked()
111 {
112     // Paste text from clipboard into recipient field
113     ui->payTo->setText(QApplication::clipboard()->text());
114 }
115
116 void SendCoinsDialog::on_addressBookButton_clicked()
117 {
118     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
119     dlg.setModel(model->getAddressTableModel());
120     if(dlg.exec())
121     {
122         ui->payTo->setText(dlg.getReturnValue());
123         ui->payAmount->setFocus();
124     }
125 }
126
127 void SendCoinsDialog::on_buttonBox_rejected()
128 {
129     reject();
130 }
131
132 void SendCoinsDialog::on_payTo_textChanged(const QString &address)
133 {
134     ui->addAsLabel->setText(model->labelForAddress(address));
135 }
136
137 void SendCoinsDialog::clear()
138 {
139     ui->payTo->setText(QString());
140     ui->addAsLabel->setText(QString());
141     ui->payAmount->setText(QString());
142     ui->payTo->setFocus();
143     ui->sendButton->setDefault(true);
144 }
145
146 void SendCoinsDialog::reject()
147 {
148     clear();
149 }
150
151 void SendCoinsDialog::accept()
152 {
153     clear();
154 }