File indexing completed on 2024-05-05 17:39:48

0001 /*
0002     KSysGuard, the KDE System Guard
0003 
0004     SPDX-FileCopyrightText: 1999, 2000 Chris Schlaeger <cs@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 
0008 */
0009 
0010 #ifndef KSG_SENSORMANAGER_H
0011 #define KSG_SENSORMANAGER_H
0012 
0013 #include <kconfig.h>
0014 
0015 #include <QEvent>
0016 #include <QHash>
0017 #include <QObject>
0018 #include <QPointer>
0019 #include <QStringList>
0020 
0021 #include "SensorAgent.h"
0022 
0023 namespace KSGRD
0024 {
0025 class SensorManagerIterator;
0026 
0027 /**
0028   The SensorManager handles all interaction with the connected
0029   hosts. Connections to a specific hosts are handled by
0030   SensorAgents. Use engage() to establish a connection and
0031   disengage() to terminate the connection.
0032  */
0033 class Q_DECL_EXPORT SensorManager : public QObject
0034 {
0035     Q_OBJECT
0036 
0037     friend class SensorManagerIterator;
0038 
0039 public:
0040     class Q_DECL_EXPORT MessageEvent : public QEvent
0041     {
0042     public:
0043         MessageEvent(const QString &message);
0044 
0045         QString message() const;
0046 
0047     private:
0048         QString mMessage;
0049     };
0050 
0051     explicit SensorManager(QObject *parent = nullptr);
0052     ~SensorManager() override;
0053 
0054     /*! Number of hosts connected to */
0055     int count() const;
0056 
0057     bool engage(const QString &hostName, const QString &shell = QStringLiteral("ssh"), const QString &command = QLatin1String(""), int port = -1);
0058     /* Returns true if we are connected or trying to connect to the host given
0059      */
0060     bool isConnected(const QString &hostName);
0061     bool disengage(SensorAgent *agent);
0062     bool disengage(const QString &hostName);
0063     bool resynchronize(const QString &hostName);
0064     void notify(const QString &msg) const;
0065 
0066     void setBroadcaster(QObject *wdg);
0067 
0068     bool sendRequest(const QString &hostName, const QString &request, SensorClient *client, int id = 0);
0069 
0070     const QString hostName(const SensorAgent *sensor) const;
0071     bool hostInfo(const QString &host, QString &shell, QString &command, int &port);
0072 
0073     QString translateUnit(const QString &unit) const;
0074     QString translateSensorPath(const QString &path) const;
0075     QString translateSensorType(const QString &type) const;
0076     QString translateSensor(const QString &u) const;
0077 
0078     void readProperties(const KConfigGroup &cfg);
0079     void saveProperties(KConfigGroup &cfg);
0080 
0081     void disconnectClient(SensorClient *client);
0082     /** Call to retranslate all the strings - for example if the language has changed */
0083     void retranslate();
0084 
0085 public Q_SLOTS:
0086     void reconfigure(const SensorAgent *agent);
0087 
0088 Q_SIGNALS:
0089     void update();
0090     void hostAdded(KSGRD::SensorAgent *sensorAgent, const QString &hostName);
0091     void hostConnectionLost(const QString &hostName);
0092 
0093 protected:
0094     QHash<QString, SensorAgent *> mAgents;
0095 
0096 private:
0097     /**
0098       These dictionary stores the localized versions of the sensor
0099       descriptions and units.
0100      */
0101     QHash<QString, QString> mDescriptions;
0102     QHash<QString, QString> mUnits;
0103     QHash<QString, QString> mDict;
0104     QHash<QString, QString> mTypes;
0105 
0106     /** Store the data from the config file to pass to the MostConnector dialog box*/
0107     QStringList mHostList;
0108     QStringList mCommandList;
0109 
0110     QPointer<QObject> mBroadcaster;
0111 };
0112 
0113 Q_DECL_EXPORT extern SensorManager *SensorMgr;
0114 
0115 class Q_DECL_EXPORT SensorManagerIterator : public QHashIterator<QString, SensorAgent *>
0116 {
0117 public:
0118     explicit SensorManagerIterator(const SensorManager *sm)
0119         : QHashIterator<QString, SensorAgent *>(sm->mAgents)
0120     {
0121     }
0122 
0123     ~SensorManagerIterator()
0124     {
0125     }
0126 };
0127 
0128 }
0129 
0130 #endif