File: passdialog.cpp

package info (click to toggle)
portabase 2.1%2Bgit20120910-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 8,648 kB
  • ctags: 3,498
  • sloc: cpp: 32,214; sh: 2,868; ansic: 2,320; makefile: 481; python: 121; xml: 23; asm: 10
file content (112 lines) | stat: -rw-r--r-- 3,873 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * passdialog.cpp
 *
 * (c) 2003-2004,2008-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/** @file passdialog.cpp
 * Source file for PasswordDialog
 */

#include <QApplication>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include "database.h"
#include "factory.h"
#include "passdialog.h"
#include "encryption/crypto.h"

/**
 * Constructor.
 *
 * @param dbase The database being created, opened, or modified
 * @param dlgMode The mode in which this dialog will be used
 * @param parent This dialog's parent widget
 */
PasswordDialog::PasswordDialog(Database *dbase, DialogMode dlgMode, QWidget *parent)
  : PBDialog(tr("Password"), parent, true), db(dbase), mode(dlgMode)
{
    QGridLayout *grid = Factory::gridLayout(vbox);
    int currentRow = 0;
    if (mode == ChangePassword) {
        grid->addWidget(new QLabel(tr("Old password") + ":", this), 0, 0);
        oldPass = new QLineEdit(this);
        oldPass->setEchoMode(QLineEdit::Password);
        grid->addWidget(oldPass, 0, 1);
        currentRow++;
    }
    QString label((mode == ChangePassword) ? tr("New password") : tr("Password"));
    grid->addWidget(new QLabel(label + ":", this), currentRow, 0);
    pass = new QLineEdit(this);
    pass->setEchoMode(QLineEdit::Password);
    grid->addWidget(pass, currentRow, 1);
    currentRow++;
    if (mode != OpenFile) {
        label = (mode == NewPassword) ? tr("Repeat password") : tr("Repeat new password");
        grid->addWidget(new QLabel(label + ":", this), currentRow, 0);
        repeatPass = new QLineEdit(this);
        repeatPass->setEchoMode(QLineEdit::Password);
        grid->addWidget(repeatPass, currentRow, 1);
        currentRow++;
    }
    finishLayout();
}

/**
 * Validate the entered password(s).  May fail if the incorrect password for
 * an existing file is entered, if two newly-entered passwords don't match,
 * if a newly-specified password isn't deemed suitable, etc.
 *
 * @return True if the password(s) have been accepted, false otherwise
 */
bool PasswordDialog::validate()
{
    QString password = pass->text();
    if (mode == OpenFile) {
        QString error = db->encryption()->setPassword(password, false);
        if (!error.isEmpty()) {
            QMessageBox::warning(this, qApp->applicationName(), error);
            return false;
        }
        error = db->load();
        if (!error.isEmpty()) {
            QMessageBox::warning(this, qApp->applicationName(), error);
            return false;
        }
    }
    else if (mode == NewPassword) {
        QString repeatPassword = repeatPass->text();
        if (password != repeatPassword) {
            QMessageBox::warning(this, qApp->applicationName(),
                                 tr("Repeated password doesn't match"));
            return false;
        }
        QString error = db->encryption()->setPassword(password, true);
        if (!error.isEmpty()) {
            QMessageBox::warning(this, qApp->applicationName(), error);
            return false;
        }
    }
    else {
        QString oldPassword = oldPass->text();
        QString repeatPassword = repeatPass->text();
        if (password != repeatPassword) {
            QMessageBox::warning(this, qApp->applicationName(),
                                 tr("Repeated password doesn't match"));
            return false;
        }
        QString error = db->encryption()->changePassword(oldPassword, password);
        if (!error.isEmpty()) {
            QMessageBox::warning(this, qApp->applicationName(), error);
            return false;
        }
    }
    return true;
}