Libkleo

nameandemailwidget.cpp
1/* -*- mode: c++; c-basic-offset:4 -*-
2 dialogs/nameandemailwidget.cpp
3
4 This file is part of Kleopatra, the KDE keymanager
5 SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
6 SPDX-FileCopyrightText: 2022 g10 Code GmbH
7 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11
12#include "nameandemailwidget.h"
13
14#include "formtextinput_p.h"
15
16#include "ui/errorlabel.h"
17#include "utils/validation.h"
18
19#include <KLocalizedString>
20
21#include <QLabel>
22#include <QLineEdit>
23#include <QStyle>
24#include <QVBoxLayout>
25
26using namespace Kleo;
27
28namespace
29{
30QString buildUserId(const QString &name, const QString &email)
31{
32 if (name.isEmpty()) {
33 return email;
34 } else if (email.isEmpty()) {
35 return name;
36 } else {
37 return QStringLiteral("%1 <%2>").arg(name, email);
38 }
39}
40}
41
42class NameAndEmailWidget::Private
43{
44 NameAndEmailWidget *const q;
45
46public:
47 struct {
48 std::unique_ptr<FormTextInput<QLineEdit>> nameInput;
49 std::unique_ptr<FormTextInput<QLineEdit>> emailInput;
50 } ui;
51
52 explicit Private(NameAndEmailWidget *qq)
53 : q{qq}
54 {
55 auto mainLayout = new QVBoxLayout{q};
56
57 {
58 ui.nameInput = FormTextInput<QLineEdit>::create(q);
59 ui.nameInput->setLabelText(i18nc("@label", "Name"));
60 ui.nameInput->setValueRequiredErrorMessage(i18n("Enter a name."));
61 setNamePattern({});
62
63 mainLayout->addWidget(ui.nameInput->label());
64 mainLayout->addWidget(ui.nameInput->hintLabel());
65 mainLayout->addWidget(ui.nameInput->widget());
66 mainLayout->addWidget(ui.nameInput->errorLabel());
67 mainLayout->addSpacing(q->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
68 }
69 connect(ui.nameInput->widget(), &QLineEdit::textChanged, q, [this]() {
70 Q_EMIT q->userIDChanged();
71 });
72
73 {
74 ui.emailInput = FormTextInput<QLineEdit>::create(q);
75 ui.emailInput->setLabelText(i18nc("@label", "Email address"));
76 ui.emailInput->setValueRequiredErrorMessage(i18n("Enter an email address."));
77 setEmailPattern({});
78
79 mainLayout->addWidget(ui.emailInput->label());
80 mainLayout->addWidget(ui.emailInput->hintLabel());
81 mainLayout->addWidget(ui.emailInput->widget());
82 mainLayout->addWidget(ui.emailInput->errorLabel());
83 }
84 connect(ui.emailInput->widget(), &QLineEdit::textChanged, q, [this]() {
85 Q_EMIT q->userIDChanged();
86 });
87 }
88
89 void setNamePattern(const QString &regexp)
90 {
91 if (regexp.isEmpty()) {
92 ui.nameInput->setValidator(Validation::simpleName(Validation::Optional));
93 ui.nameInput->setInvalidEntryErrorMessage(
94 i18n("The name must not include <, >, and @."),
95 i18nc("text for screen readers", "The name must not include less-than sign, greater-than sign, and at sign."));
96 } else {
97 ui.nameInput->setValidator(Validation::simpleName(regexp, Validation::Optional));
98 ui.nameInput->setInvalidEntryErrorMessage(i18n("The name must be in the format required by your organization and "
99 "it must not include <, >, and @."),
100 i18nc("text for screen readers",
101 "The name must be in the format required by your organization and "
102 "it must not include less-than sign, greater-than sign, and at sign."));
103 }
104 }
105
106 void setEmailPattern(const QString &regexp)
107 {
108 if (regexp.isEmpty()) {
109 ui.emailInput->setValidator(Validation::email(Validation::Optional));
110 ui.emailInput->setInvalidEntryErrorMessage(i18n("Enter an email address in the correct format, like name@example.com."));
111 } else {
112 ui.emailInput->setValidator(Validation::email(regexp, Validation::Optional));
113 ui.emailInput->setInvalidEntryErrorMessage(i18n("Enter an email address in the correct format required by your organization."));
114 }
115 }
116
117 QString name() const
118 {
119 return ui.nameInput->widget()->text().trimmed();
120 }
121
122 QString email() const
123 {
124 return ui.emailInput->widget()->text().trimmed();
125 }
126};
127
128NameAndEmailWidget::NameAndEmailWidget(QWidget *parent, Qt::WindowFlags f)
129 : QWidget{parent, f}
130 , d(new Private{this})
131{
132}
133
134NameAndEmailWidget::~NameAndEmailWidget() = default;
135
136void NameAndEmailWidget::setName(const QString &name)
137{
138 d->ui.nameInput->widget()->setText(name);
139}
140
141QString NameAndEmailWidget::name() const
142{
143 return d->name();
144}
145
146void NameAndEmailWidget::setNameIsRequired(bool required)
147{
148 d->ui.nameInput->setIsRequired(required);
149}
150
151bool NameAndEmailWidget::nameIsRequired() const
152{
153 return d->ui.nameInput->isRequired();
154}
155
156void NameAndEmailWidget::setNameLabel(const QString &label)
157{
158 if (label.isEmpty()) {
159 d->ui.nameInput->setLabelText(i18nc("@label", "Name"));
160 } else {
161 d->ui.nameInput->setLabelText(label);
162 }
163}
164
165QString NameAndEmailWidget::nameLabel() const
166{
167 return d->ui.nameInput->label()->text();
168}
169
170void NameAndEmailWidget::setNameHint(const QString &hint)
171{
172 d->ui.nameInput->setHint(hint);
173}
174
175QString NameAndEmailWidget::nameHint() const
176{
177 return d->ui.nameInput->hintLabel()->text();
178}
179
180void NameAndEmailWidget::setNamePattern(const QString &pattern)
181{
182 d->setNamePattern(pattern);
183}
184
185QString NameAndEmailWidget::nameError() const
186{
187 return d->ui.nameInput->currentError();
188}
189
190void NameAndEmailWidget::setEmail(const QString &email)
191{
192 d->ui.emailInput->widget()->setText(email);
193}
194
195QString NameAndEmailWidget::email() const
196{
197 return d->email();
198}
199
200void NameAndEmailWidget::setEmailIsRequired(bool required)
201{
202 d->ui.emailInput->setIsRequired(required);
203}
204
205bool NameAndEmailWidget::emailIsRequired() const
206{
207 return d->ui.emailInput->isRequired();
208}
209
210void NameAndEmailWidget::setEmailLabel(const QString &label)
211{
212 if (label.isEmpty()) {
213 d->ui.emailInput->setLabelText(i18nc("@label", "Email address"));
214 } else {
215 d->ui.emailInput->setLabelText(label);
216 }
217}
218
219QString NameAndEmailWidget::emailLabel() const
220{
221 return d->ui.emailInput->label()->text();
222}
223
224void NameAndEmailWidget::setEmailHint(const QString &hint)
225{
226 d->ui.emailInput->setHint(hint);
227}
228
229QString NameAndEmailWidget::emailHint() const
230{
231 return d->ui.emailInput->hintLabel()->text();
232}
233
234void NameAndEmailWidget::setEmailPattern(const QString &pattern)
235{
236 d->setEmailPattern(pattern);
237}
238
239QString NameAndEmailWidget::emailError() const
240{
241 return d->ui.emailInput->currentError();
242}
243
245{
246 return buildUserId(name(), email());
247}
248
249#include "moc_nameandemailwidget.cpp"
constexpr bool isEmpty() const
A widget containing a name and an email field.
QString userID() const
Returns the user ID built from the entered name and/or email address.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
PM_LayoutVerticalSpacing
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
typedef WindowFlags
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 13 2024 11:51:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.