-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockpage.cpp
313 lines (263 loc) · 9.38 KB
/
lockpage.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
#include <QPainter>
#include <QKeyEvent>
#include <QDebug>
#include <QFileInfo>
#ifdef WIN32
#include <Windows.h>
#endif
#ifdef Q_OS_MACOS || Q_OS_MAC32 || Q_OS_MAC64
#include <ApplicationServices/ApplicationServices.h>
#endif
#include "lockpage.h"
#include "ui_lockpage.h"
#include "goopal.h"
#include "datamgr.h"
#include "debug_log.h"
#include "commondialog.h"
LockPage::LockPage(QWidget *parent) :
QWidget(parent),
ui(new Ui::LockPage)
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
ui->setupUi(this);
chances = 5;
setAutoFillBackground(true);
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(QPixmap(DataMgr::getDataMgr()->getWorkPath() + "pic2/bg.png")));
setPalette(palette);
ui->logoLabel->setPixmap(QPixmap(DataMgr::getDataMgr()->getWorkPath() + "pic2/Achainwallet.png"));
QString language = DataMgr::getInstance()->getSettings()->value("/settings/language").toString();
if( language.isEmpty() || language == "English")
{
ui->lockedLabel->setPixmap(QPixmap(DataMgr::getDataMgr()->getWorkPath() + "pic2/locked_En.png"));
ui->lockedLabel->setGeometry(454,310,72,13);
}
else if( language == "Simplified Chinese")
{
ui->lockedLabel->setPixmap(QPixmap(DataMgr::getDataMgr()->getWorkPath() + "pic2/locked.png"));
}
ui->pwdLineEdit->setStyleSheet("color:white;background:transparent;border-width:0;border-style:outset;");
ui->pwdLineEdit->setFocus();
ui->pwdLineEdit->setContextMenuPolicy(Qt::NoContextMenu);
ui->pwdLineEdit->setAttribute(Qt::WA_InputMethodEnabled, false);
ui->unlockBtn->setStyleSheet(QString("QToolButton{background-image:url(%1pic2/arrow.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}"
"QToolButton:hover{background-image:url(%2pic2/arrow_hover.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}").arg(DataMgr::getDataMgr()->getWorkPath()).arg(DataMgr::getDataMgr()->getWorkPath()));
ui->minBtn->setStyleSheet(QString("QToolButton{background-image:url(%1pic2/minimize2.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}"
"QToolButton:hover{background-image:url(%2pic2/minimize_hover.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}").arg(DataMgr::getDataMgr()->getWorkPath()).arg(DataMgr::getDataMgr()->getWorkPath()));
ui->closeBtn->setStyleSheet(QString("QToolButton{background-image:url(%1pic2/close2.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}"
"QToolButton:hover{background-image:url(%2pic2/close_hover.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}").arg(DataMgr::getDataMgr()->getWorkPath()).arg(DataMgr::getDataMgr()->getWorkPath()));
ui->versionLabel->setText(ACHAIN_WALLET_VERSION_STR);
#ifdef WIN32
if( GetKeyState(VK_CAPITAL) ) //zxlwin
{
ui->tipLabel->setText( tr("Caps lock opened!") );
}
#endif // WIN32
#ifdef Q_OS_MACOS || Q_OS_MAC32 || Q_OS_MAC64
bool wasCapsLockDown = CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, 57);
if (wasCapsLockDown)
ui->tipLabel->setText(tr("Caps lock opened!"));
#endif
ui->pwdLineEdit->grabKeyboard();
ui->pwdLineEdit->setFocus();
timerForLockTime = new QTimer(this);
connect(timerForLockTime, SIGNAL(timeout()), this, SLOT(checkLock()));
timerForLockTime->start(1000);
checkLock();
connect(DataMgr::getInstance(), &DataMgr::onWalletUnlock, this, &LockPage::walletUnlock);
DLOG_QT_WALLET_FUNCTION_END;
}
LockPage::~LockPage()
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
delete ui;
DLOG_QT_WALLET_FUNCTION_END;
}
void LockPage::on_unlockBtn_clicked()
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
if( ui->pwdLineEdit->text().isEmpty()) {
ui->tipLabel->setText( tr("Empty!") );
return;
}
if( ui->pwdLineEdit->text().size() < 8) {
ui->tipLabel->setText(tr("At least 8 letters!"));
ui->pwdLineEdit->clear();
return;
}
QString text(ui->pwdLineEdit->text());
//zxltab DataMgr::getInstance()->walletUnlock(99999999, ui->pwdLineEdit->text());
DataMgr::getInstance()->walletUnlock(99999999, text);
qDebug() << "id_unlock_lockpage" ;
emit showShadowWidget();
ui->pwdLineEdit->setEnabled(false);
DLOG_QT_WALLET_FUNCTION_END;
}
void LockPage::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPen pen;
pen.setColor(QColor(137,176,226));
pen.setStyle(Qt::SolidLine);
pen.setWidth(1);
painter.setPen(pen);
painter.drawLine(QPoint(370,372),QPoint(596,372));
}
void LockPage::on_pwdLineEdit_returnPressed()
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
on_unlockBtn_clicked();
DLOG_QT_WALLET_FUNCTION_END;
}
void LockPage::on_minBtn_clicked()
{
//if( Goopal::getInstance()->minimizeToTray)
if (false) {
emit tray();
} else {
emit minimum();
}
}
void LockPage::on_closeBtn_clicked()
{
//if( Goopal::getInstance()->minimizeToTray)
if (false) {
emit tray();
} else {
CommonDialog commonDialog(CommonDialog::OkAndCancel);
commonDialog.setText( tr( "Close Achain Wallet?"));
bool choice = commonDialog.pop();
if( choice) {
emit closeGOP();
} else {
return;
}
}
}
void LockPage::on_pwdLineEdit_textChanged(const QString &arg1)
{
if( !arg1.isEmpty())
{
ui->tipLabel->clear();
}
if( arg1.indexOf(' ') > -1)
{
ui->pwdLineEdit->setText( ui->pwdLineEdit->text().remove(' '));
}
}
void LockPage::walletUnlock(QString result)
{
emit hideShadowWidget();
ui->pwdLineEdit->clear();
ui->pwdLineEdit->setEnabled(true);
ui->pwdLineEdit->setFocus();
qDebug() << "id_unlock_lockpage" << result;
if( result == "\"result\":null"){
emit unlock();
}
else if (result.mid(0, 8) == "\"error\":") {
ui->tipLabel->setText(tr("Wrong password!"));
chances--;
qDebug() << "chances : " << chances;
if (chances < 1) {
if (lockTime() == 0) // 如果没有 locktime文件 则创建
{
// qDebug() << "walletConfigPath: " << Goopal::getInstance()->walletConfigPath;
// QFile file(Goopal::getInstance()->walletConfigPath + "/locktime");
// qDebug() << "file open : " << file.open(QIODevice::WriteOnly);
// file.close();
}
checkLock();
}
}
}
int LockPage::lockTime()
{
QFileInfo fileInfo;
// fileInfo.setFile(Goopal::getInstance()->walletConfigPath + "/locktime");
if( fileInfo.exists())
{
QDateTime createdTime = fileInfo.lastModified();
int deltaTime = QDateTime::currentDateTime().toTime_t() - createdTime.toTime_t();
if( deltaTime < PWD_LOCK_TIME)
{
// ui->tipLabel->setText( tr("Please try after ") + QString::number( PWD_LOCK_TIME - deltaTime) + "s");
return PWD_LOCK_TIME - deltaTime;
}
else
{
// qDebug() << "remove locktime file :" << QFile::remove(Goopal::getInstance()->walletConfigPath + "/locktime");
return 0;
}
}
else
{
return 0;
}
}
void LockPage::checkLock()
{
int timeLeft = lockTime();
if( timeLeft > 0)
{
ui->pwdLineEdit->setEnabled(false);
ui->unlockBtn->setEnabled(false);
if( timeLeft > 3600)
{
int hours = timeLeft / 3600;
int minutes = (timeLeft - 3600 * hours) / 60;
ui->tipLabel->setText( tr("Please try after ") + QString::number( hours) + tr(" hours ") +
QString::number( minutes) + tr(" minutes ") + tr(". "));
}
else if( timeLeft > 60)
{
int minutes = timeLeft / 60;
ui->tipLabel->setText( tr("Please try after ") + QString::number( minutes) + tr(" minutes ") + tr(". "));
}
else
{
ui->tipLabel->setText( tr("Please try after ") + QString::number( timeLeft) + tr(" seconds ") + tr(". "));
}
ui->pwdLineEdit->clear();
return;
}
else
{
ui->pwdLineEdit->setEnabled(true);
ui->unlockBtn->setEnabled(true);
if( ui->tipLabel->text().startsWith( tr("Please try after ")))
{
ui->tipLabel->clear();
}
}
}
void LockPage::keyPressEvent(QKeyEvent *e)
{
QWidget::keyPressEvent(e);
if(e->key() == Qt::Key_CapsLock)
{
#ifdef WIN32
if( GetKeyState(VK_CAPITAL) == -127 ) // 不知道为什么跟构造函数中同样的调用返回的short不一样 //zxlwin
{
ui->tipLabel->setText( tr("Caps lock opened!") );
}
else
{
ui->tipLabel->setText( tr("Caps lock closed!") );
}
#endif // WIN32
#ifdef Q_OS_MACOS || Q_OS_MAC32 || Q_OS_MAC64
ui->tipLabel->setText(tr("Caps lock opened!"));
#endif
}
}
void LockPage::keyReleaseEvent(QKeyEvent *e)
{
QWidget::keyReleaseEvent(e);
if(e->key() == Qt::Key_CapsLock)
{
#ifdef Q_OS_MACOS || Q_OS_MAC32 || Q_OS_MAC64
ui->tipLabel->setText(tr("Caps lock closed!"));
#endif
}
}