3a8590fd65fc6b728945610d3ca27cbcbdf26ddc
[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         // 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();
21 //      delete hash;
22         iPasswordHash = aPassword.toUtf8();
23
24         /* Create the layout:
25
26         .--------title---------.
27         | text                 |
28         |                      |
29         | [***_______________] |
30         | [Cancel]      [ OK ] |
31         '----------------------'
32         */
33
34         QVBoxLayout *layout = new QVBoxLayout;
35
36         if ( !aText.isNull() )
37         {
38                 QLabel *text = new QLabel( aText );
39                 layout->addWidget( text );
40                 layout->addStretch();
41         }
42
43         iPasswordEdit = new QLineEdit;
44         iPasswordEdit->setEchoMode( QLineEdit::Password );
45         layout->addWidget( iPasswordEdit );
46
47         QHBoxLayout *buttonLayout = new QHBoxLayout;
48         QPushButton *buttonOK = new QPushButton( tr( "OK" ) );
49         QPushButton *buttonCancel = new QPushButton( tr( "Cancel" ) );
50         
51         buttonLayout->addStretch();
52         buttonLayout->addWidget( buttonOK );
53         buttonLayout->addWidget( buttonCancel );
54         buttonLayout->addStretch();
55         layout->addSpacing( 5 );
56         layout->addLayout( buttonLayout );
57
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() ) );
61
62         // Enable the layout
63         setLayout( layout );
64         
65         show();
66 }
67
68 PasswordDialog::~PasswordDialog()
69 {
70         close();
71 }
72
73 void PasswordDialog::okButtonPressed()
74 {
75         qDebug() << "PasswordDialog::okButtonPressed()";
76
77         // Get md5 hash from the password entered to the dialog
78         QCryptographicHash *hash = new QCryptographicHash( QCryptographicHash::Md5 );
79         hash->addData( iPasswordEdit->text().toUtf8() );
80         QByteArray userpw = hash->result();
81         delete hash;
82         
83         close();
84                 
85         // Compare the password hashes and emit corresponding signal tellin if the password was correct
86         if ( iPasswordHash == userpw.toHex() )
87         {
88                 emit passwordEntered( PasswordDialog::Correct );
89                 qDebug() << "Password OK";
90         }
91         else
92         {
93                 emit passwordEntered( PasswordDialog::Incorrect );
94                 qDebug() << "Incorrect password!";
95         }
96 }
97
98 void PasswordDialog::cancelButtonPressed()
99 {
100         qDebug() << "PasswordDialog::cancelButtonPressed()";
101         
102         close();
103         emit passwordEntered( PasswordDialog::Canceled );
104 }
105