File indexing completed on 2024-04-28 05:31:35

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractItemModel>
0010 
0011 #include "process_attribute_model.h"
0012 
0013 #include "processcore_export.h"
0014 
0015 namespace KSysGuard
0016 {
0017 class CGroup;
0018 
0019 class CGroupDataModelPrivate;
0020 
0021 /**
0022  * @brief The CGroupDataModel class is a list model of all cgroups from a given root
0023  * Data is exposed as per ProcessDataModel with configurable columns
0024  *
0025  * Data is refreshed on a timer
0026  */
0027 class PROCESSCORE_EXPORT CGroupDataModel : public QAbstractItemModel
0028 {
0029     Q_OBJECT
0030     /**
0031      * @copydoc ProcessDataModel::availableAttributes
0032      */
0033     Q_PROPERTY(QStringList availableAttributes READ availableAttributes CONSTANT)
0034     /**
0035      * @copydoc ProcessDataModel::enabledAttributes
0036      */
0037     Q_PROPERTY(QStringList enabledAttributes READ enabledAttributes WRITE setEnabledAttributes NOTIFY enabledAttributesChanged)
0038     /**
0039      * @copydoc ProcessDataModel::attributesModel
0040      */
0041     Q_PROPERTY(QObject *attributesModel READ attributesModel CONSTANT)
0042     /**
0043      * @copydoc ProcessDataModel::enabled
0044      */
0045     Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
0046     /**
0047      * @copydoc setRoot
0048      */
0049     Q_PROPERTY(QString setRoot READ root WRITE setRoot NOTIFY rootChanged)
0050 
0051     Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged)
0052 
0053 public:
0054     CGroupDataModel(QObject *parent = nullptr);
0055     CGroupDataModel(const QString &root, QObject *parent = nullptr);
0056     ~CGroupDataModel() override;
0057 
0058     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0059     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0060     QVariant data(const QModelIndex &index, int role) const override;
0061     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0062     QModelIndex index(int row, int column, const QModelIndex &parent) const override;
0063     QModelIndex parent(const QModelIndex &child) const override;
0064     QHash<int, QByteArray> roleNames() const override;
0065 
0066     /**
0067      * @copydoc ProcessDataModel::availableAttributes
0068      */
0069     QStringList availableAttributes() const;
0070     /**
0071      * @copydoc ProcessDataModel::enabledAttributes
0072      */
0073     QStringList enabledAttributes() const;
0074     /**
0075      * @copydoc ProcessDataModel::setEnabledAttributes
0076      */
0077     void setEnabledAttributes(const QStringList &enabledAttributes);
0078 
0079     QModelIndex getQModelIndex(CGroup *cgroup, int column) const;
0080 
0081     /**
0082      * @copydoc ProcessDataModel::attributesModel
0083      */
0084     ProcessAttributeModel *attributesModel();
0085     /**
0086      * @copydoc ProcessDataModel::isEnabled
0087      */
0088     bool isEnabled() const;
0089     /**
0090      * @copydoc ProcessDataModel::setEnabled
0091      */
0092     void setEnabled(bool isEnabled);
0093 
0094     QString root() const;
0095     /**
0096      * Set the root cgroup to start listing from
0097      * e.g "user.slice/user-1000.slice"
0098      *
0099      * The default is blank
0100      */
0101     void setRoot(const QString &root);
0102 
0103     /**
0104      * Trigger an update of the model
0105      */
0106     void update();
0107 
0108     bool isAvailable() const;
0109 
0110 Q_SIGNALS:
0111     void enabledAttributesChanged();
0112     void enabledChanged();
0113     void rootChanged();
0114     void availableChanged();
0115 
0116 protected:
0117     virtual bool filterAcceptsCGroup(const QString &id);
0118 
0119 private:
0120     QScopedPointer<CGroupDataModelPrivate> d;
0121     void update(CGroup *node);
0122 };
0123 
0124 }