# kdeui Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kpassdlg.cpp

    00001 // vi: ts=8 sts=4 sw=4
    00002 /* This file is part of the KDE libraries
    00003    Copyright (C) 1998 Pietro Iglio <iglio@fub.it>
    00004    Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
    00005
    00006    This library is free software; you can redistribute it and/or
    00007    modify it under the terms of the GNU Library General Public
    00008    License version 2 as published by the Free Software Foundation.
    00009
    00010    This library is distributed in the hope that it will be useful,
    00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013    Library General Public License for more details.
    00014
    00015    You should have received a copy of the GNU Library General Public License
    00016    along with this library; see the file COPYING.LIB.  If not, write to
    00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018    Boston, MA 02111-1307, USA.
    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  * Password line editor.
    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     setEchoMode(QLineEdit::Password); // Just in case
    00083     setAcceptDrops(false);
    00084     m_Password = new char[PassLen];
    00085     m_Password[0] = '\000';
    00086     m_Length = 0;
    00087 }
    00088
    00089 KPasswordEdit::~KPasswordEdit()
    00090 {
    00091     for (int i=0; i<PassLen; i++)
    00092         m_Password[i] = '\000';
    00093     delete[] m_Password;
    00094 }
    00095
    00096 void KPasswordEdit::insert(const QString &txt)
    00097 {
    00098     QCString localTxt = txt.local8Bit();
    00099     for(unsigned int i=0; i < localTxt.length(); i++)
    00100     {
    00101         unsigned char ke = localTxt[i];
    00102         if (m_Length < (PassLen - 1))
    00103         {
    00104             m_Password[m_Length] = ke;
    00105             m_Password[++m_Length] = '\000';
    00106         }
    00107     }
    00108     showPass();
    00109 }
    00110
    00111 void KPasswordEdit::erase()
    00112 {
    00113     m_Length = 0;
    00114     for (int i=0; i<PassLen; i++)
    00115         m_Password[i] = '\000';
    00116     setText("");
    00117 }
    00118
    00119 void KPasswordEdit::focusInEvent(QFocusEvent *e)
    00120 {
    00121     QString txt = text();
    00122     setUpdatesEnabled(false);
    00123     QLineEdit::focusInEvent(e);
    00124     setUpdatesEnabled(true);
    00125     setText(txt);
    00126 }
    00127
    00128
    00129 void KPasswordEdit::keyPressEvent(QKeyEvent *e)
    00130 {
    00131     switch (e->key()) {
    00132     case Key_Return:
    00133     case Key_Enter:
    00134     case Key_Escape:
    00135         e->ignore();
    00136         break;
    00137     case Key_Backspace:
    00138     case Key_Delete:
    00139     case 0x7f: // Delete
    00140         if (e->state() & (ControlButton | AltButton))
    00141             e->ignore();
    00142         else if (m_Length) {
    00143             m_Password[--m_Length] = '\000';
    00144             showPass();
    00145         }
    00146         break;
    00147     default:
    00148         unsigned char ke = e->text().local8Bit()[0];
    00149         if (ke >= 32) {
    00150             insert(e->text());
    00151         } else
    00152             e->ignore();
    00153         break;
    00154     }
    00155 }
    00156
    00157 bool KPasswordEdit::event(QEvent *e) {
    00158     switch(e->type()) {
    00159
    00160       case QEvent::MouseButtonPress:
    00161       case QEvent::MouseButtonRelease:
    00162       case QEvent::MouseButtonDblClick:
    00163       case QEvent::MouseMove:
    00164       case QEvent::IMStart:
    00165       case QEvent::IMCompose:
    00166         return TRUE; //Ignore
    00167
    00168       case QEvent::IMEnd:
    00169       {
    00170         QIMEvent *ie = (QIMEvent*) e;
    00171         insert( ie->text() );
    00172         return TRUE;
    00173       }
    00174
    00175       case QEvent::AccelOverride:
    00176       {
    00177         QKeyEvent *k = (QKeyEvent*) e;
    00178         switch (k->key()) {
    00179             case Key_U:
    00180                 if (k->state() & ControlButton) {
    00181                     m_Length = 0;
    00182                     m_Password[m_Length] = '\000';
    00183                     showPass();
    00184                 }
    00185         }
    00186         return TRUE; // stop bubbling
    00187       }
    00188
    00189       default:
    00190         // Do nothing
    00191         break;
    00192     }
    00193     return QLineEdit::event(e);
    00194 }
    00195
    00196 void KPasswordEdit::showPass()
    00197 {
    00198     QString tmp;
    00199
    00200     switch (m_EchoMode) {
    00201     case OneStar:
    00202         tmp.fill('*', m_Length);
    00203         setText(tmp);
    00204         break;
    00205     case ThreeStars:
    00206         tmp.fill('*', m_Length*3);
    00207         setText(tmp);
    00208         break;
    00209     case NoEcho: default:
    00210         break;
    00211     }
    00212 }
    00213
    00214
    00215 /*
    00216  * Password dialog.
    00217  */
    00218
    00219 KPasswordDialog::KPasswordDialog(Types type, bool enableKeep, int extraBttn,
    00220                                  QWidget *parent, const char *name)
    00221     : KDialogBase(parent, name, true, "", Ok|Cancel|extraBttn,
    00222                   Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type)
    00223 {
    00224     init();
    00225 }
    00226
    00227
    00228 KPasswordDialog::KPasswordDialog(int type, QString prompt, bool enableKeep,
    00229                                  int extraBttn)
    00230     : KDialogBase(0L, "Password Dialog", true, "", Ok|Cancel|extraBttn,
    00231                   Ok, true), m_Keep(enableKeep? 1 : 0), m_Type(type)
    00232 {
    00233     init();
    00234     setPrompt(prompt);
    00235 }
    00236
    00237
    00238 void KPasswordDialog::init()
    00239 {
    00240     m_Row = 0;
    00241
    00242     KConfig *cfg = KGlobal::config();
    00243     KConfigGroupSaver saver(cfg, "Passwords");
    00244     if (m_Keep && cfg->readBoolEntry("Keep", false))
    00245         m_Keep++;
    00246
    00247     m_pMain = new QWidget(this);
    00248     setMainWidget(m_pMain);
    00249     m_pGrid = new QGridLayout(m_pMain, 10, 3, 10, 0);
    00250     m_pGrid->addColSpacing(1, 10);
    00251
    00252     // Row 1: pixmap + prompt
    00253     QLabel *lbl;
    00254     QPixmap pix(locate("data", QString::fromLatin1("kdeui/pics/keys.png")));
    00255     if (!pix.isNull()) {
    00256         lbl = new QLabel(m_pMain);
    00257         lbl->setPixmap(pix);
    00258         lbl->setAlignment(AlignLeft|AlignVCenter);
    00259         lbl->setFixedSize(lbl->sizeHint());
    00260         m_pGrid->addWidget(lbl, 0, 0, AlignLeft);
    00261     }
    00262
    00263     m_pHelpLbl = new QLabel(m_pMain);
    00264     m_pHelpLbl->setAlignment(AlignLeft|AlignVCenter|WordBreak);
    00265     m_pGrid->addWidget(m_pHelpLbl, 0, 2, AlignLeft);
    00266     m_pGrid->addRowSpacing(1, 10);
    00267     m_pGrid->setRowStretch(1, 12);
    00268
    00269     // Row 2+: space for 4 extra info lines
    00270     m_pGrid->addRowSpacing(6, 5);
    00271     m_pGrid->setRowStretch(6, 12);
    00272
    00273     // Row 3: Password editor #1
    00274     lbl = new QLabel(m_pMain);
    00275     lbl->setAlignment(AlignLeft|AlignVCenter);
    00276     lbl->setText(i18n("&Password:"));
    00277     lbl->setFixedSize(lbl->sizeHint());
    00278     m_pGrid->addWidget(lbl, 7, 0, AlignLeft);
    00279
    00280     QHBoxLayout *h_lay = new QHBoxLayout();
    00281     m_pGrid->addLayout(h_lay, 7, 2);
    00282     m_pEdit = new KPasswordEdit(m_pMain);
    00283     lbl->setBuddy(m_pEdit);
    00284     QSize size = m_pEdit->sizeHint();
    00285     m_pEdit->setFixedHeight(size.height());
    00286     m_pEdit->setMinimumWidth(size.width());
    00287     h_lay->addWidget(m_pEdit, 12);
    00288     h_lay->addStretch(4);
    00289
    00290     // Row 4: Password editor #2 or keep password checkbox
    00291
    00292     if ((m_Type == Password) && m_Keep) {
    00293         m_pGrid->addRowSpacing(8, 10);
    00294         m_pGrid->setRowStretch(8, 12);
    00295         QCheckBox *cb = new QCheckBox(i18n("&Keep password"), m_pMain);
    00296         cb->setFixedSize(cb->sizeHint());
    00297         if (m_Keep > 1)
    00298             cb->setChecked(true);
    00299         else
    00300             m_Keep = 0;
    00301         connect(cb, SIGNAL(toggled(bool)), SLOT(slotKeep(bool)));
    00302         m_pGrid->addWidget(cb, 9, 2, AlignLeft|AlignVCenter);
    00303     } else if (m_Type == NewPassword) {
    00304         m_pGrid->addRowSpacing(8, 10);
    00305         lbl = new QLabel(m_pMain);
    00306         lbl->setAlignment(AlignLeft|AlignVCenter);
    00307         lbl->setText(i18n("&Verify:"));
    00308         lbl->setFixedSize(lbl->sizeHint());
    00309         m_pGrid->addWidget(lbl, 9, 0, AlignLeft);
    00310
    00311         h_lay = new QHBoxLayout();
    00312         m_pGrid->addLayout(h_lay, 9, 2);
    00313         m_pEdit2 = new KPasswordEdit(m_pMain);
    00314         lbl->setBuddy(m_pEdit2);
    00315         size = m_pEdit2->sizeHint();
    00316         m_pEdit2->setFixedHeight(size.height());
    00317         m_pEdit2->setMinimumWidth(size.width());
    00318         h_lay->addWidget(m_pEdit2, 12);
    00319         h_lay->addStretch(4);
    00320     }
    00321
    00322     erase();
    00323 }
    00324
    00325
    00326 KPasswordDialog::~KPasswordDialog()
    00327 {
    00328 }
    00329
    00330
    00331 void KPasswordDialog::setPrompt(QString prompt)
    00332 {
    00333     m_pHelpLbl->setText(prompt);
    00334     m_pHelpLbl->setFixedSize(275, m_pHelpLbl->heightForWidth(275));
    00335 }
    00336
    00337
    00338 QString KPasswordDialog::prompt() const
    00339
    00340 {
    00341     return m_pHelpLbl->text();
    00342 }
    00343
    00344
    00345 void KPasswordDialog::addLine(QString key, QString value)
    00346 {
    00347     if (m_Row > 3)
    00348         return;
    00349
    00350     QLabel *lbl = new QLabel(key, m_pMain);
    00351     lbl->setAlignment(AlignTop);
    00352     lbl->setIndent(5);
    00353     lbl->setFixedSize(lbl->sizeHint());
    00354     m_pGrid->addWidget(lbl, m_Row+2, 0, AlignLeft);
    00355
    00356     lbl = new QLabel(value, m_pMain);
    00357     lbl->setAlignment(AlignTop|WordBreak);
    00358     lbl->setIndent(5);
    00359     lbl->setFixedSize(275, lbl->heightForWidth(275));
    00360     m_pGrid->addWidget(lbl, m_Row+2, 2, AlignLeft);
    00361     m_Row++;
    00362 }
    00363
    00364
    00365 void KPasswordDialog::erase()
    00366 {
    00367     m_pEdit->erase();
    00368     m_pEdit->setFocus();
    00369     if (m_Type == NewPassword)
    00370         m_pEdit2->erase();
    00371 }
    00372
    00373
    00374 void KPasswordDialog::slotOk()
    00375 {
    00376     if (m_Type == NewPassword) {
    00377         if (strcmp(m_pEdit->password(), m_pEdit2->password())) {
    00378             KMessageBox::sorry(this, i18n("You entered two different "
    00379                     "passwords. Please try again."));
    00380             erase();
    00381             return;
    00382         }
    00383     }
    00384     if (!checkPassword(m_pEdit->password())) {
    00385         erase();
    00386         return;
    00387     }
    00388     accept();
    00389 }
    00390
    00391
    00392 void KPasswordDialog::slotCancel()
    00393 {
    00394     reject();
    00395 }
    00396
    00397
    00398 void KPasswordDialog::slotKeep(bool keep)
    00399 {
    00400     m_Keep = keep;
    00401 }
    00402
    00403
    00404 // static
    00405 int KPasswordDialog::getPassword(QCString &password, QString prompt,
    00406         int *keep)
    00407 {
    00408     bool enableKeep = keep && *keep;
    00409     KPasswordDialog *dlg = new KPasswordDialog(Password, prompt, enableKeep);
    00410     int ret = dlg->exec();
    00411     if (ret == Accepted) {
    00412         password = dlg->password();
    00413         if (enableKeep)
    00414             *keep = dlg->keep();
    00415     }
    00416     delete dlg;
    00417     return ret;
    00418 }
    00419
    00420
    00421 // static
    00422 int KPasswordDialog::getNewPassword(QCString &password, QString prompt)
    00423 {
    00424     KPasswordDialog *dlg = new KPasswordDialog(NewPassword, prompt);
    00425     int ret = dlg->exec();
    00426     if (ret == Accepted)
    00427         password = dlg->password();
    00428     delete dlg;
    00429     return ret;
    00430 }
    00431
    00432
    00433 // static
    00434 void KPasswordDialog::disableCoreDumps()
    00435 {
    00436     struct rlimit rlim;
    00437     rlim.rlim_cur = rlim.rlim_max = 0;
    00438     setrlimit(RLIMIT_CORE, &rlim);
    00439 }
    00440
    00441 void KPasswordDialog::virtual_hook( int id, void* data )
    00442 { KDialogBase::virtual_hook( id, data ); }
    00443
    00444 #include "kpassdlg.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:57:30 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001