#include "logindialog.h"
#include "ui_logindialog.h"
#include "sign_in.h"
#include "modifypassword.h"
#include "modify_login.h"
#include <QMessageBox>
#include <QFile>
#include <QIODevice>
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QTextCodec>
int num = 3; //密码容错次数
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
}
LoginDialog::~LoginDialog()
{
delete ui;
}
void LoginDialog::loadfile() { //从文件加载信息进来
QString readData;
QFile flie1("user.txt");
if(!flie1.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "ERROR","Cannot open the file!");
}
else {
QTextStream ts(&flie1);
while(!ts.atEnd()) {
readData = ts.readLine();
QStringList qlist = readData.split(";"); //分割字符串,将“admin;123456”分成两部分
ulist.append(new userObject(qlist.at(0), qlist.at(1), qlist.at(2)));
//把文件中的数据按一个用户合体组装,全部装入ulist
}
flie1.close();
}
}
void LoginDialog::rewriteFile() {
QString readData;
QFile flie1("user.txt");
if(!flie1.open(QIODevice::WriteOnly | QIODevice::Text)) {
//在这里不用append,而是用修改后的信息覆盖原信息
QMessageBox::warning(this, "ERROR","Cannot open the file!");
}
else {
QTextStream ts(&flie1);
for(int i=0; i<ulist.size(); i++) { //把不定长的ulist中的内容全部写回文件中
ts << ulist[i]->Username << ";"
<< ulist[i]->Account << ";"
<< ulist[i]->Password << endl;
}
flie1.close();
}
}
void LoginDialog::login() {
QString user = ui->username->text();
QString password = ui->pass->text();
QString readData;
int flag = 0;
QFile flie1("user.txt");
//--1》判断能否成功读文件
if(!flie1.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "ERROR","Cannot open the file!");
}
else {
QTextStream ts(&flie1);
while(!ts.atEnd()) {
readData = ts.readLine();
QStringList qlist = readData.split(";"); //分割字符串,将“admin;123456”分成两部分
//----------2》判断用户名和密码是否匹配
if(qlist.at(1) == user && qlist.at(2) == password) { //用户名和密码分别位于第二个和第三个
for(int i=0; i<abnormal_ulist.size(); i++) {
//------------------3》判断用户状态是否异常
if(user == abnormal_ulist[i]) {
QMessageBox::warning(this, "WARNING", "Sorry, "
"Your account is blocked.");
break;
}
}
flag = 1;
break;
}
}
flie1.close();
}
if(flag) {
if(num > 0) {
accept();
}
else {
QMessageBox::warning(this, "WARNING", "Sorry, "
"Your account is blocked.");
}
}
else {
QString status = QString("password and account unmatched, \n"
"NOTICE!you have %1 chances to failed.").arg(num);
num--;
if(num < 0) {
QMessageBox::warning(this, tr("Warning!"), tr("password and account unmatched.\n"
"Sorry, You have no chance left. "));
}
else {
QMessageBox::warning(this, tr("Falied!"), status);
}
ui->username->clear();
ui->pass->clear();
ui->username->setFocus();
}
//记录密码错误次数
if(num < 0) {
QString abnormal_user = ui->username->text();
QMessageBox::warning(this, tr("Warning!"), tr("If you have forgot your password, "
"try changing it."));
abnormal_ulist.append(abnormal_user);
ui->username->clear();
ui->pass->clear();
ui->username->setFocus();
}
}
void LoginDialog::on_pushButton_clicked()
{
login();
}
void LoginDialog::on_pushButton_3_clicked()
{
sign_in signIn;
signIn.show();
signIn.exec();
}
void LoginDialog::on_pushButton_4_clicked() {
QMessageBox::warning(this, "NOTICE", "Please enter your original password first");
modify_login modLog;
modLog.exec();
}
- 1
- 2
- 3
- 4
前往页