aboutsummaryrefslogtreecommitdiff
path: root/src/qt/sendcoinsdialog.cpp
blob: 8050baff38f20e5baefa773d82533a706e36076a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "sendcoinsdialog.h"
#include "ui_sendcoinsdialog.h"
#include "walletmodel.h"
#include "guiutil.h"

#include "addressbookpage.h"
#include "optionsmodel.h"

#include <QApplication>
#include <QClipboard>
#include <QMessageBox>
#include <QLocale>
#include <QDebug>
#include <QMessageBox>

SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
    QDialog(parent),
    ui(new Ui::SendCoinsDialog),
    model(0)
{
    ui->setupUi(this);
#if QT_VERSION >= 0x040700
    ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
#endif
    GUIUtil::setupAddressWidget(ui->payTo, this);

    // Set initial send-to address if provided
    if(!address.isEmpty())
    {
        ui->payTo->setText(address);
        ui->payAmount->setFocus();
    }
}

void SendCoinsDialog::setModel(WalletModel *model)
{
    this->model = model;
}

SendCoinsDialog::~SendCoinsDialog()
{
    delete ui;
}

void SendCoinsDialog::on_sendButton_clicked()
{
    bool valid;
    QString payAmount = ui->payAmount->text();
    QString label;
    qint64 payAmountParsed;

    valid = GUIUtil::parseMoney(payAmount, &payAmountParsed);

    if(!valid || payAmount.isEmpty())
    {
        QMessageBox::warning(this, tr("Send Coins"),
            tr("Must fill in an amount to pay."),
            QMessageBox::Ok, QMessageBox::Ok);
        return;
    }

    // Add address to address book under label, if specified
    label = ui->addAsLabel->text();

    QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
                          tr("Are you sure you want to send %1 BTC to %2 (%3)?").arg(GUIUtil::formatMoney(payAmountParsed), label, ui->payTo->text()),
          QMessageBox::Yes|QMessageBox::Cancel,
          QMessageBox::Cancel);

    if(retval != QMessageBox::Yes)
    {
        return;
    }

    switch(model->sendCoins(ui->payTo->text(), payAmountParsed, label))
    {
    case WalletModel::InvalidAddress:
        QMessageBox::warning(this, tr("Send Coins"),
            tr("The recepient address is not valid, please recheck."),
            QMessageBox::Ok, QMessageBox::Ok);
        ui->payTo->setFocus();
        break;
    case WalletModel::InvalidAmount:
        QMessageBox::warning(this, tr("Send Coins"),
            tr("The amount to pay must be larger than 0."),
            QMessageBox::Ok, QMessageBox::Ok);
        ui->payAmount->setFocus();
        break;
    case WalletModel::AmountExceedsBalance:
        QMessageBox::warning(this, tr("Send Coins"),
            tr("Amount exceeds your balance"),
            QMessageBox::Ok, QMessageBox::Ok);
        ui->payAmount->setFocus();
        break;
    case WalletModel::AmountWithFeeExceedsBalance:
        QMessageBox::warning(this, tr("Send Coins"),
            tr("Total exceeds your balance when the %1 transaction fee is included").
            arg(GUIUtil::formatMoney(model->getOptionsModel()->getTransactionFee())),
            QMessageBox::Ok, QMessageBox::Ok);
        ui->payAmount->setFocus();
        break;
    case WalletModel::OK:
        accept();
        break;
    }
}

void SendCoinsDialog::on_pasteButton_clicked()
{
    // Paste text from clipboard into recipient field
    ui->payTo->setText(QApplication::clipboard()->text());
}

void SendCoinsDialog::on_addressBookButton_clicked()
{
    AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab);
    dlg.setModel(model->getAddressTableModel());
    dlg.exec();
    ui->payTo->setText(dlg.getReturnValue());
    ui->payAmount->setFocus();
}

void SendCoinsDialog::on_buttonBox_rejected()
{
    reject();
}

void SendCoinsDialog::on_payTo_textChanged(const QString &address)
{
    ui->addAsLabel->setText(model->labelForAddress(address));
}

void SendCoinsDialog::clear()
{
    ui->payTo->setText(QString());
    ui->addAsLabel->setText(QString());
    ui->payAmount->setText(QString());
    ui->payTo->setFocus();
}

void SendCoinsDialog::reject()
{
    clear();
}

void SendCoinsDialog::accept()
{
    clear();
}