Enable kinetic scrolling.
[secretnotes] / src / passworddialog.cpp
1 /*
2  *  This file is part of Secret Notes.
3  *  Copyright (C) 2010 Janusz Sobczak
4  *
5  *  Secret Notes is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  Secret Notes is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with Secret Notes.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <qmessagebox.h>
19 #include "passworddialog.h"
20 #include "ui_passworddialog.h"
21 #include <QPushButton>
22
23
24 PasswordDialog::PasswordDialog(QWidget *parent, const char *password) :
25     QDialog(parent),
26     ui(new Ui::PasswordDialog),
27     oldPassword(password),
28     verifyOldPassword(true)
29 {    
30     ui->setupUi(this);
31     enableOKButton(false);
32 }
33
34 PasswordDialog::~PasswordDialog()
35 {
36     oldPassword.fill(0);
37     delete ui;
38 }
39
40 void PasswordDialog::changeEvent(QEvent *e)
41 {
42     QDialog::changeEvent(e);
43     switch (e->type()) {
44     case QEvent::LanguageChange:
45         ui->retranslateUi(this);
46         break;
47     default:
48         break;
49     }
50 }
51
52 void PasswordDialog::hideOldPassword(bool hide)
53 {
54     ui->oldEdit->setVisible(!hide);
55     ui->oldLabel->setVisible(!hide);
56     verifyOldPassword = !hide;
57 }
58
59 QString PasswordDialog::getPassword()
60 {
61     return ui->newEdit->text();
62 }
63
64 void PasswordDialog::on_oldEdit_textChanged(QString text)
65 {
66     isOldPasswordCorrect = (text == oldPassword);
67     enableOKButton((!verifyOldPassword || isOldPasswordCorrect) &&
68                    isNewPasswordCorrect);
69 }
70
71 void PasswordDialog::on_newEdit_textChanged(QString text)
72 {
73     isNewPasswordCorrect = (text.length() > 0 &&
74                             text == ui->newAgainEdit->text());
75     enableOKButton((!verifyOldPassword || isOldPasswordCorrect) &&
76                    isNewPasswordCorrect);
77 }
78
79
80 void PasswordDialog::on_newAgainEdit_textChanged(QString text)
81 {
82     isNewPasswordCorrect = (text.length() > 0 &&
83                             text == ui->newEdit->text());
84     enableOKButton((!verifyOldPassword || isOldPasswordCorrect) &&
85                    isNewPasswordCorrect);
86 }
87
88 void PasswordDialog::enableOKButton(bool ena)
89 {
90     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ena);
91 }