File indexing completed on 2024-12-01 04:36:36
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "authenticationloginwidget.h" 0008 #include "connection.h" 0009 #include "misc/passwordlineeditwidget.h" 0010 #include "registeruser/registeruserdialog.h" 0011 #include <KLineEditEventHandler> 0012 #include <KLocalizedString> 0013 #include <KMessageBox> 0014 #include <KPasswordLineEdit> 0015 #include <QFormLayout> 0016 #include <QLineEdit> 0017 #include <QPointer> 0018 #include <QPushButton> 0019 0020 AuthenticationLoginWidget::AuthenticationLoginWidget(QWidget *parent) 0021 : QWidget{parent} 0022 , mServerUrl(new QLineEdit(this)) 0023 , mAccountName(new QLineEdit(this)) 0024 , mUserName(new QLineEdit(this)) 0025 , mPasswordLineEditWidget(new PasswordLineEditWidget(this)) 0026 , mRegisterAccount(new QPushButton(i18n("Register Account"), this)) 0027 { 0028 auto mainLayout = new QFormLayout(this); 0029 mainLayout->setObjectName(QStringLiteral("mainLayout")); 0030 mainLayout->setContentsMargins({}); 0031 0032 mServerUrl->setObjectName(QStringLiteral("mServerUrl")); 0033 mServerUrl->setReadOnly(true); 0034 mainLayout->addRow(i18n("Server URL:"), mServerUrl); 0035 0036 mAccountName->setObjectName(QStringLiteral("mAccountName")); 0037 mAccountName->setClearButtonEnabled(true); 0038 KLineEditEventHandler::catchReturnKey(mAccountName); 0039 mainLayout->addRow(i18n("Account name:"), mAccountName); 0040 0041 mUserName->setObjectName(QStringLiteral("mUserName")); 0042 mUserName->setClearButtonEnabled(true); 0043 KLineEditEventHandler::catchReturnKey(mUserName); 0044 mainLayout->addRow(i18n("Username:"), mUserName); 0045 0046 mPasswordLineEditWidget->setObjectName(QStringLiteral("mPasswordLineEdit")); 0047 mainLayout->addRow(i18n("Password:"), mPasswordLineEditWidget); 0048 0049 mRegisterAccount->setObjectName(QStringLiteral("mRegisterAccount")); 0050 mainLayout->addRow(QString(), mRegisterAccount); 0051 connect(mRegisterAccount, &QPushButton::clicked, this, &AuthenticationLoginWidget::slotRegisterAccount); 0052 0053 connect(mUserName, &QLineEdit::textChanged, this, &AuthenticationLoginWidget::slotChangeOkButtonEnabled); 0054 connect(mServerUrl, &QLineEdit::textChanged, this, &AuthenticationLoginWidget::slotChangeOkButtonEnabled); 0055 connect(mAccountName, &QLineEdit::textChanged, this, &AuthenticationLoginWidget::slotChangeOkButtonEnabled); 0056 // TODO add "forgot password" 0057 } 0058 0059 void AuthenticationLoginWidget::setExistingAccountName(const QStringList &lst) 0060 { 0061 mNames = lst; 0062 } 0063 0064 AuthenticationLoginWidget::~AuthenticationLoginWidget() = default; 0065 0066 void AuthenticationLoginWidget::slotChangeOkButtonEnabled() 0067 { 0068 const QString accountName = mAccountName->text().trimmed(); 0069 Q_EMIT updateOkButton(!accountName.isEmpty() && !mNames.contains(accountName) && !mServerUrl->text().trimmed().isEmpty() 0070 && !mUserName->text().trimmed().isEmpty()); 0071 } 0072 0073 AccountManager::AccountManagerInfo AuthenticationLoginWidget::accountInfo() 0074 { 0075 const QString accountName = mAccountName->text().trimmed(); 0076 if (mAccountInfo.accountName.isEmpty()) { 0077 mAccountInfo.accountName = accountName; 0078 } 0079 mAccountInfo.displayName = accountName; 0080 mAccountInfo.serverUrl = mServerUrl->text().trimmed(); 0081 if (mAccountInfo.serverUrl.endsWith(QLatin1Char('/'))) { 0082 mAccountInfo.serverUrl.chop(1); 0083 } 0084 mAccountInfo.userName = mUserName->text().trimmed(); 0085 mAccountInfo.password = mPasswordLineEditWidget->passwordLineEdit()->password(); 0086 return mAccountInfo; 0087 } 0088 0089 void AuthenticationLoginWidget::setAccountInfo(const AccountManager::AccountManagerInfo &info) 0090 { 0091 mAccountInfo = info; 0092 mAccountName->setText(info.displayName.isEmpty() ? info.accountName : info.displayName); 0093 mUserName->setText(info.userName); 0094 mServerUrl->setText(info.serverUrl); 0095 mPasswordLineEditWidget->passwordLineEdit()->setPassword(info.password); 0096 mPasswordLineEditWidget->setAllowPasswordReset(info.canResetPassword); 0097 mRegisterAccount->setVisible(info.canRegisterAccount); 0098 } 0099 0100 void AuthenticationLoginWidget::slotRegisterAccount() 0101 { 0102 QPointer<RegisterUserDialog> dlg = new RegisterUserDialog(this); 0103 connect(dlg, &RegisterUserDialog::registerNewAccount, this, [this, dlg]() { 0104 auto mRestApi = new RocketChatRestApi::Connection(this); 0105 connect(mRestApi, &RocketChatRestApi::Connection::registerUserDone, this, &AuthenticationLoginWidget::slotRegisterUserDone); 0106 mRestApi->setServerUrl(mAccountInfo.serverUrl); 0107 mRestApi->registerNewUser(dlg->registerUserInfo()); 0108 }); 0109 dlg->exec(); 0110 delete dlg; 0111 } 0112 0113 void AuthenticationLoginWidget::slotRegisterUserDone() 0114 { 0115 KMessageBox::information( 0116 this, 0117 i18n("We have sent you an email to confirm your registration.\nIf you do not receive an email shortly, please come back and try again."), 0118 i18n("Register New User")); 0119 } 0120 0121 #include "moc_authenticationloginwidget.cpp"