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);
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 )
191 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
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";
203 :
KDialogBase(0L,
"Password Dialog", true,
"", Ok|Cancel|extraBttn,
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()
465 const bool enableKeep = (
keep && *
keep);
467 const int ret = dlg->exec();
468 if (ret == Accepted) {
482 const int ret = dlg->exec();
494 rlim.rlim_cur = rlim.rlim_max = 0;
495 setrlimit(RLIMIT_CORE, &rlim);
498 void KPasswordDialog::virtual_hook(
int id,
void* data )
499 { KDialogBase::virtual_hook(
id, data ); }
501 void KPasswordDialog::enableOkBtn()
505 && (d->allowEmptyPasswords || !m_pEdit->
password().isEmpty());
507 const TQString pass(m_pEdit->
password());
511 if ((
int) pass.length() < minPasswordLength) {
517 if ( match && d->allowEmptyPasswords && m_pEdit->
password().isEmpty() ) {
518 d->m_MatchLabel->setText( i18n(
"Password is empty") );
520 if ((
int) pass.length() < minPasswordLength) {
521 d->m_MatchLabel->setText(i18n(
"Password must be at least 1 character long",
"Password must be at least %n characters long", minPasswordLength));
523 d->m_MatchLabel->setText( match? i18n(
"Passwords match")
524 :i18n(
"Passwords do not match") );
534 const double lengthFactor = d->reasonablePasswordLength / 8.0;
537 int pwlength = (int) (pass.length() / lengthFactor);
538 if (pwlength > 5) pwlength = 5;
540 const TQRegExp numRxp(
"[0-9]",
true,
false);
541 int numeric = (int) (pass.contains(numRxp) / lengthFactor);
542 if (numeric > 3) numeric = 3;
544 const TQRegExp symbRxp(
"\\W",
false,
false);
545 int numsymbols = (int) (pass.contains(symbRxp) / lengthFactor);
546 if (numsymbols > 3) numsymbols = 3;
548 const TQRegExp upperRxp(
"[A-Z]",
true,
false);
549 int upper = (int) (pass.contains(upperRxp) / lengthFactor);
550 if (upper > 3) upper = 3;
552 int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
554 if ( pwstrength < 0 ) {
558 if ( pwstrength > 100 ) {
561 d->m_strengthBar->setProgress(pwstrength);
568 d->allowEmptyPasswords = allowed;
574 return d->allowEmptyPasswords;
578 d->minimumPasswordLength = minLength;
583 return d->minimumPasswordLength;
588 if (maxLength < 0) maxLength = 0;
589 if (maxLength >= KPasswordEdit::PassLen) maxLength = KPasswordEdit::PassLen - 1;
591 d->maximumPasswordLength = maxLength;
599 return d->maximumPasswordLength;
606 if (reasonableLength < 1) reasonableLength = 1;
609 d->reasonablePasswordLength = reasonableLength;
614 return d->reasonablePasswordLength;
619 if (warningLevel < 0) warningLevel = 0;
620 if (warningLevel > 99) warningLevel = 99;
621 d->passwordStrengthWarningLevel = warningLevel;
625 return d->passwordStrengthWarningLevel;
628 #include "kpassdlg.moc"