File indexing completed on 2024-05-12 09:38:26

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2011 Craig Drummond <craig.p.drummond@gmail.com>
0003 // SPDX-FileCopyrightText: 2018 Alexis Lopes Zubeta <contact@azubieta.net>
0004 // SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@kde.org>
0005 /*
0006  * UFW KControl Module
0007  */
0008 
0009 #include "rulelistmodel.h"
0010 #include <klocalizedstring.h>
0011 
0012 RuleListModel::RuleListModel(QObject *parent)
0013     : QAbstractTableModel(parent)
0014 {
0015 }
0016 
0017 void RuleListModel::move(int from, int to)
0018 {
0019     if (to < 0 || to >= m_rules.count()) {
0020         return;
0021     }
0022 
0023     int newPos = to > from ? to + 1 : to;
0024     bool validMove = beginMoveRows(QModelIndex(), from, from, QModelIndex(), newPos);
0025     if (validMove) {
0026         m_rules.move(from, to);
0027         endMoveRows();
0028     }
0029 }
0030 
0031 int RuleListModel::rowCount(const QModelIndex &parent) const
0032 {
0033     if (parent.isValid()) {
0034         return 0;
0035     }
0036     return m_rules.count();
0037 }
0038 
0039 int RuleListModel::columnCount(const QModelIndex &parent) const
0040 {
0041     Q_UNUSED(parent);
0042     return 6;
0043 }
0044 
0045 QVariant RuleListModel::data(const QModelIndex &index, int role) const
0046 {
0047     Q_UNUSED(role)
0048     const auto checkIndexFlags = QAbstractItemModel::CheckIndexOption::IndexIsValid | QAbstractItemModel::CheckIndexOption::ParentIsInvalid;
0049 
0050     if (!checkIndex(index, checkIndexFlags)) {
0051         return {};
0052     }
0053 
0054     const Rule *rule = m_rules.at(index.row());
0055 
0056     switch (index.column()) {
0057     case ActionColumn:
0058         return rule->actionStr();
0059     case FromColumn:
0060         return rule->fromStr();
0061     case ToColumn:
0062         return rule->toStr();
0063     case Ipv6Column:
0064         return rule->ipv6() ? QStringLiteral("IPv6") : QStringLiteral("IPv4");
0065     case LoggingColumn:
0066         return rule->loggingStr();
0067     }
0068     return QVariant();
0069 }
0070 
0071 void RuleListModel::setProfile(const Profile &profile)
0072 {
0073     qDebug() << "Profile on the model received. enabled? " << profile.enabled();
0074 
0075     beginResetModel();
0076     m_profile = profile;
0077     m_rules = m_profile.rules();
0078     endResetModel();
0079 }
0080 
0081 QVariant RuleListModel::headerData(int section, Qt::Orientation orientation, int role) const
0082 {
0083     Q_UNUSED(orientation)
0084     Q_UNUSED(role)
0085     switch (section) {
0086     case ActionColumn:
0087         return i18nc("@title:column", "Action");
0088     case FromColumn:
0089         return i18nc("@title:column", "From");
0090     case ToColumn:
0091         return i18nc("@title:column", "To");
0092     case Ipv6Column:
0093         return i18nc("@title:column", "IP");
0094     case LoggingColumn:
0095         return i18nc("@title:column", "Logging");
0096     }
0097     return QVariant();
0098 
0099 }