23 #include <tqlineedit.h>
28 #include <tqkeycode.h>
29 #include <tqcheckbox.h>
32 #include <tqwhatsthis.h>
33 #include <tqptrdict.h>
35 #include <tqtextcodec.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 TQTextCodec *origCStringCodec = TQTextCodec::codecForCStrings();
143 TQTextCodec::setCodecForCStrings(TQTextCodec::codecForLocale());
144 const char *outputPassword = text().ascii();
145 TQTextCodec::setCodecForCStrings(origCStringCodec);
146 return outputPassword;
156 setMaxLength(newLength);
165 TQLineEdit::insert(str);
168 void KPasswordEdit::keyPressEvent(TQKeyEvent *e) {
169 TQLineEdit::keyPressEvent(e);
172 void KPasswordEdit::focusInEvent(TQFocusEvent *e) {
173 TQLineEdit::focusInEvent(e);
176 bool KPasswordEdit::event(TQEvent *e) {
177 return TQLineEdit::event(e);
185 TQWidget *parent,
const char *name)
186 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
187 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
189 d->iconName =
"password";
194 TQWidget *parent,
const char *name )
195 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
196 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
198 if ( icon.stripWhiteSpace().isEmpty() )
199 d->iconName =
"password";
207 :
KDialogBase(0L,
"Password Dialog", true,
"", Ok|Cancel|extraBttn,
208 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
210 d->iconName =
"password";
215 void KPasswordDialog::init()
221 bool def = ( qstrcmp( tqAppName(),
"tdesu" ) == 0 ? defKeep : false );
222 if (m_Keep && cfg->readBoolEntry(
"Keep", def))
225 m_pMain =
new TQWidget(
this);
227 m_pGrid =
new TQGridLayout(m_pMain, 10, 3, 0, 0);
228 m_pGrid->addColSpacing(1, 10);
234 lbl =
new TQLabel(m_pMain);
236 lbl->setAlignment(AlignHCenter|AlignVCenter);
237 lbl->setFixedSize(lbl->sizeHint());
238 m_pGrid->addWidget(lbl, 0, 0, (TQ_Alignment)AlignCenter);
241 m_pHelpLbl =
new TQLabel(m_pMain);
242 m_pHelpLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
243 m_pGrid->addWidget(m_pHelpLbl, 0, 2, (TQ_Alignment)AlignLeft);
244 m_pGrid->addRowSpacing(1, 10);
245 m_pGrid->setRowStretch(1, 12);
248 m_pGrid->addRowSpacing(6, 5);
249 m_pGrid->setRowStretch(6, 12);
252 lbl =
new TQLabel(m_pMain);
253 lbl->setAlignment(AlignLeft|AlignVCenter);
254 lbl->setText(i18n(
"&Password:"));
255 lbl->setFixedSize(lbl->sizeHint());
256 m_pGrid->addWidget(lbl, 7, 0, (TQ_Alignment)AlignLeft);
258 TQHBoxLayout *h_lay =
new TQHBoxLayout();
259 m_pGrid->addLayout(h_lay, 7, 2);
262 lbl->setBuddy(m_pEdit);
263 TQSize size = m_pEdit->sizeHint();
264 m_pEdit->setFixedHeight(size.height());
265 m_pEdit->setMinimumWidth(size.width());
266 h_lay->addWidget(m_pEdit);
270 if ((m_Type ==
Password) && m_Keep) {
271 m_pGrid->addRowSpacing(8, 10);
272 m_pGrid->setRowStretch(8, 12);
273 TQCheckBox*
const cb =
new TQCheckBox(i18n(
"&Keep password"), m_pMain);
274 cb->setFixedSize(cb->sizeHint());
275 m_keepWarnLbl =
new TQLabel(m_pMain);
276 m_keepWarnLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
278 cb->setChecked(
true);
279 m_keepWarnLbl->show();
283 m_keepWarnLbl->hide();
285 connect(cb, TQT_SIGNAL(toggled(
bool)), TQT_SLOT(slotKeep(
bool)));
286 m_pGrid->addWidget(cb, 9, 2, (TQ_Alignment)(AlignLeft|AlignVCenter));
288 m_pGrid->addMultiCellWidget(m_keepWarnLbl, 13, 13, 0, 3);
290 m_pGrid->addRowSpacing(8, 10);
291 lbl =
new TQLabel(m_pMain);
292 lbl->setAlignment(AlignLeft|AlignVCenter);
293 lbl->setText(i18n(
"&Verify:"));
294 lbl->setFixedSize(lbl->sizeHint());
295 m_pGrid->addWidget(lbl, 9, 0, (TQ_Alignment)AlignLeft);
297 h_lay =
new TQHBoxLayout();
298 m_pGrid->addLayout(h_lay, 9, 2);
300 lbl->setBuddy(m_pEdit2);
301 size = m_pEdit2->sizeHint();
302 m_pEdit2->setFixedHeight(size.height());
303 m_pEdit2->setMinimumWidth(size.width());
304 h_lay->addWidget(m_pEdit2);
307 m_pGrid->addRowSpacing(10, 10);
308 m_pGrid->setRowStretch(10, 12);
310 TQHBox*
const strengthBox =
new TQHBox(m_pMain);
311 strengthBox->setSpacing(10);
312 m_pGrid->addMultiCellWidget(strengthBox, 11, 11, 0, 2);
313 TQLabel*
const passStrengthLabel =
new TQLabel(strengthBox);
314 passStrengthLabel->setAlignment(AlignLeft|AlignVCenter);
315 passStrengthLabel->setText(i18n(
"Password strength meter:"));
316 d->m_strengthBar =
new KProgress(100, strengthBox,
"PasswordStrengthMeter");
317 d->m_strengthBar->setPercentageVisible(
false);
319 const TQString strengthBarWhatsThis(i18n(
"The password strength meter gives an indication of the security "
320 "of the password you have entered. To improve the strength of "
321 "the password, try:\n"
322 " - using a longer password;\n"
323 " - using a mixture of upper- and lower-case letters;\n"
324 " - using numbers or symbols, such as #, as well as letters."));
325 TQWhatsThis::add(passStrengthLabel, strengthBarWhatsThis);
326 TQWhatsThis::add(d->m_strengthBar, strengthBarWhatsThis);
329 m_pGrid->addRowSpacing(12, 10);
330 m_pGrid->setRowStretch(12, 12);
332 d->m_MatchLabel =
new TQLabel(m_pMain);
333 d->m_MatchLabel->setAlignment(AlignLeft|AlignVCenter|WordBreak);
334 m_pGrid->addMultiCellWidget(d->m_MatchLabel, 13, 13, 0, 2);
335 d->m_MatchLabel->setText(i18n(
"Passwords do not match"));
338 connect( m_pEdit, TQT_SIGNAL(textChanged(
const TQString&)), TQT_SLOT(enableOkBtn()) );
339 connect( m_pEdit2, TQT_SIGNAL(textChanged(
const TQString&)), TQT_SLOT(enableOkBtn()) );
361 m_pHelpLbl->setText(prompt);
362 m_pHelpLbl->setFixedSize(275, m_pHelpLbl->heightForWidth(275));
368 m_keepWarnLbl->setText(warn);
376 return m_pHelpLbl->text();
386 TQLabel *lbl =
new TQLabel(key, m_pMain);
387 lbl->setAlignment(AlignLeft|AlignTop);
388 lbl->setFixedSize(lbl->sizeHint());
389 m_pGrid->addWidget(lbl, m_Row+2, 0, (TQ_Alignment)AlignLeft);
391 lbl =
new TQLabel(value, m_pMain);
392 lbl->setAlignment(AlignTop|WordBreak);
393 lbl->setFixedSize(275, lbl->heightForWidth(275));
394 m_pGrid->addWidget(lbl, m_Row+2, 2, (TQ_Alignment)AlignLeft);
399 void KPasswordDialog::erase()
408 void KPasswordDialog::slotOk()
413 "passwords. Please try again."));
417 if (d->m_strengthBar && d->m_strengthBar->progress() < d->passwordStrengthWarningLevel) {
419 i18n(
"The password you have entered has a low strength. "
420 "To improve the strength of "
421 "the password, try:\n"
422 " - using a longer password;\n"
423 " - using a mixture of upper- and lower-case letters;\n"
424 " - using numbers or symbols as well as letters.\n"
426 "Would you like to use this password anyway?"),
427 i18n(
"Low Password Strength"));
428 if (retVal == KMessageBox::Cancel)
return;
439 void KPasswordDialog::slotCancel()
445 void KPasswordDialog::slotKeep(
bool keep)
447 if (m_keepWarnLbl->text() !=
"") {
449 m_keepWarnLbl->show();
452 m_keepWarnLbl->hide();
454 TQTimer::singleShot(0,
this, SLOT(slotLayout()));
460 void KPasswordDialog::slotLayout()
470 const bool enableKeep = (keep && *
keep);
472 const int ret = dlg->exec();
473 if (ret == Accepted) {
487 const int ret = dlg->exec();
499 rlim.rlim_cur = rlim.rlim_max = 0;
500 setrlimit(RLIMIT_CORE, &rlim);
503 void KPasswordDialog::virtual_hook(
int id,
void* data )
504 { KDialogBase::virtual_hook(
id, data ); }
506 void KPasswordDialog::enableOkBtn()
510 && (d->allowEmptyPasswords || m_pEdit->
password()[0]);
512 const TQString pass(m_pEdit->
password());
516 if ((
int) pass.length() < minPasswordLength) {
522 if ( match && d->allowEmptyPasswords && m_pEdit->
password()[0] == 0 ) {
523 d->m_MatchLabel->setText( i18n(
"Password is empty") );
525 if ((
int) pass.length() < minPasswordLength) {
526 d->m_MatchLabel->setText(i18n(
"Password must be at least 1 character long",
"Password must be at least %n characters long", minPasswordLength));
528 d->m_MatchLabel->setText( match? i18n(
"Passwords match")
529 :i18n(
"Passwords do not match") );
539 const double lengthFactor = d->reasonablePasswordLength / 8.0;
542 int pwlength = (int) (pass.length() / lengthFactor);
543 if (pwlength > 5) pwlength = 5;
545 const TQRegExp numRxp(
"[0-9]",
true,
false);
546 int numeric = (int) (pass.contains(numRxp) / lengthFactor);
547 if (numeric > 3) numeric = 3;
549 const TQRegExp symbRxp(
"\\W",
false,
false);
550 int numsymbols = (int) (pass.contains(symbRxp) / lengthFactor);
551 if (numsymbols > 3) numsymbols = 3;
553 const TQRegExp upperRxp(
"[A-Z]",
true,
false);
554 int upper = (int) (pass.contains(upperRxp) / lengthFactor);
555 if (upper > 3) upper = 3;
557 int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
559 if ( pwstrength < 0 ) {
563 if ( pwstrength > 100 ) {
566 d->m_strengthBar->setProgress(pwstrength);
573 d->allowEmptyPasswords = allowed;
579 return d->allowEmptyPasswords;
583 d->minimumPasswordLength = minLength;
588 return d->minimumPasswordLength;
593 if (maxLength < 0) maxLength = 0;
594 if (maxLength >= KPasswordEdit::PassLen) maxLength = KPasswordEdit::PassLen - 1;
596 d->maximumPasswordLength = maxLength;
604 return d->maximumPasswordLength;
611 if (reasonableLength < 1) reasonableLength = 1;
614 d->reasonablePasswordLength = reasonableLength;
619 return d->reasonablePasswordLength;
624 if (warningLevel < 0) warningLevel = 0;
625 if (warningLevel > 99) warningLevel = 99;
626 d->passwordStrengthWarningLevel = warningLevel;
630 return d->passwordStrengthWarningLevel;
633 #include "kpassdlg.moc"
void setAllowEmptyPasswords(bool allowed)
Allow empty passwords? - Default: false.
TQString prompt() const
Returns the password prompt.
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.
int passwordStrengthWarningLevel() const
Password strength level below which a warning is given.
virtual ~KPasswordDialog()
Destructs the password dialog.
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.
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.
int reasonablePasswordLength() const
Password length that is expected to be reasonably safe.
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.
const char * password() const
Returns the password entered.
void setReasonablePasswordLength(int reasonableLength)
Password length that is expected to be reasonably safe.
A dialog base class with standard buttons and predefined layouts.
bool keep() const
Returns true if the user wants to keep the password.
KPasswordDialog(Types type, bool enableKeep, int extraBttn, TQWidget *parent=0, const char *name=0)
Constructs a password dialog.
int minimumPasswordLength() const
Minimum acceptable password length.
int maxPasswordLength() const
Returns the current maximum password length.
void setMainWidget(TQWidget *widget)
Sets the main user definable widget.
void erase()
Erases the current password.
The user is asked to enter a password.
const char * password() const
Returns the password.
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.
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()
void setKeepWarning(TQString warn)
Sets the text to be dynamically displayed when the keep checkbox is checked.
virtual bool checkPassword(const char *)
Virtual function that can be overridden to provide password checking in derived classes.
int maximumPasswordLength() const
Maximum acceptable password length.
Types
This enum distinguishes the two operation modes of this dialog: