File indexing completed on 2024-12-01 13:09:02

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 "adminrolesmodel.h"
0008 #include <KLocalizedString>
0009 
0010 AdminRolesModel::AdminRolesModel(QObject *parent)
0011     : QAbstractListModel(parent)
0012 {
0013 }
0014 
0015 AdminRolesModel::~AdminRolesModel() = default;
0016 
0017 int AdminRolesModel::rowCount(const QModelIndex &parent) const
0018 {
0019     if (parent.isValid()) {
0020         return 0; // flat model
0021     }
0022     return mListRoleInfos.count();
0023 }
0024 
0025 QVariant AdminRolesModel::headerData(int section, Qt::Orientation orientation, int role) const
0026 {
0027     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0028         switch (static_cast<AdminRoles>(section)) {
0029         case AdminRolesModel::Identifier:
0030             return i18n("Identifier");
0031         case AdminRolesModel::Name:
0032             return i18n("Name");
0033         case AdminRolesModel::Scope:
0034             return i18n("Scope");
0035         case AdminRolesModel::Protected:
0036             return i18n("Protected");
0037         case AdminRolesModel::Mandatory2Fa:
0038             return i18n("Two Factors Authentication");
0039         case AdminRolesModel::Description:
0040             return i18n("Description");
0041         }
0042     }
0043     return {};
0044 }
0045 
0046 int AdminRolesModel::columnCount(const QModelIndex &parent) const
0047 {
0048     Q_UNUSED(parent)
0049     constexpr int val = static_cast<int>(AdminRolesModel::LastColumn) + 1;
0050     return val;
0051 }
0052 
0053 QVector<RoleInfo> AdminRolesModel::roles() const
0054 {
0055     return mListRoleInfos;
0056 }
0057 
0058 void AdminRolesModel::clear()
0059 {
0060     if (!mListRoleInfos.isEmpty()) {
0061         beginResetModel();
0062         mListRoleInfos.clear();
0063         endResetModel();
0064     }
0065 }
0066 
0067 void AdminRolesModel::setRoles(const QVector<RoleInfo> &newRoles)
0068 {
0069     clear();
0070     if (!newRoles.isEmpty()) {
0071         beginInsertRows(QModelIndex(), 0, newRoles.count() - 1);
0072         mListRoleInfos = newRoles;
0073         endInsertRows();
0074     }
0075 }
0076 
0077 QVariant AdminRolesModel::data(const QModelIndex &index, int role) const
0078 {
0079     if (index.row() < 0 || index.row() >= mListRoleInfos.count()) {
0080         return {};
0081     }
0082     if (role != Qt::DisplayRole) {
0083         return {};
0084     }
0085 
0086     const RoleInfo &roleInfo = mListRoleInfos.at(index.row());
0087     const int col = index.column();
0088     switch (col) {
0089     case AdminRolesModel::Identifier:
0090         return roleInfo.identifier();
0091     case AdminRolesModel::Name:
0092         return roleInfo.name();
0093     case AdminRolesModel::Scope:
0094         return roleInfo.scope();
0095     case AdminRolesModel::Protected:
0096         return roleInfo.roleProtected();
0097     case AdminRolesModel::Mandatory2Fa:
0098         return roleInfo.mandatory2fa();
0099     case AdminRolesModel::Description:
0100         return roleInfo.description();
0101     }
0102     return {};
0103 }
0104 
0105 #include "moc_adminrolesmodel.cpp"