Somewhat confident now, tested on GNOME+KDE, with all types of transactions. Next...
[novacoin.git] / src / qt / sendcoinsdialog.cpp
1 #include "sendcoinsdialog.h"
2 #include "ui_sendcoinsdialog.h"
3 #include "clientmodel.h"
4 #include "guiutil.h"
5
6 #include "addressbookdialog.h"
7 #include "optionsmodel.h"
8
9 #include <QApplication>
10 #include <QClipboard>
11 #include <QMessageBox>
12 #include <QLocale>
13 #include <QDebug>
14
15 #include "util.h"
16 #include "base58.h"
17
18 SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
19     QDialog(parent),
20     ui(new Ui::SendCoinsDialog),
21     model(0)
22 {
23     ui->setupUi(this);
24
25     GUIUtil::setupAddressWidget(ui->payTo, this);
26     GUIUtil::setupAmountWidget(ui->payAmount, this);
27
28     /* Set initial address if provided */
29     if(!address.isEmpty())
30     {
31         ui->payTo->setText(address);
32         ui->payAmount->setFocus();
33     }
34 }
35
36 void SendCoinsDialog::setModel(ClientModel *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     qint64 payAmountParsed;
51
52     valid = ParseMoney(payAmount.toStdString(), payAmountParsed);
53
54     if(!valid)
55     {
56         QMessageBox::warning(this, tr("Send Coins"),
57             tr("The amount to pay must be a valid number."),
58             QMessageBox::Ok, QMessageBox::Ok);
59         return;
60     }
61
62     switch(model->sendCoins(ui->payTo->text(), payAmountParsed))
63     {
64     case ClientModel::InvalidAddress:
65         QMessageBox::warning(this, tr("Send Coins"),
66             tr("The recepient address is not valid, please recheck."),
67             QMessageBox::Ok, QMessageBox::Ok);
68         ui->payTo->setFocus();
69         break;
70     case ClientModel::InvalidAmount:
71         QMessageBox::warning(this, tr("Send Coins"),
72             tr("The amount to pay must be larger than 0."),
73             QMessageBox::Ok, QMessageBox::Ok);
74         ui->payAmount->setFocus();
75         break;
76     case ClientModel::AmountExceedsBalance:
77         QMessageBox::warning(this, tr("Send Coins"),
78             tr("Amount exceeds your balance"),
79             QMessageBox::Ok, QMessageBox::Ok);
80         ui->payAmount->setFocus();
81         break;
82     case ClientModel::AmountWithFeeExceedsBalance:
83         QMessageBox::warning(this, tr("Send Coins"),
84             tr("Total exceeds your balance when the %1 transaction fee is included").
85                 arg(QString::fromStdString(FormatMoney(model->getOptionsModel()->getTransactionFee()))),
86             QMessageBox::Ok, QMessageBox::Ok);
87         ui->payAmount->setFocus();
88         break;
89     case ClientModel::OK:
90         accept();
91         break;
92     }
93 }
94
95 void SendCoinsDialog::on_pasteButton_clicked()
96 {
97     /* Paste text from clipboard into recipient field */
98     ui->payTo->setText(QApplication::clipboard()->text());
99 }
100
101 void SendCoinsDialog::on_addressBookButton_clicked()
102 {
103     AddressBookDialog dlg;
104     dlg.setModel(model->getAddressTableModel());
105     dlg.setTab(AddressBookDialog::SendingTab);
106     dlg.exec();
107     ui->payTo->setText(dlg.getReturnValue());
108 }
109
110 void SendCoinsDialog::on_buttonBox_rejected()
111 {
112     reject();
113 }