File indexing completed on 2024-12-22 04:45:38

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 "roleeditwidget.h"
0008 #include "rolescopecombobox.h"
0009 #include <KLineEditEventHandler>
0010 #include <KLocalizedString>
0011 #include <QCheckBox>
0012 #include <QFormLayout>
0013 #include <QLineEdit>
0014 
0015 RoleEditWidget::RoleEditWidget(QWidget *parent)
0016     : QWidget{parent}
0017     , mName(new QLineEdit(this))
0018     , mDescription(new QLineEdit(this))
0019     , mTwoFactor(new QCheckBox(i18n("Use Two Factor Authentication"), this))
0020     , mRoleScopeComboBox(new RoleScopeComboBox(this))
0021 {
0022     auto mainLayout = new QFormLayout(this);
0023     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0024     mainLayout->setContentsMargins({});
0025 
0026     mName->setObjectName(QStringLiteral("mName"));
0027     KLineEditEventHandler::catchReturnKey(mName);
0028     mDescription->setObjectName(QStringLiteral("mDescription"));
0029     KLineEditEventHandler::catchReturnKey(mDescription);
0030     mTwoFactor->setObjectName(QStringLiteral("mTwoFactor"));
0031 
0032     mRoleScopeComboBox->setObjectName(QStringLiteral("mRoleScopeComboBox"));
0033 
0034     mainLayout->addRow(i18n("Name:"), mName);
0035     mainLayout->addRow(i18n("Description:"), mDescription);
0036     mainLayout->addWidget(mTwoFactor);
0037     mainLayout->addRow(i18n("Scope:"), mRoleScopeComboBox);
0038     connect(mName, &QLineEdit::textChanged, this, [this](const QString &str) {
0039         Q_EMIT updateOkButton(!str.trimmed().isEmpty());
0040     });
0041 }
0042 
0043 RoleEditWidget::~RoleEditWidget() = default;
0044 
0045 void RoleEditWidget::setRoleEditDialogInfo(const RoleEditDialogInfo &info)
0046 {
0047     mName->setText(info.mName);
0048     mDescription->setText(info.mDescription);
0049     mTwoFactor->setChecked(info.mTwoFactor);
0050     mRoleScopeComboBox->setCurrentIndex(mRoleScopeComboBox->findData(info.mScope));
0051     mName->setReadOnly(true); // Not allow to modify name.
0052     mRoleScopeComboBox->setEnabled(!info.mIsProtected);
0053 }
0054 
0055 RoleEditWidget::RoleEditDialogInfo RoleEditWidget::roleEditDialogInfo() const
0056 {
0057     RoleEditWidget::RoleEditDialogInfo info;
0058     info.mName = mName->text();
0059     info.mDescription = mDescription->text();
0060     info.mTwoFactor = mTwoFactor->isChecked();
0061     info.mScope = mRoleScopeComboBox->currentData().toString();
0062     info.mIsProtected = !mRoleScopeComboBox->isEnabled();
0063     return info;
0064 }
0065 
0066 bool RoleEditWidget::RoleEditDialogInfo::operator==(const RoleEditDialogInfo &other) const
0067 {
0068     return mName == other.mName && mDescription == other.mDescription && mScope == other.mScope && mTwoFactor == other.mTwoFactor
0069         && mIsProtected == other.mIsProtected;
0070 }
0071 
0072 QDebug operator<<(QDebug d, const RoleEditWidget::RoleEditDialogInfo &info)
0073 {
0074     d << "mName : " << info.mName;
0075     d << "mDescription : " << info.mDescription;
0076     d << "mScope : " << info.mScope;
0077     d << "mTwoFactor : " << info.mTwoFactor;
0078     d << "mIsProtected" << info.mIsProtected;
0079     return d;
0080 }
0081 
0082 #include "moc_roleeditwidget.cpp"