-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportdialog.cpp
325 lines (258 loc) · 10.9 KB
/
importdialog.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
#include "importdialog.h"
#include "ui_importdialog.h"
#include "rpcthread.h"
#include "blockchain.h"
#include "commondialog.h"
#include "namedialog.h"
#include "control/shadowwidget.h"
#include <QDir>
#include <QFileDialog>
#include <QDebug>
#include <QFile>
ImportDialog::ImportDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ImportDialog)
{
ui->setupUi(this);
// Fry::getInstance()->appendCurrentDialogVector(this);
setParent(Blockchain::getInstance()->mainFrame);
setAttribute(Qt::WA_TranslucentBackground, true);
setWindowFlags(Qt::FramelessWindowHint);
ui->widget->setObjectName("widget");
ui->widget->setStyleSheet("#widget {background-color:rgba(10, 10, 10,100);}");
ui->containerWidget->setObjectName("containerWidget");
ui->containerWidget->setStyleSheet("#containerWidget{background-color: rgb(246, 246, 246);border:1px groove rgb(180,180,180);}");
connect( Blockchain::getInstance(), SIGNAL(jsonDataUpdated(QString)), this, SLOT(jsonDataUpdated(QString)));
ui->importBtn->setEnabled(false);
ui->privateKeyLineEdit->setStyleSheet("color:black;border:1px solid #CCCCCC;border-radius:3px;");
ui->privateKeyLineEdit->setTextMargins(8,0,0,0);
ui->privateKeyLineEdit->setFocus();
shadowWidget = new ShadowWidget(this);
shadowWidget->init(this->size());
shadowWidget->hide();
}
ImportDialog::~ImportDialog()
{
delete ui;
// Fry::getInstance()->removeCurrentDialogVector(this);
}
void ImportDialog::pop()
{
// QEventLoop loop;
// show();
// ui->privateKeyLineEdit->grabKeyboard();
// connect(this,SIGNAL(accepted()),&loop,SLOT(quit()));
// loop.exec(); //进入事件 循环处理,阻塞
move(0,0);
exec();
}
void ImportDialog::on_cancelBtn_clicked()
{
close();
// qDebug() << "on_cancelBtn_clicked";
// emit accepted();
}
void ImportDialog::on_pathBtn_clicked()
{
QString file = QFileDialog::getOpenFileName(this, tr("Choose your private key file."),"","(*.key)");
file.replace("/","\\");
ui->privateKeyLineEdit->setText(file);
}
void ImportDialog::on_importBtn_clicked()
{
// 如果输入框中是 私钥文件的地址
if( ui->privateKeyLineEdit->text().contains(".key") )
{
QFile file( ui->privateKeyLineEdit->text());
if( file.open(QIODevice::ReadOnly))
{
QByteArray ba = QByteArray::fromBase64( file.readAll());
QString str(ba);
Blockchain::getInstance()->postRPC( toJsonFormat( "id_wallet_import_private_key", "wallet_import_private_key", QStringList() << str));
ui->importBtn->setEnabled(false);
shadowWidget->show();
file.close();
return;
}
else
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("Wrong file path."));
commonDialog.pop();
return;
}
}
else // 如果输入框中是 私钥
{
Blockchain::getInstance()->postRPC( toJsonFormat( "id_wallet_import_private_key", "wallet_import_private_key", QStringList() << ui->privateKeyLineEdit->text() ));
ui->importBtn->setEnabled(false);
shadowWidget->show();
}
}
QString toThousandFigure( int);
void ImportDialog::jsonDataUpdated(QString id)
{
if( id == "id_wallet_import_private_key")
{
shadowWidget->hide();
QString result = Blockchain::getInstance()->jsonDataValue(id);
if( result.mid(0,9) == "\"result\":")
{
ui->importBtn->setEnabled(true);
Blockchain::getInstance()->needToScan = true; // 导入成功,需要scan
QString name = result.mid(10, result.indexOf("\"", 10) - 10);
if( accountNameAlreadyExisted(name)) // 如果已存在该账号
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( name + tr(" already existed") );
commonDialog.pop();
}
else
{
mutexForAddressMap.lock();
int size = Blockchain::getInstance()->addressMap.size();
mutexForAddressMap.unlock();
mutexForConfigFile.lock();
Blockchain::getInstance()->configFile->setValue( QString("/accountInfo/") + QString::fromLocal8Bit("账户") + toThousandFigure(size+1),name);
mutexForConfigFile.unlock();
Blockchain::getInstance()->balanceMapInsert( name, "0.00000 " + QString(ASSET_NAME));
Blockchain::getInstance()->registerMapInsert( name, "UNKNOWN");
Blockchain::getInstance()->addressMapInsert( name, Blockchain::getInstance()->getAddress(name));
emit accountImported();
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( name + tr( " has been imported!"));
commonDialog.pop();
// RpcThread* rpcThread = new RpcThread;
// connect(rpcThread,SIGNAL(finished()),rpcThread,SLOT(deleteLater()));
// rpcThread->setLogin("a","b");
// rpcThread->setWriteData( toJsonFormat( "id_scan", "scan", QStringList() << "0" ));
// rpcThread->setWriteData( toJsonFormat( "id_wallet_scan_transaction", "wallet_scan_transaction", QStringList() << "ALL" << "true" ));
// rpcThread->start();
Blockchain::getInstance()->postRPC( toJsonFormat( "id_scan", "scan", QStringList() << "0" ));
Blockchain::getInstance()->postRPC( toJsonFormat( "id_wallet_scan_transaction", "wallet_scan_transaction", QStringList() << "ALL" << "true" ));
close();
// emit accepted();
}
}
else if( result.mid(0,8) == "\"error\":")
{
qDebug() << "import error: " << result;
if( !result.contains("Unknown key! You must specify an account name!") )
{
// 如果不是未注册账户 是错误的私钥格式
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("Illegal private key!"));
commonDialog.pop();
ui->importBtn->setEnabled(true);
return;
}
NameDialog nameDialog;
QString name = nameDialog.pop();
if( name.isEmpty()) // 如果取消 命名
{
ui->importBtn->setEnabled(true);
return;
}
if( ui->privateKeyLineEdit->text().contains(".key") ) // 如果是路径
{
QFile file( ui->privateKeyLineEdit->text());
if( file.open(QIODevice::ReadOnly))
{
QByteArray ba = QByteArray::fromBase64( file.readAll());
QString str(ba);
Blockchain::getInstance()->postRPC( toJsonFormat( "id_wallet_import_private_key_" + name, "wallet_import_private_key", QStringList() << str << name << "true"));
shadowWidget->show();
file.close();
return;
}
else
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("Wrong file path."));
commonDialog.pop();
ui->importBtn->setEnabled(true);
return;
}
}
else // 如果输入框中是 私钥
{
Blockchain::getInstance()->postRPC( toJsonFormat( "id_wallet_import_private_key_" + name, "wallet_import_private_key", QStringList() << ui->privateKeyLineEdit->text() << name << "true" ));
shadowWidget->show();
}
}
return;
}
if( id.mid(0,29) == "id_wallet_import_private_key_")
{
ui->importBtn->setEnabled(true);
shadowWidget->hide();
QString result = Blockchain::getInstance()->jsonDataValue(id);
if( result.mid(0,9) == "\"result\":")
{
Blockchain::getInstance()->needToScan = true; // 导入成功,需要scan
QString name = result.mid(10, result.indexOf("\"", 10) - 10);
mutexForAddressMap.lock();
int size = Blockchain::getInstance()->addressMap.size();
mutexForAddressMap.unlock();
mutexForConfigFile.lock();
Blockchain::getInstance()->configFile->setValue( QString("/accountInfo/") + QString::fromLocal8Bit("账户") + toThousandFigure(size+1),name);
mutexForConfigFile.unlock();
Blockchain::getInstance()->balanceMapInsert( name, "0.00000 " + QString(ASSET_NAME));
Blockchain::getInstance()->registerMapInsert( name, "NO");
Blockchain::getInstance()->addressMapInsert( name, Blockchain::getInstance()->getAddress(name));
emit accountImported();
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( name + tr(" has been imported!") );
commonDialog.pop();
Blockchain::getInstance()->postRPC( toJsonFormat( "id_scan", "scan", QStringList() << "0" ));
Blockchain::getInstance()->postRPC( toJsonFormat( "id_wallet_scan_transaction", "wallet_scan_transaction", QStringList() << "ALL" << "true" ));
close();
// emit accepted();
}
else if( result.mid(0,8) == "\"error\":")
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText( tr("Illegal private key!"));
commonDialog.pop();
}
return;
}
}
bool ImportDialog::accountNameAlreadyExisted(QString name)
{
mutexForConfigFile.lock();
Blockchain::getInstance()->configFile->beginGroup("/accountInfo");
QStringList keys = Blockchain::getInstance()->configFile->childKeys();
QString str;
bool existed = false;
foreach (str, keys)
{
if( Blockchain::getInstance()->configFile->value(str) == name)
{
existed = true;
break;
}
}
Blockchain::getInstance()->configFile->endGroup();
mutexForConfigFile.unlock();
return existed;
}
void ImportDialog::on_privateKeyLineEdit_textChanged(const QString &arg1)
{
QString str = arg1.simplified();
if( str.isEmpty())
{
ui->importBtn->setEnabled(false);
}
else
{
ui->importBtn->setEnabled(true);
}
}
void ImportDialog::on_privateKeyLineEdit_returnPressed()
{
if( ui->importBtn->isEnabled())
{
on_importBtn_clicked();
}
}