blob: 54d6e626706c146fdc0c666b846b399ea7fcc6f2 [file] [log] [blame]
Isa Nanic6e4a39a2018-12-04 14:26:02 -05001/**************************************************************************
Sébastien Blin68abac92019-01-02 17:41:31 -05002* Copyright (C) 2019-2019 by Savoir-faire Linux *
Isa Nanic6e4a39a2018-12-04 14:26:02 -05003* Author: Isa Nanic <isa.nanic@savoirfairelinux.com> *
4* *
5* This program 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* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
17**************************************************************************/
18#include <QTimer>
19
20#include "passworddialog.h"
21#include "ui_passworddialog.h"
22
23#include "lrcinstance.h"
24
25PasswordDialog::PasswordDialog(QWidget* parent)
26 :ui(new Ui::PasswordDialog),
27 QDialog(parent)
28{
29 ui->setupUi(this);
30
31 ui->currentPsswdEdit->setEchoMode(QLineEdit::Password);
32 ui->newPsswdEdit->setEchoMode(QLineEdit::Password);
33 ui->confirmNewPsswdEdit->setEchoMode(QLineEdit::Password);
34
35 if (!LRCInstance::getCurrAccConfig().archiveHasPassword) {
36 ui->currentPsswdEdit->hide();
37 } else {
38 ui->currentPsswdEdit->show();
39 }
40
41 connect(ui->currentPsswdEdit, &QLineEdit::textChanged, this, &PasswordDialog::validateNewPsswd);
42 connect(ui->newPsswdEdit, &QLineEdit::textChanged, this, &PasswordDialog::validateNewPsswd);
43 connect(ui->confirmNewPsswdEdit, &QLineEdit::textChanged, this, &PasswordDialog::validateNewPsswd);
44
45 connect(ui->confirmButton, &QPushButton::clicked, [=]() {
46 savePassword();
47 }
48 );
49
50 connect(ui->cancelButton, &QPushButton::clicked, this, &PasswordDialog::closeSlot);
51
52 ui->confirmButton->setEnabled(false);
53}
54
55PasswordDialog::~PasswordDialog()
56{
57 delete ui;
58}
59
60// only sets new password if both new passwords match
61void
62PasswordDialog::validateNewPsswd()
63{
64 ui->newPsswdEdit->setStyleSheet("border: 1px solid red;");
65 ui->confirmNewPsswdEdit->setStyleSheet("border: 1px solid red;");
66
67 if (ui->newPsswdEdit->text() == ui->confirmNewPsswdEdit->text()) {
68 ui->newPsswdEdit->setStyleSheet("border: 1px solid green;");
69 ui->confirmNewPsswdEdit->setStyleSheet("border: 1px solid green;");
70 }
71 freeConfirmButton();
72}
73
74void
75PasswordDialog::freeConfirmButton()
76{
77 if (ui->newPsswdEdit->text() == ui->confirmNewPsswdEdit->text()) {
78 ui->confirmButton->setEnabled(true);
79 }
80 else {
81 ui->confirmButton->setEnabled(false);
82 }
83}
84
85void
86PasswordDialog::savePassword()
87{
88 if (LRCInstance::editableAccountModel()->changeAccountPassword(LRCInstance::getCurrAccId(),
89 ui->currentPsswdEdit->text().toStdString(), ui->newPsswdEdit->text().toStdString())) {
90 ui->userMessageLabel->setText(tr("Password Changed Successfully"));
91
92 auto confProps = LRCInstance::accountModel().getAccountConfig(LRCInstance::getCurrAccId());
93 ui->newPsswdEdit->text().isEmpty() ? confProps.archiveHasPassword = false : confProps.archiveHasPassword = true;
94 LRCInstance::editableAccountModel()->setAccountConfig(LRCInstance::getCurrAccId(), confProps);
95
96 ui->cancelButton->hide();
97 ui->confirmButton->hide();
98 ui->currentPsswdEdit->hide();
99 ui->newPsswdEdit->hide();
100 ui->confirmNewPsswdEdit->hide();
101
102 QTimer::singleShot(900, this, SLOT(closeSlot()));
103 }
104 else {
105 ui->userMessageLabel->setText(tr("Current Password Incorrect"));
106 ui->currentPsswdEdit->setText("");
107 }
108}