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"
41 class KNewPasswordDialog::KNewPasswordDialogPrivate
50 void _k_textChanged();
62 Ui::KNewPasswordDialog ui;
66 void KNewPasswordDialog::KNewPasswordDialogPrivate::init()
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()) );
92 int KNewPasswordDialog::KNewPasswordDialogPrivate::effectivePasswordLength(
const QString &password)
102 Category previousCategory = Vowel;
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;
152 void 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"