File indexing completed on 2024-04-28 16:52:17

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 
0011 RuleListModel::RuleListModel(QObject *parent)
0012     : QAbstractListModel(parent)
0013 {
0014 }
0015 
0016 void RuleListModel::move(int from, int to)
0017 {
0018     if (to < 0 || to >= m_rules.count()) {
0019         return;
0020     }
0021 
0022     int newPos = to > from ? to + 1 : to;
0023     bool validMove = beginMoveRows(QModelIndex(), from, from, QModelIndex(), newPos);
0024     if (validMove) {
0025         m_rules.move(from, to);
0026         endMoveRows();
0027     }
0028 }
0029 
0030 int RuleListModel::rowCount(const QModelIndex &parent) const
0031 {
0032     if (parent.isValid()) {
0033         return 0;
0034     }
0035     return m_rules.count();
0036 }
0037 
0038 QVariant RuleListModel::data(const QModelIndex &index, int role) const
0039 {
0040     const auto checkIndexFlags = QAbstractItemModel::CheckIndexOption::IndexIsValid | QAbstractItemModel::CheckIndexOption::ParentIsInvalid;
0041 
0042     if (!checkIndex(index, checkIndexFlags)) {
0043         return {};
0044     }
0045 
0046     const Rule *rule = m_rules.at(index.row());
0047 
0048     switch (role) {
0049     case ActionRole:
0050         return rule->actionStr();
0051     case FromRole:
0052         return rule->fromStr();
0053     case ToRole:
0054         return rule->toStr();
0055     case Ipv6Role:
0056         return rule->ipv6() ? "IPv6" : "IPv4";
0057     case LoggingRole:
0058         return rule->loggingStr();
0059     }
0060     return {};
0061 }
0062 
0063 void RuleListModel::setProfile(const Profile &profile)
0064 {
0065     qDebug() << "Profile on the model received. enabled? " << profile.enabled();
0066 
0067     beginResetModel();
0068     m_profile = profile;
0069     m_rules = m_profile.rules();
0070     endResetModel();
0071 }
0072 
0073 QHash<int, QByteArray> RuleListModel::roleNames() const
0074 {
0075     return {
0076         {ActionRole, "action"},
0077         {FromRole, "from"},
0078         {ToRole, "to"},
0079         {Ipv6Role, "ipVersion"},
0080         {LoggingRole, "logging"},
0081     };
0082 }