1 #include "PasswordDialog.h"
6 #include <QCryptographicHash>
10 PasswordDialog::PasswordDialog( QWidget *aParent, const QString &aPassword, const QString &aText, const QString &aTitle ) :
13 setWindowTitle( aTitle.isNull() ? tr( "Enter password" ) : aTitle );
16 // Store the password hash to iPasswordHash
17 // ( aPassword should be allready encoded )
18 // QCryptographicHash *hash = new QCryptographicHash( QCryptographicHash::Md5 );
19 // hash->addData( aPassword.toUtf8() );
20 // iPasswordHash = hash->result();
22 iPasswordHash = aPassword.toUtf8();
26 .--------title---------.
29 | [***_______________] |
31 '----------------------'
34 QVBoxLayout *layout = new QVBoxLayout;
36 if ( !aText.isNull() )
38 QLabel *text = new QLabel( aText );
39 layout->addWidget( text );
43 iPasswordEdit = new QLineEdit;
44 iPasswordEdit->setEchoMode( QLineEdit::Password );
45 layout->addWidget( iPasswordEdit );
47 QHBoxLayout *buttonLayout = new QHBoxLayout;
48 QPushButton *buttonOK = new QPushButton( tr( "OK" ) );
49 QPushButton *buttonCancel = new QPushButton( tr( "Cancel" ) );
51 buttonLayout->addStretch();
52 buttonLayout->addWidget( buttonOK );
53 buttonLayout->addWidget( buttonCancel );
54 buttonLayout->addStretch();
55 layout->addSpacing( 5 );
56 layout->addLayout( buttonLayout );
58 // Connect the buttons pressed signals to corresponding slots
59 connect( buttonOK, SIGNAL( pressed() ), this, SLOT( okButtonPressed() ) );
60 connect( buttonCancel, SIGNAL( pressed() ), this, SLOT( cancelButtonPressed() ) );
66 PasswordDialog::~PasswordDialog()
70 void PasswordDialog::okButtonPressed()
72 qDebug() << "PasswordDialog::okButtonPressed()";
74 // Get md5 hash from the password entered to the dialog
75 QCryptographicHash *hash = new QCryptographicHash( QCryptographicHash::Md5 );
76 hash->addData( iPasswordEdit->text().toUtf8() );
77 QByteArray userpw = hash->result();
80 // Compare the password hashes and emit corresponding signal tellin if the password was correct
81 if ( iPasswordHash == userpw.toHex() )
83 emit passwordEntered( PasswordDialog::Correct );
84 qDebug() << "Password OK";
88 emit passwordEntered( PasswordDialog::Incorrect );
89 qDebug() << "Incorrect password!";
96 void PasswordDialog::cancelButtonPressed()
98 qDebug() << "PasswordDialog::cancelButtonPressed()";
100 emit passwordEntered( PasswordDialog::Canceled );
105 PasswordDialog * PasswordDialog::query( QWidget *aParent, const QString &aPassword, const QString &aText, const QString &aTitle )
107 // Create a PasswordDialog instance and show it
108 PasswordDialog* dlg = new PasswordDialog( aParent, aPassword, aText, aTitle );