diff --git a/src/dialogs/adduseriddialog.cpp b/src/dialogs/adduseriddialog.cpp index 3a055198e..63eec70eb 100644 --- a/src/dialogs/adduseriddialog.cpp +++ b/src/dialogs/adduseriddialog.cpp @@ -1,180 +1,227 @@ /* -*- mode: c++; c-basic-offset:4 -*- dialogs/adduseriddialog.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-FileCopyrightText: 2022 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "adduseriddialog.h" +#include "view/errorlabel.h" +#include "view/formtextinput.h" +#include + +#include #include +#include #include +#include #include #include #include #include +#include #include #include "kleopatra_debug.h" using namespace Kleo; namespace { QString buildUserId(const QString &name, const QString &email) { if (name.isEmpty()) { return email; } else if (email.isEmpty()) { return name; } else { return QStringLiteral("%1 <%2>").arg(name, email); } } } class AddUserIDDialog::Private { friend class ::Kleo::AddUserIDDialog; AddUserIDDialog *const q; struct { - QLineEdit *nameEdit; - QLineEdit *emailEdit; + std::unique_ptr> nameInput; + std::unique_ptr> emailInput; QLabel *resultLabel; QDialogButtonBox *buttonBox; } ui; public: explicit Private(AddUserIDDialog *qq) : q{qq} { q->setWindowTitle(i18nc("title:window", "Add User ID")); auto mainLayout = new QVBoxLayout{q}; - mainLayout->addWidget(new QLabel{i18n("Enter the name and/or the email address to use for the user ID."), q}); + mainLayout->addWidget(new QLabel{i18n("Enter a name and/or an email address to use for the user ID."), q}); auto gridLayout = new QGridLayout; int row = -1; + const KConfigGroup config{KSharedConfig::openConfig(), "CertificateCreationWizard"}; { - auto label = new QLabel{i18nc("@label", "Name:"), q}; - ui.nameEdit = new QLineEdit{q}; - label->setBuddy(ui.nameEdit); + ui.nameInput = FormTextInput::create(q); + ui.nameInput->label()->setText(i18nc("@label", "Name:")); + const auto regexp = config.readEntry(QLatin1String("NAME_regex")); + ui.nameInput->setValidator(regexp.isEmpty() ? Validation::pgpName(Validation::Optional, q) + : Validation::pgpName(regexp, Validation::Optional, q)); + const auto additionalRule = regexp.isEmpty() ? QString{} : i18n("Additionally, the name must adhere to rules set by your organization."); + ui.nameInput->setToolTip(xi18n( + "If a name is given, then it has to satisfy the following rules:" + "" + "The name must be at least 5 characters long." + "The first character must not be a digit." + "The name must not contain any of the following characters: <, >, @." + "" + "%1" + "", + additionalRule)); + ui.nameInput->setErrorMessage(i18n("Error: The entered name is not valid.")); row++; - gridLayout->addWidget(label, row, 0, 1, 1); - gridLayout->addWidget(ui.nameEdit, row, 1, 1, 1); + gridLayout->addWidget(ui.nameInput->label(), row, 0, 1, 1); + gridLayout->addWidget(ui.nameInput->widget(), row, 1, 1, 1); + row++; + gridLayout->addWidget(ui.nameInput->errorLabel(), row, 0, 1, 2); } - connect(ui.nameEdit, &QLineEdit::textChanged, - q, [this]() { onNameChanged(); }); + connect(ui.nameInput->widget(), &QLineEdit::textChanged, + q, [this]() { updateResultLabel(); }); { - auto label = new QLabel{i18nc("@label", "Email:"), q}; - ui.emailEdit = new QLineEdit{q}; - label->setBuddy(ui.emailEdit); + ui.emailInput = FormTextInput::create(q); + ui.emailInput->label()->setText(i18nc("@label", "Email:")); + const auto regexp = config.readEntry(QLatin1String("EMAIL_regex")); + ui.emailInput->setValidator(regexp.isEmpty() ? Validation::email(Validation::Optional, q) + : Validation::email(regexp, Validation::Optional, q)); + if (regexp.isEmpty()) { + ui.emailInput->setToolTip(xi18n("If an email address is given, then it has to be a valid address.")); + } else { + ui.emailInput->setToolTip(xi18n("If an email address is given, then it has to be a valid address " + "and it must satisfy to rules set by your organization.")); + } + ui.emailInput->setErrorMessage(i18n("Error: The entered email address is not valid.")); row++; - gridLayout->addWidget(label, row, 0, 1, 1); - gridLayout->addWidget(ui.emailEdit, row, 1, 1, 1); + gridLayout->addWidget(ui.emailInput->label(), row, 0, 1, 1); + gridLayout->addWidget(ui.emailInput->widget(), row, 1, 1, 1); + row++; + gridLayout->addWidget(ui.emailInput->errorLabel(), row, 0, 1, 2); } - connect(ui.emailEdit, &QLineEdit::textChanged, - q, [this]() { onEmailChanged(); }); + connect(ui.emailInput->widget(), &QLineEdit::textChanged, + q, [this]() { updateResultLabel(); }); mainLayout->addLayout(gridLayout); mainLayout->addWidget(new KSeparator{Qt::Horizontal, q}); { auto label = new QLabel{i18n("This is how the new user ID will be stored in the certificate:"), q}; mainLayout->addWidget(label); } { ui.resultLabel = new QLabel{q}; ui.resultLabel->setMinimumSize(300, 0); ui.resultLabel->setAlignment(Qt::AlignCenter); ui.resultLabel->setTextFormat(Qt::PlainText); QFont font; font.setBold(true); font.setWeight(75); ui.resultLabel->setFont(font); mainLayout->addWidget(ui.resultLabel); } mainLayout->addWidget(new KSeparator{Qt::Horizontal, q}); mainLayout->addStretch(1); ui.buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q}; mainLayout->addWidget(ui.buttonBox); - connect(ui.buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept); + connect(ui.buttonBox, &QDialogButtonBox::accepted, q, [this]() { checkAccept(); }); connect(ui.buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject); updateResultLabel(); } QString name() const { - return ui.nameEdit->text().trimmed(); + return ui.nameInput->widget()->text().trimmed(); } QString email() const { - return ui.emailEdit->text().trimmed(); + return ui.emailInput->widget()->text().trimmed(); } private: - void onNameChanged() - { - updateResultLabel(); - } - - void onEmailChanged() + void checkAccept() { - updateResultLabel(); + QStringList errors; + if (ui.resultLabel->text().isEmpty()) { + errors.push_back(i18n("Name and email address cannot both be empty.")); + } + if (!ui.nameInput->hasAcceptableInput()) { + errors.push_back(i18n("The entered name is not valid.")); + } + if (!ui.emailInput->hasAcceptableInput()) { + errors.push_back(i18n("The entered email address is not valid.")); + } + if (errors.size() > 1) { + KMessageBox::errorList(q, i18n("Sorry, the entered data is not acceptable."), errors); + } else if (!errors.empty()) { + KMessageBox::sorry(q, errors.first()); + } else { + q->accept(); + } } void updateResultLabel() { ui.resultLabel->setText(buildUserId(name(), email())); } }; AddUserIDDialog::AddUserIDDialog(QWidget *parent, Qt::WindowFlags f) : QDialog{parent, f} , d(new Private{this}) { } AddUserIDDialog::~AddUserIDDialog() = default; void AddUserIDDialog::setName(const QString &name) { - d->ui.nameEdit->setText(name); + d->ui.nameInput->widget()->setText(name); } QString AddUserIDDialog::name() const { return d->name(); } void AddUserIDDialog::setEmail(const QString &email) { - d->ui.emailEdit->setText(email); + d->ui.emailInput->widget()->setText(email); } QString AddUserIDDialog::email() const { return d->email(); }