-
Notifications
You must be signed in to change notification settings - Fork 0
/
applydelegatepage.cpp
443 lines (367 loc) · 14.9 KB
/
applydelegatepage.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "applydelegatepage.h"
#include "ui_applydelegatepage.h"
#include "fry.h"
#include "commondialog.h"
#include "debug_log.h"
#include "rpcthread.h"
#include "gop_common_define.h"
#include <QPainter>
#include <QUrl>
#include <QDesktopServices>
#include <QDebug>
ApplyDelegatePage::ApplyDelegatePage( QString name, QWidget *parent) :
QWidget(parent),
inited(false),
ui(new Ui::ApplyDelegatePage)
{
ui->setupUi(this);
setAutoFillBackground(true);
QPalette palette;
palette.setColor(QPalette::Background, QColor(255,255,255));
setPalette(palette);
connect( Fry::getInstance(), SIGNAL(jsonDataUpdated(QString)), this, SLOT(jsonDataUpdated(QString)));
mutexForRegisterMap.lock();
for (QMap<QString,QString>::const_iterator i = Fry::getInstance()->registerMap.constBegin(); i != Fry::getInstance()->registerMap.constEnd(); ++i)
{
if( i.value() != "" && !Fry::getInstance()->delegateAccountList.contains( i.key()))
{
ui->accountComboBox->addItem( i.key());
}
}
mutexForRegisterMap.unlock();
if( name.isEmpty())
{
ui->accountComboBox->setCurrentIndex(0);
accountName = ui->accountComboBox->currentText();
}
else
{
ui->accountComboBox->setCurrentText(name);
accountName = name;
}
// setStyleSheet("#ApplyDelegatePage{background-color: rgb(255, 255, 255);}");
ui->accountComboBox->setStyleSheet("QComboBox {border: 1px solid gray;border-radius: 3px;padding: 1px 2px 1px 8px;min-width: 9em;}"
"QComboBox::drop-down {subcontrol-origin: padding;subcontrol-position: top right;width: 20px;"
"border-left-width: 1px;border-left-color: darkgray;border-left-style: solid;"
"border-top-right-radius: 3px;border-bottom-right-radius: 3px;}"
"QComboBox::down-arrow {image: url(pic2/comboBoxArrow.png);}"
);
QRegExp regx("[a-z][a-z0-9]+$");
QValidator *validator = new QRegExpValidator(regx, this);
ui->addressNameLineEdit->setValidator( validator );
ui->addressNameLineEdit->setPlaceholderText( tr("Set your account name"));
ui->addressNameLineEdit->setAttribute(Qt::WA_InputMethodEnabled, false);
ui->addressNameLineEdit->setStyleSheet("color:black;border:1px solid #CCCCCC;border-radius:3px;");
ui->addressNameLineEdit->setTextMargins(8,0,0,0);
ui->wrongLabel->hide();
ui->wrongLabel2->hide();
ui->correctLabel->hide();
init();
inited = true;
}
ApplyDelegatePage::~ApplyDelegatePage()
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
delete ui;
DLOG_QT_WALLET_FUNCTION_END;
}
void ApplyDelegatePage::init()
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
// 如果是已注册账户
if( Fry::getInstance()->registerMapValue(accountName) != "NO"
&& !Fry::getInstance()->registerMapValue(accountName).isEmpty())
{
ui->label_6->hide();
ui->addressNameLineEdit->hide();
ui->correctLabel->hide();
ui->wrongLabel->hide();
ui->wrongLabel2->hide();
ui->tipLabel2->hide();
ui->upgradeBtn->move(180,346);
}
else
{
ui->label_6->show();
ui->addressNameLineEdit->show();
ui->tipLabel2->show();
ui->upgradeBtn->move(180,430);
ui->addressNameLineEdit->setText( accountName);
}
balance = Fry::getInstance()->balanceMapValue(accountName);
balance = balance.mid(0,balance.indexOf(' '));
balance.remove(',');
ui->balanceLabel->setText( balance + " GOP" );
if( balance.toDouble() > 1221.83 - 0.0000001)
{
ui->upgradeBtn->setEnabled(true);
ui->tipLabel->hide();
}
else
{
ui->upgradeBtn->setEnabled(false);
ui->tipLabel->show();
}
ui->addressLabel->setText(Fry::getInstance()->addressMapValue(accountName));
DLOG_QT_WALLET_FUNCTION_END;
}
void ApplyDelegatePage::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setBrush(QColor(229,229,229));
painter.setPen(QColor(177,177,177));
painter.drawRect(QRect(-1,-1,828,92));
}
void ApplyDelegatePage::on_accountComboBox_currentIndexChanged(const QString &arg1)
{
if( !inited) return;
accountName = arg1;
init();
}
void ApplyDelegatePage::refresh()
{
init();
}
void ApplyDelegatePage::retranslator()
{
ui->retranslateUi(this);
}
void ApplyDelegatePage::jsonDataUpdated(QString id)
{
if( id == "id_blockchain_get_account")
{
QString result = Fry::getInstance()->jsonDataValue(id);
if( result == "\"result\":null")
{
ui->wrongLabel->hide();
ui->wrongLabel2->hide();
ui->correctLabel->show();
if( balance.toDouble() >= 1221.83 - 0.0000001)
{
ui->upgradeBtn->setEnabled(true);
}
}
else
{
ui->wrongLabel->show();
ui->wrongLabel2->hide();
ui->correctLabel->hide();
ui->upgradeBtn->setEnabled(false);
}
return;
}
if( id.mid(0,25) == "id_wallet_account_rename_")
{
QString result = Fry::getInstance()->jsonDataValue(id);
QString newName = id.mid(25);
if( result == "\"result\":null")
{
mutexForConfigFile.lock();
Fry::getInstance()->configFile->beginGroup("/accountInfo");
QStringList accountNameKey = Fry::getInstance()->configFile->childKeys();
for( int i = 0; i < accountNameKey.size(); i++)
{
if( Fry::getInstance()->configFile->value( accountNameKey.at(i)) == accountName)
{
Fry::getInstance()->configFile->setValue( accountNameKey.at(i), newName);
break;
}
}
Fry::getInstance()->configFile->endGroup();
mutexForConfigFile.unlock();
Fry::getInstance()->addressMapRemove( accountName);
Fry::getInstance()->balanceMapRemove( accountName);
Fry::getInstance()->registerMapRemove( accountName);
accountName = newName;
emit accountRenamed();
Fry::getInstance()->postRPC( toJsonFormat( "id_wallet_set_transaction_fee", "wallet_set_transaction_fee", QStringList() << "0.01" ));
}
else
{
emit hideShadowWidget();
qDebug() << "rename " + accountName + " to " + newName + " failed :" + result;
}
return;
}
if( id == "id_wallet_set_transaction_fee")
{
QString result = Fry::getInstance()->jsonDataValue(id);
if( result == "\"result\":{\"amount\":1000,\"asset_id\":0}")
{
if( Fry::getInstance()->registerMapValue(accountName) != "NO" && !Fry::getInstance()->registerMapValue(accountName).isEmpty() )
{
Fry::getInstance()->postRPC( toJsonFormat( "id_wallet_account_update_registration", "wallet_account_update_registration", QStringList() << accountName << accountName << "[]" << "100" ));
}
else
{
Fry::getInstance()->postRPC( toJsonFormat( "id_wallet_account_register", "wallet_account_register", QStringList() << accountName << accountName << "" << "100" ));
}
}
else
{
emit hideShadowWidget();
qDebug() << "set transaction fee failed: " << result;
}
return;
}
if( id == "id_wallet_account_update_registration")
{
emit hideShadowWidget();
QString result = Fry::getInstance()->jsonDataValue(id);
if( result.mid(0,20) == "\"result\":{\"index\":0,")
{
// 记录申请代理的账户, 为了防止重复申请
QString recordId = result.mid( result.indexOf("\"entry_id\"") + 12, 40); // 记录申请代理的id 为了可以查询有没有被确认
mutexForConfigFile.lock();
Fry::getInstance()->configFile->setValue("applyingForDelegateAccount/" + accountName,recordId);
Fry::getInstance()->configFile->setValue("/recordId/" + recordId , 0);
mutexForConfigFile.unlock();
mutexForPendingFile.lock();
if( !Fry::getInstance()->pendingFile->open(QIODevice::ReadWrite))
{
qDebug() << "pending.dat not exist";
return;
}
QByteArray ba = QByteArray::fromBase64( Fry::getInstance()->pendingFile->readAll());
ba += QString( recordId + "," + accountName + "," + accountName + "," + "0" + "," + "1221.83" + ";").toUtf8();
ba = ba.toBase64();
Fry::getInstance()->pendingFile->resize(0);
QTextStream ts(Fry::getInstance()->pendingFile);
ts << ba;
Fry::getInstance()->pendingFile->close();
mutexForPendingFile.unlock();
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText(tr("Application for a delegate has been submitted."));
commonDialog.pop();
emit back(accountName);
}
else
{
qDebug() << "wallet_account_update_registration failed: " << result;
int pos = result.indexOf("\"message\":\"") + 11;
QString errorMessage = result.mid(pos, result.indexOf("\"", pos) - pos);
if( errorMessage == "invalid transaction expiration")
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("You need to wait for synchronization to complete."));
commonDialog.pop();
}
else
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("Upgrade failed!"));
commonDialog.pop();
}
}
return;
}
if( id == "id_wallet_account_register")
{
emit hideShadowWidget();
QString result = Fry::getInstance()->jsonDataValue(id);
if( result.mid(0,20) == "\"result\":{\"index\":0,")
{
// 记录申请代理的账户, 为了防止重复申请
QString recordId = result.mid( result.indexOf("\"entry_id\"") + 12, 40); // 记录申请代理的id 为了可以查询有没有被确认
mutexForConfigFile.lock();
Fry::getInstance()->configFile->setValue("applyingForDelegateAccount/" + accountName,recordId);
Fry::getInstance()->configFile->setValue("/recordId/" + recordId , 0);
mutexForConfigFile.unlock();
mutexForPendingFile.lock();
if( !Fry::getInstance()->pendingFile->open(QIODevice::ReadWrite))
{
qDebug() << "pending.dat not exist";
return;
}
QByteArray ba = QByteArray::fromBase64( Fry::getInstance()->pendingFile->readAll());
ba += QString( recordId + "," + accountName + "," + accountName + "," + "0" + "," + "1221.83" + ";").toUtf8();
ba = ba.toBase64();
Fry::getInstance()->pendingFile->resize(0);
QTextStream ts(Fry::getInstance()->pendingFile);
ts << ba;
Fry::getInstance()->pendingFile->close();
mutexForPendingFile.unlock();
// Fry::getInstance()->configFile->setValue("/recordId/" + recordId , 0);
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText(tr("Application for a delegate has been submitted."));
commonDialog.pop();
emit back(accountName);
}
else
{
qDebug() << "wallet_account_register delegate failed: " << result;
int pos = result.indexOf("\"message\":\"") + 11;
QString errorMessage = result.mid(pos, result.indexOf("\"", pos) - pos);
if( errorMessage == "invalid transaction expiration")
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("You need to wait for synchronization to complete."));
commonDialog.pop();
}
else
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("Upgrade failed!"));
commonDialog.pop();
}
}
return;
}
}
void ApplyDelegatePage::on_upgradeBtn_clicked()
{
// 如果是已注册账户
if( Fry::getInstance()->registerMapValue(accountName) != "NO"
&& !Fry::getInstance()->registerMapValue(accountName).isEmpty())
{
Fry::getInstance()->postRPC( toJsonFormat( "id_wallet_set_transaction_fee", "wallet_set_transaction_fee", QStringList() << "0.01" ));
emit showShadowWidget();
}
else
{
//rename
QString addrUpdateName = ui->addressNameLineEdit->text();
if( accountName != addrUpdateName) // 如果改了地址名
{
Fry::getInstance()->postRPC( toJsonFormat( "id_wallet_account_rename_" + addrUpdateName, "wallet_account_rename", QStringList() << accountName << addrUpdateName ));
emit showShadowWidget();
}
else
{
Fry::getInstance()->postRPC( toJsonFormat( "id_wallet_set_transaction_fee", "wallet_set_transaction_fee", QStringList() << "0.01" ));
emit showShadowWidget();
}
}
}
bool isExistInWallet(QString strName);
void ApplyDelegatePage::on_addressNameLineEdit_textChanged(const QString &arg1)
{
if( arg1.isEmpty())
{
ui->correctLabel->hide();
ui->wrongLabel->hide();
ui->wrongLabel2->hide();
ui->upgradeBtn->setEnabled(false);
return;
}
QString addrName = arg1;
if( ADDRNAME_MAX_LENGTH < addrName.size() )
{
ui->upgradeBtn->setEnabled(false);
ui->correctLabel->hide();
ui->wrongLabel->hide();
ui->wrongLabel2->show();
return;
}
//检查地址名是否在钱包内已经存在
if( isExistInWallet(addrName) && addrName != accountName)
{
ui->wrongLabel->show();
ui->wrongLabel2->hide();
ui->correctLabel->hide();
ui->upgradeBtn->setEnabled(false);
return;
}
//检查地址名是否已经在链上注册了 blockchain_get_account(accountName)
Fry::getInstance()->postRPC( toJsonFormat( "id_blockchain_get_account", "blockchain_get_account", QStringList() << ui->addressNameLineEdit->text() ));
}