KDEUI
knewpassworddialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "knewpassworddialog.h"
00023
00024 #include <QtGui/QApplication>
00025 #include <QtGui/QProgressBar>
00026 #include <QtCore/QRegExp>
00027 #include <QtCore/QSize>
00028 #include <QtCore/QString>
00029
00030 #include <kapplication.h>
00031 #include <kglobal.h>
00032 #include <kicon.h>
00033 #include <klocale.h>
00034 #include <kmessagebox.h>
00035 #include <klineedit.h>
00036 #include <ktitlewidget.h>
00037
00038 #include "ui_knewpassworddialog.h"
00039
00040 class KNewPasswordDialog::KNewPasswordDialogPrivate
00041 {
00042 public:
00043 KNewPasswordDialogPrivate( KNewPasswordDialog *parent )
00044 : q( parent ),
00045 minimumPasswordLength(0), passwordStrengthWarningLevel(1),reasonablePasswordLength(8)
00046 {}
00047
00048 void init();
00049 void _k_textChanged();
00050
00051 KNewPasswordDialog *q;
00052
00053 int minimumPasswordLength;
00054 int passwordStrengthWarningLevel;
00055 int reasonablePasswordLength;
00056
00057 QString pass;
00058
00059 Ui::KNewPasswordDialog ui;
00060 };
00061
00062
00063 void KNewPasswordDialog::KNewPasswordDialogPrivate::init()
00064 {
00065 q->setButtons( Ok | Cancel );
00066 q->showButtonSeparator( true );
00067 q->setDefaultButton( Ok );
00068
00069 ui.setupUi( q->mainWidget() );
00070
00071 ui.labelIcon->setPixmap( KIcon("dialog-password").pixmap(96, 96) );
00072 ui.labelMatch->setHidden(true);
00073
00074 const QString strengthBarWhatsThis(i18n("The password strength meter gives an indication of the security "
00075 "of the password you have entered. To improve the strength of "
00076 "the password, try:\n"
00077 " - using a longer password;\n"
00078 " - using a mixture of upper- and lower-case letters;\n"
00079 " - using numbers or symbols, such as #, as well as letters."));
00080 ui.labelStrengthMeter->setWhatsThis(strengthBarWhatsThis);
00081 ui.strengthBar->setWhatsThis(strengthBarWhatsThis);
00082
00083 connect( ui.linePassword, SIGNAL(textChanged(const QString&)), q, SLOT(_k_textChanged()) );
00084 connect( ui.lineVerifyPassword, SIGNAL(textChanged(const QString&)), q, SLOT(_k_textChanged()) );
00085
00086 _k_textChanged();
00087 }
00088
00089
00090 void KNewPasswordDialog::KNewPasswordDialogPrivate::_k_textChanged()
00091 {
00092 const bool match = ui.linePassword->text() == ui.lineVerifyPassword->text();
00093
00094 const int minPasswordLength = q->minimumPasswordLength();
00095
00096 if ( ui.linePassword->text().length() < minPasswordLength) {
00097 q->enableButtonOk(false);
00098 } else {
00099 q->enableButtonOk( match );
00100 }
00101
00102 if ( match && !q->allowEmptyPasswords() && ui.linePassword->text().isEmpty()) {
00103 ui.labelMatch->setPixmap( KIcon("dialog-error") );
00104 ui.labelMatch->setText( i18n("Password is empty") );
00105 }
00106 else {
00107 if ( ui.linePassword->text().length() < minPasswordLength ) {
00108 ui.labelMatch->setPixmap( KIcon("dialog-error") );
00109 ui.labelMatch->setText(i18np("Password must be at least 1 character long", "Password must be at least %1 characters long", minPasswordLength));
00110 } else {
00111 ui.labelMatch->setPixmap( match ? KIcon("dialog-ok") : KIcon("dialog-error") );
00112
00113 ui.labelMatch->setText( match? i18n("Passwords match")
00114 :i18n("Passwords do not match") );
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124 const double lengthFactor = reasonablePasswordLength / 8.0;
00125
00126 int pwlength = (int) ( ui.linePassword->text().length()/ lengthFactor);
00127 if (pwlength > 5) {
00128 pwlength = 5;
00129 }
00130
00131 const QRegExp numRxp("[0-9]", Qt::CaseSensitive, QRegExp::RegExp);
00132 int numeric = (int) (ui.linePassword->text().count(numRxp) / lengthFactor);
00133 if (numeric > 3) {
00134 numeric = 3;
00135 }
00136
00137 const QRegExp symbRxp("\\W", Qt::CaseInsensitive, QRegExp::RegExp);
00138 int numsymbols = (int) (ui.linePassword->text().count(symbRxp) / lengthFactor);
00139 if (numsymbols > 3) {
00140 numsymbols = 3;
00141 }
00142
00143 const QRegExp upperRxp("[A-Z]", Qt::CaseSensitive, QRegExp::RegExp);
00144 int upper = (int) (ui.linePassword->text().count(upperRxp) / lengthFactor);
00145 if (upper > 3) {
00146 upper = 3;
00147 }
00148
00149 int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
00150
00151 if ( pwstrength < 0 ) {
00152 pwstrength = 0;
00153 }
00154
00155 if ( pwstrength > 100 ) {
00156 pwstrength = 100;
00157 }
00158 ui.strengthBar->setValue(pwstrength);
00159 }
00160
00161
00162
00163
00164
00165 KNewPasswordDialog::KNewPasswordDialog( QWidget *parent)
00166 : KDialog(parent), d(new KNewPasswordDialogPrivate(this))
00167 {
00168 d->init();
00169 }
00170
00171
00172 KNewPasswordDialog::~KNewPasswordDialog()
00173 {
00174 delete d;
00175 }
00176
00177
00178 void KNewPasswordDialog::setPrompt(const QString &prompt)
00179 {
00180 d->ui.labelPrompt->setText(prompt);
00181 }
00182
00183
00184 QString KNewPasswordDialog::prompt() const
00185 {
00186 return d->ui.labelPrompt->text();
00187 }
00188
00189
00190 void KNewPasswordDialog::setPixmap(const QPixmap &pixmap)
00191 {
00192 d->ui.labelIcon->setPixmap(pixmap);
00193 d->ui.labelIcon->setFixedSize( d->ui.labelIcon->sizeHint() );
00194 }
00195
00196
00197 QPixmap KNewPasswordDialog::pixmap() const
00198 {
00199 return *d->ui.labelIcon->pixmap();
00200 }
00201
00202
00203 void KNewPasswordDialog::accept()
00204 {
00205 if ( d->ui.linePassword->text() != d->ui.lineVerifyPassword->text() ) {
00206 d->ui.labelMatch->setPixmap( KTitleWidget::ErrorMessage );
00207 d->ui.labelMatch->setText( i18n("You entered two different "
00208 "passwords. Please try again.") );
00209
00210 d->ui.linePassword->clear();
00211 d->ui.lineVerifyPassword->clear();
00212 return;
00213 }
00214 if (d->ui.strengthBar && d->ui.strengthBar->value() < d->passwordStrengthWarningLevel) {
00215 int retVal = KMessageBox::warningContinueCancel(this,
00216 i18n( "The password you have entered has a low strength. "
00217 "To improve the strength of "
00218 "the password, try:\n"
00219 " - using a longer password;\n"
00220 " - using a mixture of upper- and lower-case letters;\n"
00221 " - using numbers or symbols as well as letters.\n"
00222 "\n"
00223 "Would you like to use this password anyway?"),
00224 i18n("Low Password Strength"));
00225 if (retVal == KMessageBox::Cancel) return;
00226 }
00227 if ( !checkPassword(d->ui.linePassword->text()) ) {
00228 return;
00229 }
00230 d->pass = d->ui.linePassword->text();
00231 emit newPassword( d->pass );
00232 KDialog::accept();
00233 }
00234
00235
00236 void KNewPasswordDialog::setAllowEmptyPasswords(bool allowed)
00237 {
00238 setMinimumPasswordLength( allowed ? 0 : 1 );
00239 d->_k_textChanged();
00240 }
00241
00242
00243 bool KNewPasswordDialog::allowEmptyPasswords() const
00244 {
00245 return d->minimumPasswordLength == 0;
00246 }
00247
00248 void KNewPasswordDialog::setMinimumPasswordLength(int minLength)
00249 {
00250 d->minimumPasswordLength = minLength;
00251 d->_k_textChanged();
00252 }
00253
00254 int KNewPasswordDialog::minimumPasswordLength() const
00255 {
00256 return d->minimumPasswordLength;
00257 }
00258
00259 void KNewPasswordDialog::setMaximumPasswordLength(int maxLength)
00260 {
00261 d->ui.linePassword->setMaxLength(maxLength);
00262 d->ui.lineVerifyPassword->setMaxLength(maxLength);
00263 }
00264
00265 int KNewPasswordDialog::maximumPasswordLength() const
00266 {
00267 return d->ui.linePassword->maxLength();
00268 }
00269
00270
00271
00272 void KNewPasswordDialog::setReasonablePasswordLength(int reasonableLength)
00273 {
00274
00275 if (reasonableLength < 1) {
00276 reasonableLength = 1;
00277 }
00278 if (reasonableLength >= maximumPasswordLength()) {
00279 reasonableLength = maximumPasswordLength();
00280 }
00281
00282 d->reasonablePasswordLength = reasonableLength;
00283
00284 }
00285
00286 int KNewPasswordDialog::reasonablePasswordLength() const
00287 {
00288 return d->reasonablePasswordLength;
00289 }
00290
00291
00292 void KNewPasswordDialog::setPasswordStrengthWarningLevel(int warningLevel)
00293 {
00294 if (warningLevel < 0) {
00295 warningLevel = 0;
00296 }
00297 if (warningLevel > 99) {
00298 warningLevel = 99;
00299 }
00300 d->passwordStrengthWarningLevel = warningLevel;
00301 }
00302
00303 int KNewPasswordDialog::passwordStrengthWarningLevel() const
00304 {
00305 return d->passwordStrengthWarningLevel;
00306 }
00307
00308 QString KNewPasswordDialog::password() const
00309 {
00310 return d->pass;
00311 }
00312
00313 bool KNewPasswordDialog::checkPassword(const QString &)
00314 {
00315 return true;
00316 }
00317
00318 #include "knewpassworddialog.moc"
00319
00320