File indexing completed on 2024-05-05 05:34:26

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