File indexing completed on 2024-12-01 04:36:51

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 "twoauthenticationpasswordwidget.h"
0008 #include "rocketchataccount.h"
0009 #include <KAuthorized>
0010 #include <KLocalizedString>
0011 #include <KPasswordLineEdit>
0012 
0013 #include <QHBoxLayout>
0014 #include <QPushButton>
0015 
0016 TwoAuthenticationPasswordWidget::TwoAuthenticationPasswordWidget(QWidget *parent)
0017     : QWidget(parent)
0018     , mTwoFactorAuthenticationPasswordLineEdit(new KPasswordLineEdit(this))
0019 {
0020     auto twoFactorLayout = new QHBoxLayout(this);
0021     twoFactorLayout->setObjectName(QStringLiteral("twoFactorLayout"));
0022     twoFactorLayout->setContentsMargins({});
0023 
0024     mTwoFactorAuthenticationPasswordLineEdit->setObjectName(QStringLiteral("mTwoFactorAuthenticationPasswordLineEdit"));
0025     mTwoFactorAuthenticationPasswordLineEdit->lineEdit()->setPlaceholderText(i18n("Enter code"));
0026     mTwoFactorAuthenticationPasswordLineEdit->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0027     twoFactorLayout->addWidget(mTwoFactorAuthenticationPasswordLineEdit);
0028 
0029     auto sendNewEmailCode = new QPushButton(i18n("Send new code"), this);
0030     sendNewEmailCode->setObjectName(QStringLiteral("sendNewEmailCode"));
0031     twoFactorLayout->addWidget(sendNewEmailCode);
0032     connect(sendNewEmailCode, &QPushButton::clicked, this, &TwoAuthenticationPasswordWidget::slotSendNewEmailCode);
0033     connect(mTwoFactorAuthenticationPasswordLineEdit, &KPasswordLineEdit::passwordChanged, this, [this](const QString &password) {
0034         Q_EMIT updateButtonOk(!password.isEmpty());
0035     });
0036 }
0037 
0038 TwoAuthenticationPasswordWidget::~TwoAuthenticationPasswordWidget() = default;
0039 
0040 QString TwoAuthenticationPasswordWidget::code() const
0041 {
0042     return mTwoFactorAuthenticationPasswordLineEdit->lineEdit()->text();
0043 }
0044 
0045 void TwoAuthenticationPasswordWidget::clear()
0046 {
0047     mTwoFactorAuthenticationPasswordLineEdit->lineEdit()->clear();
0048 }
0049 
0050 void TwoAuthenticationPasswordWidget::slotSendNewEmailCode()
0051 {
0052     mRocketChatAccount->sendUserEmailCode();
0053 }
0054 
0055 RocketChatAccount *TwoAuthenticationPasswordWidget::rocketChatAccount() const
0056 {
0057     return mRocketChatAccount;
0058 }
0059 
0060 void TwoAuthenticationPasswordWidget::setRocketChatAccount(RocketChatAccount *newRocketChatAccount)
0061 {
0062     mRocketChatAccount = newRocketChatAccount;
0063 }
0064 
0065 #include "moc_twoauthenticationpasswordwidget.cpp"