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

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 "loglistmodel.h"
0010 
0011 #include <QDateTime>
0012 #include <QRegularExpression>
0013 
0014 LogListModel::LogListModel(QObject *parent)
0015     : QAbstractListModel(parent)
0016 {
0017 }
0018 
0019 bool LogListModel::busy() const
0020 {
0021     return m_busy;
0022 }
0023 
0024 void LogListModel::setBusy(bool busy)
0025 {
0026     if (m_busy != busy) {
0027         m_busy = busy;
0028         Q_EMIT busyChanged();
0029     }
0030 }
0031 
0032 int LogListModel::rowCount(const QModelIndex &parent) const
0033 {
0034     if (parent.isValid()) {
0035         return 0;
0036     }
0037 
0038     return m_logsData.size();
0039 }
0040 
0041 QVariant LogListModel::data(const QModelIndex &index, int role) const
0042 {
0043     const auto checkIndexFlags = QAbstractItemModel::CheckIndexOption::IndexIsValid | QAbstractItemModel::CheckIndexOption::ParentIsInvalid;
0044 
0045     if (!checkIndex(index, checkIndexFlags)) {
0046         return {};
0047     }
0048 
0049     LogData data = m_logsData.at(index.row());
0050     switch (role) {
0051     case SourceAddressRole:
0052         return data.sourceAddress;
0053     case SourcePortRole:
0054         return data.sourcePort;
0055     case DestinationAddressRole:
0056         return data.destinationAddress;
0057     case DestinationPortRole:
0058         return data.destinationPort;
0059     case ProtocolRole:
0060         return data.protocol;
0061     case InterfaceRole:
0062         return data.interface;
0063     case ActionRole:
0064         return data.action;
0065     case TimeRole:
0066         return data.time;
0067     case DateRole:
0068         return data.date;
0069     };
0070 
0071     return {};
0072 }
0073 
0074 QHash<int, QByteArray> LogListModel::roleNames() const
0075 {
0076     return {
0077         {SourceAddressRole, "sourceAddress"},
0078         {SourcePortRole, "sourcePort"},
0079         {DestinationAddressRole, "destinationAddress"},
0080         {DestinationPortRole, "destinationPort"},
0081         {ProtocolRole, "protocol"},
0082         {InterfaceRole, "interface"},
0083         {ActionRole, "action"},
0084         {TimeRole, "time"},
0085         {DateRole, "date"},
0086     };
0087 }
0088 
0089 void LogListModel::appendLogData(const QVector<LogData> &newData)
0090 {
0091     if (newData.isEmpty()) {
0092         return;
0093     }
0094     beginResetModel();
0095     m_logsData = newData;
0096     endResetModel();
0097     Q_EMIT countChanged();
0098 }