keepassxc
132 строки · 3.9 Кб
1/*
2* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3*
4* This program is free software: you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation, either version 2 or (at your option)
7* version 3 of the License.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "PasswordEditWidget.h"19#include "ui_KeyComponentWidget.h"20#include "ui_PasswordEditWidget.h"21
22#include "keys/CompositeKey.h"23#include "keys/PasswordKey.h"24
25PasswordEditWidget::PasswordEditWidget(QWidget* parent)26: KeyComponentWidget(parent)27, m_compUi(new Ui::PasswordEditWidget())28{
29initComponent();30}
31
32PasswordEditWidget::~PasswordEditWidget() = default;33
34bool PasswordEditWidget::addToCompositeKey(QSharedPointer<CompositeKey> key)35{
36QString pw = m_compUi->enterPasswordEdit->text();37if (!pw.isEmpty()) {38key->addKey(QSharedPointer<PasswordKey>::create(pw));39return true;40}41return false;42}
43
44/**
45* @param visible changed password visibility state
46*/
47void PasswordEditWidget::setPasswordVisible(bool visible)48{
49m_compUi->enterPasswordEdit->setShowPassword(visible);50}
51
52/**
53* @return password visibility state
54*/
55bool PasswordEditWidget::isPasswordVisible() const56{
57return m_compUi->enterPasswordEdit->isPasswordVisible();58}
59
60bool PasswordEditWidget::isEmpty() const61{
62return (visiblePage() == Page::Edit) && m_compUi->enterPasswordEdit->text().isEmpty();63}
64
65PasswordHealth::Quality PasswordEditWidget::getPasswordQuality() const66{
67QString pwd = m_compUi->enterPasswordEdit->text();68PasswordHealth passwordHealth(pwd);69
70return passwordHealth.quality();71}
72
73QWidget* PasswordEditWidget::componentEditWidget()74{
75m_compEditWidget = new QWidget();76m_compUi->setupUi(m_compEditWidget);77m_compUi->enterPasswordEdit->enablePasswordGenerator();78m_compUi->enterPasswordEdit->setRepeatPartner(m_compUi->repeatPasswordEdit);79return m_compEditWidget;80}
81
82void PasswordEditWidget::initComponentEditWidget(QWidget* widget)83{
84Q_UNUSED(widget);85Q_ASSERT(m_compEditWidget);86m_compUi->enterPasswordEdit->setFocus();87
88m_compUi->enterPasswordEdit->setQualityVisible(true);89m_compUi->repeatPasswordEdit->setQualityVisible(false);90}
91
92void PasswordEditWidget::initComponent()93{
94// These need to be set in total for each credential type for translation purposes95m_ui->groupBox->setTitle(tr("Password"));96m_ui->addButton->setText(tr("Add Password"));97m_ui->changeButton->setText(tr("Change Password"));98m_ui->removeButton->setText(tr("Remove Password"));99m_ui->changeOrRemoveLabel->setText(tr("Password set, click to change or remove"));100
101m_ui->componentDescription->setText(102tr("<p>A password is the primary method for securing your database.</p>"103"<p>Good passwords are long and unique. KeePassXC can generate one for you.</p>"));104}
105
106void PasswordEditWidget::hideEvent(QHideEvent* event)107{
108if (!isVisible() && m_compUi->enterPasswordEdit) {109m_compUi->enterPasswordEdit->setText("");110m_compUi->repeatPasswordEdit->setText("");111}112
113QWidget::hideEvent(event);114}
115
116bool PasswordEditWidget::validate(QString& errorMessage) const117{
118if (m_compUi->enterPasswordEdit->text() != m_compUi->repeatPasswordEdit->text()) {119errorMessage = tr("Passwords do not match.");120return false;121}122
123return true;124}
125
126void PasswordEditWidget::setPassword(const QString& password)127{
128Q_ASSERT(m_compEditWidget);129
130m_compUi->enterPasswordEdit->setText(password);131m_compUi->repeatPasswordEdit->setText(password);132}
133