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 
0009     SPDX-License-Identifier: LGPL-2.0-only
0010 */
0011 #ifndef KNEWPASSWORDDIALOG_H
0012 #define KNEWPASSWORDDIALOG_H
0013 
0014 #include <QDialog>
0015 #include <memory>
0016 
0017 #include <kwidgetsaddons_export.h>
0018 
0019 class QWidget;
0020 
0021 /**
0022  * @class KNewPasswordDialog knewpassworddialog.h KNewPasswordDialog
0023  *
0024  * @short A password input dialog.
0025  *
0026  * This dialog asks the user to enter a new password.
0027  *
0028  * The password has to be entered twice to check if the passwords
0029  * match. A hint about the strength of the entered password is also
0030  * shown.
0031  *
0032  * \section usage Usage Example
0033  * \subsection asynchronous Asynchronous
0034  *
0035  * \code
0036  *  KNewPasswordDialog *dlg = new KNewPasswordDialog( parent );
0037  *  dlg->setPrompt(i18n("Enter a password"));
0038  *  connect(dlg, &KNewPasswordDialog::newPassword, this, [this](const QString &pass) { setPassword(pass); });
0039  *  connect(dlg, &QDialog::rejected, this, [this]() { slotCancel(); });
0040  *  dlg->show();
0041  * \endcode
0042  *
0043  * \subsection synchronous Synchronous
0044  *
0045  * \code
0046  *  KNewPasswordDialog dlg(parent);
0047  *  dlg.setPrompt(i18n("Enter a password"));
0048  *  if(dlg.exec()) {
0049  *      setPassword(dlg.password());
0050  *  }
0051  * \endcode
0052  *
0053  * \image html knewpassworddialog.png "KNewPasswordDialog"
0054  *
0055  * @author Geert Jansen <jansen@kde.org>
0056  * @author Olivier Goffart <ogoffart@kde.org>
0057  */
0058 class KWIDGETSADDONS_EXPORT KNewPasswordDialog : public QDialog
0059 {
0060     Q_OBJECT
0061 
0062 public:
0063     /**
0064      * Constructs a password dialog.
0065      *
0066      * @param parent Passed to lower level constructor.
0067      */
0068     explicit KNewPasswordDialog(QWidget *parent = nullptr);
0069 
0070     /**
0071      * Destructs the password dialog.
0072      */
0073     ~KNewPasswordDialog() override;
0074 
0075     /**
0076      * Sets the password prompt.
0077      */
0078     void setPrompt(const QString &prompt);
0079 
0080     /**
0081      * Returns the password prompt.
0082      */
0083     QString prompt() const;
0084 
0085     /**
0086      * Sets the icon that appears next to the prompt in the dialog. The default icon represents a simple key.
0087      * @since 5.63
0088      */
0089     void setIcon(const QIcon &icon);
0090 
0091     /**
0092      * Returns the icon that appears next to the prompt in the dialog. The default icon represents a simple key.
0093      * @since 5.63
0094      */
0095     QIcon icon() const;
0096 
0097 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(5, 63)
0098     /**
0099      * Sets the pixmap that appears next to the prompt in the dialog. The default pixmap represents a simple key.
0100      *
0101      * the recommended size is KIconLoader::SizeHuge
0102      *
0103      * @deprecated since 5.63 use setIcon()
0104      */
0105     KWIDGETSADDONS_DEPRECATED_VERSION(5, 63, "Use KNewPasswordDialog::setIcon(const QIcon &)")
0106     void setPixmap(const QPixmap &);
0107 
0108     /**
0109      * Returns the pixmap that appears next to the prompt in the dialog
0110      * @deprecated since 5.63 use icon()
0111      */
0112     KWIDGETSADDONS_DEPRECATED_VERSION(5, 63, "Use KNewPasswordDialog::icon()")
0113     QPixmap pixmap() const;
0114 #endif
0115 
0116     /**
0117      * Allow empty passwords? - Default: true
0118      *
0119      * same as setMinimumPasswordLength( allowed ? 0 : 1 )
0120      */
0121     void setAllowEmptyPasswords(bool allowed);
0122 
0123     /**
0124      * Allow empty passwords?
0125      *
0126      * @return true if minimumPasswordLength() == 0
0127      */
0128     bool allowEmptyPasswords() const;
0129 
0130     /**
0131      * Minimum acceptable password length.
0132      *
0133      * Default: 0
0134      *
0135      * @param minLength The new minimum password length
0136      */
0137     void setMinimumPasswordLength(int minLength);
0138 
0139     /**
0140      * Minimum acceptable password length.
0141      */
0142     int minimumPasswordLength() const;
0143 
0144     /**
0145      * Maximum acceptable password length.
0146      *
0147      * @param maxLength The new maximum password length.
0148      */
0149     void setMaximumPasswordLength(int maxLength);
0150 
0151     /**
0152      * Maximum acceptable password length.
0153      */
0154     int maximumPasswordLength() const;
0155 
0156     /**
0157      * Password length that is expected to be reasonably safe.
0158      *
0159      * Used to compute the strength level
0160      *
0161      * Default: 8 - the standard UNIX password length
0162      *
0163      * @param reasonableLength The new reasonable password length.
0164      */
0165     void setReasonablePasswordLength(int reasonableLength);
0166 
0167     /**
0168      * Password length that is expected to be reasonably safe.
0169      */
0170     int reasonablePasswordLength() const;
0171 
0172     /**
0173      * Set the password strength level below which a warning is given
0174      * Value is in the range 0 to 99. Empty passwords score 0;
0175      * non-empty passwords score up to 100, depending on their length and whether they
0176      * contain numbers, mixed case letters and punctuation.
0177      *
0178      * Default: 1 - warn if the password has no discernible strength whatsoever
0179      * @param warningLevel The level below which a warning should be given.
0180      */
0181     void setPasswordStrengthWarningLevel(int warningLevel);
0182 
0183     /**
0184      * Password strength level below which a warning is given
0185      */
0186     int passwordStrengthWarningLevel() const;
0187 
0188     /**
0189      * When the verification password does not match, the background color
0190      * of the verification field is set to @p color. As soon as the passwords match,
0191      * the original color of the verification field is restored.
0192      *
0193      * Default: the background color from the current theme.
0194      * @since 5.17
0195      */
0196     void setBackgroundWarningColor(const QColor &color);
0197 
0198     /**
0199      * The color used as warning for the verification password field's background.
0200      * @since 5.17
0201      */
0202     QColor backgroundWarningColor() const;
0203 
0204     /**
0205      * Returns the password entered.
0206      * @note Only has meaningful data after accept has been called
0207      *       if you want to access the password from a subclass use
0208      *       checkAndGetPassword()
0209      */
0210     QString password() const;
0211 
0212     /**
0213      * Whether to show the visibility trailing action in the line edit.
0214      * Default is true. This can be used to honor the lineedit_reveal_password
0215      * kiosk key, for example:
0216      * \code
0217      * passwordDialog.setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0218      * \endcode
0219      * @since 5.31
0220      */
0221     void setRevealPasswordAvailable(bool reveal);
0222 
0223     /**
0224      * Whether the visibility trailing action in the line edit is visible.
0225      * @since 5.31
0226      */
0227     bool isRevealPasswordAvailable() const;
0228 
0229     /**
0230      * @internal
0231      */
0232     void accept() override;
0233 
0234 protected:
0235     /**
0236      * Virtual function that can be overridden to provide password
0237      * checking in derived classes. It should return @p true if the
0238      * password is valid, @p false otherwise.
0239      */
0240     virtual bool checkPassword(const QString &);
0241 
0242     /**
0243      * Checks input password.
0244      * If the password is right, returns true
0245      * and fills pwd with the password.
0246      * Otherwise returns false and pwd will be null.
0247      * @since 4.2
0248      */
0249     bool checkAndGetPassword(QString *pwd);
0250 
0251 Q_SIGNALS:
0252 
0253     /**
0254      * The dialog has been accepted, and the new password is @p password
0255      */
0256     void newPassword(const QString &password);
0257 
0258 private:
0259     std::unique_ptr<class KNewPasswordDialogPrivate> const d;
0260 };
0261 
0262 #endif // KNEWPASSWORDDIALOG_H