File indexing completed on 2024-02-18 15:43:56

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 "usersinrolemodel.h"
0008 #include <KLocalizedString>
0009 
0010 UsersInRoleModel::UsersInRoleModel(QObject *parent)
0011     : CustomBaseModel(parent)
0012 {
0013 }
0014 
0015 UsersInRoleModel::~UsersInRoleModel() = default;
0016 
0017 void UsersInRoleModel::checkFullList()
0018 {
0019     setHasFullList(mUsers.count() == mUsers.total());
0020 }
0021 
0022 int UsersInRoleModel::rowCount(const QModelIndex &parent) const
0023 {
0024     Q_UNUSED(parent)
0025     return mUsers.count();
0026 }
0027 
0028 QList<int> UsersInRoleModel::hideColumns() const
0029 {
0030     return {UserId, UserName};
0031 }
0032 
0033 void UsersInRoleModel::addMoreElements(const QJsonObject &obj)
0034 {
0035     const int numberOfElement = mUsers.count();
0036     mUsers.parseMoreUsers(obj, Users::UserInRoles, {}); // Don't use RoleInfo as we don't need to show it
0037     beginInsertRows(QModelIndex(), numberOfElement, mUsers.count() - 1);
0038     endInsertRows();
0039     checkFullList();
0040 }
0041 
0042 void UsersInRoleModel::parseElements(const QJsonObject &obj)
0043 {
0044     if (!mUsers.isEmpty()) {
0045         beginResetModel();
0046         mUsers.clear();
0047         endResetModel();
0048     }
0049     mUsers.parseUsers(obj, Users::UserInRoles, {});
0050     if (!mUsers.isEmpty()) {
0051         beginInsertRows(QModelIndex(), 0, mUsers.count() - 1);
0052         endInsertRows();
0053     }
0054     checkFullList();
0055     Q_EMIT totalChanged();
0056 }
0057 
0058 QVariant UsersInRoleModel::data(const QModelIndex &index, int role) const
0059 {
0060     if (index.row() < 0 || index.row() >= mUsers.count()) {
0061         return {};
0062     }
0063     if (role != Qt::DisplayRole) {
0064         return {};
0065     }
0066     const User &user = mUsers.at(index.row());
0067     const int col = index.column();
0068     switch (static_cast<UsersInRoleRoles>(col)) {
0069     case UsersInRoleRoles::Name:
0070         return user.name().isEmpty() ? user.userName() : user.name();
0071     case UsersInRoleRoles::Email:
0072         return user.userEmailsInfo().email;
0073     case UsersInRoleRoles::UserId:
0074         return user.userId();
0075     case UsersInRoleRoles::UserName:
0076         return user.userName();
0077     }
0078     return {};
0079 }
0080 
0081 QVariant UsersInRoleModel::headerData(int section, Qt::Orientation orientation, int role) const
0082 {
0083     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0084         switch (static_cast<UsersInRoleRoles>(section)) {
0085         case UsersInRoleModel::Name:
0086             return i18n("Name");
0087         case UsersInRoleModel::Email:
0088             return i18n("Emails");
0089         case UsersInRoleModel::UserId:
0090         case UsersInRoleModel::UserName:
0091             return {};
0092         }
0093     }
0094     return {};
0095 }
0096 
0097 int UsersInRoleModel::columnCount(const QModelIndex &parent) const
0098 {
0099     Q_UNUSED(parent)
0100     constexpr int val = static_cast<int>(UsersInRoleModel::LastColumn) + 1;
0101     return val;
0102 }
0103 
0104 int UsersInRoleModel::total() const
0105 {
0106     return mUsers.total();
0107 }
0108 
0109 #include "moc_usersinrolemodel.cpp"