2014-10-19 01:38:47 +08:00
|
|
|
/*
|
2019-06-24 22:01:18 +08:00
|
|
|
Copyright © 2014-2019 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2014-10-19 01:38:47 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-10-19 01:38:47 +08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-10-19 01:38:47 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-10-19 01:38:47 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-19 01:38:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "setpassworddialog.h"
|
|
|
|
#include "ui_setpassworddialog.h"
|
2016-07-30 17:10:49 +08:00
|
|
|
|
|
|
|
#include <QApplication>
|
2014-10-19 01:38:47 +08:00
|
|
|
#include <QPushButton>
|
|
|
|
|
2014-12-06 14:17:13 +08:00
|
|
|
const double SetPasswordDialog::reasonablePasswordLength = 8.;
|
|
|
|
|
2014-11-27 23:38:23 +08:00
|
|
|
SetPasswordDialog::SetPasswordDialog(QString body, QString extraButton, QWidget* parent)
|
|
|
|
: QDialog(parent)
|
|
|
|
, ui(new Ui::SetPasswordDialog)
|
2015-10-11 01:58:59 +08:00
|
|
|
, body(body + "\n\n")
|
2014-10-19 01:38:47 +08:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
connect(ui->passwordlineEdit, SIGNAL(textChanged(QString)), this, SLOT(onPasswordEdit()));
|
2014-10-19 01:38:47 +08:00
|
|
|
connect(ui->repasswordlineEdit, SIGNAL(textChanged(QString)), this, SLOT(onPasswordEdit()));
|
2014-11-27 23:38:23 +08:00
|
|
|
|
2015-10-11 01:58:59 +08:00
|
|
|
ui->body->setText(body + "\n\n");
|
2017-02-26 19:52:45 +08:00
|
|
|
QPushButton* ok = ui->buttonBox->button(QDialogButtonBox::Ok);
|
2016-07-30 17:10:49 +08:00
|
|
|
ok->setEnabled(false);
|
|
|
|
ok->setText(QApplication::tr("Ok"));
|
2017-02-26 19:52:45 +08:00
|
|
|
QPushButton* cancel = ui->buttonBox->button(QDialogButtonBox::Cancel);
|
2016-07-30 17:10:49 +08:00
|
|
|
cancel->setText(QApplication::tr("Cancel"));
|
2014-11-27 23:38:23 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (!extraButton.isEmpty()) {
|
2014-11-27 23:38:23 +08:00
|
|
|
QPushButton* third = new QPushButton(extraButton);
|
|
|
|
ui->buttonBox->addButton(third, QDialogButtonBox::YesRole);
|
2017-02-26 19:52:45 +08:00
|
|
|
connect(third, &QPushButton::clicked, this, [&]() { this->done(Tertiary); });
|
2014-11-27 23:38:23 +08:00
|
|
|
}
|
2014-10-19 01:38:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SetPasswordDialog::~SetPasswordDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPasswordDialog::onPasswordEdit()
|
|
|
|
{
|
2014-12-06 14:17:13 +08:00
|
|
|
QString pswd = ui->passwordlineEdit->text();
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (pswd.isEmpty()) {
|
2015-10-17 00:57:21 +08:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
|
|
|
ui->body->setText(body);
|
2017-02-26 19:52:45 +08:00
|
|
|
} else if (pswd.length() < 6) {
|
2015-06-04 23:45:20 +08:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
|
|
|
ui->body->setText(body + tr("The password is too short"));
|
2017-02-26 19:52:45 +08:00
|
|
|
} else if (pswd != ui->repasswordlineEdit->text()) {
|
2014-10-19 01:38:47 +08:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
2015-06-04 23:45:20 +08:00
|
|
|
ui->body->setText(body + tr("The password doesn't match."));
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
2015-06-04 23:45:20 +08:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
|
|
|
|
ui->body->setText(body);
|
2015-01-23 21:05:46 +08:00
|
|
|
}
|
2014-12-06 14:17:13 +08:00
|
|
|
|
2015-10-11 01:58:59 +08:00
|
|
|
ui->passStrengthMeter->setValue(getPasswordStrength(pswd));
|
2015-06-05 01:21:22 +08:00
|
|
|
}
|
|
|
|
|
2015-06-05 01:53:27 +08:00
|
|
|
int SetPasswordDialog::getPasswordStrength(QString pass)
|
2015-06-05 01:21:22 +08:00
|
|
|
{
|
2015-06-05 01:53:27 +08:00
|
|
|
if (pass.size() < 6)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
double fscore = 0;
|
|
|
|
QHash<QChar, int> charCounts;
|
2017-02-26 19:52:45 +08:00
|
|
|
for (QChar c : pass) {
|
2015-06-05 01:53:27 +08:00
|
|
|
charCounts[c]++;
|
|
|
|
fscore += 5. / charCounts[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
int variations = -1;
|
|
|
|
variations += pass.contains(QRegExp("[0-9]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
|
|
|
|
variations += pass.contains(QRegExp("[a-z]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
|
|
|
|
variations += pass.contains(QRegExp("[A-Z]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
|
|
|
|
variations += pass.contains(QRegExp("[\\W]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
|
|
|
|
|
|
|
|
int score = fscore;
|
|
|
|
score += variations * 10;
|
|
|
|
score -= 20;
|
|
|
|
score = std::min(score, 100);
|
|
|
|
score = std::max(score, 0);
|
|
|
|
|
|
|
|
return score;
|
2014-10-19 01:38:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SetPasswordDialog::getPassword()
|
|
|
|
{
|
|
|
|
return ui->passwordlineEdit->text();
|
|
|
|
}
|