24 #include <tqlineedit.h> 29 #include <tqkeycode.h> 30 #include <tqcheckbox.h> 33 #include <tqwhatsthis.h> 34 #include <tqptrdict.h> 37 #include <tdeglobal.h> 39 #include <tdeapplication.h> 41 #include <kiconloader.h> 42 #include <tdemessagebox.h> 43 #include <tdeaboutdialog.h> 44 #include <tdeconfig.h> 45 #include <kstandarddirs.h> 46 #include <kprogress.h> 49 #include <sys/resource.h> 53 #include "../tdesu/defaults.h" 59 const int KPasswordEdit::PassLen = 200;
61 class KPasswordDialog::KPasswordDialogPrivate
64 KPasswordDialogPrivate()
70 TQLabel *m_MatchLabel;
82 : TQLineEdit(parent, name)
89 const TQString val = cfg->
readEntry(
"EchoMode",
"OneStar");
90 if (val ==
"ThreeStars") {
91 setEchoMode(PasswordThreeStars);
93 else if (val ==
"NoEcho") {
94 setEchoMode(TQLineEdit::NoEcho);
97 setEchoMode(TQLineEdit::Password);
100 setInputMethodEnabled(
true );
104 : TQLineEdit(parent, name)
106 setEchoMode((TQLineEdit::EchoMode)echoMode);
111 : TQLineEdit(parent, name)
113 setEchoMode(echoMode);
118 : TQLineEdit(parent, name)
120 if (echoMode == KPasswordEdit::NoEcho) {
121 setEchoMode(TQLineEdit::NoEcho);
123 else if (echoMode == KPasswordEdit::ThreeStars) {
124 setEchoMode(TQLineEdit::PasswordThreeStars);
126 else if (echoMode == KPasswordEdit::OneStar) {
127 setEchoMode(TQLineEdit::Password);
132 void KPasswordEdit::init()
134 setAcceptDrops(
false);
142 return text().ascii();
152 setMaxLength(newLength);
161 TQLineEdit::insert(str);
164 void KPasswordEdit::keyPressEvent(TQKeyEvent *e) {
165 TQLineEdit::keyPressEvent(e);
168 void KPasswordEdit::focusInEvent(TQFocusEvent *e) {
169 TQLineEdit::focusInEvent(e);
172 bool KPasswordEdit::event(TQEvent *e) {
173 return TQLineEdit::event(e);
181 TQWidget *parent,
const char *name)
182 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
183 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
185 d->iconName =
"password";
190 TQWidget *parent,
const char *name )
192 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
194 if ( icon.stripWhiteSpace().isEmpty() )
195 d->iconName =
"password";
204 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
206 d->iconName =
"password";
211 void KPasswordDialog::init()
217 bool def = ( qstrcmp( tqAppName(),
"tdesu" ) == 0 ? defKeep : false );
221 m_pMain =
new TQWidget(
this);
223 m_pGrid =
new TQGridLayout(m_pMain, 10, 3, 0, 0);
224 m_pGrid->addColSpacing(1, 10);
230 lbl =
new TQLabel(m_pMain);
232 lbl->setAlignment(AlignHCenter|AlignVCenter);
233 lbl->setFixedSize(lbl->sizeHint());
234 m_pGrid->addWidget(lbl, 0, 0, (TQ_Alignment)AlignCenter);
237 m_pHelpLbl =
new TQLabel(m_pMain);
238 m_pHelpLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
239 m_pGrid->addWidget(m_pHelpLbl, 0, 2, (TQ_Alignment)AlignLeft);
240 m_pGrid->addRowSpacing(1, 10);
241 m_pGrid->setRowStretch(1, 12);
244 m_pGrid->addRowSpacing(6, 5);
245 m_pGrid->setRowStretch(6, 12);
248 lbl =
new TQLabel(m_pMain);
249 lbl->setAlignment(AlignLeft|AlignVCenter);
250 lbl->setText(i18n(
"&Password:"));
251 lbl->setFixedSize(lbl->sizeHint());
252 m_pGrid->addWidget(lbl, 7, 0, (TQ_Alignment)AlignLeft);
254 TQHBoxLayout *h_lay =
new TQHBoxLayout();
255 m_pGrid->addLayout(h_lay, 7, 2);
258 lbl->setBuddy(m_pEdit);
259 TQSize size = m_pEdit->sizeHint();
260 m_pEdit->setFixedHeight(size.height());
261 m_pEdit->setMinimumWidth(size.width());
262 h_lay->addWidget(m_pEdit);
266 if ((m_Type ==
Password) && m_Keep) {
267 m_pGrid->addRowSpacing(8, 10);
268 m_pGrid->setRowStretch(8, 12);
269 TQCheckBox*
const cb =
new TQCheckBox(i18n(
"&Keep password"), m_pMain);
270 cb->setFixedSize(cb->sizeHint());
271 m_keepWarnLbl =
new TQLabel(m_pMain);
272 m_keepWarnLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
274 cb->setChecked(
true);
275 m_keepWarnLbl->show();
279 m_keepWarnLbl->hide();
281 connect(cb, TQT_SIGNAL(toggled(
bool)), TQT_SLOT(slotKeep(
bool)));
282 m_pGrid->addWidget(cb, 9, 2, (TQ_Alignment)(AlignLeft|AlignVCenter));
284 m_pGrid->addMultiCellWidget(m_keepWarnLbl, 13, 13, 0, 3);
286 m_pGrid->addRowSpacing(8, 10);
287 lbl =
new TQLabel(m_pMain);
288 lbl->setAlignment(AlignLeft|AlignVCenter);
289 lbl->setText(i18n(
"&Verify:"));
290 lbl->setFixedSize(lbl->sizeHint());
291 m_pGrid->addWidget(lbl, 9, 0, (TQ_Alignment)AlignLeft);
293 h_lay =
new TQHBoxLayout();
294 m_pGrid->addLayout(h_lay, 9, 2);
296 lbl->setBuddy(m_pEdit2);
297 size = m_pEdit2->sizeHint();
298 m_pEdit2->setFixedHeight(size.height());
299 m_pEdit2->setMinimumWidth(size.width());
300 h_lay->addWidget(m_pEdit2);
303 m_pGrid->addRowSpacing(10, 10);
304 m_pGrid->setRowStretch(10, 12);
306 TQHBox*
const strengthBox =
new TQHBox(m_pMain);
307 strengthBox->setSpacing(10);
308 m_pGrid->addMultiCellWidget(strengthBox, 11, 11, 0, 2);
309 TQLabel*
const passStrengthLabel =
new TQLabel(strengthBox);
310 passStrengthLabel->setAlignment(AlignLeft|AlignVCenter);
311 passStrengthLabel->setText(i18n(
"Password strength meter:"));
312 d->m_strengthBar =
new KProgress(100, strengthBox,
"PasswordStrengthMeter");
313 d->m_strengthBar->setPercentageVisible(
false);
315 const TQString strengthBarWhatsThis(i18n(
"The password strength meter gives an indication of the security " 316 "of the password you have entered. To improve the strength of " 317 "the password, try:\n" 318 " - using a longer password;\n" 319 " - using a mixture of upper- and lower-case letters;\n" 320 " - using numbers or symbols, such as #, as well as letters."));
321 TQWhatsThis::add(passStrengthLabel, strengthBarWhatsThis);
322 TQWhatsThis::add(d->m_strengthBar, strengthBarWhatsThis);
325 m_pGrid->addRowSpacing(12, 10);
326 m_pGrid->setRowStretch(12, 12);
328 d->m_MatchLabel =
new TQLabel(m_pMain);
329 d->m_MatchLabel->setAlignment(AlignLeft|AlignVCenter|WordBreak);
330 m_pGrid->addMultiCellWidget(d->m_MatchLabel, 13, 13, 0, 2);
331 d->m_MatchLabel->setText(i18n(
"Passwords do not match"));
334 connect( m_pEdit, TQT_SIGNAL(textChanged(
const TQString&)), TQT_SLOT(enableOkBtn()) );
335 connect( m_pEdit2, TQT_SIGNAL(textChanged(
const TQString&)), TQT_SLOT(enableOkBtn()) );
357 m_pHelpLbl->setText(prompt);
358 m_pHelpLbl->setFixedSize(275, m_pHelpLbl->heightForWidth(275));
364 m_keepWarnLbl->setText(warn);
372 return m_pHelpLbl->text();
382 TQLabel *lbl =
new TQLabel(key, m_pMain);
383 lbl->setAlignment(AlignLeft|AlignTop);
384 lbl->setFixedSize(lbl->sizeHint());
385 m_pGrid->addWidget(lbl, m_Row+2, 0, (TQ_Alignment)AlignLeft);
387 lbl =
new TQLabel(value, m_pMain);
388 lbl->setAlignment(AlignTop|WordBreak);
389 lbl->setFixedSize(275, lbl->heightForWidth(275));
390 m_pGrid->addWidget(lbl, m_Row+2, 2, (TQ_Alignment)AlignLeft);
395 void KPasswordDialog::erase()
404 void KPasswordDialog::slotOk()
409 "passwords. Please try again."));
413 if (d->m_strengthBar && d->m_strengthBar->progress() < d->passwordStrengthWarningLevel) {
415 i18n(
"The password you have entered has a low strength. " 416 "To improve the strength of " 417 "the password, try:\n" 418 " - using a longer password;\n" 419 " - using a mixture of upper- and lower-case letters;\n" 420 " - using numbers or symbols as well as letters.\n" 422 "Would you like to use this password anyway?"),
423 i18n(
"Low Password Strength"));
424 if (retVal == KMessageBox::Cancel)
return;
435 void KPasswordDialog::slotCancel()
441 void KPasswordDialog::slotKeep(
bool keep)
443 if (m_keepWarnLbl->text() !=
"") {
445 m_keepWarnLbl->show();
448 m_keepWarnLbl->hide();
450 TQTimer::singleShot(0,
this, SLOT(slotLayout()));
456 void KPasswordDialog::slotLayout()
466 const bool enableKeep = (keep && *
keep);
468 const int ret = dlg->exec();
469 if (ret == Accepted) {
483 const int ret = dlg->exec();
495 rlim.rlim_cur = rlim.rlim_max = 0;
496 setrlimit(RLIMIT_CORE, &rlim);
499 void KPasswordDialog::virtual_hook(
int id,
void* data )
500 { KDialogBase::virtual_hook(
id, data ); }
502 void KPasswordDialog::enableOkBtn()
506 && (d->allowEmptyPasswords || m_pEdit->
password()[0]);
508 const TQString pass(m_pEdit->
password());
512 if ((
int) pass.length() < minPasswordLength) {
518 if ( match && d->allowEmptyPasswords && m_pEdit->
password()[0] == 0 ) {
519 d->m_MatchLabel->setText( i18n(
"Password is empty") );
521 if ((
int) pass.length() < minPasswordLength) {
522 d->m_MatchLabel->setText(i18n(
"Password must be at least 1 character long",
"Password must be at least %n characters long", minPasswordLength));
524 d->m_MatchLabel->setText( match? i18n(
"Passwords match")
525 :i18n(
"Passwords do not match") );
535 const double lengthFactor = d->reasonablePasswordLength / 8.0;
538 int pwlength = (int) (pass.length() / lengthFactor);
539 if (pwlength > 5) pwlength = 5;
541 const TQRegExp numRxp(
"[0-9]",
true,
false);
542 int numeric = (int) (pass.contains(numRxp) / lengthFactor);
543 if (numeric > 3) numeric = 3;
545 const TQRegExp symbRxp(
"\\W",
false,
false);
546 int numsymbols = (int) (pass.contains(symbRxp) / lengthFactor);
547 if (numsymbols > 3) numsymbols = 3;
549 const TQRegExp upperRxp(
"[A-Z]",
true,
false);
550 int upper = (int) (pass.contains(upperRxp) / lengthFactor);
551 if (upper > 3) upper = 3;
553 int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
555 if ( pwstrength < 0 ) {
559 if ( pwstrength > 100 ) {
562 d->m_strengthBar->setProgress(pwstrength);
569 d->allowEmptyPasswords = allowed;
575 return d->allowEmptyPasswords;
579 d->minimumPasswordLength = minLength;
584 return d->minimumPasswordLength;
589 if (maxLength < 0) maxLength = 0;
590 if (maxLength >= KPasswordEdit::PassLen) maxLength = KPasswordEdit::PassLen - 1;
592 d->maximumPasswordLength = maxLength;
600 return d->maximumPasswordLength;
607 if (reasonableLength < 1) reasonableLength = 1;
610 d->reasonablePasswordLength = reasonableLength;
615 return d->reasonablePasswordLength;
620 if (warningLevel < 0) warningLevel = 0;
621 if (warningLevel > 99) warningLevel = 99;
622 d->passwordStrengthWarningLevel = warningLevel;
626 return d->passwordStrengthWarningLevel;
629 #include "kpassdlg.moc"
void setAllowEmptyPasswords(bool allowed)
Allow empty passwords? - Default: false.
void setPasswordStrengthWarningLevel(int warningLevel)
Set the password strength level below which a warning is given Value is in the range 0 to 99...
The user is asked to enter a password and to confirm it a second time.
virtual ~KPasswordDialog()
Destructs the password dialog.
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
void addLine(TQString key, TQString value)
Adds a line of information to the dialog.
void setMaxPasswordLength(int newLength)
Set the current maximum password length.
static int getNewPassword(TQCString &password, TQString prompt)
Pops up the dialog, asks the user for a password and returns it.
A safe password input widget.
void setPrompt(TQString prompt)
Sets the password prompt.
bool keep() const
Returns true if the user wants to keep the password.
void enableButtonOK(bool state)
Enable or disable (gray out) the OK button.
A progress indicator widget.
void clearPassword()
Clears the password input field.
static TDEIconLoader * iconLoader()
KPasswordEdit(TQWidget *parent=0, const char *name=0)
Constructs a password input widget using the user's global "echo mode" setting.
static void disableCoreDumps()
Static helper function that disables core dumps.
void setMinimumPasswordLength(int minLength)
Minimum acceptable password length.
static void sorry(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
Display an "Sorry" dialog.
void setReasonablePasswordLength(int reasonableLength)
Password length that is expected to be reasonably safe.
A dialog base class with standard buttons and predefined layouts.
const char * password() const
Returns the password entered.
KPasswordDialog(Types type, bool enableKeep, int extraBttn, TQWidget *parent=0, const char *name=0)
Constructs a password dialog.
void setMainWidget(TQWidget *widget)
Sets the main user definable widget.
const char * password() const
Returns the password.
void erase()
Erases the current password.
int passwordStrengthWarningLevel() const
Password strength level below which a warning is given.
The user is asked to enter a password.
int reasonablePasswordLength() const
Password length that is expected to be reasonably safe.
static int warningContinueCancel(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const KGuiItem &buttonContinue=KStdGuiItem::cont(), const TQString &dontAskAgainName=TQString::null, int options=Notify)
Display a "warning" dialog.
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
bool allowEmptyPasswords() const
Allow empty passwords?
~KPasswordEdit()
Destructs the widget.
static int getPassword(TQCString &password, TQString prompt, int *keep=0L)
Pops up the dialog, asks the user for a password, and returns it.
void setMaximumPasswordLength(int maxLength)
Maximum acceptable password length.
virtual void insert(const TQString &)
Reimplementation.
static TDEConfig * config()
int maximumPasswordLength() const
Maximum acceptable password length.
void setKeepWarning(TQString warn)
Sets the text to be dynamically displayed when the keep checkbox is checked.
int maxPasswordLength() const
Returns the current maximum password length.
virtual bool checkPassword(const char *)
Virtual function that can be overridden to provide password checking in derived classes.
Types
This enum distinguishes the two operation modes of this dialog:
TQString prompt() const
Returns the password prompt.
int minimumPasswordLength() const
Minimum acceptable password length.