File indexing completed on 2024-05-05 05:30:35

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QDBusContext>
0010 #include <QObject>
0011 
0012 #include <systemstats/SensorInfo.h>
0013 
0014 namespace KSysGuard
0015 {
0016     class SensorPlugin;
0017     class SensorContainer;
0018     class SensorProperty;
0019 }
0020 
0021 class Client;
0022 class QDBusServiceWatcher;
0023 
0024 /**
0025  * The main central application
0026  */
0027 class Daemon : public QObject, public QDBusContext
0028 {
0029     Q_OBJECT
0030 public:
0031     enum class ReplaceIfRunning {
0032         Replace,
0033         DoNotReplace
0034     };
0035 
0036     Daemon();
0037     ~Daemon();
0038     bool init(ReplaceIfRunning replaceIfRunning);
0039     KSysGuard::SensorProperty *findSensor(const QString &path) const;
0040 
0041     void setQuitOnLastClientDisconnect(bool quit);
0042 
0043 public Q_SLOTS:
0044     // DBus
0045     KSysGuard::SensorInfoMap allSensors() const;
0046     KSysGuard::SensorInfoMap sensors(const QStringList &sensorsIds) const;
0047 
0048     void subscribe(const QStringList &sensorIds);
0049     void unsubscribe(const QStringList &sensorIds);
0050 
0051     KSysGuard::SensorDataList sensorData(const QStringList &sensorIds);
0052 
0053 Q_SIGNALS:
0054     // DBus
0055     void sensorAdded(const QString &sensorId);
0056     void sensorRemoved(const QString &sensorId);
0057     // not emitted directly as we use targetted signals via lower level API
0058     void newSensorData(const KSysGuard::SensorDataList &sensorData);
0059 
0060 protected:
0061     // virtual for autotest to override and not load real plugins
0062     virtual void loadProviders();
0063 
0064     void sendFrame();
0065     void registerProvider(KSysGuard::SensorPlugin *);
0066 
0067 private:
0068     void onServiceDisconnected(const QString &service);
0069     bool registerDBusService(const QString &serviceName, ReplaceIfRunning replace);
0070 
0071     QList<KSysGuard::SensorPlugin *> m_providers;
0072     QHash<QString /*subscriber DBus base name*/, Client*> m_clients;
0073     QHash<QString /*id*/, KSysGuard::SensorContainer *> m_containers;
0074     QDBusServiceWatcher *m_serviceWatcher;
0075     bool m_quitOnLastClientDisconnect = true;
0076 };