2013-03-10 17:23:10 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PasswordGeneratorWidget.h"
|
|
|
|
#include "ui_PasswordGeneratorWidget.h"
|
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
#include <QLineEdit>
|
|
|
|
|
2013-06-28 22:29:03 -04:00
|
|
|
#include "core/Config.h"
|
2014-01-12 17:33:36 -05:00
|
|
|
#include "core/PasswordGenerator.h"
|
|
|
|
#include "core/FilePath.h"
|
2013-06-28 22:29:03 -04:00
|
|
|
|
2013-03-10 17:23:10 -04:00
|
|
|
PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
2014-01-12 17:33:36 -05:00
|
|
|
, m_updatingSpinBox(false)
|
|
|
|
, m_generator(new PasswordGenerator())
|
2013-03-10 17:23:10 -04:00
|
|
|
, m_ui(new Ui::PasswordGeneratorWidget())
|
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
2013-03-12 17:42:06 -04:00
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
m_ui->togglePasswordButton->setIcon(filePath()->onOffIcon("actions", "password-show"));
|
|
|
|
|
|
|
|
connect(m_ui->editNewPassword->lineEdit(), SIGNAL(textChanged(QString)), SLOT(updateApplyEnabled(QString)));
|
2014-01-13 15:40:23 -05:00
|
|
|
connect(m_ui->togglePasswordButton, SIGNAL(toggled(bool)), m_ui->editNewPassword, SLOT(setEcho(bool)));
|
2013-03-12 17:42:06 -04:00
|
|
|
connect(m_ui->buttonApply, SIGNAL(clicked()), SLOT(emitNewPassword()));
|
2013-06-28 22:29:03 -04:00
|
|
|
connect(m_ui->buttonApply, SIGNAL(clicked()), SLOT(saveSettings()));
|
2013-03-12 17:42:06 -04:00
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
connect(m_ui->sliderLength, SIGNAL(valueChanged(int)), SLOT(sliderMoved()));
|
|
|
|
connect(m_ui->spinBoxLength, SIGNAL(valueChanged(int)), SLOT(spinBoxChanged()));
|
|
|
|
|
|
|
|
connect(m_ui->optionButtons, SIGNAL(buttonClicked(int)), SLOT(updateGenerator()));
|
|
|
|
|
|
|
|
m_ui->editNewPassword->setGenerator(m_generator.data());
|
|
|
|
|
2013-06-28 22:29:03 -04:00
|
|
|
loadSettings();
|
2014-01-12 17:33:36 -05:00
|
|
|
reset();
|
2013-03-10 17:23:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PasswordGeneratorWidget::~PasswordGeneratorWidget()
|
|
|
|
{
|
2013-03-12 17:42:06 -04:00
|
|
|
}
|
|
|
|
|
2013-06-28 22:29:03 -04:00
|
|
|
void PasswordGeneratorWidget::loadSettings()
|
|
|
|
{
|
|
|
|
m_ui->checkBoxLower->setChecked(config()->get("generator/LowerCase", true).toBool());
|
|
|
|
m_ui->checkBoxUpper->setChecked(config()->get("generator/UpperCase", true).toBool());
|
|
|
|
m_ui->checkBoxNumbers->setChecked(config()->get("generator/Numbers", true).toBool());
|
|
|
|
m_ui->checkBoxSpecialChars->setChecked(config()->get("generator/SpecialChars", false).toBool());
|
|
|
|
|
|
|
|
m_ui->checkBoxExcludeAlike->setChecked(config()->get("generator/ExcludeAlike", true).toBool());
|
|
|
|
m_ui->checkBoxEnsureEvery->setChecked(config()->get("generator/EnsureEvery", true).toBool());
|
|
|
|
|
|
|
|
m_ui->spinBoxLength->setValue(config()->get("generator/Length", 16).toInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasswordGeneratorWidget::saveSettings()
|
2013-03-12 17:42:06 -04:00
|
|
|
{
|
2013-06-28 22:29:03 -04:00
|
|
|
config()->set("generator/LowerCase", m_ui->checkBoxLower->isChecked());
|
|
|
|
config()->set("generator/UpperCase", m_ui->checkBoxUpper->isChecked());
|
|
|
|
config()->set("generator/Numbers", m_ui->checkBoxNumbers->isChecked());
|
|
|
|
config()->set("generator/SpecialChars", m_ui->checkBoxSpecialChars->isChecked());
|
2013-03-12 17:42:06 -04:00
|
|
|
|
2013-06-28 22:29:03 -04:00
|
|
|
config()->set("generator/ExcludeAlike", m_ui->checkBoxExcludeAlike->isChecked());
|
|
|
|
config()->set("generator/EnsureEvery", m_ui->checkBoxEnsureEvery->isChecked());
|
2013-03-12 17:42:06 -04:00
|
|
|
|
2013-06-28 22:29:03 -04:00
|
|
|
config()->set("generator/Length", m_ui->spinBoxLength->value());
|
|
|
|
}
|
2013-03-12 17:42:06 -04:00
|
|
|
|
2013-06-28 22:29:03 -04:00
|
|
|
void PasswordGeneratorWidget::reset()
|
|
|
|
{
|
2014-01-12 17:33:36 -05:00
|
|
|
m_ui->editNewPassword->lineEdit()->setText("");
|
|
|
|
m_ui->togglePasswordButton->setChecked(config()->get("security/passwordscleartext").toBool());
|
|
|
|
|
|
|
|
updateGenerator();
|
2013-03-12 17:42:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasswordGeneratorWidget::updateApplyEnabled(const QString& password)
|
|
|
|
{
|
|
|
|
m_ui->buttonApply->setEnabled(!password.isEmpty());
|
|
|
|
}
|
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
void PasswordGeneratorWidget::emitNewPassword()
|
2013-03-12 17:42:06 -04:00
|
|
|
{
|
2014-01-12 17:33:36 -05:00
|
|
|
Q_EMIT newPassword(m_ui->editNewPassword->lineEdit()->text());
|
|
|
|
}
|
2013-03-12 17:42:06 -04:00
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
void PasswordGeneratorWidget::sliderMoved()
|
|
|
|
{
|
|
|
|
if (m_updatingSpinBox) {
|
2013-03-12 17:42:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
m_ui->spinBoxLength->setValue(m_ui->sliderLength->value());
|
|
|
|
|
|
|
|
updateGenerator();
|
2013-03-12 17:42:06 -04:00
|
|
|
}
|
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
void PasswordGeneratorWidget::spinBoxChanged()
|
2013-03-12 17:42:06 -04:00
|
|
|
{
|
2016-07-31 16:07:47 -04:00
|
|
|
if (m_updatingSpinBox) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
// Interlock so that we don't update twice - this causes issues as the spinbox can go higher than slider
|
|
|
|
m_updatingSpinBox = true;
|
|
|
|
|
|
|
|
m_ui->sliderLength->setValue(m_ui->spinBoxLength->value());
|
|
|
|
|
|
|
|
m_updatingSpinBox = false;
|
|
|
|
|
|
|
|
updateGenerator();
|
2013-03-12 17:42:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PasswordGenerator::CharClasses PasswordGeneratorWidget::charClasses()
|
|
|
|
{
|
|
|
|
PasswordGenerator::CharClasses classes;
|
|
|
|
|
|
|
|
if (m_ui->checkBoxLower->isChecked()) {
|
|
|
|
classes |= PasswordGenerator::LowerLetters;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ui->checkBoxUpper->isChecked()) {
|
|
|
|
classes |= PasswordGenerator::UpperLetters;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ui->checkBoxNumbers->isChecked()) {
|
|
|
|
classes |= PasswordGenerator::Numbers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ui->checkBoxSpecialChars->isChecked()) {
|
|
|
|
classes |= PasswordGenerator::SpecialCharacters;
|
|
|
|
}
|
|
|
|
|
|
|
|
return classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
PasswordGenerator::GeneratorFlags PasswordGeneratorWidget::generatorFlags()
|
|
|
|
{
|
|
|
|
PasswordGenerator::GeneratorFlags flags;
|
|
|
|
|
|
|
|
if (m_ui->checkBoxExcludeAlike->isChecked()) {
|
|
|
|
flags |= PasswordGenerator::ExcludeLookAlike;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ui->checkBoxEnsureEvery->isChecked()) {
|
|
|
|
flags |= PasswordGenerator::CharFromEveryGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
2013-03-10 17:23:10 -04:00
|
|
|
}
|
2014-01-12 17:33:36 -05:00
|
|
|
|
|
|
|
void PasswordGeneratorWidget::updateGenerator()
|
|
|
|
{
|
2016-07-31 16:07:47 -04:00
|
|
|
PasswordGenerator::CharClasses classes = charClasses();
|
|
|
|
PasswordGenerator::GeneratorFlags flags = generatorFlags();
|
|
|
|
|
|
|
|
int minLength = 0;
|
|
|
|
if (flags.testFlag(PasswordGenerator::CharFromEveryGroup)) {
|
|
|
|
if (classes.testFlag(PasswordGenerator::LowerLetters)) {
|
|
|
|
minLength++;
|
|
|
|
}
|
|
|
|
if (classes.testFlag(PasswordGenerator::UpperLetters)) {
|
|
|
|
minLength++;
|
|
|
|
}
|
|
|
|
if (classes.testFlag(PasswordGenerator::Numbers)) {
|
|
|
|
minLength++;
|
|
|
|
}
|
|
|
|
if (classes.testFlag(PasswordGenerator::SpecialCharacters)) {
|
|
|
|
minLength++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
minLength = qMax(minLength, 1);
|
|
|
|
|
|
|
|
if (m_ui->spinBoxLength->value() < minLength) {
|
|
|
|
m_updatingSpinBox = true;
|
|
|
|
m_ui->spinBoxLength->setValue(minLength);
|
|
|
|
m_ui->sliderLength->setValue(minLength);
|
|
|
|
m_updatingSpinBox = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ui->spinBoxLength->setMinimum(minLength);
|
|
|
|
m_ui->sliderLength->setMinimum(minLength);
|
|
|
|
|
2014-01-12 17:33:36 -05:00
|
|
|
m_generator->setLength(m_ui->spinBoxLength->value());
|
2016-07-31 16:07:47 -04:00
|
|
|
m_generator->setCharClasses(classes);
|
|
|
|
m_generator->setFlags(flags);
|
2014-01-12 17:33:36 -05:00
|
|
|
|
|
|
|
if (m_generator->isValid()) {
|
|
|
|
QString password = m_generator->generatePassword();
|
|
|
|
m_ui->editNewPassword->setEditText(password);
|
|
|
|
}
|
|
|
|
}
|