kpassdlg.cpp

00001 // vi: ts=8 sts=4 sw=4
00002 /* This file is part of the KDE libraries
00003    Copyright (C) 1998 Pietro Iglio <iglio@fub.it>
00004    Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
00005    Copyright (C) 2004,2005 Andrew Coles <andrew_coles@yahoo.co.uk>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License version 2 as published by the Free Software Foundation.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 #include <unistd.h>
00022 
00023 #include <qwidget.h>
00024 #include <qlineedit.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qsize.h>
00028 #include <qevent.h>
00029 #include <qkeycode.h>
00030 #include <qcheckbox.h>
00031 #include <qregexp.h>
00032 #include <qhbox.h>
00033 #include <qwhatsthis.h>
00034 #include <qptrdict.h>
00035 
00036 #include <kglobal.h>
00037 #include <kdebug.h>
00038 #include <kapplication.h>
00039 #include <klocale.h>
00040 #include <kiconloader.h>
00041 #include <kmessagebox.h>
00042 #include <kaboutdialog.h>
00043 #include <kconfig.h>
00044 #include <kstandarddirs.h>
00045 #include <kprogress.h>
00046 
00047 #include <sys/time.h>
00048 #include <sys/resource.h>
00049 
00050 #include "kpassdlg.h"
00051 
00052 /*
00053  * Password line editor.
00054  */
00055 
00056 // BCI: Add a real d-pointer and put the int into that
00057 
00058 static QPtrDict<int>* d_ptr = 0;
00059 
00060 static void cleanup_d_ptr() {
00061     delete d_ptr;
00062 }
00063 
00064 static int * ourMaxLength( const KPasswordEdit* const e ) {
00065     if ( !d_ptr ) {
00066         d_ptr = new QPtrDict<int>;
00067         d_ptr->setAutoDelete(true);
00068         qAddPostRoutine( cleanup_d_ptr );
00069     }
00070     int* ret = d_ptr->find( (void*) e );
00071     if ( ! ret ) {
00072         ret = new int;
00073         d_ptr->replace( (void*) e, ret );
00074     }
00075     return ret;
00076 }
00077 
00078 static void delete_d( const KPasswordEdit* const e ) {
00079     if ( d_ptr )
00080         d_ptr->remove( (void*) e );
00081 }
00082 
00083 const int KPasswordEdit::PassLen = 200;
00084 
00085 class KPasswordDialog::KPasswordDialogPrivate
00086 {
00087     public:
00088     KPasswordDialogPrivate()
00089      : m_MatchLabel( 0 ), iconName( 0 ), allowEmptyPasswords( false ),
00090        minimumPasswordLength(0), maximumPasswordLength(KPasswordEdit::PassLen - 1),
00091        passwordStrengthWarningLevel(1), m_strengthBar(0),
00092        reasonablePasswordLength(8)
00093         {}
00094     QLabel *m_MatchLabel;
00095     QString iconName;
00096     bool allowEmptyPasswords;
00097     int minimumPasswordLength;
00098     int maximumPasswordLength;
00099     int passwordStrengthWarningLevel;
00100     KProgress* m_strengthBar;
00101     int reasonablePasswordLength;
00102 };
00103 
00104 
00105 KPasswordEdit::KPasswordEdit(QWidget *parent, const char *name)
00106     : QLineEdit(parent, name)
00107 {
00108     init();
00109 
00110     KConfig* const cfg = KGlobal::config();
00111     KConfigGroupSaver saver(cfg, "Passwords");
00112 
00113     const QString val = cfg->readEntry("EchoMode", "OneStar");
00114     if (val == "ThreeStars")
00115     m_EchoMode = ThreeStars;
00116     else if (val == "NoEcho")
00117     m_EchoMode = NoEcho;
00118     else
00119     m_EchoMode = OneStar;
00120 
00121 }
00122 
00123 KPasswordEdit::KPasswordEdit(QWidget *parent, const char *name, int echoMode)
00124     : QLineEdit(parent, name), m_EchoMode(echoMode)
00125 {
00126     init();
00127 }
00128 
00129 KPasswordEdit::KPasswordEdit(EchoModes echoMode, QWidget *parent, const char *name)
00130     : QLineEdit(parent, name), m_EchoMode(echoMode)
00131 {
00132     init();
00133 }
00134 
00135 KPasswordEdit::KPasswordEdit(EchoMode echoMode, QWidget *parent, const char *name)
00136     : QLineEdit(parent, name)
00137     , m_EchoMode( echoMode == QLineEdit::NoEcho ? NoEcho : OneStar )
00138 {
00139     init();
00140 }
00141 
00142 void KPasswordEdit::init()
00143 {
00144     setEchoMode(QLineEdit::Password); // Just in case
00145     setAcceptDrops(false);
00146     int* t = ourMaxLength(this);
00147     *t = (PassLen - 1); // the internal max length
00148     m_Password = new char[PassLen];
00149     m_Password[0] = '\000';
00150     m_Length = 0;
00151 }
00152 
00153 KPasswordEdit::~KPasswordEdit()
00154 {
00155     memset(m_Password, 0, PassLen * sizeof(char));
00156     delete[] m_Password;
00157     delete_d(this);
00158 }
00159 
00160 void KPasswordEdit::insert(const QString &txt)
00161 {
00162     const QCString localTxt = txt.local8Bit();
00163     const unsigned int lim = localTxt.length();
00164     const int m_MaxLength = maxPasswordLength();
00165     for(unsigned int i=0; i < lim; ++i)
00166     {
00167         const unsigned char ke = localTxt[i];
00168         if (m_Length < m_MaxLength)
00169         {
00170             m_Password[m_Length] = ke;
00171             m_Password[++m_Length] = '\000';
00172         }
00173     }
00174     showPass();
00175 }
00176 
00177 void KPasswordEdit::erase()
00178 {
00179     m_Length = 0;
00180     memset(m_Password, 0, PassLen * sizeof(char));
00181     setText("");
00182 }
00183 
00184 void KPasswordEdit::focusInEvent(QFocusEvent *e)
00185 {
00186     const QString txt = text();
00187     setUpdatesEnabled(false);
00188     QLineEdit::focusInEvent(e);
00189     setUpdatesEnabled(true);
00190     setText(txt);
00191 }
00192 
00193 
00194 void KPasswordEdit::keyPressEvent(QKeyEvent *e)
00195 {
00196     switch (e->key()) {
00197     case Key_Return:
00198     case Key_Enter:
00199     case Key_Escape:
00200     e->ignore();
00201     break;
00202     case Key_Backspace:
00203     case Key_Delete:
00204     case 0x7f: // Delete
00205     if (e->state() & (ControlButton | AltButton))
00206         e->ignore();
00207     else if (m_Length) {
00208         m_Password[--m_Length] = '\000';
00209         showPass();
00210     }
00211     break;
00212     default:
00213     const unsigned char ke = e->text().local8Bit()[0];
00214     if (ke >= 32) {
00215         insert(e->text());
00216     } else
00217         e->ignore();
00218     break;
00219     }
00220 }
00221 
00222 bool KPasswordEdit::event(QEvent *e) {
00223     switch(e->type()) {
00224 
00225       case QEvent::MouseButtonPress:
00226       case QEvent::MouseButtonRelease:
00227       case QEvent::MouseButtonDblClick:
00228       case QEvent::MouseMove:
00229       case QEvent::IMStart:
00230       case QEvent::IMCompose:
00231         return true; //Ignore
00232 
00233       case QEvent::IMEnd:
00234       {
00235         QIMEvent* const ie = (QIMEvent*) e;
00236         if (!ie->text().isEmpty())
00237           insert( ie->text() );
00238         return true;
00239       }
00240 
00241       case QEvent::AccelOverride:
00242       {
00243         QKeyEvent* const k = (QKeyEvent*) e;
00244         switch (k->key()) {
00245             case Key_U:
00246                 if (k->state() & ControlButton) {
00247                     m_Length = 0;
00248                     m_Password[m_Length] = '\000';
00249                     showPass();
00250                 }
00251         }
00252         return true; // stop bubbling
00253       }
00254 
00255       default:
00256         // Do nothing
00257         break;
00258     }
00259     return QLineEdit::event(e);
00260 }
00261 
00262 void KPasswordEdit::showPass()
00263 {
00264     QString tmp;
00265 
00266     switch (m_EchoMode) {
00267     case OneStar:
00268     tmp.fill('*', m_Length);
00269     setText(tmp);
00270     break;
00271     case ThreeStars:
00272     tmp.fill('*', m_Length*3);
00273     setText(tmp);
00274     break;
00275     case NoEcho: default:
00276     emit textChanged(QString::null); //To update the password comparison if need be.
00277     break;
00278     }
00279 }
00280 
00281 void KPasswordEdit::setMaxPasswordLength(int newLength)
00282 {
00283     if (newLength >= PassLen) newLength = PassLen - 1; // belt and braces
00284     if (newLength < 0) newLength = 0;
00285     int* t = ourMaxLength(this);
00286     *t = newLength; 
00287     while (m_Length > newLength) {
00288         m_Password[m_Length] = '\000';
00289         --m_Length;
00290     }
00291     showPass();
00292 }
00293 
00294 int KPasswordEdit::maxPasswordLength() const
00295 {
00296     return *(ourMaxLength(this));
00297 }
00298 /*
00299  * Password dialog.
00300  */
00301 
00302 KPasswordDialog::KPasswordDialog(Types type, bool enableKeep, int extraBttn,
00303                                  QWidget *parent, const char *name)
00304     : KDialogBase(parent, name, true, "", Ok|Cancel|extraBttn,
00305                   Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), d(new KPasswordDialogPrivate)
00306 {
00307     d->iconName = "password";
00308     init();
00309 }
00310 
00311 KPasswordDialog::KPasswordDialog(Types type, bool enableKeep, int extraBttn, const QString& icon,
00312                   QWidget *parent, const char *name )
00313     : KDialogBase(parent, name, true, "", Ok|Cancel|extraBttn,
00314                   Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), d(new KPasswordDialogPrivate)
00315 {
00316     if ( icon.stripWhiteSpace().isEmpty() )
00317     d->iconName = "password";
00318     else
00319     d->iconName = icon;
00320     init();
00321 }
00322 
00323 KPasswordDialog::KPasswordDialog(int type, QString prompt, bool enableKeep,
00324                                  int extraBttn)
00325     : KDialogBase(0L, "Password Dialog", true, "", Ok|Cancel|extraBttn,
00326                   Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), d(new KPasswordDialogPrivate)
00327 {
00328     d->iconName = "password";
00329     init();
00330     setPrompt(prompt);
00331 }
00332 
00333 void KPasswordDialog::init()
00334 {
00335     m_Row = 0;
00336 
00337     KConfig* const cfg = KGlobal::config();
00338     const KConfigGroupSaver saver(cfg, "Passwords");
00339     if (m_Keep && cfg->readBoolEntry("Keep", false))
00340     ++m_Keep;
00341 
00342     m_pMain = new QWidget(this);
00343     setMainWidget(m_pMain);
00344     m_pGrid = new QGridLayout(m_pMain, 10, 3, 0, 0);
00345     m_pGrid->addColSpacing(1, 10);
00346 
00347     // Row 1: pixmap + prompt
00348     QLabel *lbl;
00349     const QPixmap pix( KGlobal::iconLoader()->loadIcon( d->iconName, KIcon::NoGroup, KIcon::SizeHuge, 0, 0, true));
00350     if (!pix.isNull()) {
00351     lbl = new QLabel(m_pMain);
00352     lbl->setPixmap(pix);
00353     lbl->setAlignment(AlignHCenter|AlignVCenter);
00354     lbl->setFixedSize(lbl->sizeHint());
00355     m_pGrid->addWidget(lbl, 0, 0, AlignCenter);
00356     }
00357 
00358     m_pHelpLbl = new QLabel(m_pMain);
00359     m_pHelpLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
00360     m_pGrid->addWidget(m_pHelpLbl, 0, 2, AlignLeft);
00361     m_pGrid->addRowSpacing(1, 10);
00362     m_pGrid->setRowStretch(1, 12);
00363 
00364     // Row 2+: space for 4 extra info lines
00365     m_pGrid->addRowSpacing(6, 5);
00366     m_pGrid->setRowStretch(6, 12);
00367 
00368     // Row 3: Password editor #1
00369     lbl = new QLabel(m_pMain);
00370     lbl->setAlignment(AlignLeft|AlignVCenter);
00371     lbl->setText(i18n("&Password:"));
00372     lbl->setFixedSize(lbl->sizeHint());
00373     m_pGrid->addWidget(lbl, 7, 0, AlignLeft);
00374 
00375     QHBoxLayout *h_lay = new QHBoxLayout();
00376     m_pGrid->addLayout(h_lay, 7, 2);
00377     m_pEdit = new KPasswordEdit(m_pMain);
00378     m_pEdit2 = 0;
00379     lbl->setBuddy(m_pEdit);
00380     QSize size = m_pEdit->sizeHint();
00381     m_pEdit->setFixedHeight(size.height());
00382     m_pEdit->setMinimumWidth(size.width());
00383     h_lay->addWidget(m_pEdit);
00384 
00385     // Row 4: Password editor #2 or keep password checkbox
00386 
00387     if ((m_Type == Password) && m_Keep) {
00388     m_pGrid->addRowSpacing(8, 10);
00389     m_pGrid->setRowStretch(8, 12);
00390     QCheckBox* const cb = new QCheckBox(i18n("&Keep password"), m_pMain);
00391     cb->setFixedSize(cb->sizeHint());
00392     if (m_Keep > 1)
00393         cb->setChecked(true);
00394     else
00395         m_Keep = 0;
00396     connect(cb, SIGNAL(toggled(bool)), SLOT(slotKeep(bool)));
00397     m_pGrid->addWidget(cb, 9, 2, AlignLeft|AlignVCenter);
00398     } else if (m_Type == NewPassword) {
00399     m_pGrid->addRowSpacing(8, 10);
00400     lbl = new QLabel(m_pMain);
00401     lbl->setAlignment(AlignLeft|AlignVCenter);
00402     lbl->setText(i18n("&Verify:"));
00403     lbl->setFixedSize(lbl->sizeHint());
00404     m_pGrid->addWidget(lbl, 9, 0, AlignLeft);
00405 
00406     h_lay = new QHBoxLayout();
00407     m_pGrid->addLayout(h_lay, 9, 2);
00408     m_pEdit2 = new KPasswordEdit(m_pMain);
00409     lbl->setBuddy(m_pEdit2);
00410     size = m_pEdit2->sizeHint();
00411     m_pEdit2->setFixedHeight(size.height());
00412     m_pEdit2->setMinimumWidth(size.width());
00413     h_lay->addWidget(m_pEdit2);
00414 
00415         // Row 6: Password strength meter
00416         m_pGrid->addRowSpacing(10, 10);
00417         m_pGrid->setRowStretch(10, 12);
00418 
00419         QHBox* const strengthBox = new QHBox(m_pMain);
00420         strengthBox->setSpacing(10);
00421         m_pGrid->addMultiCellWidget(strengthBox, 11, 11, 0, 2);
00422         QLabel* const passStrengthLabel = new QLabel(strengthBox);
00423         passStrengthLabel->setAlignment(AlignLeft|AlignVCenter);
00424         passStrengthLabel->setText(i18n("Password strength meter:"));
00425         d->m_strengthBar = new KProgress(100, strengthBox, "PasswordStrengthMeter");
00426         d->m_strengthBar->setPercentageVisible(false);
00427 
00428         const QString strengthBarWhatsThis(i18n("The password strength meter gives an indication of the security "
00429                                                 "of the password you have entered.  To improve the strength of "
00430                                                 "the password, try:\n"
00431                                                 " - using a longer password;\n"
00432                                                 " - using a mixture of upper- and lower-case letters;\n"
00433                                                 " - using numbers or symbols, such as #, as well as letters."));
00434         QWhatsThis::add(passStrengthLabel, strengthBarWhatsThis);
00435         QWhatsThis::add(d->m_strengthBar, strengthBarWhatsThis);
00436 
00437         // Row 6: Label saying whether the passwords match
00438         m_pGrid->addRowSpacing(12, 10);
00439         m_pGrid->setRowStretch(12, 12);
00440 
00441         d->m_MatchLabel = new QLabel(m_pMain);
00442         d->m_MatchLabel->setAlignment(AlignLeft|AlignVCenter|WordBreak);
00443         m_pGrid->addMultiCellWidget(d->m_MatchLabel, 13, 13, 0, 2);
00444         d->m_MatchLabel->setText(i18n("Passwords do not match"));
00445 
00446 
00447         connect( m_pEdit, SIGNAL(textChanged(const QString&)), SLOT(enableOkBtn()) );
00448         connect( m_pEdit2, SIGNAL(textChanged(const QString&)), SLOT(enableOkBtn()) );
00449         enableOkBtn();
00450     }
00451 
00452     erase();
00453 }
00454 
00455 
00456 KPasswordDialog::~KPasswordDialog()
00457 {
00458     delete d;
00459 }
00460 
00461 
00462 void KPasswordDialog::clearPassword()
00463 {
00464     m_pEdit->erase();
00465 }
00466 
00467 /* KDE 4: Make it const QString & */
00468 void KPasswordDialog::setPrompt(QString prompt)
00469 {
00470     m_pHelpLbl->setText(prompt);
00471     m_pHelpLbl->setFixedSize(275, m_pHelpLbl->heightForWidth(275));
00472 }
00473 
00474 
00475 QString KPasswordDialog::prompt() const
00476 
00477 {
00478     return m_pHelpLbl->text();
00479 }
00480 
00481 
00482 /* KDE 4: Make them const QString & */
00483 void KPasswordDialog::addLine(QString key, QString value)
00484 {
00485     if (m_Row > 3)
00486     return;
00487 
00488     QLabel *lbl = new QLabel(key, m_pMain);
00489     lbl->setAlignment(AlignLeft|AlignTop);
00490     lbl->setFixedSize(lbl->sizeHint());
00491     m_pGrid->addWidget(lbl, m_Row+2, 0, AlignLeft);
00492 
00493     lbl = new QLabel(value, m_pMain);
00494     lbl->setAlignment(AlignTop|WordBreak);
00495     lbl->setFixedSize(275, lbl->heightForWidth(275));
00496     m_pGrid->addWidget(lbl, m_Row+2, 2, AlignLeft);
00497     ++m_Row;
00498 }
00499 
00500 
00501 void KPasswordDialog::erase()
00502 {
00503     m_pEdit->erase();
00504     m_pEdit->setFocus();
00505     if (m_Type == NewPassword)
00506     m_pEdit2->erase();
00507 }
00508 
00509 
00510 void KPasswordDialog::slotOk()
00511 {
00512     if (m_Type == NewPassword) {
00513     if (strcmp(m_pEdit->password(), m_pEdit2->password())) {
00514         KMessageBox::sorry(this, i18n("You entered two different "
00515             "passwords. Please try again."));
00516         erase();
00517         return;
00518     }
00519     if (d->m_strengthBar && d->m_strengthBar->progress() < d->passwordStrengthWarningLevel) {
00520         int retVal = KMessageBox::warningContinueCancel(this,
00521         i18n(   "The password you have entered has a low strength. "
00522             "To improve the strength of "
00523             "the password, try:\n"
00524             " - using a longer password;\n"
00525             " - using a mixture of upper- and lower-case letters;\n"
00526             " - using numbers or symbols as well as letters.\n"
00527             "\n"
00528             "Would you like to use this password anyway?"),
00529         i18n("Low Password Strength"));
00530         if (retVal == KMessageBox::Cancel) return;
00531     }
00532     }
00533     if (!checkPassword(m_pEdit->password())) {
00534     erase();
00535     return;
00536     }
00537     accept();
00538 }
00539 
00540 
00541 void KPasswordDialog::slotCancel()
00542 {
00543     reject();
00544 }
00545 
00546 
00547 void KPasswordDialog::slotKeep(bool keep)
00548 {
00549     m_Keep = keep;
00550 }
00551 
00552 
00553 // static . antlarr: KDE 4: Make it const QString & prompt
00554 int KPasswordDialog::getPassword(QCString &password, QString prompt,
00555     int *keep)
00556 {
00557     const bool enableKeep = (keep && *keep);
00558     KPasswordDialog* const dlg = new KPasswordDialog(int(Password), prompt, enableKeep);
00559     const int ret = dlg->exec();
00560     if (ret == Accepted) {
00561     password = dlg->password();
00562     if (enableKeep)
00563         *keep = dlg->keep();
00564     }
00565     delete dlg;
00566     return ret;
00567 }
00568 
00569 
00570 // static . antlarr: KDE 4: Make it const QString & prompt
00571 int KPasswordDialog::getNewPassword(QCString &password, QString prompt)
00572 {
00573     KPasswordDialog* const dlg = new KPasswordDialog(NewPassword, prompt);
00574     const int ret = dlg->exec();
00575     if (ret == Accepted)
00576     password = dlg->password();
00577     delete dlg;
00578     return ret;
00579 }
00580 
00581 
00582 // static
00583 void KPasswordDialog::disableCoreDumps()
00584 {
00585     struct rlimit rlim;
00586     rlim.rlim_cur = rlim.rlim_max = 0;
00587     setrlimit(RLIMIT_CORE, &rlim);
00588 }
00589 
00590 void KPasswordDialog::virtual_hook( int id, void* data )
00591 { KDialogBase::virtual_hook( id, data ); }
00592 
00593 void KPasswordDialog::enableOkBtn()
00594 {
00595     if (m_Type == NewPassword) {
00596       const bool match = strcmp(m_pEdit->password(), m_pEdit2->password()) == 0
00597                    && (d->allowEmptyPasswords || m_pEdit->password()[0]);
00598 
00599       const QString pass(m_pEdit->password());
00600 
00601       const int minPasswordLength = minimumPasswordLength();
00602 
00603       if ((int) pass.length() < minPasswordLength) {
00604           enableButtonOK(false);
00605       } else {
00606           enableButtonOK( match );
00607       }
00608 
00609       if ( match && d->allowEmptyPasswords && m_pEdit->password()[0] == 0 ) {
00610           d->m_MatchLabel->setText( i18n("Password is empty") );
00611       } else {
00612           if ((int) pass.length() < minPasswordLength) {
00613               d->m_MatchLabel->setText(i18n("Password must be at least 1 character long", "Password must be at least %n characters long", minPasswordLength));
00614           } else {
00615               d->m_MatchLabel->setText( match? i18n("Passwords match")
00616                                               :i18n("Passwords do not match") );
00617           }
00618       }
00619 
00620       // Password strength calculator
00621       // Based on code in the Master Password dialog in Firefox
00622       // (pref-masterpass.js)
00623       // Original code triple-licensed under the MPL, GPL, and LGPL
00624       // so is license-compatible with this file
00625 
00626       const double lengthFactor = d->reasonablePasswordLength / 8.0;
00627 
00628       
00629       int pwlength = (int) (pass.length() / lengthFactor);
00630       if (pwlength > 5) pwlength = 5;
00631 
00632       const QRegExp numRxp("[0-9]", true, false);
00633       int numeric = (int) (pass.contains(numRxp) / lengthFactor);
00634       if (numeric > 3) numeric = 3;
00635 
00636       const QRegExp symbRxp("\\W", false, false);
00637       int numsymbols = (int) (pass.contains(symbRxp) / lengthFactor);
00638       if (numsymbols > 3) numsymbols = 3;
00639 
00640       const QRegExp upperRxp("[A-Z]", true, false);
00641       int upper = (int) (pass.contains(upperRxp) / lengthFactor);
00642       if (upper > 3) upper = 3;
00643 
00644       int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
00645 
00646       if ( pwstrength < 0 ) {
00647           pwstrength = 0;
00648       }
00649   
00650       if ( pwstrength > 100 ) {
00651           pwstrength = 100;
00652       }
00653       d->m_strengthBar->setProgress(pwstrength);
00654 
00655    }
00656 }
00657 
00658 
00659 void KPasswordDialog::setAllowEmptyPasswords(bool allowed) {
00660     d->allowEmptyPasswords = allowed;
00661     enableOkBtn();
00662 }
00663 
00664 
00665 bool KPasswordDialog::allowEmptyPasswords() const {
00666     return d->allowEmptyPasswords;
00667 }
00668 
00669 void KPasswordDialog::setMinimumPasswordLength(int minLength) {
00670     d->minimumPasswordLength = minLength;
00671     enableOkBtn();
00672 }
00673 
00674 int KPasswordDialog::minimumPasswordLength() const {
00675     return d->minimumPasswordLength;
00676 }
00677 
00678 void KPasswordDialog::setMaximumPasswordLength(int maxLength) {
00679 
00680     if (maxLength < 0) maxLength = 0;
00681     if (maxLength >= KPasswordEdit::PassLen) maxLength = KPasswordEdit::PassLen - 1;
00682 
00683     d->maximumPasswordLength = maxLength;
00684 
00685     m_pEdit->setMaxPasswordLength(maxLength);
00686     if (m_pEdit2) m_pEdit2->setMaxPasswordLength(maxLength);
00687 
00688 }
00689 
00690 int KPasswordDialog::maximumPasswordLength() const {
00691     return d->maximumPasswordLength;
00692 }
00693 
00694 // reasonable password length code contributed by Steffen Mthing
00695 
00696 void KPasswordDialog::setReasonablePasswordLength(int reasonableLength) {
00697 
00698     if (reasonableLength < 1) reasonableLength = 1;
00699     if (reasonableLength >= maximumPasswordLength()) reasonableLength = maximumPasswordLength();
00700 
00701     d->reasonablePasswordLength = reasonableLength;
00702 
00703 }
00704 
00705 int KPasswordDialog::reasonablePasswordLength() const {
00706   return d->reasonablePasswordLength;
00707 }
00708 
00709 
00710 void KPasswordDialog::setPasswordStrengthWarningLevel(int warningLevel) {
00711     if (warningLevel < 0) warningLevel = 0;
00712     if (warningLevel > 99) warningLevel = 99;
00713     d->passwordStrengthWarningLevel = warningLevel;
00714 }
00715 
00716 int KPasswordDialog::passwordStrengthWarningLevel() const {
00717     return d->passwordStrengthWarningLevel;
00718 }
00719 
00720 #include "kpassdlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys