File indexing completed on 2024-05-12 16:27:22

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "passwordlineeditwidget.h"
0008 #include "dialogs/resetpassworddialog.h"
0009 #include "misc/lineeditcatchreturnkey.h"
0010 #include <KAuthorized>
0011 #include <KLocalizedString>
0012 #include <KPasswordLineEdit>
0013 #include <QHBoxLayout>
0014 #include <QPointer>
0015 #include <QPushButton>
0016 
0017 PasswordLineEditWidget::PasswordLineEditWidget(QWidget *parent)
0018     : QWidget(parent)
0019     , mPasswordLineEdit(new KPasswordLineEdit(this))
0020     , mResetPasswordButton(new QPushButton(i18n("Reset Password"), this))
0021 {
0022     auto mainLayout = new QHBoxLayout(this);
0023     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0024     mainLayout->setContentsMargins({});
0025 
0026     mPasswordLineEdit->setObjectName(QStringLiteral("mPasswordLineEdit"));
0027     mPasswordLineEdit->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0028     mainLayout->addWidget(mPasswordLineEdit);
0029 
0030     mResetPasswordButton->setObjectName(QStringLiteral("mResetPasswordButton"));
0031     mainLayout->addWidget(mPasswordLineEdit);
0032     mainLayout->addWidget(mResetPasswordButton);
0033     new LineEditCatchReturnKey(mPasswordLineEdit->lineEdit(), this);
0034     connect(mResetPasswordButton, &QPushButton::clicked, this, &PasswordLineEditWidget::slotResetPasswordButton);
0035 }
0036 
0037 PasswordLineEditWidget::~PasswordLineEditWidget() = default;
0038 
0039 KPasswordLineEdit *PasswordLineEditWidget::passwordLineEdit() const
0040 {
0041     return mPasswordLineEdit;
0042 }
0043 
0044 void PasswordLineEditWidget::setAllowPasswordReset(bool allowPassword)
0045 {
0046     mResetPasswordButton->setVisible(allowPassword);
0047 }
0048 
0049 void PasswordLineEditWidget::slotResetPasswordButton()
0050 {
0051     QPointer<ResetPasswordDialog> dialog = new ResetPasswordDialog(this);
0052     if (dialog->exec()) {
0053         Q_EMIT resetPasswordRequested(dialog->email());
0054     }
0055     delete dialog;
0056 }
0057 
0058 #include "moc_passwordlineeditwidget.cpp"