1 /* 2 KSysGuard, the KDE System Guard 3 4 Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org> 5 6 This program is free software; you can redistribute it and/or 7 modify it under the terms of the GNU General Public License as 8 published by the Free Software Foundation; either version 2 of 9 the License or (at your option) version 3 or any later version 10 accepted by the membership of KDE e.V. (or its successor approved 11 by the membership of KDE e.V.), which shall act as a proxy 12 defined in Section 14 of version 3 of the license. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. 21 */ 22 23 #ifndef KSG_SENSORBROWSER_H 24 #define KSG_SENSORBROWSER_H 25 26 #include <QMouseEvent> 27 #include <QTreeWidget> 28 #include <QMap> 29 #include <QHash> 30 #include <ksgrd/SensorClient.h> 31 #include <QSortFilterProxyModel> 32 33 class QMouseEvent; 34 35 namespace KSGRD { 36 class SensorManager; 37 class SensorAgent; 38 } 39 40 class SensorInfo; 41 class HostInfo; 42 43 class SensorBrowserModel : public QAbstractItemModel, private KSGRD::SensorClient 44 { 45 Q_OBJECT 46 public: 47 SensorBrowserModel(); 48 ~SensorBrowserModel() override; 49 int columnCount( const QModelIndex &) const override; 50 QVariant data( const QModelIndex & parent, int role) const override; 51 QVariant headerData ( int section, Qt::Orientation orientation, int role) const override; 52 QModelIndex index ( int row, int column, const QModelIndex & parent) const override; 53 QModelIndex parent ( const QModelIndex & index ) const override; 54 int rowCount ( const QModelIndex & parent = QModelIndex() ) const override; 55 56 57 QStringList listSensors( const QString &hostName ) const; ///Returns a list of sensors names. E.g. (cpu/0, mem/free, mem/cache, etc) 58 QStringList listSensors( int parentId ) const; ///Used recursively by listSensor(QString) 59 QStringList listHosts( ) const; ///Returns a list of host names. E.g. (localhost, 192.168.0.1,...) 60 SensorInfo *getSensorInfo(QModelIndex index) const; getHostInfo(int hostId)61 HostInfo *getHostInfo(int hostId) const { return mHostInfoMap.value(hostId);} hasSensor(int hostId,const QString & sensor)62 bool hasSensor(int hostId, const QString &sensor) const { return mHostSensorsMap.value(hostId).contains(sensor);} 63 int makeTreeBranch(int parentId, const QString &name); 64 int makeSensor(HostInfo *hostInfo, int parentId, const QString &sensorName, const QString &name, const QString &sensorType); 65 void removeSensor(HostInfo *hostInfo, int parentId, const QString &sensorName); 66 void addHost(KSGRD::SensorAgent *sensorAgent, const QString &hostName); 67 void clear(); 68 void disconnectHost(uint id); 69 void disconnectHost(const HostInfo *hostInfo); 70 void disconnectHost(const QString &hostname); 71 Qt::ItemFlags flags ( const QModelIndex & index ) const override; 72 QMimeData * mimeData ( const QModelIndexList & indexes ) const override; 73 void retranslate(); /// Retranslate the model 74 Q_SIGNALS: 75 void sensorsAddedToHost(const QModelIndex &index ); 76 public Q_SLOTS: 77 void update(); 78 void hostAdded(KSGRD::SensorAgent *sensorAgent, const QString &hostName); 79 /** 80 * Remove host from this model. The proper way this is called is with the signal in SensorManager 81 * (from disengage), otherwise 82 * if this is called directly the SensorManager container will be out of sync with ours. Calling disconnectHost 83 * from this object will also call disengage. 84 */ 85 void hostRemoved(const QString &hostName); 86 87 private: 88 void answerReceived( int id, const QList<QByteArray>& ) override; 89 void removeEmptyParentTreeBranches(int hostId, int id, int parentid); 90 HostInfo* findHostInfoByHostName(const QString &hostName) const; 91 void removeAllSensorUnderBranch(HostInfo* hostInfo, int parentId); 92 93 int mIdCount; ///The lowest id that has not been used yet 94 QMap<int, HostInfo*> mHostInfoMap; ///So each host has a number 95 QHash<int, QList<int> > mTreeMap; ///This describes the structure of the tree. It maps a parent branch number (which can be equal to the hostinfo number if it's a host branch) to a list of children. The children themselves either have branches in the map, or else just relate to a sensor info 96 QHash<int, int > mParentsTreeMap; /// 97 QHash<int, QString> mTreeNodeNames; ///Maps the mTreeMap node id's to (translated) names 98 QHash<int, QHash<QString,bool> > mHostSensorsMap; ///Maps a host id to a hash of sensor names. Let's us quickly check if a sensor is registered for a given host. bool is just ignored 99 QHash<int, SensorInfo *> mSensorInfoMap; ///Each sensor has a unique number as well. This relates to the ID in mTreeMap 100 }; 101 102 class SensorBrowserTreeWidget : public QTreeView 103 { 104 Q_OBJECT 105 106 public: 107 SensorBrowserTreeWidget( QWidget* parent, KSGRD::SensorManager* sm ); 108 ~SensorBrowserTreeWidget() override; 109 listHosts()110 QStringList listHosts() const 111 { return mSensorBrowserModel.listHosts(); } listSensors(const QString & hostName)112 QStringList listSensors( const QString &hostName ) const 113 { return mSensorBrowserModel.listSensors(hostName); } model()114 QSortFilterProxyModel & model() 115 { return mSortFilterProxyModel; } 116 117 public Q_SLOTS: 118 void disconnect(); 119 void hostReconfigured( const QString &hostName ); 120 protected Q_SLOTS: 121 void expandItem(const QModelIndex& model_index); 122 void updateView(); 123 private: 124 void retranslateUi(); 125 void changeEvent( QEvent * event ) override; 126 127 KSGRD::SensorManager* mSensorManager; 128 129 QString mDragText; 130 SensorBrowserModel mSensorBrowserModel; 131 QSortFilterProxyModel mSortFilterProxyModel; 132 }; 133 134 /** 135 * The SensorBrowserWidget is the graphical front-end of the SensorManager. It 136 * displays the currently available hosts and their sensors. 137 */ 138 class SensorBrowserWidget : public QWidget 139 { 140 Q_OBJECT 141 public: 142 explicit SensorBrowserWidget( QWidget* parent, KSGRD::SensorManager* sm ); 143 ~SensorBrowserWidget(); listHosts()144 QStringList listHosts() const 145 { return m_treeWidget->listHosts(); } listSensors(const QString & hostName)146 QStringList listSensors( const QString &hostName ) const 147 { return m_treeWidget->listSensors(hostName); } 148 149 private: 150 SensorBrowserTreeWidget *m_treeWidget; 151 152 }; 153 154 class SensorInfo 155 { 156 public: 157 SensorInfo( HostInfo *hostInfo, const QString &name, 158 const QString &description, const QString &type ); 159 160 /** 161 Returns the name of the sensor. e.g. "cpu/free". Not translated. 162 */ 163 QString name() const; 164 165 /** 166 Returns the description of the sensor. e.g. "free" 167 */ 168 QString description() const; 169 170 /** 171 Returns the type of the sensor. e.g. "Integer" 172 */ 173 QString type() const; 174 175 /** 176 Returns the host that this sensor is on. 177 */ 178 HostInfo *hostInfo() const; 179 180 private: 181 QString mName; 182 QString mDesc; 183 QString mType; 184 HostInfo *mHostInfo; 185 }; 186 187 class HostInfo 188 { 189 public: HostInfo(int id,KSGRD::SensorAgent * agent,const QString & name)190 HostInfo( int id, KSGRD::SensorAgent *agent, const QString &name) : mId(id), mSensorAgent(agent), mHostName(name) {} 191 192 /** 193 Returns the unique id of the host. 194 */ id()195 int id() const {return mId;} 196 197 /** 198 Returns a pointer to the sensor agent of the host. 199 */ sensorAgent()200 KSGRD::SensorAgent* sensorAgent() const {return mSensorAgent;} 201 202 /** 203 Returns the name of the host. 204 */ hostName()205 QString hostName() const { return mHostName;} 206 207 private: 208 int mId; 209 KSGRD::SensorAgent* mSensorAgent; 210 const QString mHostName; 211 }; 212 213 #endif 214