-
Notifications
You must be signed in to change notification settings - Fork 95
/
addtokenpage.cpp
executable file
·172 lines (154 loc) · 5.43 KB
/
addtokenpage.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <boost/asio/connect.hpp>
#include "addtokenpage.h"
#include "ui_addtokenpage.h"
#include "guiconstants.h"
#include "wallet.h"
#include "walletmodel.h"
#include "token.h"
#include "clientmodel.h"
#include "qvalidatedlineedit.h"
#include "contractabi.h"
#include "main.h"
#include "addresstablemodel.h"
#include "optionsmodel.h"
#include <QRegularExpressionValidator>
#include <QMessageBox>
AddTokenPage::AddTokenPage(QWidget *parent) :
QWidget(parent),
ui(new Ui::AddTokenPage),
m_tokenABI(0),
m_model(0),
m_clientModel(0)
{
ui->setupUi(this);
(void)ui->lineEditContractAddress;
(void)ui->lineEditTokenName;
(void)ui->lineEditTokenSymbol;
(void)ui->lineEditDecimals;
m_tokenABI = new Token();
connect(ui->lineEditContractAddress, SIGNAL(textChanged(const QString &)), this, SLOT(on_addressChanged()));
connect(ui->lineEditTokenName, SIGNAL(textChanged(const QString &)), SLOT(on_updateConfirmButton()));
connect(ui->lineEditTokenSymbol, SIGNAL(textChanged(const QString &)), SLOT(on_updateConfirmButton()));
ui->lineEditSenderAddress->setAddressColumn(AddressTableModel::Address);
ui->lineEditSenderAddress->setTypeRole(AddressTableModel::TypeRole);
ui->lineEditSenderAddress->setReceive(AddressTableModel::Receive);
if(ui->lineEditSenderAddress->isEditable())
((QValidatedLineEdit*)ui->lineEditSenderAddress->lineEdit())->setEmptyIsValid(false);
m_validTokenAddress = false;
}
AddTokenPage::~AddTokenPage()
{
delete ui;
if(m_tokenABI)
delete m_tokenABI;
m_tokenABI = 0;
}
void AddTokenPage::setClientModel(ClientModel *clientModel)
{
m_clientModel = clientModel;
if (m_clientModel)
{
connect(m_clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(on_numBlocksChanged(int)));
on_numBlocksChanged(1);
}
}
void AddTokenPage::clearAll()
{
ui->lineEditContractAddress->setText("");
ui->lineEditTokenName->setText("");
ui->lineEditTokenSymbol->setText("");
ui->lineEditDecimals->setText("");
ui->lineEditSenderAddress->setCurrentIndex(-1);
}
void AddTokenPage::setModel(WalletModel *_model)
{
m_model = _model;
on_zeroBalanceAddressToken(bZeroBalanceAddressToken);
connect(m_model->getOptionsModel(), SIGNAL(zeroBalanceAddressTokenChanged(bool)), this, SLOT(on_zeroBalanceAddressToken(bool)));
}
void AddTokenPage::on_clearButton_clicked()
{
clearAll();
}
void AddTokenPage::on_confirmButton_clicked()
{
if(ui->lineEditSenderAddress->isValidAddress())
{
CTokenInfo tokenInfo;
tokenInfo.strContractAddress = ui->lineEditContractAddress->text().toStdString();
tokenInfo.strTokenName = ui->lineEditTokenName->text().toStdString();
tokenInfo.strTokenSymbol = ui->lineEditTokenSymbol->text().toStdString();
tokenInfo.nDecimals = ui->lineEditDecimals->text().toInt();
tokenInfo.strSenderAddress = ui->lineEditSenderAddress->currentText().toStdString();
if(m_model)
{
if(!m_model->isMineAddress(tokenInfo.strSenderAddress))
{
QString symbol = QString::fromStdString(tokenInfo.strTokenSymbol);
QString address = QString::fromStdString(tokenInfo.strSenderAddress);
QString message = tr("The %1 address \"%2\" \n").arg(symbol, address);
QMessageBox::warning(this, tr("Invalid token address"), message);
}
else if(m_model->existTokenEntry(tokenInfo))
{
QMessageBox::information(this, tr("Token exist"), tr("The token already exist with the specified contract and sender addresses."));
}
else
{
m_model->addTokenEntry(tokenInfo);
clearAll();
if(!fLogEvents)
{
QMessageBox::information(this, tr("Log events"), tr("Enable log events from the option menu in order to receive token transactions."));
}
}
}
}
}
void AddTokenPage::on_addressChanged()
{
QString tokenAddress = ui->lineEditContractAddress->text();
if(m_tokenABI)
{
m_tokenABI->setAddress(tokenAddress.toStdString());
std::string name, symbol, decimals;
bool ret = m_tokenABI->name(name);
ret &= m_tokenABI->symbol(symbol);
ret &= m_tokenABI->decimals(decimals);
ui->lineEditTokenName->setText(QString::fromStdString(name));
ui->lineEditTokenSymbol->setText(QString::fromStdString(symbol));
ui->lineEditDecimals->setText(QString::fromStdString(decimals));
m_validTokenAddress = ret;
}
ui->confirmButton->setEnabled(m_validTokenAddress);
}
void AddTokenPage::on_numBlocksChanged(int newHeight)
{
if (lastUpdatedHeight < newHeight) {
ui->lineEditSenderAddress->on_refresh();
lastUpdatedHeight = newHeight;
}
}
void AddTokenPage::on_updateConfirmButton()
{
bool enabled = true;
if(ui->lineEditTokenName->text().isEmpty())
{
enabled = false;
}
if(ui->lineEditTokenSymbol->text().isEmpty())
{
enabled = false;
}
enabled &= m_validTokenAddress;
ui->confirmButton->setEnabled(enabled);
}
void AddTokenPage::on_zeroBalanceAddressToken(bool enable)
{
QAbstractItemModel *addressTableModel = 0;
if(enable && m_model)
{
addressTableModel = m_model->getAddressTableModel();
}
ui->lineEditSenderAddress->setAddressTableModel(addressTableModel);
}