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

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2018 Alexis Lopes Zubeta <contact@azubieta.net>
0003 // SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@kde.org>
0004 
0005 #ifndef CONECTIONSMODEL_H
0006 #define CONECTIONSMODEL_H
0007 
0008 #include <QAbstractListModel>
0009 #include <QTimer>
0010 
0011 #include <QLoggingCategory>
0012 
0013 #include "netstathelper.h"
0014 
0015 Q_DECLARE_LOGGING_CATEGORY(ConnectionsModelDebug)
0016 
0017 struct ConnectionsData {
0018     QString protocol;
0019     QString localAddress;
0020     QString foreignAddress;
0021     QString status;
0022     QString pid;
0023     QString program;
0024 
0025     bool operator==(const ConnectionsData &other) const
0026     {
0027         return other.protocol == protocol && other.localAddress == localAddress && other.foreignAddress == foreignAddress && other.status == status
0028             && other.pid == pid && other.program == program;
0029     }
0030 };
0031 
0032 class ConnectionsModel : public QAbstractListModel
0033 {
0034     Q_OBJECT
0035 
0036     Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
0037     Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
0038 
0039 public:
0040     enum ConnectionsModelRoles { ProtocolRole = Qt::UserRole + 1, LocalAddressRole, ForeignAddressRole, StatusRole, PidRole, ProgramRole };
0041     Q_ENUM(ConnectionsModelRoles)
0042 
0043     explicit ConnectionsModel(QObject *parent = nullptr);
0044 
0045     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0046 
0047     bool busy() const;
0048 
0049     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0050 
0051     QHash<int, QByteArray> roleNames() const override;
0052 
0053     Q_INVOKABLE void start();
0054     Q_INVOKABLE void stop();
0055 
0056 Q_SIGNALS:
0057     void countChanged();
0058     void busyChanged();
0059     void showErrorMessage(const QString &message);
0060 
0061 protected slots:
0062     void refreshConnections(const QVector<QStringList> &results);
0063 
0064 private:
0065     QVector<ConnectionsData> m_connectionsData;
0066     QTimer timer;
0067     bool m_busy = false;
0068     NetstatHelper m_netstatHelper;
0069 };
0070 
0071 #endif // CONECTIONSMODEL_H