Warning, file /network/ruqola/src/core/model/personalaccesstokeninfosmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "personalaccesstokeninfosmodel.h"
0008 #include <KLocalizedString>
0009 
0010 PersonalAccessTokenInfosModel::PersonalAccessTokenInfosModel(QObject *parent)
0011     : QAbstractListModel{parent}
0012 {
0013 }
0014 
0015 PersonalAccessTokenInfosModel::~PersonalAccessTokenInfosModel() = default;
0016 
0017 int PersonalAccessTokenInfosModel::rowCount(const QModelIndex &parent) const
0018 {
0019     Q_UNUSED(parent)
0020     return mPersonalAccessTokenInfos.count();
0021 }
0022 
0023 QVariant PersonalAccessTokenInfosModel::data(const QModelIndex &index, int role) const
0024 {
0025     if (index.row() < 0 || index.row() >= mPersonalAccessTokenInfos.count()) {
0026         return {};
0027     }
0028     if (role != Qt::DisplayRole) {
0029         return {};
0030     }
0031 
0032     const auto &info = mPersonalAccessTokenInfos.at(index.row());
0033     const int col = index.column();
0034     switch (col) {
0035     case PersonalAccessTokenInfosModel::CreateAt: {
0036         return info.createAtDisplayDateTime();
0037     }
0038     case PersonalAccessTokenInfosModel::CreateAtDateTime: {
0039         return info.createdAt();
0040     }
0041     case PersonalAccessTokenInfosModel::LastTokenPart: {
0042         return info.lastTokenPart();
0043     }
0044     case PersonalAccessTokenInfosModel::ByPassTwoFactor: {
0045         return info.bypassTwoFactor() ? i18n("Ignored") : i18n("Required");
0046     }
0047     case PersonalAccessTokenInfosModel::Name: {
0048         return info.name();
0049     }
0050     }
0051     return {};
0052 }
0053 
0054 void PersonalAccessTokenInfosModel::clear()
0055 {
0056     if (!mPersonalAccessTokenInfos.isEmpty()) {
0057         beginResetModel();
0058         mPersonalAccessTokenInfos.clear();
0059         endResetModel();
0060     }
0061 }
0062 
0063 void PersonalAccessTokenInfosModel::insertPersonalAccessTokenInfos(const PersonalAccessTokenInfos &infos)
0064 {
0065     clear();
0066     if (!infos.isEmpty()) {
0067         beginInsertRows(QModelIndex(), 0, infos.count() - 1);
0068         mPersonalAccessTokenInfos = infos;
0069         endInsertRows();
0070     }
0071 }
0072 
0073 QVariant PersonalAccessTokenInfosModel::headerData(int section, Qt::Orientation orientation, int role) const
0074 {
0075     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0076         switch (static_cast<PersonalAccessTokenInfosRoles>(section)) {
0077         case PersonalAccessTokenInfosModel::Name:
0078             return i18n("Name");
0079         case PersonalAccessTokenInfosModel::CreateAt:
0080             return i18n("Create At");
0081         case PersonalAccessTokenInfosModel::ByPassTwoFactor:
0082             return i18n("Two Factor Authentication");
0083         case PersonalAccessTokenInfosModel::LastTokenPart:
0084             return i18n("Last token part");
0085         case PersonalAccessTokenInfosModel::CreateAtDateTime:
0086             return {};
0087         }
0088     }
0089     return {};
0090 }
0091 
0092 int PersonalAccessTokenInfosModel::columnCount(const QModelIndex &parent) const
0093 {
0094     Q_UNUSED(parent)
0095     constexpr int val = static_cast<int>(PersonalAccessTokenInfosModel::LastColumn) + 1;
0096     return val;
0097 }
0098 
0099 void PersonalAccessTokenInfosModel::removeToken(const QString &tokenName)
0100 {
0101     const int roomCount = mPersonalAccessTokenInfos.count();
0102     for (int i = 0; i < roomCount; ++i) {
0103         if (mPersonalAccessTokenInfos.at(i).name() == tokenName) {
0104             beginRemoveRows(QModelIndex(), i, i);
0105             mPersonalAccessTokenInfos.removeAt(i);
0106             endRemoveRows();
0107             break;
0108         }
0109     }
0110 }
0111 
0112 #include "moc_personalaccesstokeninfosmodel.cpp"