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 );
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) {
443 void KPasswordDialog::slotCancel()
449 void KPasswordDialog::slotKeep(
bool keep)
451 if (m_keepWarnLbl->text() !=
"") {
453 m_keepWarnLbl->show();
456 m_keepWarnLbl->hide();
458 TQTimer::singleShot(0,
this, SLOT(slotLayout()));
464 void KPasswordDialog::slotLayout()
474 const bool enableKeep = (
keep && *
keep);
476 const int ret = dlg->exec();
477 if (ret == Accepted) {
491 const int ret = dlg->exec();
503 rlim.rlim_cur = rlim.rlim_max = 0;
504 setrlimit(RLIMIT_CORE, &rlim);
507 void KPasswordDialog::virtual_hook(
int id,
void* data )
508 { KDialogBase::virtual_hook(
id, data ); }
510 void KPasswordDialog::enableOkBtn()
514 && (d->allowEmptyPasswords || m_pEdit->
password()[0]);
516 const TQString pass(m_pEdit->
password());
520 if ((
int) pass.length() < minPasswordLength) {
526 if ( match && d->allowEmptyPasswords && m_pEdit->
password()[0] == 0 ) {
527 d->m_MatchLabel->setText( i18n(
"Password is empty") );
529 if ((
int) pass.length() < minPasswordLength) {
530 d->m_MatchLabel->setText(i18n(
"Password must be at least 1 character long",
"Password must be at least %n characters long", minPasswordLength));
532 d->m_MatchLabel->setText( match? i18n(
"Passwords match")
533 :i18n(
"Passwords do not match") );
543 const double lengthFactor = d->reasonablePasswordLength / 8.0;
546 int pwlength = (int) (pass.length() / lengthFactor);
547 if (pwlength > 5) pwlength = 5;
549 const TQRegExp numRxp(
"[0-9]",
true,
false);
550 int numeric = (int) (pass.contains(numRxp) / lengthFactor);
551 if (numeric > 3) numeric = 3;
553 const TQRegExp symbRxp(
"\\W",
false,
false);
554 int numsymbols = (int) (pass.contains(symbRxp) / lengthFactor);
555 if (numsymbols > 3) numsymbols = 3;
557 const TQRegExp upperRxp(
"[A-Z]",
true,
false);
558 int upper = (int) (pass.contains(upperRxp) / lengthFactor);
559 if (upper > 3) upper = 3;
561 int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
563 if ( pwstrength < 0 ) {
567 if ( pwstrength > 100 ) {
570 d->m_strengthBar->setProgress(pwstrength);
577 d->allowEmptyPasswords = allowed;
583 return d->allowEmptyPasswords;
587 d->minimumPasswordLength = minLength;
592 return d->minimumPasswordLength;
597 if (maxLength < 0) maxLength = 0;
598 if (maxLength >= KPasswordEdit::PassLen) maxLength = KPasswordEdit::PassLen - 1;
600 d->maximumPasswordLength = maxLength;
608 return d->maximumPasswordLength;
615 if (reasonableLength < 1) reasonableLength = 1;
618 d->reasonablePasswordLength = reasonableLength;
623 return d->reasonablePasswordLength;
628 if (warningLevel < 0) warningLevel = 0;
629 if (warningLevel > 99) warningLevel = 99;
630 d->passwordStrengthWarningLevel = warningLevel;
634 return d->passwordStrengthWarningLevel;
637 #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.
bool allowEmptyPasswords() const
Allow empty passwords?
void addLine(TQString key, TQString value)
Adds a line of information to the dialog.
static int getPassword(TQCString &password, TQString prompt, int *keep=0L)
Pops up the dialog, asks the user for a password, and returns it.
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 char *)
Virtual function that can be overridden to provide password checking in derived classes.
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.
static int getNewPassword(TQCString &password, TQString prompt)
Pops up the dialog, asks the user for a password and returns it.
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.
const char * password() const
Returns the password entered.
virtual ~KPasswordDialog()
Destructs the password dialog.
A safe password input widget.
const char * password() const
Returns the password.
~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.
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()