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

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 "commandsmodelfilterproxymodel.h"
0008 #include "commandsmodel.h"
0009 #include "rocketchataccount.h"
0010 
0011 CommandsModelFilterProxyModel::CommandsModelFilterProxyModel(RocketChatAccount *account, QObject *parent)
0012     : QSortFilterProxyModel(parent)
0013     , mRocketChatAccount(account)
0014 {
0015     setFilterCaseSensitivity(Qt::CaseInsensitive);
0016     setFilterRole(CommandsModel::CommandName);
0017     sort(0);
0018 }
0019 
0020 void CommandsModelFilterProxyModel::setRoomId(const QString &roomId)
0021 {
0022     mRoomId = roomId;
0023 }
0024 
0025 CommandsModelFilterProxyModel::~CommandsModelFilterProxyModel() = default;
0026 
0027 bool CommandsModelFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0028 {
0029     if (!sourceModel()) {
0030         return false;
0031     }
0032     if (left.isValid() && right.isValid()) {
0033         const QString leftString = sourceModel()->data(left, CommandsModel::CommandName).toString();
0034         const QString rightString = sourceModel()->data(right, CommandsModel::CommandName).toString();
0035         return QString::localeAwareCompare(leftString, rightString) < 0;
0036     } else {
0037         return false;
0038     }
0039 }
0040 
0041 bool CommandsModelFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0042 {
0043     if (mRocketChatAccount) {
0044         const QModelIndex modelIndex = sourceModel()->index(source_row, 0, source_parent);
0045         const QStringList permissions = modelIndex.data(CommandsModel::Permissions).toStringList();
0046 
0047         // const QString commandName = modelIndex.data(CommandsModel::CommandName).toString();
0048         Room *room = mRocketChatAccount->room(mRoomId);
0049         // qDebug() << " permissions " << permissions << " mRoomId " << mRoomId << " room " << room << " commandName " << commandName;
0050         for (const QString &permission : permissions) {
0051             if (permission.isEmpty()) {
0052                 return false;
0053             }
0054             // const bool accountHasPermission = mRocketChatAccount->hasPermission(permission);
0055             // const bool roomHasPermission = room && room->hasPermission(permission);
0056             // qDebug() << " accountHasPermission " << accountHasPermission << " roomHasPermission " << roomHasPermission << " commandName " << commandName;
0057             if (!mRocketChatAccount->hasPermission(permission) && !(room && room->hasPermission(permission))) {
0058                 return false;
0059             }
0060         }
0061     }
0062     return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0063 }
0064 
0065 #include "moc_commandsmodelfilterproxymodel.cpp"