File indexing completed on 2025-01-05 04:35:35

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2020-2022 Harald Sitter <sitter@kde.org>
0003 
0004 #include "model.h"
0005 
0006 #include <QMetaEnum>
0007 
0008 #include "aceobject.h"
0009 
0010 int Model::rowCount(const QModelIndex &parent) const
0011 {
0012     Q_UNUSED(parent);
0013     return m_acl.count();
0014 }
0015 
0016 QVariant Model::data(const QModelIndex &index, int intRole) const
0017 {
0018     if (!index.isValid()) {
0019         return {};
0020     }
0021 
0022     const auto ace = m_acl.at(index.row());
0023     switch (static_cast<Role>(intRole)) {
0024     case Role::Sid:
0025         return ace->sid;
0026     case Role::Type:
0027         return ace->type;
0028     case Role::Flags:
0029         return ace->flags;
0030     case Role::Mask:
0031         return ace->mask;
0032     case Role::ACEObject:
0033         return QVariant::fromValue(new ACEObject(ace));
0034     }
0035 
0036     return {};
0037 }
0038 
0039 QHash<int, QByteArray> Model::roleNames() const
0040 {
0041     static QHash<int, QByteArray> roles;
0042     if (!roles.isEmpty()) {
0043         return roles;
0044     }
0045 
0046     const QMetaEnum roleEnum = QMetaEnum::fromType<Role>();
0047     for (int i = 0; i < roleEnum.keyCount(); ++i) {
0048         const int value = roleEnum.value(i);
0049         Q_ASSERT(value != -1);
0050         roles[static_cast<int>(value)] = QByteArray("ROLE_") + roleEnum.valueToKey(value);
0051     }
0052     return roles;
0053 }
0054 
0055 void Model::resetData(const QList<std::shared_ptr<ACE>> &acl)
0056 {
0057     beginResetModel();
0058     m_acl = acl;
0059     endResetModel();
0060     Q_EMIT emptyChanged();
0061 }
0062 
0063 QList<std::shared_ptr<ACE>> Model::acl() const
0064 {
0065     return m_acl;
0066 }