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

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "process_attribute.h"
0008 #include "cgroup.h"
0009 
0010 #include <QMetaMethod>
0011 
0012 using namespace KSysGuard;
0013 
0014 class Q_DECL_HIDDEN KSysGuard::ProcessAttribute::Private
0015 {
0016 public:
0017     QString m_id;
0018 
0019     QString m_name;
0020     QString m_shortName;
0021     QString m_description;
0022     qreal m_min = 0;
0023     qreal m_max = 0;
0024     KSysGuard::Unit m_unit = KSysGuard::UnitInvalid; // Both a format hint and implies data type (i.e double/string)
0025 
0026     QHash<KSysGuard::Process *, QVariant> m_data;
0027     int m_watchCount = 0;
0028 
0029     bool m_defaultVisible = false;
0030 
0031     Processes::UpdateFlags m_updateFlags = Processes::StandardInformation;
0032 };
0033 
0034 ProcessAttribute::ProcessAttribute(const QString &id, QObject *parent)
0035     : ProcessAttribute(id, QString(), parent)
0036 {
0037 }
0038 
0039 ProcessAttribute::ProcessAttribute(const QString &id, const QString &name, QObject *parent)
0040     : QObject(parent)
0041     , d(new Private)
0042 {
0043     d->m_id = id;
0044     d->m_name = name;
0045 }
0046 
0047 ProcessAttribute::~ProcessAttribute()
0048 {
0049 }
0050 
0051 QString ProcessAttribute::id() const
0052 {
0053     return d->m_id;
0054 }
0055 
0056 bool ProcessAttribute::enabled() const
0057 {
0058     return d->m_watchCount > 0;
0059 }
0060 
0061 QString ProcessAttribute::name() const
0062 {
0063     return d->m_name;
0064 }
0065 
0066 void ProcessAttribute::setName(const QString &name)
0067 {
0068     d->m_name = name;
0069 }
0070 
0071 QString ProcessAttribute::shortName() const
0072 {
0073     return d->m_shortName.isEmpty() ? d->m_name : d->m_shortName;
0074 }
0075 
0076 void ProcessAttribute::setShortName(const QString &name)
0077 {
0078     d->m_shortName = name;
0079 }
0080 
0081 QString ProcessAttribute::description() const
0082 {
0083     return d->m_description;
0084 }
0085 
0086 void ProcessAttribute::setDescription(const QString &description)
0087 {
0088     d->m_description = description;
0089 }
0090 
0091 qreal ProcessAttribute::min() const
0092 {
0093     return d->m_min;
0094 }
0095 
0096 void ProcessAttribute::setMin(const qreal min)
0097 {
0098     d->m_min = min;
0099 }
0100 
0101 qreal ProcessAttribute::max() const
0102 {
0103     return d->m_max;
0104 }
0105 
0106 void ProcessAttribute::setMax(const qreal max)
0107 {
0108     d->m_max = max;
0109 }
0110 
0111 KSysGuard::Unit ProcessAttribute::unit() const
0112 {
0113     return d->m_unit;
0114 }
0115 
0116 void ProcessAttribute::setUnit(KSysGuard::Unit unit)
0117 {
0118     d->m_unit = unit;
0119 }
0120 
0121 bool KSysGuard::ProcessAttribute::isVisibleByDefault() const
0122 {
0123     return d->m_defaultVisible;
0124 }
0125 
0126 void KSysGuard::ProcessAttribute::setVisibleByDefault(bool visible)
0127 {
0128     d->m_defaultVisible = visible;
0129 }
0130 
0131 Processes::UpdateFlags ProcessAttribute::requiredUpdateFlags() const
0132 {
0133     return d->m_updateFlags;
0134 }
0135 
0136 void ProcessAttribute::setRequiredUpdateFlags(Processes::UpdateFlags flags)
0137 {
0138     d->m_updateFlags = flags;
0139 }
0140 
0141 QVariant ProcessAttribute::data(KSysGuard::Process *process) const
0142 {
0143     return d->m_data.value(process);
0144 }
0145 
0146 void ProcessAttribute::setData(KSysGuard::Process *process, const QVariant &value)
0147 {
0148     d->m_data[process] = value;
0149     Q_EMIT dataChanged(process);
0150 }
0151 
0152 void ProcessAttribute::clearData(KSysGuard::Process *process)
0153 {
0154     d->m_data.remove(process);
0155     Q_EMIT dataChanged(process);
0156 }
0157 
0158 QVariant ProcessAttribute::cgroupData(KSysGuard::CGroup *cgroup, const QList<KSysGuard::Process *> &groupProcesses) const
0159 {
0160     Q_UNUSED(cgroup)
0161 
0162     if (groupProcesses.isEmpty()) {
0163         return QVariant{};
0164     }
0165 
0166     qreal total = std::accumulate(groupProcesses.constBegin(), groupProcesses.constEnd(), 0.0, [this](qreal total, KSysGuard::Process *process) {
0167         return total + data(process).toDouble();
0168     });
0169     return QVariant(total);
0170 }
0171 
0172 void ProcessAttribute::connectNotify(const QMetaMethod &signal)
0173 {
0174     if (signal != QMetaMethod::fromSignal(&ProcessAttribute::dataChanged)) {
0175         return;
0176     }
0177     d->m_watchCount++;
0178     if (d->m_watchCount == 1) {
0179         Q_EMIT enabledChanged(true);
0180     }
0181 }
0182 
0183 void ProcessAttribute::disconnectNotify(const QMetaMethod &signal)
0184 {
0185     if (signal.isValid() && signal != QMetaMethod::fromSignal(&ProcessAttribute::dataChanged)) {
0186         return;
0187     }
0188     d->m_watchCount--;
0189     if (d->m_watchCount == 0) {
0190         Q_EMIT enabledChanged(false);
0191     }
0192 }