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