qtmeetings sources to Maemo garage
[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( QWidget *aParent, const QString &aPassword, const QString &aText, const QString &aTitle ) :
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
66 PasswordDialog::~PasswordDialog()
67 {
68 }
69
70 void PasswordDialog::okButtonPressed()
71 {
72         qDebug() << "PasswordDialog::okButtonPressed()";
73
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();
78         delete hash; 
79         
80         // Compare the password hashes and emit corresponding signal tellin if the password was correct
81         if ( iPasswordHash == userpw.toHex() )
82         {
83                 emit passwordEntered( PasswordDialog::Correct );
84                 qDebug() << "Password OK";
85         }
86         else
87         {
88                 emit passwordEntered( PasswordDialog::Incorrect );
89                 qDebug() << "Incorrect password!";
90         }
91
92         // Close the dialog
93         close();
94 }
95
96 void PasswordDialog::cancelButtonPressed()
97 {
98         qDebug() << "PasswordDialog::cancelButtonPressed()";
99
100         emit passwordEntered( PasswordDialog::Canceled );
101
102         close();
103 }
104
105 PasswordDialog * PasswordDialog::query( QWidget *aParent, const QString &aPassword, const QString &aText, const QString &aTitle )
106 {
107         // Create a PasswordDialog instance and show it
108         PasswordDialog* dlg = new PasswordDialog( aParent, aPassword, aText, aTitle );
109         dlg->show();
110         return dlg;
111 }
112