24 #include <tqlineedit.h>
29 #include <tqkeycode.h>
30 #include <tqcheckbox.h>
33 #include <tqwhatsthis.h>
34 #include <tqptrdict.h>
36 #include <tqtextcodec.h>
38 #include <tdeglobal.h>
40 #include <tdeapplication.h>
42 #include <kiconloader.h>
43 #include <tdemessagebox.h>
44 #include <tdeaboutdialog.h>
45 #include <tdeconfig.h>
46 #include <kstandarddirs.h>
47 #include <kprogress.h>
50 #include <sys/resource.h>
54 #include "../tdesu/defaults.h"
60 const int KPasswordEdit::PassLen = 200;
62 class KPasswordDialog::KPasswordDialogPrivate
65 KPasswordDialogPrivate()
71 TQLabel *m_MatchLabel;
83 : TQLineEdit(parent, name)
90 const TQString val = cfg->
readEntry(
"EchoMode",
"OneStar");
91 if (val ==
"ThreeStars") {
92 setEchoMode(PasswordThreeStars);
94 else if (val ==
"NoEcho") {
95 setEchoMode(TQLineEdit::NoEcho);
98 setEchoMode(TQLineEdit::Password);
101 setInputMethodEnabled(
true );
105 : TQLineEdit(parent, name)
107 setEchoMode((TQLineEdit::EchoMode)echoMode);
112 : TQLineEdit(parent, name)
114 setEchoMode(echoMode);
119 : TQLineEdit(parent, name)
121 if (echoMode == KPasswordEdit::NoEcho) {
122 setEchoMode(TQLineEdit::NoEcho);
124 else if (echoMode == KPasswordEdit::ThreeStars) {
125 setEchoMode(TQLineEdit::PasswordThreeStars);
127 else if (echoMode == KPasswordEdit::OneStar) {
128 setEchoMode(TQLineEdit::Password);
133 void KPasswordEdit::init()
135 setAcceptDrops(
false);
143 TQTextCodec *origCStringCodec = TQTextCodec::codecForCStrings();
144 TQTextCodec::setCodecForCStrings(TQTextCodec::codecForLocale());
145 const char *outputPassword = text().ascii();
146 TQTextCodec::setCodecForCStrings(origCStringCodec);
147 return outputPassword;
157 setMaxLength(newLength);
169 void KPasswordEdit::keyPressEvent(TQKeyEvent *e) {
170 TQLineEdit::keyPressEvent(e);
173 void KPasswordEdit::focusInEvent(TQFocusEvent *e) {
174 TQLineEdit::focusInEvent(e);
177 bool KPasswordEdit::event(TQEvent *e) {
178 return TQLineEdit::event(e);
186 TQWidget *parent,
const char *name)
187 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
188 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
190 d->iconName =
"password";
195 TQWidget *parent,
const char *name )
196 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
197 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
199 if ( icon.stripWhiteSpace().isEmpty() )
200 d->iconName =
"password";
208 :
KDialogBase(0L,
"Password Dialog", true,
"", Ok|Cancel|extraBttn,
209 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
211 d->iconName =
"password";
216 void KPasswordDialog::init()
222 bool def = ( qstrcmp( tqAppName(),
"tdesu" ) == 0 ? defKeep : false );
226 m_pMain =
new TQWidget(
this);
228 m_pGrid =
new TQGridLayout(m_pMain, 10, 3, 0, 0);
229 m_pGrid->addColSpacing(1, 10);
235 lbl =
new TQLabel(m_pMain);
237 lbl->setAlignment(AlignHCenter|AlignVCenter);
238 lbl->setFixedSize(lbl->sizeHint());
239 m_pGrid->addWidget(lbl, 0, 0, (TQ_Alignment)AlignCenter);
242 m_pHelpLbl =
new TQLabel(m_pMain);
243 m_pHelpLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
244 m_pGrid->addWidget(m_pHelpLbl, 0, 2, (TQ_Alignment)AlignLeft);
245 m_pGrid->addRowSpacing(1, 10);
246 m_pGrid->setRowStretch(1, 12);
249 m_pGrid->addRowSpacing(6, 5);
250 m_pGrid->setRowStretch(6, 12);
253 lbl =
new TQLabel(m_pMain);
254 lbl->setAlignment(AlignLeft|AlignVCenter);
255 lbl->setText(i18n(
"&Password:"));
256 lbl->setFixedSize(lbl->sizeHint());
257 m_pGrid->addWidget(lbl, 7, 0, (TQ_Alignment)AlignLeft);
259 TQHBoxLayout *h_lay =
new TQHBoxLayout();
260 m_pGrid->addLayout(h_lay, 7, 2);
263 lbl->setBuddy(m_pEdit);
264 TQSize size = m_pEdit->sizeHint();
265 m_pEdit->setFixedHeight(size.height());
266 m_pEdit->setMinimumWidth(size.width());
267 h_lay->addWidget(m_pEdit);
271 if ((m_Type ==
Password) && m_Keep) {
272 m_pGrid->addRowSpacing(8, 10);
273 m_pGrid->setRowStretch(8, 12);
274 TQCheckBox*
const cb =
new TQCheckBox(i18n(
"&Keep password"), m_pMain);
275 cb->setFixedSize(cb->sizeHint());
276 m_keepWarnLbl =
new TQLabel(m_pMain);
277 m_keepWarnLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
279 cb->setChecked(
true);
280 m_keepWarnLbl->show();
284 m_keepWarnLbl->hide();
286 connect(cb, TQT_SIGNAL(toggled(
bool)), TQT_SLOT(slotKeep(
bool)));
287 m_pGrid->addWidget(cb, 9, 2, (TQ_Alignment)(AlignLeft|AlignVCenter));
289 m_pGrid->addMultiCellWidget(m_keepWarnLbl, 13, 13, 0, 3);
291 m_pGrid->addRowSpacing(8, 10);
292 lbl =
new TQLabel(m_pMain);
293 lbl->setAlignment(AlignLeft|AlignVCenter);
294 lbl->setText(i18n(
"&Verify:"));
295 lbl->setFixedSize(lbl->sizeHint());
296 m_pGrid->addWidget(lbl, 9, 0, (TQ_Alignment)AlignLeft);
298 h_lay =
new TQHBoxLayout();
299 m_pGrid->addLayout(h_lay, 9, 2);
301 lbl->setBuddy(m_pEdit2);
302 size = m_pEdit2->sizeHint();
303 m_pEdit2->setFixedHeight(size.height());
304 m_pEdit2->setMinimumWidth(size.width());
305 h_lay->addWidget(m_pEdit2);
308 m_pGrid->addRowSpacing(10, 10);
309 m_pGrid->setRowStretch(10, 12);
311 TQHBox*
const strengthBox =
new TQHBox(m_pMain);
312 strengthBox->setSpacing(10);
313 m_pGrid->addMultiCellWidget(strengthBox, 11, 11, 0, 2);
314 TQLabel*
const passStrengthLabel =
new TQLabel(strengthBox);
315 passStrengthLabel->setAlignment(AlignLeft|AlignVCenter);
316 passStrengthLabel->setText(i18n(
"Password strength meter:"));
317 d->m_strengthBar =
new KProgress(100, strengthBox,
"PasswordStrengthMeter");
318 d->m_strengthBar->setPercentageVisible(
false);
320 const TQString strengthBarWhatsThis(i18n(
"The password strength meter gives an indication of the security "
321 "of the password you have entered. To improve the strength of "
322 "the password, try:\n"
323 " - using a longer password;\n"
324 " - using a mixture of upper- and lower-case letters;\n"
325 " - using numbers or symbols, such as #, as well as letters."));
326 TQWhatsThis::add(passStrengthLabel, strengthBarWhatsThis);
327 TQWhatsThis::add(d->m_strengthBar, strengthBarWhatsThis);
330 m_pGrid->addRowSpacing(12, 10);
331 m_pGrid->setRowStretch(12, 12);
333 d->m_MatchLabel =
new TQLabel(m_pMain);
334 d->m_MatchLabel->setAlignment(AlignLeft|AlignVCenter|WordBreak);
335 m_pGrid->addMultiCellWidget(d->m_MatchLabel, 13, 13, 0, 2);
336 d->m_MatchLabel->setText(i18n(
"Passwords do not match"));
339 connect( m_pEdit, TQT_SIGNAL(textChanged(
const TQString&)), TQT_SLOT(enableOkBtn()) );
340 connect( m_pEdit2, TQT_SIGNAL(textChanged(
const TQString&)), TQT_SLOT(enableOkBtn()) );
362 m_pHelpLbl->setText(prompt);
363 m_pHelpLbl->setFixedSize(275, m_pHelpLbl->heightForWidth(275));
369 m_keepWarnLbl->setText(warn);
377 return m_pHelpLbl->text();
387 TQLabel *lbl =
new TQLabel(key, m_pMain);
388 lbl->setAlignment(AlignLeft|AlignTop);
389 lbl->setFixedSize(lbl->sizeHint());
390 m_pGrid->addWidget(lbl, m_Row+2, 0, (TQ_Alignment)AlignLeft);
392 lbl =
new TQLabel(value, m_pMain);
393 lbl->setAlignment(AlignTop|WordBreak);
394 lbl->setFixedSize(275, lbl->heightForWidth(275));
395 m_pGrid->addWidget(lbl, m_Row+2, 2, (TQ_Alignment)AlignLeft);
400 void KPasswordDialog::erase()
409 void KPasswordDialog::slotOk()
414 "passwords. Please try again."));
418 if (d->m_strengthBar && d->m_strengthBar->progress() < d->passwordStrengthWarningLevel) {
420 i18n(
"The password you have entered has a low strength. "
421 "To improve the strength of "
422 "the password, try:\n"
423 " - using a longer password;\n"
424 " - using a mixture of upper- and lower-case letters;\n"
425 " - using numbers or symbols as well as letters.\n"
427 "Would you like to use this password anyway?"),
428 i18n(
"Low Password Strength"));
429 if (retVal == KMessageBox::Cancel)
return;
440 void KPasswordDialog::slotCancel()
446 void KPasswordDialog::slotKeep(
bool keep)
448 if (m_keepWarnLbl->text() !=
"") {
450 m_keepWarnLbl->show();
453 m_keepWarnLbl->hide();
455 TQTimer::singleShot(0,
this, SLOT(slotLayout()));
461 void KPasswordDialog::slotLayout()
471 const bool enableKeep = (keep && *
keep);
473 const int ret = dlg->exec();
474 if (ret == Accepted) {
488 const int ret = dlg->exec();
500 rlim.rlim_cur = rlim.rlim_max = 0;
501 setrlimit(RLIMIT_CORE, &rlim);
504 void KPasswordDialog::virtual_hook(
int id,
void* data )
505 { KDialogBase::virtual_hook(
id, data ); }
507 void KPasswordDialog::enableOkBtn()
511 && (d->allowEmptyPasswords || m_pEdit->
password()[0]);
513 const TQString pass(m_pEdit->
password());
517 if ((
int) pass.length() < minPasswordLength) {
523 if ( match && d->allowEmptyPasswords && m_pEdit->
password()[0] == 0 ) {
524 d->m_MatchLabel->setText( i18n(
"Password is empty") );
526 if ((
int) pass.length() < minPasswordLength) {
527 d->m_MatchLabel->setText(i18n(
"Password must be at least 1 character long",
"Password must be at least %n characters long", minPasswordLength));
529 d->m_MatchLabel->setText( match? i18n(
"Passwords match")
530 :i18n(
"Passwords do not match") );
540 const double lengthFactor = d->reasonablePasswordLength / 8.0;
543 int pwlength = (int) (pass.length() / lengthFactor);
544 if (pwlength > 5) pwlength = 5;
546 const TQRegExp numRxp(
"[0-9]",
true,
false);
547 int numeric = (int) (pass.contains(numRxp) / lengthFactor);
548 if (numeric > 3) numeric = 3;
550 const TQRegExp symbRxp(
"\\W",
false,
false);
551 int numsymbols = (int) (pass.contains(symbRxp) / lengthFactor);
552 if (numsymbols > 3) numsymbols = 3;
554 const TQRegExp upperRxp(
"[A-Z]",
true,
false);
555 int upper = (int) (pass.contains(upperRxp) / lengthFactor);
556 if (upper > 3) upper = 3;
558 int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
560 if ( pwstrength < 0 ) {
564 if ( pwstrength > 100 ) {
567 d->m_strengthBar->setProgress(pwstrength);
574 d->allowEmptyPasswords = allowed;
580 return d->allowEmptyPasswords;
584 d->minimumPasswordLength = minLength;
589 return d->minimumPasswordLength;
594 if (maxLength < 0) maxLength = 0;
595 if (maxLength >= KPasswordEdit::PassLen) maxLength = KPasswordEdit::PassLen - 1;
597 d->maximumPasswordLength = maxLength;
605 return d->maximumPasswordLength;
612 if (reasonableLength < 1) reasonableLength = 1;
615 d->reasonablePasswordLength = reasonableLength;
620 return d->reasonablePasswordLength;
625 if (warningLevel < 0) warningLevel = 0;
626 if (warningLevel > 99) warningLevel = 99;
627 d->passwordStrengthWarningLevel = warningLevel;
631 return d->passwordStrengthWarningLevel;
634 #include "kpassdlg.moc"