KWidgetsAddons

knewpassworddialog.cpp
1// vi: ts=8 sts=4 sw=4
2/*
3 This file is part of the KDE libraries
4 SPDX-FileCopyrightText: 1998 Pietro Iglio <iglio@fub.it>
5 SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org>
6 SPDX-FileCopyrightText: 2004, 2005 Andrew Coles <andrew_coles@yahoo.co.uk>
7 SPDX-FileCopyrightText: 2007 Michaƫl Larouche <larouche@kde.org>
8
9 SPDX-License-Identifier: LGPL-2.0-only
10*/
11#include "knewpassworddialog.h"
12
13#include <QMessageBox>
14#include <QPushButton>
15#include <QString>
16#include <QStyleOption>
17
18#include <ktitlewidget.h>
19
20#include "ui_knewpassworddialog.h"
21
22class KNewPasswordDialogPrivate
23{
24 Q_DECLARE_TR_FUNCTIONS(KNewPasswordDialog)
25
26public:
27 KNewPasswordDialogPrivate(KNewPasswordDialog *parent)
28 : q(parent)
29 {
30 }
31
32 void init();
33 void passwordStatusChanged();
34 void showMessageWidget(const QString &message, KMessageWidget::MessageType type);
35
36 KNewPasswordDialog *const q;
37
38 QString pass;
39 QIcon icon;
40
41 Ui::KNewPasswordDialog ui;
42};
43
44void KNewPasswordDialogPrivate::init()
45{
46 ui.setupUi(q);
47 q->setIcon(QIcon::fromTheme(QStringLiteral("dialog-password")));
48
49 ui.statusMsgWidget->hide();
50
52 passwordStatusChanged();
53 });
54}
55
56void KNewPasswordDialogPrivate::passwordStatusChanged()
57{
58 switch (ui.pwdWidget->passwordStatus()) {
60 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
61 //~ singular Password must be at least %n character long
62 //~ plural Password must be at least %n characters long
63 showMessageWidget(tr("Password must be at least %n character(s) long.", "@info", ui.pwdWidget->minimumPasswordLength()), KMessageWidget::Error);
64 break;
66 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
67 showMessageWidget(tr("Password is empty.", "@info"), KMessageWidget::Warning);
68 break;
70 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
71 showMessageWidget(tr("Passwords do not match.", "@info"), KMessageWidget::Warning);
72 break;
75 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
76 showMessageWidget(tr("Passwords match.", "@info"), KMessageWidget::Positive);
77 break;
78 }
79}
80
81void KNewPasswordDialogPrivate::showMessageWidget(const QString &message, KMessageWidget::MessageType type)
82{
83 ui.statusMsgWidget->setText(message);
84 ui.statusMsgWidget->setMessageType(type);
85 ui.statusMsgWidget->animatedShow();
86}
87
88/*
89 * Password dialog.
90 */
91
93 : QDialog(parent)
94 , d(new KNewPasswordDialogPrivate(this))
95{
96 d->init();
97}
98
100
102{
103 d->ui.labelPrompt->setText(prompt);
104 d->ui.labelPrompt->setMinimumSize(d->ui.labelPrompt->sizeHint());
105}
106
108{
109 return d->ui.labelPrompt->text();
110}
111
113{
114 d->icon = icon;
115 QStyleOption option;
116 option.initFrom(this);
117 const int iconSize = style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option, this);
118 d->ui.labelIcon->setPixmap(icon.pixmap(iconSize, iconSize));
119 d->ui.labelIcon->setFixedSize(d->ui.labelIcon->sizeHint());
120}
121
123{
124 return d->icon;
125}
126
128{
129 pwd->clear();
130
131 if (d->ui.pwdWidget->passwordStatus() == KNewPasswordWidget::WeakPassword) {
133 tr("Low Password Strength", "@title:window"),
134 tr("The password you have entered has a low strength. "
135 "To improve the strength of "
136 "the password, try:\n"
137 " - using a longer password;\n"
138 " - using a mixture of upper- and lower-case letters;\n"
139 " - using numbers or symbols as well as letters.\n"
140 "\n"
141 "Would you like to use this password anyway?"),
143
145 return false;
146 }
147 }
148 if (!checkPassword(d->ui.pwdWidget->password())) {
149 return false;
150 }
151
152 *pwd = d->ui.pwdWidget->password();
153 return true;
154}
155
157{
158 QString pwd;
159 if (!checkAndGetPassword(&pwd)) {
160 return;
161 }
162 d->pass = pwd;
163 Q_EMIT newPassword(d->pass);
165}
166
168{
169 d->ui.pwdWidget->setAllowEmptyPasswords(allowed);
170}
171
173{
174 return d->ui.pwdWidget->allowEmptyPasswords();
175}
176
178{
179 d->ui.pwdWidget->setMinimumPasswordLength(minLength);
180}
181
183{
184 return d->ui.pwdWidget->minimumPasswordLength();
185}
186
188{
189 d->ui.pwdWidget->setMaximumPasswordLength(maxLength);
190}
191
193{
194 return d->ui.pwdWidget->maximumPasswordLength();
195}
196
197// reasonable password length code contributed by Steffen Mthing
198
200{
201 d->ui.pwdWidget->setReasonablePasswordLength(reasonableLength);
202}
203
205{
206 return d->ui.pwdWidget->reasonablePasswordLength();
207}
208
210{
211 d->ui.pwdWidget->setPasswordStrengthWarningLevel(warningLevel);
212}
213
215{
216 return d->ui.pwdWidget->passwordStrengthWarningLevel();
217}
218
220{
221 d->ui.pwdWidget->setBackgroundWarningColor(color);
222}
223
225{
226 return d->ui.pwdWidget->backgroundWarningColor();
227}
228
230{
231 return d->pass;
232}
233
234#if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(6, 0)
235#pragma GCC diagnostic push
236#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
238{
239 d->ui.pwdWidget->setRevealPasswordAvailable(reveal);
240}
241
243{
244 return d->ui.pwdWidget->isRevealPasswordAvailable();
245}
246#pragma GCC diagnostic pop
247#endif
248
249KPassword::RevealMode KNewPasswordDialog::revealPasswordMode() const
250{
251 return d->ui.pwdWidget->revealPasswordMode();
252}
253
254void KNewPasswordDialog::setRevealPasswordMode(KPassword::RevealMode revealPasswordMode)
255{
256 d->ui.pwdWidget->setRevealPasswordMode(revealPasswordMode);
257}
258
260{
261 return true;
262}
263
264#include "moc_knewpassworddialog.cpp"
MessageType
Available message types.
@ Error
Error message type.
@ Positive
Positive message type.
@ Warning
Warning message type.
A password input dialog.
void setPasswordStrengthWarningLevel(int warningLevel)
Set the password strength level below which a warning is given Value is in the range 0 to 99.
void setRevealPasswordAvailable(bool reveal)
Whether to show the visibility trailing action in the line edit.
void setBackgroundWarningColor(const QColor &color)
When the verification password does not match, the background color of the verification field is set ...
QColor backgroundWarningColor() const
The color used as warning for the verification password field's background.
void setIcon(const QIcon &icon)
Sets the icon that appears next to the prompt in the dialog.
void setReasonablePasswordLength(int reasonableLength)
Password length that is expected to be reasonably safe.
void setAllowEmptyPasswords(bool allowed)
Allow empty passwords? - Default: true.
bool allowEmptyPasswords() const
Allow empty passwords?
void setMaximumPasswordLength(int maxLength)
Maximum acceptable password length.
virtual bool checkPassword(const QString &)
Virtual function that can be overridden to provide password checking in derived classes.
bool checkAndGetPassword(QString *pwd)
Checks input password.
int minimumPasswordLength() const
Minimum acceptable password length.
int passwordStrengthWarningLevel() const
Password strength level below which a warning is given.
void setMinimumPasswordLength(int minLength)
Minimum acceptable password length.
QString prompt() const
Returns the password prompt.
KPassword::RevealMode revealPasswordMode
int maximumPasswordLength() const
Maximum acceptable password length.
bool isRevealPasswordAvailable() const
Whether the visibility trailing action in the line edit is visible.
QIcon icon() const
Returns the icon that appears next to the prompt in the dialog.
void newPassword(const QString &password)
The dialog has been accepted, and the new password is password.
void setPrompt(const QString &prompt)
Sets the password prompt.
QString password() const
Returns the password entered.
void setRevealPasswordMode(KPassword::RevealMode revealPasswordMode)
Set when the reveal password button will be visible.
KNewPasswordDialog(QWidget *parent=nullptr)
Constructs a password dialog.
~KNewPasswordDialog() override
Destructs the password dialog.
int reasonablePasswordLength() const
Password length that is expected to be reasonably safe.
void passwordStatusChanged()
Notify about the current status of the password being typed.
@ PasswordNotVerified
Password and verification password don't match.
@ StrongPassword
Passwords match and the strength level is good.
@ WeakPassword
Passwords match but the strength level is not enough.
@ EmptyPasswordNotAllowed
Both passwords fields empty, but minimum length > 0.
@ PasswordTooShort
Password length is too low.
virtual void accept()
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, StandardButton defaultButton)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
QString tr(const char *sourceText, const char *disambiguation, int n)
PM_MessageBoxIconSize
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
void initFrom(const QWidget *widget)
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.