23 #include <tqlineedit.h>
28 #include <tqkeycode.h>
29 #include <tqcheckbox.h>
32 #include <tqwhatsthis.h>
33 #include <tqptrdict.h>
36 #include <tdeglobal.h>
38 #include <tdeapplication.h>
40 #include <kiconloader.h>
41 #include <tdemessagebox.h>
42 #include <tdeaboutdialog.h>
43 #include <tdeconfig.h>
44 #include <kstandarddirs.h>
45 #include <kprogress.h>
48 #include <sys/resource.h>
52 #include "../tdesu/defaults.h"
58 const int KPasswordEdit::PassLen = 200;
60 class KPasswordDialog::KPasswordDialogPrivate
63 KPasswordDialogPrivate()
69 TQLabel *m_MatchLabel;
81 : TQLineEdit(parent, name)
88 const TQString val = cfg->
readEntry(
"EchoMode",
"OneStar");
89 if (val ==
"ThreeStars") {
90 setEchoMode(PasswordThreeStars);
92 else if (val ==
"NoEcho") {
93 setEchoMode(TQLineEdit::NoEcho);
96 setEchoMode(TQLineEdit::Password);
99 setInputMethodEnabled(
true );
103 : TQLineEdit(parent, name)
105 setEchoMode((TQLineEdit::EchoMode)echoMode);
110 : TQLineEdit(parent, name)
112 setEchoMode(echoMode);
117 : TQLineEdit(parent, name)
119 if (echoMode == KPasswordEdit::NoEcho) {
120 setEchoMode(TQLineEdit::NoEcho);
122 else if (echoMode == KPasswordEdit::ThreeStars) {
123 setEchoMode(TQLineEdit::PasswordThreeStars);
125 else if (echoMode == KPasswordEdit::OneStar) {
126 setEchoMode(TQLineEdit::Password);
131 void KPasswordEdit::init()
133 setAcceptDrops(
false);
151 setMaxLength(newLength);
160 TQLineEdit::insert(str);
163 void KPasswordEdit::keyPressEvent(TQKeyEvent *e) {
164 TQLineEdit::keyPressEvent(e);
167 void KPasswordEdit::focusInEvent(TQFocusEvent *e) {
168 TQLineEdit::focusInEvent(e);
171 bool KPasswordEdit::event(TQEvent *e) {
172 return TQLineEdit::event(e);
180 TQWidget *parent,
const char *name)
181 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
182 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
184 d->iconName =
"password";
189 TQWidget *parent,
const char *name )
190 :
KDialogBase(parent, name, true,
"", Ok|Cancel|extraBttn,
191 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
193 if ( icon.stripWhiteSpace().isEmpty() )
194 d->iconName =
"password";
202 :
KDialogBase(0L,
"Password Dialog", true,
"", Ok|Cancel|extraBttn,
203 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type), m_keepWarnLbl(0), d(new KPasswordDialogPrivate)
205 d->iconName =
"password";
210 void KPasswordDialog::init()
216 bool def = ( qstrcmp( tqAppName(),
"tdesu" ) == 0 ? defKeep : false );
220 m_pMain =
new TQWidget(
this);
222 m_pGrid =
new TQGridLayout(m_pMain, 10, 3, 0, 0);
223 m_pGrid->addColSpacing(1, 10);
229 lbl =
new TQLabel(m_pMain);
231 lbl->setAlignment(AlignHCenter|AlignVCenter);
232 lbl->setFixedSize(lbl->sizeHint());
233 m_pGrid->addWidget(lbl, 0, 0, TQt::AlignCenter);
236 m_pHelpLbl =
new TQLabel(m_pMain);
237 m_pHelpLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
238 m_pGrid->addWidget(m_pHelpLbl, 0, 2, TQt::AlignLeft);
239 m_pGrid->addRowSpacing(1, 10);
240 m_pGrid->setRowStretch(1, 12);
243 m_pGrid->addRowSpacing(6, 5);
244 m_pGrid->setRowStretch(6, 12);
247 lbl =
new TQLabel(m_pMain);
248 lbl->setAlignment(AlignLeft|AlignVCenter);
249 lbl->setText(i18n(
"&Password:"));
250 lbl->setFixedSize(lbl->sizeHint());
251 m_pGrid->addWidget(lbl, 7, 0, TQt::AlignLeft);
253 TQHBoxLayout *h_lay =
new TQHBoxLayout();
254 m_pGrid->addLayout(h_lay, 7, 2);
257 lbl->setBuddy(m_pEdit);
258 TQSize size = m_pEdit->sizeHint();
259 m_pEdit->setFixedHeight(size.height());
260 m_pEdit->setMinimumWidth(size.width());
261 h_lay->addWidget(m_pEdit);
265 if ((m_Type ==
Password) && m_Keep) {
266 m_pGrid->addRowSpacing(8, 10);
267 m_pGrid->setRowStretch(8, 12);
268 TQCheckBox*
const cb =
new TQCheckBox(i18n(
"&Keep password"), m_pMain);
269 cb->setFixedSize(cb->sizeHint());
270 m_keepWarnLbl =
new TQLabel(m_pMain);
271 m_keepWarnLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
273 cb->setChecked(
true);
274 m_keepWarnLbl->show();
278 m_keepWarnLbl->hide();
280 connect(cb, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(slotKeep(
bool)));
281 m_pGrid->addWidget(cb, 9, 2, TQt::AlignLeft|TQt::AlignVCenter);
283 m_pGrid->addMultiCellWidget(m_keepWarnLbl, 13, 13, 0, 3);
285 m_pGrid->addRowSpacing(8, 10);
286 lbl =
new TQLabel(m_pMain);
287 lbl->setAlignment(AlignLeft|AlignVCenter);
288 lbl->setText(i18n(
"&Verify:"));
289 lbl->setFixedSize(lbl->sizeHint());
290 m_pGrid->addWidget(lbl, 9, 0, TQt::AlignLeft);
292 h_lay =
new TQHBoxLayout();
293 m_pGrid->addLayout(h_lay, 9, 2);
295 lbl->setBuddy(m_pEdit2);
296 size = m_pEdit2->sizeHint();
297 m_pEdit2->setFixedHeight(size.height());
298 m_pEdit2->setMinimumWidth(size.width());
299 h_lay->addWidget(m_pEdit2);
302 m_pGrid->addRowSpacing(10, 10);
303 m_pGrid->setRowStretch(10, 12);
305 TQHBox*
const strengthBox =
new TQHBox(m_pMain);
306 strengthBox->setSpacing(10);
307 m_pGrid->addMultiCellWidget(strengthBox, 11, 11, 0, 2);
308 TQLabel*
const passStrengthLabel =
new TQLabel(strengthBox);
309 passStrengthLabel->setAlignment(AlignLeft|AlignVCenter);
310 passStrengthLabel->setText(i18n(
"Password strength meter:"));
311 d->m_strengthBar =
new KProgress(100, strengthBox,
"PasswordStrengthMeter");
312 d->m_strengthBar->setPercentageVisible(
false);
314 const TQString strengthBarWhatsThis(i18n(
"The password strength meter gives an indication of the security "
315 "of the password you have entered. To improve the strength of "
316 "the password, try:\n"
317 " - using a longer password;\n"
318 " - using a mixture of upper- and lower-case letters;\n"
319 " - using numbers or symbols, such as #, as well as letters."));
320 TQWhatsThis::add(passStrengthLabel, strengthBarWhatsThis);
321 TQWhatsThis::add(d->m_strengthBar, strengthBarWhatsThis);
324 m_pGrid->addRowSpacing(12, 10);
325 m_pGrid->setRowStretch(12, 12);
327 d->m_MatchLabel =
new TQLabel(m_pMain);
328 d->m_MatchLabel->setAlignment(AlignLeft|AlignVCenter|WordBreak);
329 m_pGrid->addMultiCellWidget(d->m_MatchLabel, 13, 13, 0, 2);
330 d->m_MatchLabel->setText(i18n(
"Passwords do not match"));
333 connect( m_pEdit, TQ_SIGNAL(textChanged(
const TQString&)), TQ_SLOT(enableOkBtn()) );
334 connect( m_pEdit2, TQ_SIGNAL(textChanged(
const TQString&)), TQ_SLOT(enableOkBtn()) );
356 m_pHelpLbl->setText(
prompt);
357 m_pHelpLbl->setFixedSize(275, m_pHelpLbl->heightForWidth(275));
363 m_keepWarnLbl->setText(warn);
371 return m_pHelpLbl->text();
381 TQLabel *lbl =
new TQLabel(key, m_pMain);
382 lbl->setAlignment(AlignLeft|AlignTop);
383 lbl->setFixedSize(lbl->sizeHint());
384 m_pGrid->addWidget(lbl, m_Row+2, 0, TQt::AlignLeft);
386 lbl =
new TQLabel(value, m_pMain);
387 lbl->setAlignment(AlignTop|WordBreak);
388 lbl->setFixedSize(275, lbl->heightForWidth(275));
389 m_pGrid->addWidget(lbl, m_Row+2, 2, TQt::AlignLeft);
394 void KPasswordDialog::erase()
403 void KPasswordDialog::slotOk()
408 "passwords. Please try again."));
412 if (d->m_strengthBar && d->m_strengthBar->progress() < d->passwordStrengthWarningLevel) {
414 i18n(
"The password you have entered has a low strength. "
415 "To improve the strength of "
416 "the password, try:\n"
417 " - using a longer password;\n"
418 " - using a mixture of upper- and lower-case letters;\n"
419 " - using numbers or symbols as well as letters.\n"
421 "Would you like to use this password anyway?"),
422 i18n(
"Low Password Strength"));
423 if (retVal == KMessageBox::Cancel)
return;
434 void KPasswordDialog::slotCancel()
440 void KPasswordDialog::slotKeep(
bool keep)
442 if (m_keepWarnLbl->text() !=
"") {
444 m_keepWarnLbl->show();
447 m_keepWarnLbl->hide();
449 TQTimer::singleShot(0,
this, TQ_SLOT(slotLayout()));
455 void KPasswordDialog::slotLayout()
464 const bool enableKeep = (
keep && *
keep);
466 const int ret = dlg->exec();
467 if (ret == Accepted) {
481 const int ret = dlg->exec();
493 rlim.rlim_cur = rlim.rlim_max = 0;
494 setrlimit(RLIMIT_CORE, &rlim);
497 void KPasswordDialog::virtual_hook(
int id,
void* data )
498 { KDialogBase::virtual_hook(
id, data ); }
500 void KPasswordDialog::enableOkBtn()
504 && (d->allowEmptyPasswords || !m_pEdit->
password().isEmpty());
506 const TQString pass(m_pEdit->
password());
510 if ((
int) pass.length() < minPasswordLength) {
516 if ( match && d->allowEmptyPasswords && m_pEdit->
password().isEmpty() ) {
517 d->m_MatchLabel->setText( i18n(
"Password is empty") );
519 if ((
int) pass.length() < minPasswordLength) {
520 d->m_MatchLabel->setText(i18n(
"Password must be at least 1 character long",
"Password must be at least %n characters long", minPasswordLength));
522 d->m_MatchLabel->setText( match? i18n(
"Passwords match")
523 :i18n(
"Passwords do not match") );
533 const double lengthFactor = d->reasonablePasswordLength / 8.0;
536 int pwlength = (int) (pass.length() / lengthFactor);
537 if (pwlength > 5) pwlength = 5;
539 const TQRegExp numRxp(
"[0-9]",
true,
false);
540 int numeric = (int) (pass.contains(numRxp) / lengthFactor);
541 if (numeric > 3) numeric = 3;
543 const TQRegExp symbRxp(
"\\W",
false,
false);
544 int numsymbols = (int) (pass.contains(symbRxp) / lengthFactor);
545 if (numsymbols > 3) numsymbols = 3;
547 const TQRegExp upperRxp(
"[A-Z]",
true,
false);
548 int upper = (int) (pass.contains(upperRxp) / lengthFactor);
549 if (upper > 3) upper = 3;
551 int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
553 if ( pwstrength < 0 ) {
557 if ( pwstrength > 100 ) {
560 d->m_strengthBar->setProgress(pwstrength);
567 d->allowEmptyPasswords = allowed;
573 return d->allowEmptyPasswords;
577 d->minimumPasswordLength = minLength;
582 return d->minimumPasswordLength;
587 if (maxLength < 0) maxLength = 0;
588 if (maxLength >= KPasswordEdit::PassLen) maxLength = KPasswordEdit::PassLen - 1;
590 d->maximumPasswordLength = maxLength;
598 return d->maximumPasswordLength;
605 if (reasonableLength < 1) reasonableLength = 1;
608 d->reasonablePasswordLength = reasonableLength;
613 return d->reasonablePasswordLength;
618 if (warningLevel < 0) warningLevel = 0;
619 if (warningLevel > 99) warningLevel = 99;
620 d->passwordStrengthWarningLevel = warningLevel;
624 return d->passwordStrengthWarningLevel;
627 #include "kpassdlg.moc"
A dialog base class with standard buttons and predefined layouts.
void setMainWidget(TQWidget *widget)
Sets the main user definable widget.
void enableButtonOK(bool state)
Enable or disable (gray out) the OK button.
static void sorry(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
Display an "Sorry" dialog.
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?
void addLine(TQString key, TQString value)
Adds a line of information to the dialog.
TQString password() const
Returns the password entered.
bool keep() const
Returns true if the user wants to keep the password.
int passwordStrengthWarningLevel() const
Password strength level below which a warning is given.
void clearPassword()
Clears the password input field.
int reasonablePasswordLength() const
Password length that is expected to be reasonably safe.
void setPasswordStrengthWarningLevel(int warningLevel)
Set the password strength level below which a warning is given Value is in the range 0 to 99.
TQString prompt() const
Returns the password prompt.
KPasswordDialog(Types type, bool enableKeep, int extraBttn, TQWidget *parent=0, const char *name=0)
Constructs a password dialog.
void setMaximumPasswordLength(int maxLength)
Maximum acceptable password length.
int maximumPasswordLength() const
Maximum acceptable password length.
void setKeepWarning(TQString warn)
Sets the text to be dynamically displayed when the keep checkbox is checked.
virtual bool checkPassword(const TQString &)
Virtual function that can be overridden to provide password checking in derived classes.
static int getPassword(TQString &password, TQString prompt, int *keep=0L)
Pops up the dialog, asks the user for a password, and returns it.
void setMinimumPasswordLength(int minLength)
Minimum acceptable password length.
int minimumPasswordLength() const
Minimum acceptable password length.
Types
This enum distinguishes the two operation modes of this dialog:
@ NewPassword
The user is asked to enter a password and to confirm it a second time.
@ Password
The user is asked to enter a password.
void setReasonablePasswordLength(int reasonableLength)
Password length that is expected to be reasonably safe.
static void disableCoreDumps()
Static helper function that disables core dumps.
void setPrompt(TQString prompt)
Sets the password prompt.
void setAllowEmptyPasswords(bool allowed)
Allow empty passwords? - Default: false.
static int getNewPassword(TQString &password, TQString prompt)
Pops up the dialog, asks the user for a password and returns it.
virtual ~KPasswordDialog()
Destructs the password dialog.
A safe password input widget.
~KPasswordEdit()
Destructs the widget.
int maxPasswordLength() const
Returns the current maximum password length.
KPasswordEdit(TQWidget *parent=0, const char *name=0)
Constructs a password input widget using the user's global "echo mode" setting.
void setMaxPasswordLength(int newLength)
Set the current maximum password length.
TQString password() const
Returns the password.
void erase()
Erases the current password.
virtual void insert(const TQString &)
Reimplementation.
A progress indicator widget.
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
static TDEIconLoader * iconLoader()
static TDEConfig * config()