Moved the logicasl part of the progresbar/password dialog from WindowManager to Engin...
[qtmeetings] / src / UserInterface / Utils / PasswordDialog.cpp
1 #include "PasswordDialog.h"
2 #include <QLineEdit>
3 #include <QPushButton>
4 #include <QVBoxLayout>
5 #include <QByteArray>
6 #include <QCryptographicHash>
7 #include <QtDebug>
8 #include <QLabel>
9
10 PasswordDialog::PasswordDialog( const QString &aPassword, const QString &aText, const QString &aTitle, QWidget *aParent ) :
11                 QDialog( aParent )
12 {
13         setWindowTitle( aTitle.isNull() ? tr( "Enter password" ) : aTitle );
14         setModal( true );
15
16         iPasswordHash = aPassword.toUtf8();
17
18         /* Create the layout:
19
20         .--------title---------.
21         | text                 |
22         |                      |
23         | [***_______________] |
24         | [Cancel]      [ OK ] |
25         '----------------------'
26         */
27
28         QVBoxLayout *layout = new QVBoxLayout;
29
30         if ( !aText.isNull() )
31         {
32                 QLabel *text = new QLabel( aText );
33                 layout->addWidget( text );
34                 layout->addStretch();
35         }
36
37         iPasswordEdit = new QLineEdit;
38         iPasswordEdit->setEchoMode( QLineEdit::Password );
39         layout->addWidget( iPasswordEdit );
40
41         QHBoxLayout *buttonLayout = new QHBoxLayout;
42         QPushButton *buttonOK = new QPushButton( tr( "OK" ) );
43         QPushButton *buttonCancel = new QPushButton( tr( "Cancel" ) );
44         
45         buttonLayout->addStretch();
46         buttonLayout->addWidget( buttonOK );
47         buttonLayout->addWidget( buttonCancel );
48         buttonLayout->addStretch();
49         layout->addSpacing( 5 );
50         layout->addLayout( buttonLayout );
51
52         // Connect the buttons pressed signals to corresponding slots
53         connect( buttonOK, SIGNAL( pressed() ), this, SLOT( okButtonPressed() ) );
54         connect( buttonCancel, SIGNAL( pressed() ), this, SLOT( cancelButtonPressed() ) );
55
56         // Enable the layout
57         setLayout( layout );
58 }
59
60 PasswordDialog::~PasswordDialog()
61 {
62 }
63
64 void PasswordDialog::okButtonPressed()
65 {
66         qDebug() << "PasswordDialog::okButtonPressed()";
67
68         // Get md5 hash from the password entered to the dialog
69         QCryptographicHash *hash = new QCryptographicHash( QCryptographicHash::Md5 );
70         hash->addData( iPasswordEdit->text().toUtf8() );
71         QByteArray userpw = hash->result();
72         delete hash;
73         
74         close();
75                 
76         // Compare the password hashes and emit corresponding signal tellin if the password was correct
77         if ( iPasswordHash == userpw.toHex() )
78         {
79                 emit passwordEntered( PasswordDialog::Correct );
80                 qDebug() << "Password OK";
81         }
82         else
83         {
84                 emit passwordEntered( PasswordDialog::Incorrect );
85                 qDebug() << "Incorrect password!";
86         }
87 }
88
89 void PasswordDialog::cancelButtonPressed()
90 {
91         qDebug() << "PasswordDialog::cancelButtonPressed()";
92         
93         close();
94         emit passwordEntered( PasswordDialog::Canceled );
95 }
96