Changeset View
Changeset View
Standalone View
Standalone View
src/passworddialog.cpp
Show All 32 Lines | PasswordDialog::PasswordDialog(Pass &pass, const PasswordConfiguration &passConfig, QWidget *parent) | ||||
m_allFields = false; | m_allFields = false; | ||||
m_isNew = false; | m_isNew = false; | ||||
ui->setupUi(this); | ui->setupUi(this); | ||||
setLength(m_passConfig.length); | setLength(m_passConfig.length); | ||||
setPasswordCharTemplate(m_passConfig.selected); | setPasswordCharTemplate(m_passConfig.selected); | ||||
connect(&m_pass, &Pass::finishedShow, this, &PasswordDialog::setPass); | connect(&m_pass, &Pass::finishedShow, this, &PasswordDialog::setPass); | ||||
connect(ui->createPasswordOptionsButton, &QAbstractButton::toggled, this, &PasswordDialog::togglePasswordGenerationOption); | |||||
togglePasswordGenerationOption(false); | |||||
} | } | ||||
/** | /** | ||||
* @brief PasswordDialog::PasswordDialog complete constructor. | * @brief PasswordDialog::PasswordDialog complete constructor. | ||||
* @param file | * @param file | ||||
* @param isNew | * @param isNew | ||||
* @param parent pointer | * @param parent pointer | ||||
*/ | */ | ||||
Show All 17 Lines | PasswordDialog::PasswordDialog(Pass &pass, const QString &file, const bool &isNew, QWidget *parent) | ||||
setLength(m_passConfig.length); | setLength(m_passConfig.length); | ||||
setPasswordCharTemplate(m_passConfig.selected); | setPasswordCharTemplate(m_passConfig.selected); | ||||
connect(&m_pass, &Pass::finishedShow, this, &PasswordDialog::setPass); | connect(&m_pass, &Pass::finishedShow, this, &PasswordDialog::setPass); | ||||
connect(&m_pass, &Pass::decryptionError, this, &PasswordDialog::close); | connect(&m_pass, &Pass::decryptionError, this, &PasswordDialog::close); | ||||
connect(this, &PasswordDialog::accepted, this, &PasswordDialog::dialogAccepted); | connect(this, &PasswordDialog::accepted, this, &PasswordDialog::dialogAccepted); | ||||
connect(this, &PasswordDialog::rejected, this, &PasswordDialog::dialogCancelled); | connect(this, &PasswordDialog::rejected, this, &PasswordDialog::dialogCancelled); | ||||
connect(ui->createPasswordButton, &QAbstractButton::clicked, this, &PasswordDialog::createPassword); | connect(ui->createPasswordButton, &QAbstractButton::clicked, this, &PasswordDialog::createPassword); | ||||
connect(ui->checkBoxShow, &QCheckBox::stateChanged, this, &PasswordDialog::showPasswordChanged); | connect(ui->createPasswordOptionsButton, &QAbstractButton::toggled, this, &PasswordDialog::togglePasswordGenerationOption); | ||||
togglePasswordGenerationOption(false); | |||||
} | } | ||||
/** | /** | ||||
* @brief Pass{}{}wordDialog::~PasswordDialog basic destructor. | * @brief Pass{}{}wordDialog::~PasswordDialog basic destructor. | ||||
*/ | */ | ||||
PasswordDialog::~PasswordDialog() = default; | PasswordDialog::~PasswordDialog() = default; | ||||
/** | /** | ||||
* @brief PasswordDialog::on_checkBoxShow_stateChanged hide or show passwords. | |||||
* @param arg1 | |||||
*/ | |||||
void PasswordDialog::showPasswordChanged(int arg1) | |||||
{ | |||||
if (arg1) | |||||
ui->lineEditPassword->setEchoMode(QLineEdit::Normal); | |||||
else | |||||
ui->lineEditPassword->setEchoMode(QLineEdit::Password); | |||||
} | |||||
/** | |||||
* @brief PasswordDialog::on_createPasswordButton_clicked generate a random | * @brief PasswordDialog::on_createPasswordButton_clicked generate a random | ||||
* passwords. | * passwords. | ||||
* @todo refactor when process is untangled from MainWindow class. | * @todo refactor when process is untangled from MainWindow class. | ||||
*/ | */ | ||||
void PasswordDialog::createPassword() | void PasswordDialog::createPassword() | ||||
{ | { | ||||
ui->widget->setEnabled(false); | setEnabled(false); | ||||
QString newPass = m_pass.Generate_b(static_cast<unsigned int>(ui->spinBox_pwdLength->value()), | QString newPass = m_pass.Generate_b(static_cast<unsigned int>(ui->spinBox_pwdLength->value()), | ||||
m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(ui->passwordTemplateSwitch->currentIndex())]); | m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(ui->passwordTemplateSwitch->currentIndex())]); | ||||
if (newPass.length() > 0) | if (newPass.length() > 0) | ||||
ui->lineEditPassword->setText(newPass); | ui->lineEditPassword->setPassword(newPass); | ||||
ui->widget->setEnabled(true); | setEnabled(true); | ||||
} | } | ||||
/** | /** | ||||
* @brief PasswordDialog::on_accepted handle Ok click for QDialog | * @brief PasswordDialog::on_accepted handle Ok click for QDialog | ||||
*/ | */ | ||||
void PasswordDialog::dialogAccepted() | void PasswordDialog::dialogAccepted() | ||||
{ | { | ||||
QString newValue = getPassword(); | QString newValue = getPassword(); | ||||
Show All 16 Lines | |||||
/** | /** | ||||
* @brief PasswordDialog::setPassword populate the (templated) fields. | * @brief PasswordDialog::setPassword populate the (templated) fields. | ||||
* @param password | * @param password | ||||
*/ | */ | ||||
void PasswordDialog::setPassword(const QString& password) | void PasswordDialog::setPassword(const QString& password) | ||||
{ | { | ||||
FileContent fileContent = FileContent::parse(password, m_templating ? m_fields : QStringList(), m_allFields); | FileContent fileContent = FileContent::parse(password, m_templating ? m_fields : QStringList(), m_allFields); | ||||
ui->lineEditPassword->setText(fileContent.getPassword()); | ui->lineEditPassword->setPassword(fileContent.getPassword()); | ||||
QWidget *previous = ui->checkBoxShow; | QWidget *previous = ui->createPasswordButton; | ||||
// first set templated values | // first set templated values | ||||
NamedValues namedValues = fileContent.getNamedValues(); | NamedValues namedValues = fileContent.getNamedValues(); | ||||
for (QLineEdit *line : std::as_const(templateLines)) { | for (QLineEdit *line : std::as_const(templateLines)) { | ||||
line->setText(namedValues.takeValue(line->objectName())); | line->setText(namedValues.takeValue(line->objectName())); | ||||
previous = line; | previous = line; | ||||
} | } | ||||
// show remaining values (if there are) | // show remaining values (if there are) | ||||
otherLines.clear(); | otherLines.clear(); | ||||
Show All 12 Lines | |||||
/** | /** | ||||
* @brief PasswordDialog::getPassword join the (templated) fields to a QString | * @brief PasswordDialog::getPassword join the (templated) fields to a QString | ||||
* for writing back. | * for writing back. | ||||
* @return collappsed password. | * @return collappsed password. | ||||
*/ | */ | ||||
QString PasswordDialog::getPassword() | QString PasswordDialog::getPassword() | ||||
{ | { | ||||
QString passFile = ui->lineEditPassword->text() + QLatin1Char('\n'); | QString passFile = ui->lineEditPassword->password() + QLatin1Char('\n'); | ||||
QList<QLineEdit *> allLines(templateLines); | QList<QLineEdit *> allLines(templateLines); | ||||
allLines.append(otherLines); | allLines.append(otherLines); | ||||
for (QLineEdit *line : allLines) { | for (QLineEdit *line : allLines) { | ||||
QString text = line->text(); | QString text = line->text(); | ||||
if (text.isEmpty()) | if (text.isEmpty()) | ||||
continue; | continue; | ||||
passFile += line->objectName() + QStringLiteral(": ") + text + QLatin1Char('\n'); | passFile += line->objectName() + QStringLiteral(": ") + text + QLatin1Char('\n'); | ||||
} | } | ||||
passFile += ui->plainTextEdit->toPlainText(); | passFile += ui->plainTextEdit->toPlainText(); | ||||
return passFile; | return passFile; | ||||
} | } | ||||
/** | /** | ||||
* @brief PasswordDialog::setTemplate set the template and create the fields. | * @brief PasswordDialog::setTemplate set the template and create the fields. | ||||
* @param rawFields | * @param rawFields | ||||
*/ | */ | ||||
void PasswordDialog::setTemplate(const QString& rawFields, bool useTemplate) | void PasswordDialog::setTemplate(const QString& rawFields, bool useTemplate) | ||||
{ | { | ||||
m_fields = rawFields.split(QLatin1Char('\n')); | m_fields = rawFields.split(QLatin1Char('\n')); | ||||
m_templating = useTemplate; | m_templating = useTemplate; | ||||
templateLines.clear(); | templateLines.clear(); | ||||
if (m_templating) { | if (m_templating) { | ||||
QWidget *previous = ui->checkBoxShow; | QWidget *previous = ui->createPasswordButton; | ||||
for (const QString &field : std::as_const(m_fields)) { | for (const QString &field : std::as_const(m_fields)) { | ||||
if (field.isEmpty()) | if (field.isEmpty()) | ||||
continue; | continue; | ||||
auto *line = new QLineEdit(); | auto *line = new QLineEdit(); | ||||
line->setObjectName(field); | line->setObjectName(field); | ||||
ui->formLayout->addRow(new QLabel(field), line); | ui->formLayout->addRow(new QLabel(i18nc("Field name", "%1:", field)), line); | ||||
setTabOrder(previous, line); | setTabOrder(previous, line); | ||||
templateLines.append(line); | templateLines.append(line); | ||||
previous = line; | previous = line; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
Show All 26 Lines | void PasswordDialog::setPasswordCharTemplate(int t) | ||||
ui->passwordTemplateSwitch->setCurrentIndex(t); | ui->passwordTemplateSwitch->setCurrentIndex(t); | ||||
} | } | ||||
void PasswordDialog::setPass(const QString &output) | void PasswordDialog::setPass(const QString &output) | ||||
{ | { | ||||
setPassword(output); | setPassword(output); | ||||
// TODO(bezet): enable ui | // TODO(bezet): enable ui | ||||
} | } | ||||
void PasswordDialog::togglePasswordGenerationOption(bool checked) | |||||
{ | |||||
if (checked) { | |||||
ui->createPasswordOptionsButton->setIcon(QIcon::fromTheme(QStringLiteral("collapse-symbolic"))); | |||||
ui->createPasswordOptionsButton->setToolTip(i18nc("@info:tooltip", "Collapse password options")); | |||||
ui->createPasswordOptionsButton->setAccessibleName(i18nc("@info:tooltip", "Collapse password options")); | |||||
ui->label_characterset->setVisible(true); | |||||
ui->passwordTemplateSwitch->setVisible(true); | |||||
ui->label_length->setVisible(true); | |||||
ui->spinBox_pwdLength->setVisible(true); | |||||
} else { | |||||
ui->createPasswordOptionsButton->setIcon(QIcon::fromTheme(QStringLiteral("expand-symbolic"))); | |||||
ui->createPasswordOptionsButton->setToolTip(i18nc("@info:tooltip", "Toggle password options")); | |||||
ui->createPasswordOptionsButton->setAccessibleName(i18nc("@info:tooltip", "Toggle password options")); | |||||
ui->label_characterset->setVisible(false); | |||||
ui->passwordTemplateSwitch->setVisible(false); | |||||
ui->label_length->setVisible(false); | |||||
ui->spinBox_pwdLength->setVisible(false); | |||||
} | |||||
} |