File indexing completed on 2024-04-28 15:32:08

0001 // vi: ts=8 sts=4 sw=4
0002 /*
0003     This file is part of the KDE libraries
0004     SPDX-FileCopyrightText: 1998 Pietro Iglio <iglio@fub.it>
0005     SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org>
0006     SPDX-FileCopyrightText: 2004, 2005 Andrew Coles <andrew_coles@yahoo.co.uk>
0007     SPDX-FileCopyrightText: 2006, 2007 Olivier Goffart <ogoffart @ kde.org>
0008     SPDX-FileCopyrightText: 2015 Elvis Angelaccio <elvis.angelaccio@kde.org>
0009 
0010     SPDX-License-Identifier: LGPL-2.0-only
0011 */
0012 #ifndef KNEWPASSWORDWIDGET_H
0013 #define KNEWPASSWORDWIDGET_H
0014 
0015 #include <QWidget>
0016 #include <memory>
0017 
0018 #include <kwidgetsaddons_export.h>
0019 
0020 /**
0021  * @class KNewPasswordWidget knewpasswordwidget.h KNewPasswordWidget
0022  *
0023  * @short A password input widget.
0024  *
0025  * This widget allows the user to enter a new password.
0026  *
0027  * The password has to be entered twice to check if the passwords
0028  * match. A hint about the strength of the entered password is also
0029  * shown.
0030  *
0031  * In order to embed this widget in your custom password dialog,
0032  * you may want to connect to the passwordStatus() signal.
0033  * This way you can e.g. disable the OK button if the passwords
0034  * don't match, warn the user if the password is too weak, and so on.
0035  *
0036  * \section usage Usage Example
0037  * \subsection Setup
0038  *
0039  * \code
0040  *  KNewPasswordWidget *m_passwordWidget = new KNewPasswordWidget(this);
0041  *  // set a background warning color (taken from the current color scheme)
0042  *  KColorScheme colorScheme(QPalette::Active, KColorScheme::View);
0043  *  m_passwordWidget->setBackgroundWarningColor(colorScheme.background(KColorScheme::NegativeBackground).color());
0044  *  // listen to password status updates
0045  *  connect(m_passwordWidget, &KNewPasswordWidget::passwordStatusChanged, this, &MyCustomDialog::slotPasswordStatusChanged);
0046  *  ...
0047  * \endcode
0048  *
0049  * \subsection update Update your custom dialog
0050  *
0051  * @snippet knewpasswordwidget_test.cpp update_custom_dialog
0052  *
0053  * \subsection accept Accept your custom dialog
0054  *
0055  * @snippet knewpasswordwidget_test.cpp accept_custom_dialog
0056  *
0057  * @author Geert Jansen <jansen@kde.org>
0058  * @author Olivier Goffart <ogoffart@kde.org>
0059  * @author Elvis Angelaccio <elvis.angelaccio@kde.org>
0060  * @since 5.16
0061  */
0062 class KWIDGETSADDONS_EXPORT KNewPasswordWidget : public QWidget
0063 {
0064     Q_OBJECT
0065     Q_PROPERTY(PasswordStatus passwordStatus READ passwordStatus)
0066     Q_PROPERTY(bool allowEmptyPasswords READ allowEmptyPasswords WRITE setAllowEmptyPasswords)
0067     Q_PROPERTY(int minimumPasswordLength READ minimumPasswordLength WRITE setMinimumPasswordLength)
0068     Q_PROPERTY(int maximumPasswordLength READ maximumPasswordLength WRITE setMaximumPasswordLength)
0069     Q_PROPERTY(int reasonablePasswordLength READ reasonablePasswordLength WRITE setReasonablePasswordLength)
0070     Q_PROPERTY(int passwordStrengthWarningLevel READ passwordStrengthWarningLevel WRITE setPasswordStrengthWarningLevel)
0071     Q_PROPERTY(QColor backgroundWarningColor READ backgroundWarningColor WRITE setBackgroundWarningColor)
0072     Q_PROPERTY(bool passwordStrengthMeterVisible READ isPasswordStrengthMeterVisible WRITE setPasswordStrengthMeterVisible)
0073     /**
0074      * @since 5.31
0075      */
0076     Q_PROPERTY(bool revealPasswordAvailable READ isRevealPasswordAvailable WRITE setRevealPasswordAvailable)
0077 
0078 public:
0079     /**
0080      * Status of the password being typed in the widget.
0081      */
0082     enum PasswordStatus {
0083         EmptyPasswordNotAllowed, /**< Both passwords fields empty, but minimum length > 0. */
0084         PasswordTooShort, /**< Password length is too low. */
0085         PasswordNotVerified, /**< Password and verification password don't match. */
0086         WeakPassword, /**< Passwords match but the strength level is not enough. */
0087         StrongPassword, /**< Passwords match and the strength level is good. */
0088     };
0089     Q_ENUM(PasswordStatus)
0090 
0091     /**
0092      * Constructs a password widget.
0093      *
0094      * @param parent Passed to lower level constructor.
0095      */
0096     explicit KNewPasswordWidget(QWidget *parent = nullptr);
0097 
0098     /**
0099      * Destructs the password widget.
0100      */
0101     ~KNewPasswordWidget() override;
0102 
0103     /**
0104      * The current status of the password in the widget.
0105      */
0106     PasswordStatus passwordStatus() const;
0107 
0108     /**
0109      * Allow empty passwords?
0110      *
0111      * @return true if minimumPasswordLength() == 0
0112      */
0113     bool allowEmptyPasswords() const;
0114 
0115     /**
0116      * Minimum acceptable password length.
0117      */
0118     int minimumPasswordLength() const;
0119 
0120     /**
0121      * Maximum acceptable password length.
0122      */
0123     int maximumPasswordLength() const;
0124 
0125     /**
0126      * Password length that is expected to be reasonably safe.
0127      */
0128     int reasonablePasswordLength() const;
0129 
0130     /**
0131      * Password strength level below which a warning is given
0132      */
0133     int passwordStrengthWarningLevel() const;
0134 
0135     /**
0136      * The color used as warning for the verification password field's background.
0137      */
0138     QColor backgroundWarningColor() const;
0139 
0140     /**
0141      * Whether the password strength meter is visible.
0142      */
0143     bool isPasswordStrengthMeterVisible() const;
0144 
0145     /**
0146      * Whether the visibility trailing action in the line edit is visible.
0147      * @since 5.31
0148      */
0149     bool isRevealPasswordAvailable() const;
0150 
0151     /**
0152      * Returns the password entered.
0153      * @note Only returns meaningful data when passwordStatus
0154      *       is either WeakPassword or StrongPassword.
0155      */
0156     QString password() const;
0157 
0158 public Q_SLOTS:
0159 
0160     /**
0161      * Allow empty passwords? - Default: true
0162      *
0163      * same as setMinimumPasswordLength( allowed ? 0 : 1 )
0164      */
0165     void setAllowEmptyPasswords(bool allowed);
0166 
0167     /**
0168      * Minimum acceptable password length.
0169      *
0170      * Default: 0
0171      *
0172      * @param minLength The new minimum password length
0173      */
0174     void setMinimumPasswordLength(int minLength);
0175 
0176     /**
0177      * Maximum acceptable password length.
0178      *
0179      * @param maxLength The new maximum password length.
0180      */
0181     void setMaximumPasswordLength(int maxLength);
0182 
0183     /**
0184      * Password length that is expected to be reasonably safe.
0185      * The value is guaranteed to be in the range from 1 to maximumPasswordLength().
0186      *
0187      * Used to compute the strength level
0188      *
0189      * Default: 8 - the standard UNIX password length
0190      *
0191      * @param reasonableLength The new reasonable password length.
0192      */
0193     void setReasonablePasswordLength(int reasonableLength);
0194 
0195     /**
0196      * Set the password strength level below which a warning is given
0197      * The value is guaranteed to be in the range from 0 to 99. Empty passwords score 0;
0198      * non-empty passwords score up to 100, depending on their length and whether they
0199      * contain numbers, mixed case letters and punctuation.
0200      *
0201      * Default: 1 - warn if the password has no discernible strength whatsoever
0202      * @param warningLevel The level below which a warning should be given.
0203      */
0204     void setPasswordStrengthWarningLevel(int warningLevel);
0205 
0206     /**
0207      * When the verification password does not match, the background color
0208      * of the verification field is set to @p color. As soon as the passwords match,
0209      * the original color of the verification field is restored.
0210      */
0211     void setBackgroundWarningColor(const QColor &color);
0212 
0213     /**
0214      * Whether to show the password strength meter (label and progress bar).
0215      * Default is true.
0216      */
0217     void setPasswordStrengthMeterVisible(bool visible);
0218 
0219     /**
0220      * Whether to show the visibility trailing action in the line edit.
0221      * Default is true. This can be used to honor the lineedit_reveal_password
0222      * kiosk key, for example:
0223      * \code
0224      * passwordWidget.setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0225      * \endcode
0226      * @since 5.31
0227      */
0228     void setRevealPasswordAvailable(bool reveal);
0229 
0230 Q_SIGNALS:
0231 
0232     /**
0233      * Notify about the current status of the password being typed.
0234      */
0235     void passwordStatusChanged();
0236 
0237 private:
0238     std::unique_ptr<class KNewPasswordWidgetPrivate> const d;
0239 
0240     Q_DISABLE_COPY(KNewPasswordWidget)
0241 };
0242 
0243 #endif // KNEWPASSWORDWIDGET_H