25#include <QtGui/QApplication>
26#include <QtGui/QProgressBar>
27#include <QtCore/QRegExp>
28#include <QtCore/QSize>
29#include <QtCore/QString>
39#include "ui_knewpassworddialog.h"
41class KNewPasswordDialog::KNewPasswordDialogPrivate
46 minimumPasswordLength(0), passwordStrengthWarningLevel(1),reasonablePasswordLength(8)
50 void _k_textChanged();
54 int minimumPasswordLength;
55 int passwordStrengthWarningLevel;
56 int reasonablePasswordLength;
58 int effectivePasswordLength(
const QString &
password);
62 Ui::KNewPasswordDialog ui;
66void KNewPasswordDialog::KNewPasswordDialogPrivate::init()
68 q->setButtons( Ok | Cancel );
69 q->setDefaultButton( Ok );
71 ui.setupUi( q->mainWidget() );
73 ui.labelIcon->setPixmap(
KIcon(
"dialog-password").pixmap(96, 96) );
74 ui.labelMatch->setHidden(
true);
76 const QString strengthBarWhatsThis(
i18n(
"The password strength meter gives an indication of the security "
77 "of the password you have entered. To improve the strength of "
78 "the password, try:\n"
79 " - using a longer password;\n"
80 " - using a mixture of upper- and lower-case letters;\n"
81 " - using numbers or symbols, such as #, as well as letters."));
82 ui.labelStrengthMeter->setWhatsThis(strengthBarWhatsThis);
83 ui.strengthBar->setWhatsThis(strengthBarWhatsThis);
85 connect( ui.linePassword, SIGNAL(textChanged(QString)), q, SLOT(_k_textChanged()) );
86 connect( ui.lineVerifyPassword, SIGNAL(textChanged(QString)), q, SLOT(_k_textChanged()) );
92int KNewPasswordDialog::KNewPasswordDialogPrivate::effectivePasswordLength(
const QString &password)
102 Category previousCategory = Vowel;
103 QString vowels(
"aeiou");
106 for (
int i = 0; i < password.length(); ++i) {
107 QChar currentChar = password.at(i);
108 if (!password.left(i).contains(currentChar)) {
109 Category currentCategory;
110 switch (currentChar.category()) {
111 case QChar::Letter_Uppercase:
112 currentCategory = Upper;
114 case QChar::Letter_Lowercase:
115 if (vowels.contains(currentChar)) {
116 currentCategory = Vowel;
118 currentCategory = Consonant;
121 case QChar::Number_DecimalDigit:
122 currentCategory = Digit;
125 currentCategory = Special;
128 switch (currentCategory) {
130 if (previousCategory != Consonant) {
135 if (previousCategory != Vowel) {
140 if (previousCategory != currentCategory) {
145 previousCategory = currentCategory;
152void KNewPasswordDialog::KNewPasswordDialogPrivate::_k_textChanged()
154 const bool match = ui.linePassword->text() == ui.lineVerifyPassword->text();
156 const int minPasswordLength = q->minimumPasswordLength();
158 if ( ui.linePassword->text().length() < minPasswordLength) {
159 q->enableButtonOk(
false);
161 q->enableButtonOk( match );
164 if ( match && !q->allowEmptyPasswords() && ui.linePassword->text().isEmpty()) {
165 ui.labelMatch->setPixmap(
KIcon(
"dialog-error") );
166 ui.labelMatch->setText(
i18n(
"Password is empty") );
169 if ( ui.linePassword->text().length() < minPasswordLength ) {
170 ui.labelMatch->setPixmap(
KIcon(
"dialog-error") );
171 ui.labelMatch->setText(
i18np(
"Password must be at least 1 character long",
"Password must be at least %1 characters long", minPasswordLength));
173 ui.labelMatch->setPixmap( match ?
KIcon(
"dialog-ok") :
KIcon(
"dialog-error") );
175 ui.labelMatch->setText( match?
i18n(
"Passwords match")
176 :
i18n(
"Passwords do not match") );
181 int pwstrength = (20 * ui.linePassword->text().length() + 80 * effectivePasswordLength(ui.linePassword->text())) / qMax(reasonablePasswordLength, 2);
182 if (pwstrength < 0) {
184 }
else if (pwstrength > 100) {
187 ui.strengthBar->setValue(pwstrength);
195 :
KDialog(parent), d(new KNewPasswordDialogPrivate(this))
209 d->ui.labelPrompt->setText(prompt);
215 return d->ui.labelPrompt->text();
221 d->ui.labelIcon->setPixmap(pixmap);
222 d->ui.labelIcon->setFixedSize( d->ui.labelIcon->sizeHint() );
228 return *d->ui.labelIcon->pixmap();
234 if ( d->ui.linePassword->text() != d->ui.lineVerifyPassword->text() ) {
236 d->ui.labelMatch->setText(
i18n(
"You entered two different "
237 "passwords. Please try again.") );
239 d->ui.linePassword->clear();
240 d->ui.lineVerifyPassword->clear();
243 if (d->ui.strengthBar && d->ui.strengthBar->value() < d->passwordStrengthWarningLevel) {
245 i18n(
"The password you have entered has a low strength. "
246 "To improve the strength of "
247 "the password, try:\n"
248 " - using a longer password;\n"
249 " - using a mixture of upper- and lower-case letters;\n"
250 " - using numbers or symbols as well as letters.\n"
252 "Would you like to use this password anyway?"),
253 i18n(
"Low Password Strength"));
260 *pwd = d->ui.linePassword->text();
285 return d->minimumPasswordLength == 0;
290 d->minimumPasswordLength = minLength;
296 return d->minimumPasswordLength;
301 d->ui.linePassword->setMaxLength(maxLength);
302 d->ui.lineVerifyPassword->setMaxLength(maxLength);
307 return d->ui.linePassword->maxLength();
315 if (reasonableLength < 1) {
316 reasonableLength = 1;
322 d->reasonablePasswordLength = reasonableLength;
328 return d->reasonablePasswordLength;
334 if (warningLevel < 0) {
337 if (warningLevel > 99) {
340 d->passwordStrengthWarningLevel = warningLevel;
345 return d->passwordStrengthWarningLevel;
358#include "knewpassworddialog.moc"
A dialog base class with standard buttons and predefined layouts.
A wrapper around QIcon that provides KDE icon features.
static int warningYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
Display a "warning" 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 setReasonablePasswordLength(int reasonableLength)
Password length that is expected to be reasonably safe.
void setAllowEmptyPasswords(bool allowed)
Allow empty passwords? - Default: true.
void setPixmap(const QPixmap &)
Sets the pixmap that appears next to the prompt in the dialog.
bool allowEmptyPasswords() const
Allow empty passwords?
QPixmap pixmap() const
Returns the pixmap that appears next to the prompt in the dialog.
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.
int maximumPasswordLength() const
Maximum acceptable password length.
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.
KNewPasswordDialog(QWidget *parent=0)
Constructs a password dialog.
int reasonablePasswordLength() const
Password length that is expected to be reasonably safe.
virtual ~KNewPasswordDialog()
Destructs the password dialog.
QString i18n(const char *text)
QString i18np(const char *sing, const char *plur, const A1 &a1)