2
* Copyright (C) 2007 John Tapsell <tapsell@kde.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Library General Public License version 2 as
6
* published by the Free Software Foundation
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details
13
* You should have received a copy of the GNU Library General Public
14
* License along with this program; if not, write to the
15
* Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#include "systemmonitor.h"
27
#include <Plasma/DataContainer>
29
#include "../../ksysguard/gui/ksgrd/SensorManager.h"
31
SystemMonitorEngine::SystemMonitorEngine(QObject* parent, const QVariantList& args)
32
: Plasma::DataEngine(parent)
36
KSGRD::SensorMgr = new KSGRD::SensorManager(this);
37
KSGRD::SensorMgr->engage("localhost", "", "ksysguardd");
40
connect(KSGRD::SensorMgr, SIGNAL(update()), this, SLOT(updateMonitorsList()));
44
SystemMonitorEngine::~SystemMonitorEngine()
48
void SystemMonitorEngine::updateMonitorsList()
50
KSGRD::SensorMgr->sendRequest("localhost", "monitors", (KSGRD::SensorClient*)this, -1);
53
QStringList SystemMonitorEngine::sources() const
58
bool SystemMonitorEngine::sourceRequestEvent(const QString &name)
60
// NB: do not follow this example in your own data engines!
61
// This is kept for backwards compatilibility.
62
// Visualizations should instead listen to sourceAdded()
63
if (m_sensors.isEmpty()) {
64
// we don't have our first data yet, so let's trust the requester, at least fo rnow
65
// when we get our list of sensors later, then we'll know for sure and remove
66
// this source if they were wrong
67
setData(name, DataEngine::Data());
74
bool SystemMonitorEngine::updateSourceEvent(const QString &sensorName)
76
const int index = m_sensors.indexOf(sensorName);
79
KSGRD::SensorMgr->sendRequest("localhost", sensorName, (KSGRD::SensorClient*)this, index);
80
KSGRD::SensorMgr->sendRequest("localhost", QString("%1?").arg(sensorName), (KSGRD::SensorClient*)this, -(index + 2));
86
void SystemMonitorEngine::updateSensors()
88
DataEngine::SourceDict sources = containerDict();
89
DataEngine::SourceDict::iterator it = sources.begin();
90
if (m_waitingFor != 0) {
91
scheduleSourcesUpdated();
96
while (it != sources.end()) {
98
QString sensorName = it.key();
99
KSGRD::SensorMgr->sendRequest( "localhost", sensorName, (KSGRD::SensorClient*)this, -1);
104
void SystemMonitorEngine::answerReceived(int id, const QList<QByteArray> &answer)
107
if (answer.isEmpty() || m_sensors.count() <= (-id - 2)) {
108
kDebug() << "sensor info answer was empty, (" << answer.isEmpty() << ") or sensors does not exist to us ("
109
<< (m_sensors.count() < (-id - 2)) << ") for index" << (-id - 2);
113
DataEngine::SourceDict sources = containerDict();
114
DataEngine::SourceDict::const_iterator it = sources.constFind(m_sensors.value(-id - 2));
116
const QStringList newSensorInfo = QString::fromUtf8(answer[0]).split('\t');
118
if (newSensorInfo.count() < 4) {
119
kDebug() << "bad sensor info, only" << newSensorInfo.count()
120
<< "entries, and we were expecting 4. Answer was " << answer;
121
if(it != sources.constEnd())
122
kDebug() << "value =" << it.value()->data()["value"] << "type=" << it.value()->data()["type"];
126
const QString sensorName = newSensorInfo[0];
127
const QString min = newSensorInfo[1];
128
const QString max = newSensorInfo[2];
129
const QString unit = newSensorInfo[3];
131
if (it != sources.constEnd()) {
132
it.value()->setData("name", sensorName);
133
it.value()->setData("min", min);
134
it.value()->setData("max", max);
135
it.value()->setData("units", unit);
136
scheduleSourcesUpdated();
143
QSet<QString> sensors;
147
foreach (const QByteArray &sens, answer) {
148
const QStringList newSensorInfo = QString::fromUtf8(sens).split('\t');
149
if (newSensorInfo.count() < 2) {
152
if(newSensorInfo.at(1) == "logfile")
153
continue; // logfile data type not currently supported
155
const QString newSensor = newSensorInfo[0];
156
sensors.insert(newSensor);
157
m_sensors.append(newSensor);
159
// HACK: for backwards compability
160
// in case this source was created in sourceRequestEvent, stop it being
161
// automagically removed when disconnected from
162
Plasma::DataContainer *s = containerForSource( newSensor );
164
disconnect( s, SIGNAL(becameUnused(QString)), this, SLOT(removeSource(QString)) );
168
d.insert("value", QVariant());
169
d.insert("type", newSensorInfo[1]);
170
setData(newSensor, d);
171
KSGRD::SensorMgr->sendRequest( "localhost", QString("%1?").arg(newSensor), (KSGRD::SensorClient*)this, -(count + 2));
175
QHash<QString, Plasma::DataContainer*> sourceDict = containerDict();
176
QHashIterator<QString, Plasma::DataContainer*> it(sourceDict);
177
while (it.hasNext()) {
179
if (!sensors.contains(it.key())) {
180
removeSource(it.key());
189
if (!answer.isEmpty()) {
190
reply = QString::fromUtf8(answer[0]);
193
DataEngine::SourceDict sources = containerDict();
194
DataEngine::SourceDict::const_iterator it = sources.constFind(m_sensors.value(id));
195
if (it != sources.constEnd()) {
196
it.value()->setData("value", reply);
199
if (m_waitingFor == 0) {
200
scheduleSourcesUpdated();
204
void SystemMonitorEngine::sensorLost( int )
209
#include "systemmonitor.moc"