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

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "myaccount2fadisabletotpwidget.h"
0008 #include "ddpapi/ddpclient.h"
0009 #include "misc/lineeditcatchreturnkey.h"
0010 #include "rocketchataccount.h"
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 #include <QLabel>
0014 #include <QLineEdit>
0015 #include <QPushButton>
0016 #include <QVBoxLayout>
0017 
0018 MyAccount2FaDisableTotpWidget::MyAccount2FaDisableTotpWidget(RocketChatAccount *account, QWidget *parent)
0019     : QWidget{parent}
0020     , mRocketChatAccount(account)
0021     , mDisableCodeLineEdit(new QLineEdit(this))
0022 {
0023     auto mainLayout = new QVBoxLayout(this);
0024     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0025     mainLayout->setContentsMargins({});
0026 
0027     auto label = new QLabel(i18n("Open your authentication app and enter the code.\nYou can also use one of your backup codes."), this);
0028     label->setObjectName(QStringLiteral("label"));
0029     mainLayout->addWidget(label);
0030 
0031     auto hboxLayout = new QHBoxLayout;
0032     hboxLayout->setObjectName(QStringLiteral("hboxLayout"));
0033     hboxLayout->setContentsMargins({});
0034     mainLayout->addLayout(hboxLayout);
0035 
0036     mDisableCodeLineEdit->setObjectName(QStringLiteral("mDisableCodeLineEdit"));
0037     mDisableCodeLineEdit->setPlaceholderText(i18n("Enter authentication code"));
0038     new LineEditCatchReturnKey(mDisableCodeLineEdit, this);
0039     hboxLayout->addWidget(mDisableCodeLineEdit);
0040 
0041     auto verifyButton = new QPushButton(i18n("Verify"), this);
0042     verifyButton->setObjectName(QStringLiteral("verifyButton"));
0043     hboxLayout->addWidget(verifyButton);
0044     verifyButton->setEnabled(false);
0045     connect(verifyButton, &QPushButton::clicked, this, &MyAccount2FaDisableTotpWidget::slotVerify);
0046     connect(mDisableCodeLineEdit, &QLineEdit::textChanged, this, [verifyButton](const QString &str) {
0047         verifyButton->setEnabled(!str.trimmed().isEmpty());
0048     });
0049 
0050     auto regenerateCode = new QPushButton(i18n("Regenerate Code"), this);
0051     regenerateCode->setObjectName(QStringLiteral("regenerateCode"));
0052     mainLayout->addWidget(regenerateCode);
0053     connect(regenerateCode, &QPushButton::clicked, this, &MyAccount2FaDisableTotpWidget::slotRegenerateCode);
0054     mainLayout->addStretch(1);
0055     if (mRocketChatAccount) {
0056         connect(mRocketChatAccount, &RocketChatAccount::disabledTotpValid, this, &MyAccount2FaDisableTotpWidget::slotTotpInvalid);
0057     }
0058 }
0059 
0060 MyAccount2FaDisableTotpWidget::~MyAccount2FaDisableTotpWidget() = default;
0061 
0062 void MyAccount2FaDisableTotpWidget::slotTotpInvalid(bool check)
0063 {
0064     if (check) {
0065         Q_EMIT hide2FaDisableTotpWidget();
0066     } else {
0067         KMessageBox::error(this, i18n("Invalid two factor code."), i18n("Check Two Factor Code"));
0068     }
0069 }
0070 
0071 void MyAccount2FaDisableTotpWidget::slotVerify()
0072 {
0073     mRocketChatAccount->ddp()->disable2fa(mDisableCodeLineEdit->text());
0074 }
0075 
0076 void MyAccount2FaDisableTotpWidget::slotRegenerateCode()
0077 {
0078     mRocketChatAccount->ddp()->regenerateCodes2fa(mDisableCodeLineEdit->text());
0079 }
0080 
0081 #include "moc_myaccount2fadisabletotpwidget.cpp"