File indexing completed on 2024-06-23 05:14:05
0001 /* -*- mode: c++; c-basic-offset:4 -*- 0002 dialogs/nameandemailwidget.cpp 0003 0004 This file is part of Kleopatra, the KDE keymanager 0005 SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB 0006 SPDX-FileCopyrightText: 2022 g10 Code GmbH 0007 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de> 0008 0009 SPDX-License-Identifier: GPL-2.0-or-later 0010 */ 0011 0012 #include <config-kleopatra.h> 0013 0014 #include "nameandemailwidget.h" 0015 0016 #include "view/errorlabel.h" 0017 #include "view/formtextinput.h" 0018 0019 #include <utils/validation.h> 0020 0021 #include <KLocalizedString> 0022 0023 #include <QLineEdit> 0024 #include <QVBoxLayout> 0025 0026 #include "kleopatra_debug.h" 0027 0028 using namespace Kleo; 0029 0030 namespace 0031 { 0032 QString buildUserId(const QString &name, const QString &email) 0033 { 0034 if (name.isEmpty()) { 0035 return email; 0036 } else if (email.isEmpty()) { 0037 return name; 0038 } else { 0039 return QStringLiteral("%1 <%2>").arg(name, email); 0040 } 0041 } 0042 } 0043 0044 class NameAndEmailWidget::Private 0045 { 0046 NameAndEmailWidget *const q; 0047 0048 public: 0049 struct { 0050 std::unique_ptr<FormTextInput<QLineEdit>> nameInput; 0051 std::unique_ptr<FormTextInput<QLineEdit>> emailInput; 0052 } ui; 0053 0054 explicit Private(NameAndEmailWidget *qq) 0055 : q{qq} 0056 { 0057 auto mainLayout = new QVBoxLayout{q}; 0058 0059 { 0060 ui.nameInput = FormTextInput<QLineEdit>::create(q); 0061 ui.nameInput->setLabelText(i18nc("@label", "Name")); 0062 ui.nameInput->setValueRequiredErrorMessage(i18n("Enter a name.")); 0063 setNamePattern({}); 0064 0065 mainLayout->addWidget(ui.nameInput->label()); 0066 mainLayout->addWidget(ui.nameInput->hintLabel()); 0067 mainLayout->addWidget(ui.nameInput->errorLabel()); 0068 mainLayout->addWidget(ui.nameInput->widget()); 0069 } 0070 connect(ui.nameInput->widget(), &QLineEdit::textChanged, q, [this]() { 0071 Q_EMIT q->userIDChanged(); 0072 }); 0073 0074 { 0075 ui.emailInput = FormTextInput<QLineEdit>::create(q); 0076 ui.emailInput->setLabelText(i18nc("@label", "Email address")); 0077 ui.emailInput->setValueRequiredErrorMessage(i18n("Enter an email address.")); 0078 setEmailPattern({}); 0079 0080 mainLayout->addWidget(ui.emailInput->label()); 0081 mainLayout->addWidget(ui.emailInput->hintLabel()); 0082 mainLayout->addWidget(ui.emailInput->errorLabel()); 0083 mainLayout->addWidget(ui.emailInput->widget()); 0084 } 0085 connect(ui.emailInput->widget(), &QLineEdit::textChanged, q, [this]() { 0086 Q_EMIT q->userIDChanged(); 0087 }); 0088 } 0089 0090 void setNamePattern(const QString ®exp) 0091 { 0092 if (regexp.isEmpty()) { 0093 ui.nameInput->setValidator(Validation::simpleName(Validation::Optional)); 0094 ui.nameInput->setInvalidEntryErrorMessage( 0095 i18n("The name must not include <, >, and @."), 0096 i18nc("text for screen readers", "The name must not include less-than sign, greater-than sign, and at sign.")); 0097 } else { 0098 ui.nameInput->setValidator(Validation::simpleName(regexp, Validation::Optional)); 0099 ui.nameInput->setInvalidEntryErrorMessage(i18n("The name must be in the format required by your organization and " 0100 "it must not include <, >, and @."), 0101 i18nc("text for screen readers", 0102 "The name must be in the format required by your organization and " 0103 "it must not include less-than sign, greater-than sign, and at sign.")); 0104 } 0105 } 0106 0107 void setEmailPattern(const QString ®exp) 0108 { 0109 if (regexp.isEmpty()) { 0110 ui.emailInput->setValidator(Validation::email(Validation::Optional)); 0111 ui.emailInput->setInvalidEntryErrorMessage(i18n("Enter an email address in the correct format, like name@example.com.")); 0112 } else { 0113 ui.emailInput->setValidator(Validation::email(regexp, Validation::Optional)); 0114 ui.emailInput->setInvalidEntryErrorMessage(i18n("Enter an email address in the correct format required by your organization.")); 0115 } 0116 } 0117 0118 QString name() const 0119 { 0120 return ui.nameInput->widget()->text().trimmed(); 0121 } 0122 0123 QString email() const 0124 { 0125 return ui.emailInput->widget()->text().trimmed(); 0126 } 0127 }; 0128 0129 NameAndEmailWidget::NameAndEmailWidget(QWidget *parent, Qt::WindowFlags f) 0130 : QWidget{parent, f} 0131 , d(new Private{this}) 0132 { 0133 } 0134 0135 NameAndEmailWidget::~NameAndEmailWidget() = default; 0136 0137 void NameAndEmailWidget::setName(const QString &name) 0138 { 0139 d->ui.nameInput->widget()->setText(name); 0140 } 0141 0142 QString NameAndEmailWidget::name() const 0143 { 0144 return d->name(); 0145 } 0146 0147 void NameAndEmailWidget::setNameIsRequired(bool required) 0148 { 0149 d->ui.nameInput->setIsRequired(required); 0150 } 0151 0152 bool NameAndEmailWidget::nameIsRequired() const 0153 { 0154 return d->ui.nameInput->isRequired(); 0155 } 0156 0157 void NameAndEmailWidget::setNameLabel(const QString &label) 0158 { 0159 if (label.isEmpty()) { 0160 d->ui.nameInput->setLabelText(i18nc("@label", "Name")); 0161 } else { 0162 d->ui.nameInput->setLabelText(label); 0163 } 0164 } 0165 0166 QString NameAndEmailWidget::nameLabel() const 0167 { 0168 return d->ui.nameInput->label()->text(); 0169 } 0170 0171 void NameAndEmailWidget::setNameHint(const QString &hint) 0172 { 0173 d->ui.nameInput->setHint(hint); 0174 } 0175 0176 QString NameAndEmailWidget::nameHint() const 0177 { 0178 return d->ui.nameInput->hintLabel()->text(); 0179 } 0180 0181 void NameAndEmailWidget::setNamePattern(const QString &pattern) 0182 { 0183 d->setNamePattern(pattern); 0184 } 0185 0186 QString NameAndEmailWidget::nameError() const 0187 { 0188 return d->ui.nameInput->currentError(); 0189 } 0190 0191 void NameAndEmailWidget::setEmail(const QString &email) 0192 { 0193 d->ui.emailInput->widget()->setText(email); 0194 } 0195 0196 QString NameAndEmailWidget::email() const 0197 { 0198 return d->email(); 0199 } 0200 0201 void NameAndEmailWidget::setEmailIsRequired(bool required) 0202 { 0203 d->ui.emailInput->setIsRequired(required); 0204 } 0205 0206 bool NameAndEmailWidget::emailIsRequired() const 0207 { 0208 return d->ui.emailInput->isRequired(); 0209 } 0210 0211 void NameAndEmailWidget::setEmailLabel(const QString &label) 0212 { 0213 if (label.isEmpty()) { 0214 d->ui.emailInput->setLabelText(i18nc("@label", "Email address")); 0215 } else { 0216 d->ui.emailInput->setLabelText(label); 0217 } 0218 } 0219 0220 QString NameAndEmailWidget::emailLabel() const 0221 { 0222 return d->ui.emailInput->label()->text(); 0223 } 0224 0225 void NameAndEmailWidget::setEmailHint(const QString &hint) 0226 { 0227 d->ui.emailInput->setHint(hint); 0228 } 0229 0230 QString NameAndEmailWidget::emailHint() const 0231 { 0232 return d->ui.emailInput->hintLabel()->text(); 0233 } 0234 0235 void NameAndEmailWidget::setEmailPattern(const QString &pattern) 0236 { 0237 d->setEmailPattern(pattern); 0238 } 0239 0240 QString NameAndEmailWidget::emailError() const 0241 { 0242 return d->ui.emailInput->currentError(); 0243 } 0244 0245 QString NameAndEmailWidget::userID() const 0246 { 0247 return buildUserId(name(), email()); 0248 } 0249 0250 #include "moc_nameandemailwidget.cpp"