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