File indexing completed on 2024-02-25 16:44:34

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "adminoauthmodel.h"
0008 #include <KLocalizedString>
0009 #include <QDateTime>
0010 
0011 AdminOauthModel::AdminOauthModel(QObject *parent)
0012     : QAbstractListModel(parent)
0013 {
0014 }
0015 
0016 AdminOauthModel::~AdminOauthModel() = default;
0017 
0018 int AdminOauthModel::rowCount(const QModelIndex &parent) const
0019 {
0020     if (parent.isValid()) {
0021         return 0; // flat model
0022     }
0023     return mAdminOauth.count();
0024 }
0025 
0026 QVariant AdminOauthModel::headerData(int section, Qt::Orientation orientation, int role) const
0027 {
0028     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0029         switch (static_cast<AdminOauthRoles>(section)) {
0030         case AdminOauthModel::Identifier:
0031             return i18n("Identifier");
0032         case AdminOauthModel::Name:
0033             return i18n("Name");
0034         case AdminOauthModel::ClientId:
0035             return i18n("Client Id");
0036         case AdminOauthModel::ClientSecret:
0037             return i18n("Client Secret");
0038         case AdminOauthModel::RedirectUri:
0039             return i18n("Redirect Url");
0040         case AdminOauthModel::Active:
0041             return i18n("Active");
0042         case AdminOauthModel::CreatedAt:
0043             return i18n("Created At");
0044         case AdminOauthModel::CreatedAtStr:
0045         case AdminOauthModel::CreatedBy:
0046             return i18n("Created By");
0047         }
0048     }
0049     return {};
0050 }
0051 
0052 int AdminOauthModel::columnCount(const QModelIndex &parent) const
0053 {
0054     Q_UNUSED(parent)
0055     constexpr int val = static_cast<int>(AdminOauthModel::LastColumn) + 1;
0056     return val;
0057 }
0058 
0059 const QVector<OauthInfo> &AdminOauthModel::adminOauth() const
0060 {
0061     return mAdminOauth;
0062 }
0063 
0064 void AdminOauthModel::clear()
0065 {
0066     if (!mAdminOauth.isEmpty()) {
0067         beginResetModel();
0068         mAdminOauth.clear();
0069         endResetModel();
0070     }
0071 }
0072 
0073 void AdminOauthModel::setAdminOauth(const QVector<OauthInfo> &newAdminInvites)
0074 {
0075     clear();
0076     if (!newAdminInvites.isEmpty()) {
0077         beginInsertRows(QModelIndex(), 0, newAdminInvites.count() - 1);
0078         mAdminOauth = newAdminInvites;
0079         endInsertRows();
0080     }
0081 }
0082 
0083 QVariant AdminOauthModel::data(const QModelIndex &index, int role) const
0084 {
0085     if (index.row() < 0 || index.row() >= mAdminOauth.count()) {
0086         return {};
0087     }
0088     if (role != Qt::DisplayRole) {
0089         return {};
0090     }
0091 
0092     const OauthInfo &info = mAdminOauth.at(index.row());
0093     const int col = index.column();
0094     switch (col) {
0095     case AdminOauthModel::Identifier:
0096         return info.identifier();
0097     case AdminOauthModel::Name:
0098         return info.name();
0099     case AdminOauthModel::ClientId:
0100         return info.clientId();
0101     case AdminOauthModel::ClientSecret:
0102         return info.clientSecret();
0103     case AdminOauthModel::RedirectUri:
0104         return info.redirectUri();
0105     case AdminOauthModel::Active:
0106         return info.active();
0107     case AdminOauthModel::CreatedAt:
0108         return info.createdDateTime();
0109     case AdminOauthModel::CreatedBy:
0110         return info.createdBy();
0111     case AdminOauthModel::CreatedAtStr:
0112         return info.createdDateTime().toString();
0113     }
0114     return {};
0115 }
0116 
0117 void AdminOauthModel::removeOauth(const QString &identifier)
0118 {
0119     const int roomCount = mAdminOauth.count();
0120     for (int i = 0; i < roomCount; ++i) {
0121         if (mAdminOauth.at(i).identifier() == identifier) {
0122             beginRemoveRows(QModelIndex(), i, i);
0123             mAdminOauth.removeAt(i);
0124             endRemoveRows();
0125             break;
0126         }
0127     }
0128 }
0129 
0130 void AdminOauthModel::addMoreOauth(const OauthInfo &info)
0131 {
0132     const int numberOfElement = mAdminOauth.count();
0133     mAdminOauth.append(info);
0134     beginInsertRows(QModelIndex(), numberOfElement, numberOfElement);
0135     endInsertRows();
0136 }
0137 
0138 #include "moc_adminoauthmodel.cpp"