Merge branch 'master' of https://git.maemo.org/projects/qtmeetings
[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         iText = new QLabel( aText );
32         layout->addWidget( iText );
33         layout->addStretch();
34
35         iPasswordEdit = new QLineEdit;
36         iPasswordEdit->setEchoMode( QLineEdit::Password );
37         layout->addWidget( iPasswordEdit );
38
39         QHBoxLayout *buttonLayout = new QHBoxLayout;
40         QPushButton *buttonOK = new QPushButton( tr( "OK" ) );
41         QPushButton *buttonCancel = new QPushButton( tr( "Cancel" ) );
42         
43         buttonLayout->addStretch();
44         buttonLayout->addWidget( buttonOK );
45         buttonLayout->addWidget( buttonCancel );
46         buttonLayout->addStretch();
47         layout->addSpacing( 5 );
48         layout->addLayout( buttonLayout );
49
50         // Connect the user action signals to corresponding slots
51         connect( buttonOK, SIGNAL( released() ), this, SLOT( okButtonPressed() ) );
52         connect( buttonCancel, SIGNAL( released() ), this, SLOT( cancelButtonPressed() ) );
53
54         // Enable the layout
55         setLayout( layout );
56 }
57
58 PasswordDialog::~PasswordDialog()
59 {
60 }
61
62 void PasswordDialog::okButtonPressed()
63 {
64         qDebug() << "PasswordDialog::okButtonPressed()";
65
66         // Get md5 hash from the password entered to the dialog
67         QCryptographicHash *hash = new QCryptographicHash( QCryptographicHash::Md5 );
68         hash->addData( iPasswordEdit->text().toUtf8() );
69         QByteArray userpw = hash->result();
70         delete hash;
71         
72         close();
73                 
74         // Compare the password hashes and emit corresponding signal tellin if the password was correct
75         if ( iPasswordHash == userpw.toHex() )
76         {
77                 emit passwordEntered( PasswordDialog::Correct );
78                 qDebug() << "Password OK";
79         }
80         else
81         {
82                 emit passwordEntered( PasswordDialog::Incorrect );
83                 qDebug() << "Incorrect password!";
84         }
85 }
86
87 void PasswordDialog::cancelButtonPressed()
88 {
89         qDebug() << "PasswordDialog::cancelButtonPressed()";
90         
91         close();
92         emit passwordEntered( PasswordDialog::Canceled );
93 }
94
95 void PasswordDialog::update( const QString &aText )
96 {
97         qDebug() << "PasswordDialog::update()";
98         iText->setText( aText );
99 }