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: 2006,2007 Olivier Goffart <ogoffart @ kde.org> |
8 | |
9 | SPDX-License-Identifier: LGPL-2.0-only |
10 | */ |
11 | #ifndef KNEWPASSWORDDIALOG_H |
12 | #define KNEWPASSWORDDIALOG_H |
13 | |
14 | #include <KPassword> |
15 | #include <QDialog> |
16 | #include <memory> |
17 | |
18 | #include <kwidgetsaddons_export.h> |
19 | |
20 | class QWidget; |
21 | |
22 | /** |
23 | * @class KNewPasswordDialog knewpassworddialog.h KNewPasswordDialog |
24 | * |
25 | * @short A password input dialog. |
26 | * |
27 | * This dialog asks the user to enter a new password. |
28 | * |
29 | * The password has to be entered twice to check if the passwords |
30 | * match. A hint about the strength of the entered password is also |
31 | * shown. |
32 | * |
33 | * \section usage Usage Example |
34 | * \subsection asynchronous Asynchronous |
35 | * |
36 | * \code |
37 | * KNewPasswordDialog *dlg = new KNewPasswordDialog( parent ); |
38 | * dlg->setPrompt(i18n("Enter a password")); |
39 | * connect(dlg, &KNewPasswordDialog::newPassword, this, [this](const QString &pass) { setPassword(pass); }); |
40 | * connect(dlg, &QDialog::rejected, this, [this]() { slotCancel(); }); |
41 | * dlg->show(); |
42 | * \endcode |
43 | * |
44 | * \subsection synchronous Synchronous |
45 | * |
46 | * \code |
47 | * KNewPasswordDialog dlg(parent); |
48 | * dlg.setPrompt(i18n("Enter a password")); |
49 | * if(dlg.exec()) { |
50 | * setPassword(dlg.password()); |
51 | * } |
52 | * \endcode |
53 | * |
54 | * \image html knewpassworddialog.png "KNewPasswordDialog" |
55 | * |
56 | * @author Geert Jansen <jansen@kde.org> |
57 | * @author Olivier Goffart <ogoffart@kde.org> |
58 | */ |
59 | class KWIDGETSADDONS_EXPORT KNewPasswordDialog : public QDialog |
60 | { |
61 | Q_OBJECT |
62 | |
63 | /** |
64 | * @since 6.0 |
65 | */ |
66 | Q_PROPERTY(KPassword::RevealMode revealPasswordMode READ revealPasswordMode WRITE setRevealPasswordMode) |
67 | |
68 | public: |
69 | /** |
70 | * Constructs a password dialog. |
71 | * |
72 | * @param parent Passed to lower level constructor. |
73 | */ |
74 | explicit KNewPasswordDialog(QWidget *parent = nullptr); |
75 | |
76 | /** |
77 | * Destructs the password dialog. |
78 | */ |
79 | ~KNewPasswordDialog() override; |
80 | |
81 | /** |
82 | * Sets the password prompt. |
83 | */ |
84 | void setPrompt(const QString &prompt); |
85 | |
86 | /** |
87 | * Returns the password prompt. |
88 | */ |
89 | QString prompt() const; |
90 | |
91 | /** |
92 | * Sets the icon that appears next to the prompt in the dialog. The default icon represents a simple key. |
93 | * @since 5.63 |
94 | */ |
95 | void setIcon(const QIcon &icon); |
96 | |
97 | /** |
98 | * Returns the icon that appears next to the prompt in the dialog. The default icon represents a simple key. |
99 | * @since 5.63 |
100 | */ |
101 | QIcon icon() const; |
102 | |
103 | /** |
104 | * Allow empty passwords? - Default: true |
105 | * |
106 | * same as setMinimumPasswordLength( allowed ? 0 : 1 ) |
107 | */ |
108 | void setAllowEmptyPasswords(bool allowed); |
109 | |
110 | /** |
111 | * Allow empty passwords? |
112 | * |
113 | * @return true if minimumPasswordLength() == 0 |
114 | */ |
115 | bool allowEmptyPasswords() const; |
116 | |
117 | /** |
118 | * Minimum acceptable password length. |
119 | * |
120 | * Default: 0 |
121 | * |
122 | * @param minLength The new minimum password length |
123 | */ |
124 | void setMinimumPasswordLength(int minLength); |
125 | |
126 | /** |
127 | * Minimum acceptable password length. |
128 | */ |
129 | int minimumPasswordLength() const; |
130 | |
131 | /** |
132 | * Maximum acceptable password length. |
133 | * |
134 | * @param maxLength The new maximum password length. |
135 | */ |
136 | void setMaximumPasswordLength(int maxLength); |
137 | |
138 | /** |
139 | * Maximum acceptable password length. |
140 | */ |
141 | int maximumPasswordLength() const; |
142 | |
143 | /** |
144 | * Password length that is expected to be reasonably safe. |
145 | * |
146 | * Used to compute the strength level |
147 | * |
148 | * Default: 8 - the standard UNIX password length |
149 | * |
150 | * @param reasonableLength The new reasonable password length. |
151 | */ |
152 | void setReasonablePasswordLength(int reasonableLength); |
153 | |
154 | /** |
155 | * Password length that is expected to be reasonably safe. |
156 | */ |
157 | int reasonablePasswordLength() const; |
158 | |
159 | /** |
160 | * Set the password strength level below which a warning is given |
161 | * Value is in the range 0 to 99. Empty passwords score 0; |
162 | * non-empty passwords score up to 100, depending on their length and whether they |
163 | * contain numbers, mixed case letters and punctuation. |
164 | * |
165 | * Default: 1 - warn if the password has no discernible strength whatsoever |
166 | * @param warningLevel The level below which a warning should be given. |
167 | */ |
168 | void setPasswordStrengthWarningLevel(int warningLevel); |
169 | |
170 | /** |
171 | * Password strength level below which a warning is given |
172 | */ |
173 | int passwordStrengthWarningLevel() const; |
174 | |
175 | /** |
176 | * When the verification password does not match, the background color |
177 | * of the verification field is set to @p color. As soon as the passwords match, |
178 | * the original color of the verification field is restored. |
179 | * |
180 | * Default: the background color from the current theme. |
181 | * @since 5.17 |
182 | */ |
183 | void setBackgroundWarningColor(const QColor &color); |
184 | |
185 | /** |
186 | * The color used as warning for the verification password field's background. |
187 | * @since 5.17 |
188 | */ |
189 | QColor backgroundWarningColor() const; |
190 | |
191 | /** |
192 | * Returns the password entered. |
193 | * @note Only has meaningful data after accept has been called |
194 | * if you want to access the password from a subclass use |
195 | * checkAndGetPassword() |
196 | */ |
197 | QString password() const; |
198 | |
199 | #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(6, 0) |
200 | /** |
201 | * Whether to show the visibility trailing action in the line edit. |
202 | * Default is true. This can be used to honor the lineedit_reveal_password |
203 | * kiosk key, for example: |
204 | * \code |
205 | * passwordDialog.setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password"))); |
206 | * \endcode |
207 | * @since 5.31 |
208 | */ |
209 | [[deprecated("Use setRevealPasswordMode instead." )]] void setRevealPasswordAvailable(bool reveal); |
210 | |
211 | /** |
212 | * Whether the visibility trailing action in the line edit is visible. |
213 | * @since 5.31 |
214 | */ |
215 | [[deprecated("Use revealPasswordMode instead." )]] bool isRevealPasswordAvailable() const; |
216 | #endif |
217 | |
218 | /** |
219 | * Whether the visibility trailing action in the line edit is visible. |
220 | * @since 6.0 |
221 | */ |
222 | KPassword::RevealMode revealPasswordMode() const; |
223 | |
224 | /** |
225 | * Set when the reveal password button will be visible. |
226 | * |
227 | * The default is RevealPasswordMode::OnlyNew and the reveal password button will |
228 | * only be visible when entering a new password. |
229 | * |
230 | * This can be used to honor the lineedit_reveal_password kiosk key, for example: |
231 | * |
232 | * @code{.cpp} |
233 | * if (KAuthorized::authorize(QStringLiteral("lineedit_reveal_password"))) { |
234 | * newPasswordDialog.setRevealPasswordMode(KPassword::RevealMode::OnlyNew); |
235 | * } else { |
236 | * newPasswordDialog.setRevealPasswordMode(KPassword::RevealMode::Never); |
237 | * } |
238 | * @endcode |
239 | * @since 6.0 |
240 | */ |
241 | void setRevealPasswordMode(KPassword::RevealMode revealPasswordMode); |
242 | |
243 | /** |
244 | * @internal |
245 | */ |
246 | void accept() override; |
247 | |
248 | protected: |
249 | /** |
250 | * Virtual function that can be overridden to provide password |
251 | * checking in derived classes. It should return @p true if the |
252 | * password is valid, @p false otherwise. |
253 | */ |
254 | virtual bool checkPassword(const QString &); |
255 | |
256 | /** |
257 | * Checks input password. |
258 | * If the password is right, returns true |
259 | * and fills pwd with the password. |
260 | * Otherwise returns false and pwd will be null. |
261 | * @since 4.2 |
262 | */ |
263 | bool checkAndGetPassword(QString *pwd); |
264 | |
265 | Q_SIGNALS: |
266 | |
267 | /** |
268 | * The dialog has been accepted, and the new password is @p password |
269 | */ |
270 | void newPassword(const QString &password); |
271 | |
272 | private: |
273 | std::unique_ptr<class KNewPasswordDialogPrivate> const d; |
274 | }; |
275 | |
276 | #endif // KNEWPASSWORDDIALOG_H |
277 | |