File indexing completed on 2024-05-12 05:35:57

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 #pragma once
0006 
0007 #include <QAbstractTableModel>
0008 #include <QTimer>
0009 
0010 #include <QLoggingCategory>
0011 
0012 #include "netstathelper.h"
0013 
0014 Q_DECLARE_LOGGING_CATEGORY(ConnectionsModelDebug)
0015 
0016 struct ConnectionsData {
0017     QString protocol;
0018     QString localAddress;
0019     QString foreignAddress;
0020     QString status;
0021     QString pid;
0022     QString program;
0023 
0024     bool operator==(const ConnectionsData &other) const
0025     {
0026         return other.protocol == protocol && other.localAddress == localAddress && other.foreignAddress == foreignAddress && other.status == status
0027             && other.pid == pid && other.program == program;
0028     }
0029 };
0030 
0031 class ConnectionsModel : public QAbstractTableModel
0032 {
0033     Q_OBJECT
0034 
0035     Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
0036     Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
0037 
0038 public:
0039     enum ConnectionsModelColumns { ProtocolColumn = 0, LocalAddressColumn, ForeignAddressColumn, StatusColumn, PidColumn, ProgramColumn };
0040     Q_ENUM(ConnectionsModelColumns)
0041 
0042     explicit ConnectionsModel(QObject *parent = nullptr);
0043 
0044     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0045     int columnCount(const QModelIndex &parent) const override;
0046 
0047     bool busy() const;
0048 
0049     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0050     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0051 
0052     Q_INVOKABLE void start();
0053     Q_INVOKABLE void stop();
0054 
0055 Q_SIGNALS:
0056     void countChanged();
0057     void busyChanged();
0058     void showErrorMessage(const QString &message);
0059 
0060 protected slots:
0061     void refreshConnections(const QList<QStringList> &results);
0062 
0063 private:
0064     QList<ConnectionsData> m_connectionsData;
0065     QTimer timer;
0066     bool m_busy = false;
0067     NetstatHelper m_netstatHelper;
0068 };