File indexing completed on 2024-12-08 04:34:27

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 "registeruserwidget.h"
0008 #include "rocketchataccount.h"
0009 #include <KAuthorized>
0010 #include <KLocalizedString>
0011 #include <KPasswordLineEdit>
0012 #include <QFormLayout>
0013 #include <QLineEdit>
0014 #include <QPushButton>
0015 
0016 RegisterUserWidget::RegisterUserWidget(QWidget *parent)
0017     : QWidget(parent)
0018     , mRegisterButton(new QPushButton(i18n("Register"), this))
0019     , mUserName(new QLineEdit(this))
0020     , mEmail(new QLineEdit(this))
0021     , mPasswordLineEdit(new KPasswordLineEdit(this))
0022     , mConfirmPasswordLineEdit(new KPasswordLineEdit(this))
0023 {
0024     auto mainLayout = new QFormLayout(this);
0025     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0026     mainLayout->setContentsMargins({});
0027 
0028     mUserName->setObjectName(QStringLiteral("mUserName"));
0029     mainLayout->addRow(i18n("Username:"), mUserName);
0030     connect(mUserName, &QLineEdit::textChanged, this, &RegisterUserWidget::slotUpdateRegisterButton);
0031 
0032     mEmail->setObjectName(QStringLiteral("mEmail"));
0033     mainLayout->addRow(i18n("Email:"), mEmail);
0034     connect(mEmail, &QLineEdit::textChanged, this, &RegisterUserWidget::slotUpdateRegisterButton);
0035 
0036     mPasswordLineEdit->setObjectName(QStringLiteral("mPasswordLineEdit"));
0037     mPasswordLineEdit->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0038     mainLayout->addRow(i18n("Password:"), mPasswordLineEdit);
0039     connect(mPasswordLineEdit, &KPasswordLineEdit::passwordChanged, this, &RegisterUserWidget::slotUpdateRegisterButton);
0040 
0041     mConfirmPasswordLineEdit->setObjectName(QStringLiteral("mConfirmPasswordLineEdit"));
0042     mConfirmPasswordLineEdit->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0043     mainLayout->addRow(i18n("Confirm Password:"), mConfirmPasswordLineEdit);
0044     connect(mConfirmPasswordLineEdit, &KPasswordLineEdit::passwordChanged, this, &RegisterUserWidget::slotUpdateRegisterButton);
0045 
0046     mRegisterButton->setObjectName(QStringLiteral("mRegisterButton"));
0047     connect(mRegisterButton, &QPushButton::clicked, this, &RegisterUserWidget::slotRegisterNewUser);
0048     mainLayout->addWidget(mRegisterButton);
0049     mRegisterButton->setEnabled(false);
0050 }
0051 
0052 RegisterUserWidget::~RegisterUserWidget() = default;
0053 
0054 void RegisterUserWidget::slotUpdateRegisterButton()
0055 {
0056     const bool enableRegisterButton = !mUserName->text().trimmed().isEmpty() && !mEmail->text().trimmed().isEmpty() && !mPasswordLineEdit->password().isEmpty()
0057         && (mPasswordLineEdit->password() == mConfirmPasswordLineEdit->password());
0058     mRegisterButton->setEnabled(enableRegisterButton);
0059 }
0060 
0061 void RegisterUserWidget::slotRegisterNewUser()
0062 {
0063     mRegisterButton->setEnabled(false);
0064     Q_EMIT registerNewAccount();
0065 }
0066 
0067 RocketChatRestApi::RegisterUserJob::RegisterUserInfo RegisterUserWidget::registerUserInfo() const
0068 {
0069     RocketChatRestApi::RegisterUserJob::RegisterUserInfo info;
0070     info.email = mEmail->text();
0071     info.name = mUserName->text();
0072     info.username = mUserName->text().remove(QLatin1Char(' '));
0073     info.password = mPasswordLineEdit->password();
0074     return info;
0075 }
0076 
0077 #include "moc_registeruserwidget.cpp"