91397a7d25f0fe48f5c4aefa0a5cdfa04cccfaef
[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     GUIUtil::setupAddressWidget(ui->payTo, this);
27
28     // Set initial send-to address if provided
29     if(!address.isEmpty())
30     {
31         ui->payTo->setText(address);
32         ui->payAmount->setFocus();
33     }
34 }
35
36 void SendCoinsDialog::setModel(WalletModel *model)
37 {
38     this->model = model;
39 }
40
41 SendCoinsDialog::~SendCoinsDialog()
42 {
43     delete ui;
44 }
45
46 void SendCoinsDialog::on_sendButton_clicked()
47 {
48     bool valid;
49     QString payAmount = ui->payAmount->text();
50     QString label;
51     qint64 payAmountParsed;
52
53     valid = GUIUtil::parseMoney(payAmount, &payAmountParsed);
54
55     if(!valid || payAmount.isEmpty())
56     {
57         QMessageBox::warning(this, tr("Send Coins"),
58             tr("Must fill in an amount to pay."),
59             QMessageBox::Ok, QMessageBox::Ok);
60         return;
61     }
62
63     // Add address to address book under label, if specified
64     label = ui->addAsLabel->text();
65
66     QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
67                           tr("Are you sure you want to send %1 BTC to %2 (%3)?").arg(GUIUtil::formatMoney(payAmountParsed), label, ui->payTo->text()),
68           QMessageBox::Yes|QMessageBox::Cancel,
69           QMessageBox::Cancel);
70
71     if(retval != QMessageBox::Yes)
72     {
73         return;
74     }
75
76     switch(model->sendCoins(ui->payTo->text(), payAmountParsed, label))
77     {
78     case WalletModel::InvalidAddress:
79         QMessageBox::warning(this, tr("Send Coins"),
80             tr("The recepient address is not valid, please recheck."),
81             QMessageBox::Ok, QMessageBox::Ok);
82         ui->payTo->setFocus();
83         break;
84     case WalletModel::InvalidAmount:
85         QMessageBox::warning(this, tr("Send Coins"),
86             tr("The amount to pay must be larger than 0."),
87             QMessageBox::Ok, QMessageBox::Ok);
88         ui->payAmount->setFocus();
89         break;
90     case WalletModel::AmountExceedsBalance:
91         QMessageBox::warning(this, tr("Send Coins"),
92             tr("Amount exceeds your balance"),
93             QMessageBox::Ok, QMessageBox::Ok);
94         ui->payAmount->setFocus();
95         break;
96     case WalletModel::AmountWithFeeExceedsBalance:
97         QMessageBox::warning(this, tr("Send Coins"),
98             tr("Total exceeds your balance when the %1 transaction fee is included").
99             arg(GUIUtil::formatMoney(model->getOptionsModel()->getTransactionFee())),
100             QMessageBox::Ok, QMessageBox::Ok);
101         ui->payAmount->setFocus();
102         break;
103     case WalletModel::OK:
104         accept();
105         break;
106     }
107 }
108
109 void SendCoinsDialog::on_pasteButton_clicked()
110 {
111     // Paste text from clipboard into recipient field
112     ui->payTo->setText(QApplication::clipboard()->text());
113 }
114
115 void SendCoinsDialog::on_addressBookButton_clicked()
116 {
117     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
118     dlg.setModel(model->getAddressTableModel());
119     if(dlg.exec())
120     {
121         ui->payTo->setText(dlg.getReturnValue());
122         ui->payAmount->setFocus();
123     }
124 }
125
126 void SendCoinsDialog::on_buttonBox_rejected()
127 {
128     reject();
129 }
130
131 void SendCoinsDialog::on_payTo_textChanged(const QString &address)
132 {
133     ui->addAsLabel->setText(model->labelForAddress(address));
134 }
135
136 void SendCoinsDialog::clear()
137 {
138     ui->payTo->setText(QString());
139     ui->addAsLabel->setText(QString());
140     ui->payAmount->setText(QString());
141     ui->payTo->setFocus();
142 }
143
144 void SendCoinsDialog::reject()
145 {
146     clear();
147 }
148
149 void SendCoinsDialog::accept()
150 {
151     clear();
152 }