1 /*
2     KSysGuard, the KDE System Guard
3 
4     SPDX-FileCopyrightText: 1999, 2000 Chris Schlaeger <cs@kde.org>
5 
6     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
7 
8 */
9 
10 #ifndef KSG_SENSORMANAGER_H
11 #define KSG_SENSORMANAGER_H
12 
13 #include <kconfig.h>
14 
15 #include <QEvent>
16 #include <QHash>
17 #include <QObject>
18 #include <QPointer>
19 #include <QStringList>
20 
21 #include "SensorAgent.h"
22 
23 namespace KSGRD
24 {
25 class SensorManagerIterator;
26 
27 /**
28   The SensorManager handles all interaction with the connected
29   hosts. Connections to a specific hosts are handled by
30   SensorAgents. Use engage() to establish a connection and
31   disengage() to terminate the connection.
32  */
33 class Q_DECL_EXPORT SensorManager : public QObject
34 {
35     Q_OBJECT
36 
37     friend class SensorManagerIterator;
38 
39 public:
40     class Q_DECL_EXPORT MessageEvent : public QEvent
41     {
42     public:
43         MessageEvent(const QString &message);
44 
45         QString message() const;
46 
47     private:
48         QString mMessage;
49     };
50 
51     explicit SensorManager(QObject *parent = nullptr);
52     ~SensorManager() override;
53 
54     /*! Number of hosts connected to */
55     int count() const;
56 
57     bool engage(const QString &hostName, const QString &shell = QStringLiteral("ssh"), const QString &command = QLatin1String(""), int port = -1);
58     /* Returns true if we are connected or trying to connect to the host given
59      */
60     bool isConnected(const QString &hostName);
61     bool disengage(SensorAgent *agent);
62     bool disengage(const QString &hostName);
63     bool resynchronize(const QString &hostName);
64     void notify(const QString &msg) const;
65 
66     void setBroadcaster(QObject *wdg);
67 
68     bool sendRequest(const QString &hostName, const QString &request, SensorClient *client, int id = 0);
69 
70     const QString hostName(const SensorAgent *sensor) const;
71     bool hostInfo(const QString &host, QString &shell, QString &command, int &port);
72 
73     QString translateUnit(const QString &unit) const;
74     QString translateSensorPath(const QString &path) const;
75     QString translateSensorType(const QString &type) const;
76     QString translateSensor(const QString &u) const;
77 
78     void readProperties(const KConfigGroup &cfg);
79     void saveProperties(KConfigGroup &cfg);
80 
81     void disconnectClient(SensorClient *client);
82     /** Call to retranslate all the strings - for example if the language has changed */
83     void retranslate();
84 
85 public Q_SLOTS:
86     void reconfigure(const SensorAgent *agent);
87 
88 Q_SIGNALS:
89     void update();
90     void hostAdded(KSGRD::SensorAgent *sensorAgent, const QString &hostName);
91     void hostConnectionLost(const QString &hostName);
92 
93 protected:
94     QHash<QString, SensorAgent *> mAgents;
95 
96 private:
97     /**
98       These dictionary stores the localized versions of the sensor
99       descriptions and units.
100      */
101     QHash<QString, QString> mDescriptions;
102     QHash<QString, QString> mUnits;
103     QHash<QString, QString> mDict;
104     QHash<QString, QString> mTypes;
105 
106     /** Store the data from the config file to pass to the MostConnector dialog box*/
107     QStringList mHostList;
108     QStringList mCommandList;
109 
110     QPointer<QObject> mBroadcaster;
111 };
112 
113 Q_DECL_EXPORT extern SensorManager *SensorMgr;
114 
115 class Q_DECL_EXPORT SensorManagerIterator : public QHashIterator<QString, SensorAgent *>
116 {
117 public:
SensorManagerIterator(const SensorManager * sm)118     explicit SensorManagerIterator(const SensorManager *sm)
119         : QHashIterator<QString, SensorAgent *>(sm->mAgents)
120     {
121     }
122 
~SensorManagerIterator()123     ~SensorManagerIterator()
124     {
125     }
126 };
127 
128 }
129 
130 #endif
131