File indexing completed on 2024-12-08 12:53:32
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 "directoryusersmodel.h" 0008 #include <KLocalizedString> 0009 0010 DirectoryUsersModel::DirectoryUsersModel(QObject *parent) 0011 : CustomBaseModel(parent) 0012 { 0013 } 0014 0015 DirectoryUsersModel::~DirectoryUsersModel() = default; 0016 0017 void DirectoryUsersModel::checkFullList() 0018 { 0019 setHasFullList(mUsers.count() == mUsers.total()); 0020 } 0021 0022 int DirectoryUsersModel::rowCount(const QModelIndex &parent) const 0023 { 0024 Q_UNUSED(parent) 0025 return mUsers.count(); 0026 } 0027 0028 QList<int> DirectoryUsersModel::hideColumns() const 0029 { 0030 return {UserId, JoinAtDateTime, UserName}; 0031 } 0032 0033 Users::ParseType DirectoryUsersModel::parseType() const 0034 { 0035 return Users::ParseType::Directory; 0036 } 0037 0038 void DirectoryUsersModel::setRoles(const QVector<RoleInfo> &newRoles) 0039 { 0040 mRoleInfo = newRoles; 0041 } 0042 0043 void DirectoryUsersModel::addMoreElements(const QJsonObject &obj) 0044 { 0045 const int numberOfElement = mUsers.count(); 0046 mUsers.parseMoreUsers(obj, parseType(), mRoleInfo); 0047 beginInsertRows(QModelIndex(), numberOfElement, mUsers.count() - 1); 0048 endInsertRows(); 0049 checkFullList(); 0050 } 0051 0052 void DirectoryUsersModel::parseElements(const QJsonObject &obj) 0053 { 0054 if (!mUsers.isEmpty()) { 0055 beginResetModel(); 0056 mUsers.clear(); 0057 endResetModel(); 0058 } 0059 mUsers.parseUsers(obj, parseType(), mRoleInfo); 0060 if (!mUsers.isEmpty()) { 0061 beginInsertRows(QModelIndex(), 0, mUsers.count() - 1); 0062 endInsertRows(); 0063 } 0064 checkFullList(); 0065 Q_EMIT totalChanged(); 0066 } 0067 0068 QVariant DirectoryUsersModel::data(const QModelIndex &index, int role) const 0069 { 0070 if (index.row() < 0 || index.row() >= mUsers.count()) { 0071 return {}; 0072 } 0073 if (role != Qt::DisplayRole) { 0074 return {}; 0075 } 0076 const User &user = mUsers.at(index.row()); 0077 const int col = index.column(); 0078 switch (static_cast<DirectoryUsersRoles>(col)) { 0079 case DirectoryUsersRoles::Name: 0080 return user.name().isEmpty() ? user.userName() : user.name(); 0081 case DirectoryUsersRoles::Email: 0082 return user.userEmailsInfo().email; 0083 case DirectoryUsersRoles::JoinAt: 0084 return user.createdAt().toString(); // TODO verify it 0085 case DirectoryUsersRoles::JoinAtDateTime: 0086 return user.createdAt(); 0087 case DirectoryUsersRoles::UserId: 0088 return user.userId(); 0089 case DirectoryUsersModel::UserName: 0090 return user.userName(); 0091 } 0092 return {}; 0093 } 0094 0095 QVariant DirectoryUsersModel::headerData(int section, Qt::Orientation orientation, int role) const 0096 { 0097 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { 0098 switch (static_cast<DirectoryUsersRoles>(section)) { 0099 case DirectoryUsersModel::Name: 0100 return i18n("Name"); 0101 case DirectoryUsersModel::Email: 0102 return i18n("Emails"); 0103 case DirectoryUsersModel::JoinAt: 0104 return i18n("Join At"); 0105 case DirectoryUsersModel::JoinAtDateTime: 0106 case DirectoryUsersModel::UserId: 0107 case DirectoryUsersModel::UserName: 0108 return {}; 0109 } 0110 } 0111 return {}; 0112 } 0113 0114 int DirectoryUsersModel::columnCount(const QModelIndex &parent) const 0115 { 0116 Q_UNUSED(parent) 0117 constexpr int val = static_cast<int>(DirectoryUsersModel::LastColumn) + 1; 0118 return val; 0119 } 0120 0121 int DirectoryUsersModel::total() const 0122 { 0123 return mUsers.total(); 0124 } 0125 0126 #include "moc_directoryusersmodel.cpp"