00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <unistd.h>
00021
00022 #include <qwidget.h>
00023 #include <qlineedit.h>
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qsize.h>
00027 #include <qevent.h>
00028 #include <qkeycode.h>
00029 #include <qcheckbox.h>
00030
00031 #include <kglobal.h>
00032 #include <kapplication.h>
00033 #include <klocale.h>
00034 #include <kiconloader.h>
00035 #include <kmessagebox.h>
00036 #include <kaboutdialog.h>
00037 #include <kconfig.h>
00038 #include <kstandarddirs.h>
00039
00040 #include <sys/time.h>
00041 #include <sys/resource.h>
00042
00043 #include "kpassdlg.h"
00044
00045
00046
00047
00048
00049 const int KPasswordEdit::PassLen = 100;
00050
00051 KPasswordEdit::KPasswordEdit(QWidget *parent, const char *name)
00052 : QLineEdit(parent, name), m_EchoMode(OneStar)
00053 {
00054 init();
00055
00056 KConfig *cfg = KGlobal::config();
00057 KConfigGroupSaver saver(cfg, "Passwords");
00058
00059 QString val = cfg->readEntry("EchoMode", "OneStar");
00060 if (val == "ThreeStars")
00061 m_EchoMode = ThreeStars;
00062 else if (val == "NoEcho")
00063 m_EchoMode = NoEcho;
00064 else
00065 m_EchoMode = OneStar;
00066 }
00067
00068 KPasswordEdit::KPasswordEdit(QWidget *parent, const char *name, int echoMode)
00069 : QLineEdit(parent, name), m_EchoMode(echoMode)
00070 {
00071 init();
00072 }
00073
00074 KPasswordEdit::KPasswordEdit(EchoMode echoMode, QWidget *parent, const char *name)
00075 : QLineEdit(parent, name), m_EchoMode(echoMode)
00076 {
00077 init();
00078 }
00079
00080 void KPasswordEdit::init()
00081 {
00082 setAcceptDrops(false);
00083 m_Password = new char[PassLen];
00084 m_Password[0] = '\000';
00085 m_Length = 0;
00086 }
00087
00088 KPasswordEdit::~KPasswordEdit()
00089 {
00090 for (int i=0; i<PassLen; i++)
00091 m_Password[i] = '\000';
00092 delete[] m_Password;
00093 }
00094
00095
00096 void KPasswordEdit::erase()
00097 {
00098 m_Length = 0;
00099 for (int i=0; i<PassLen; i++)
00100 m_Password[i] = '\000';
00101 setText("");
00102 }
00103
00104 void KPasswordEdit::focusInEvent(QFocusEvent *e)
00105 {
00106 QString txt = text();
00107 setUpdatesEnabled(false);
00108 QLineEdit::focusInEvent(e);
00109 setUpdatesEnabled(true);
00110 setText(txt);
00111 }
00112
00113
00114 void KPasswordEdit::keyPressEvent(QKeyEvent *e)
00115 {
00116 switch (e->key()) {
00117 case Key_Return:
00118 case Key_Enter:
00119 case Key_Escape:
00120 e->ignore();
00121 break;
00122 case Key_Backspace:
00123 case Key_Delete:
00124 case 0x7f:
00125 if (e->state() & (ControlButton | AltButton))
00126 e->ignore();
00127 else if (m_Length) {
00128 m_Password[--m_Length] = '\000';
00129 showPass();
00130 }
00131 break;
00132 default:
00133 unsigned char ke = e->text().local8Bit()[0];
00134 if (ke >= 32) {
00135 if (m_Length < (PassLen - 1)) {
00136 m_Password[m_Length] = ke;
00137 m_Password[++m_Length] = '\000';
00138 showPass();
00139 }
00140 } else
00141 e->ignore();
00142 break;
00143 }
00144 }
00145
00146 bool KPasswordEdit::event(QEvent *e) {
00147 switch(e->type()) {
00148
00149 case QEvent::MouseButtonPress:
00150 case QEvent::MouseButtonRelease:
00151 case QEvent::MouseButtonDblClick:
00152 case QEvent::MouseMove:
00153 return TRUE;
00154
00155 case QEvent::AccelOverride:
00156 {
00157 QKeyEvent *k = (QKeyEvent*) e;
00158 switch (k->key()) {
00159 case Key_U:
00160 if (k->state() & ControlButton) {
00161 m_Length = 0;
00162 m_Password[m_Length] = '\000';
00163 showPass();
00164 }
00165 }
00166 return TRUE;
00167 }
00168
00169 default:
00170
00171 break;
00172 }
00173 return QLineEdit::event(e);
00174 }
00175
00176 void KPasswordEdit::showPass()
00177 {
00178 QString tmp;
00179
00180 switch (m_EchoMode) {
00181 case OneStar:
00182 tmp.fill('*', m_Length);
00183 setText(tmp);
00184 break;
00185 case ThreeStars:
00186 tmp.fill('*', m_Length*3);
00187 setText(tmp);
00188 break;
00189 case NoEcho: default:
00190 break;
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199 KPasswordDialog::KPasswordDialog(Types type, bool enableKeep, int extraBttn,
00200 QWidget *parent, const char *name)
00201 : KDialogBase(parent, name, true, "", Ok|Cancel|extraBttn,
00202 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type)
00203 {
00204 init();
00205 }
00206
00207
00208 KPasswordDialog::KPasswordDialog(int type, QString prompt, bool enableKeep,
00209 int extraBttn)
00210 : KDialogBase(0L, "Password Dialog", true, "", Ok|Cancel|extraBttn,
00211 Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type)
00212 {
00213 init();
00214 setPrompt(prompt);
00215 }
00216
00217
00218 void KPasswordDialog::init()
00219 {
00220 m_Row = 0;
00221
00222 KConfig *cfg = KGlobal::config();
00223 KConfigGroupSaver saver(cfg, "Passwords");
00224 if (m_Keep && cfg->readBoolEntry("Keep", false))
00225 m_Keep++;
00226
00227 m_pMain = new QWidget(this);
00228 setMainWidget(m_pMain);
00229 m_pGrid = new QGridLayout(m_pMain, 10, 3, 10, 0);
00230 m_pGrid->addColSpacing(1, 10);
00231
00232
00233 QLabel *lbl;
00234 QPixmap pix(locate("data", QString::fromLatin1("kdeui/pics/keys.png")));
00235 if (!pix.isNull()) {
00236 lbl = new QLabel(m_pMain);
00237 lbl->setPixmap(pix);
00238 lbl->setAlignment(AlignLeft|AlignVCenter);
00239 lbl->setFixedSize(lbl->sizeHint());
00240 m_pGrid->addWidget(lbl, 0, 0, AlignLeft);
00241 }
00242
00243 m_pHelpLbl = new QLabel(m_pMain);
00244 m_pHelpLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
00245 m_pGrid->addWidget(m_pHelpLbl, 0, 2, AlignLeft);
00246 m_pGrid->addRowSpacing(1, 10);
00247 m_pGrid->setRowStretch(1, 12);
00248
00249
00250 m_pGrid->addRowSpacing(6, 5);
00251 m_pGrid->setRowStretch(6, 12);
00252
00253
00254 lbl = new QLabel(m_pMain);
00255 lbl->setAlignment(AlignLeft|AlignVCenter);
00256 lbl->setText(i18n("&Password:"));
00257 lbl->setFixedSize(lbl->sizeHint());
00258 m_pGrid->addWidget(lbl, 7, 0, AlignLeft);
00259
00260 QHBoxLayout *h_lay = new QHBoxLayout();
00261 m_pGrid->addLayout(h_lay, 7, 2);
00262 m_pEdit = new KPasswordEdit(m_pMain);
00263 lbl->setBuddy(m_pEdit);
00264 QSize size = m_pEdit->sizeHint();
00265 m_pEdit->setFixedHeight(size.height());
00266 m_pEdit->setMinimumWidth(size.width());
00267 h_lay->addWidget(m_pEdit, 12);
00268 h_lay->addStretch(4);
00269
00270
00271
00272 if ((m_Type == Password) && m_Keep) {
00273 m_pGrid->addRowSpacing(8, 10);
00274 m_pGrid->setRowStretch(8, 12);
00275 QCheckBox *cb = new QCheckBox(i18n("&Keep password"), m_pMain);
00276 cb->setFixedSize(cb->sizeHint());
00277 if (m_Keep > 1)
00278 cb->setChecked(true);
00279 else
00280 m_Keep = 0;
00281 connect(cb, SIGNAL(toggled(bool)), SLOT(slotKeep(bool)));
00282 m_pGrid->addWidget(cb, 9, 2, AlignLeft|AlignVCenter);
00283 } else if (m_Type == NewPassword) {
00284 m_pGrid->addRowSpacing(8, 10);
00285 lbl = new QLabel(m_pMain);
00286 lbl->setAlignment(AlignLeft|AlignVCenter);
00287 lbl->setText(i18n("&Verify:"));
00288 lbl->setFixedSize(lbl->sizeHint());
00289 m_pGrid->addWidget(lbl, 9, 0, AlignLeft);
00290
00291 h_lay = new QHBoxLayout();
00292 m_pGrid->addLayout(h_lay, 9, 2);
00293 m_pEdit2 = new KPasswordEdit(m_pMain);
00294 lbl->setBuddy(m_pEdit2);
00295 size = m_pEdit2->sizeHint();
00296 m_pEdit2->setFixedHeight(size.height());
00297 m_pEdit2->setMinimumWidth(size.width());
00298 h_lay->addWidget(m_pEdit2, 12);
00299 h_lay->addStretch(4);
00300 }
00301
00302 erase();
00303 }
00304
00305
00306 KPasswordDialog::~KPasswordDialog()
00307 {
00308 }
00309
00310
00311 void KPasswordDialog::setPrompt(QString prompt)
00312 {
00313 m_pHelpLbl->setText(prompt);
00314 m_pHelpLbl->setFixedSize(275, m_pHelpLbl->heightForWidth(275));
00315 }
00316
00317
00318 QString KPasswordDialog::prompt() const
00319
00320 {
00321 return m_pHelpLbl->text();
00322 }
00323
00324
00325 void KPasswordDialog::addLine(QString key, QString value)
00326 {
00327 if (m_Row > 3)
00328 return;
00329
00330 QLabel *lbl = new QLabel(key, m_pMain);
00331 lbl->setAlignment(AlignTop);
00332 lbl->setIndent(5);
00333 lbl->setFixedSize(lbl->sizeHint());
00334 m_pGrid->addWidget(lbl, m_Row+2, 0, AlignLeft);
00335
00336 lbl = new QLabel(value, m_pMain);
00337 lbl->setAlignment(AlignTop|WordBreak);
00338 lbl->setIndent(5);
00339 lbl->setFixedSize(275, lbl->heightForWidth(275));
00340 m_pGrid->addWidget(lbl, m_Row+2, 2, AlignLeft);
00341 m_Row++;
00342 }
00343
00344
00345 void KPasswordDialog::erase()
00346 {
00347 m_pEdit->erase();
00348 m_pEdit->setFocus();
00349 if (m_Type == NewPassword)
00350 m_pEdit2->erase();
00351 }
00352
00353
00354 void KPasswordDialog::slotOk()
00355 {
00356 if (m_Type == NewPassword) {
00357 if (strcmp(m_pEdit->password(), m_pEdit2->password())) {
00358 KMessageBox::sorry(this, i18n("You entered two different "
00359 "passwords. Please try again."));
00360 erase();
00361 return;
00362 }
00363 }
00364 if (!checkPassword(m_pEdit->password())) {
00365 erase();
00366 return;
00367 }
00368 accept();
00369 }
00370
00371
00372 void KPasswordDialog::slotCancel()
00373 {
00374 reject();
00375 }
00376
00377
00378 void KPasswordDialog::slotKeep(bool keep)
00379 {
00380 m_Keep = keep;
00381 }
00382
00383
00384
00385 int KPasswordDialog::getPassword(QCString &password, QString prompt,
00386 int *keep)
00387 {
00388 bool enableKeep = keep && *keep;
00389 KPasswordDialog *dlg = new KPasswordDialog(Password, prompt, enableKeep);
00390 int ret = dlg->exec();
00391 if (ret == Accepted) {
00392 password = dlg->password();
00393 if (enableKeep)
00394 *keep = dlg->keep();
00395 }
00396 delete dlg;
00397 return ret;
00398 }
00399
00400
00401
00402 int KPasswordDialog::getNewPassword(QCString &password, QString prompt)
00403 {
00404 KPasswordDialog *dlg = new KPasswordDialog(NewPassword, prompt);
00405 int ret = dlg->exec();
00406 if (ret == Accepted)
00407 password = dlg->password();
00408 delete dlg;
00409 return ret;
00410 }
00411
00412
00413
00414 void KPasswordDialog::disableCoreDumps()
00415 {
00416 struct rlimit rlim;
00417 rlim.rlim_cur = rlim.rlim_max = 0;
00418 setrlimit(RLIMIT_CORE, &rlim);
00419 }
00420
00421 void KPasswordDialog::virtual_hook( int id, void* data )
00422 { KDialogBase::virtual_hook( id, data ); }
00423
00424 #include "kpassdlg.moc"