Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'KPasswordEdit (./kdelibs/kdeui/kpassdlg.h:40)

class KPasswordEdit
    : public QLineEdit
{
    Q_OBJECT

public:
    KPasswordEdit(QWidget *parent=0, const char *name=0);
    ~KPasswordEdit();

    /** 
     * Returns the password. The memory is freed in the destructor
     * so you should make a copy.
     */
    const char *password() { return m_Password; }

    /** Erase the current password. */
    void erase();

    static const int PassLen = 100;
    enum EchoModes { OneStar, ThreeStars, NoEcho };

protected:
    virtual void keyPressEvent(QKeyEvent *);

private:
    void showPass();

    char *m_Password;
    int m_EchoMode, m_Length;
};


/**
 * This dialog asks the user to enter a password. The functions you're 
 * probably interested in are @ref #getPassword and @ref #getNewPassword.
 *
 * @sect Usage example
 *
 * <pre>
 * QCString password;
 * int result = KPasswordDialog::getPassword(password, i18n("Password"));
 * if (result == KPasswordDialog::Accepted)
 *     use(password);
 * </pre>
 *
 * @sect Security notes
 *
 * Keeping passwords in memory can be a potential security hole. You should
 * handle this situation with care.
 *
 * @li You may want to use @ref #disableCoreDump to disable core dumps. 
 * Core dumps are dangerous because they are an image of the process memory, 
 * and thus include any passwords that were in memory.
 *
 * @li You should delete passwords as soon as they are not needed anymore.
 * The functions @ref #getPassword and @ref #getNewPassword return the 
 * password as a QCString. I believe this is safer than a QString. A QString 
 * stores its characters internally as 16-bit wide values, so conversions are 
 * needed, both for creating the QString and by using it. The temporary 
 * memory used for these conversion is probably not erased. This could lead 
 * to stray  passwords in memory, even if you think you erased all of them.
 *
 * @short A password input dialog.
 * @author Geert Jansen <jansen@kde.org>
 */


kdelibs'KPasswordEdit::KPasswordEdit() (./kdelibs/kdeui/kpassdlg.cpp:50)

KPasswordEdit::KPasswordEdit(QWidget *parent, const char *name)
    : QLineEdit(parent, name), m_EchoMode(OneStar)
{
    m_Password = new char[PassLen];
    m_Password[0] = '\000';
    m_Length = 0;

    KConfig *cfg = KGlobal::config();
    KConfigGroupSaver(cfg, "Passwords");

    QString val = cfg->readEntry("EchoMode", "OneStar");
    if (val == "ThreeStars")
	m_EchoMode = ThreeStars;
    else if (val == "NoEcho")
	m_EchoMode = NoEcho;
    else
	m_EchoMode = OneStar;
}



kdelibs'KPasswordEdit::~KPasswordEdit() (./kdelibs/kdeui/kpassdlg.cpp:70)

KPasswordEdit::~KPasswordEdit()
{
    for (int i=0; i<PassLen; i++)
	m_Password[i] = '\000';
    delete[] m_Password;
}



kdelibs'KPasswordEdit::erase() (./kdelibs/kdeui/kpassdlg.cpp:78)

void KPasswordEdit::erase()
{
    m_Length = 0;
    for (int i=0; i<PassLen; i++)
	m_Password[i] = '\000';
    setText("");
}



kdelibs'KPasswordEdit::keyPressEvent() (./kdelibs/kdeui/kpassdlg.cpp:87)

void KPasswordEdit::keyPressEvent(QKeyEvent *e)
{    
    switch (e->key()) {
    case Key_Return:
    case Key_Escape:
	e->ignore();
	break;
    case Key_Backspace:
	if (m_Length) {
	    m_Password[--m_Length] = '\000';
	    showPass();
	}
	break;
    default:
	if ((m_Length < PassLen) && e->ascii()) {
	    m_Password[m_Length] = (char) e->ascii();
	    m_Password[++m_Length] = '\000';
	    showPass();
	}
	break;
    }
}


kdelibs'KPasswordEdit::showPass() (./kdelibs/kdeui/kpassdlg.cpp:110)

void KPasswordEdit::showPass()
{    
    QString tmp;

    switch (m_EchoMode) {
    case OneStar:
	tmp.fill('*', m_Length);
	setText(tmp);
	break;
    case ThreeStars:
	tmp.fill('*', m_Length*3);
	setText(tmp);
	break;
    case NoEcho: default:
	break;
    }
}


/*
 * Password dialog.
 */