~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/systemmonitor/systemmonitor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 John Tapsell <tapsell@kde.org>
 
3
 *
 
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
 
7
 *
 
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
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
#include "systemmonitor.h"
 
20
 
 
21
#include <QTimer>
 
22
#include <QProcess>
 
23
 
 
24
#include <KDebug>
 
25
#include <KLocale>
 
26
 
 
27
#include <Plasma/DataContainer>
 
28
 
 
29
#include "../../ksysguard/gui/ksgrd/SensorManager.h"
 
30
 
 
31
SystemMonitorEngine::SystemMonitorEngine(QObject* parent, const QVariantList& args)
 
32
    : Plasma::DataEngine(parent)
 
33
{
 
34
    Q_UNUSED(args)
 
35
 
 
36
    KSGRD::SensorMgr = new KSGRD::SensorManager(this);
 
37
    KSGRD::SensorMgr->engage("localhost", "", "ksysguardd");
 
38
 
 
39
    m_waitingFor= 0;
 
40
    connect(KSGRD::SensorMgr, SIGNAL(update()), this, SLOT(updateMonitorsList()));
 
41
    updateMonitorsList();
 
42
}
 
43
 
 
44
SystemMonitorEngine::~SystemMonitorEngine()
 
45
{
 
46
}
 
47
 
 
48
void SystemMonitorEngine::updateMonitorsList()
 
49
{
 
50
    KSGRD::SensorMgr->sendRequest("localhost", "monitors", (KSGRD::SensorClient*)this, -1);
 
51
}
 
52
 
 
53
QStringList SystemMonitorEngine::sources() const
 
54
{
 
55
    return m_sensors;
 
56
}
 
57
 
 
58
bool SystemMonitorEngine::sourceRequestEvent(const QString &name)
 
59
{
 
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());
 
68
        return true;
 
69
    }
 
70
 
 
71
    return false;
 
72
}
 
73
 
 
74
bool SystemMonitorEngine::updateSourceEvent(const QString &sensorName)
 
75
{
 
76
    const int index = m_sensors.indexOf(sensorName);
 
77
 
 
78
    if (index != -1) {
 
79
        KSGRD::SensorMgr->sendRequest("localhost", sensorName, (KSGRD::SensorClient*)this, index);
 
80
        KSGRD::SensorMgr->sendRequest("localhost", QString("%1?").arg(sensorName), (KSGRD::SensorClient*)this, -(index + 2));
 
81
    }
 
82
 
 
83
    return false;
 
84
}
 
85
 
 
86
void SystemMonitorEngine::updateSensors()
 
87
{
 
88
    DataEngine::SourceDict sources = containerDict();
 
89
    DataEngine::SourceDict::iterator it = sources.begin();
 
90
    if (m_waitingFor != 0) {
 
91
        scheduleSourcesUpdated();
 
92
    }
 
93
 
 
94
    m_waitingFor = 0;
 
95
 
 
96
    while (it != sources.end()) {
 
97
        m_waitingFor++;
 
98
        QString sensorName = it.key();
 
99
        KSGRD::SensorMgr->sendRequest( "localhost", sensorName, (KSGRD::SensorClient*)this, -1);
 
100
        ++it;
 
101
    }
 
102
}
 
103
 
 
104
void SystemMonitorEngine::answerReceived(int id, const QList<QByteArray> &answer)
 
105
{
 
106
    if (id < -1) {
 
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);
 
110
            return;
 
111
        }
 
112
 
 
113
        DataEngine::SourceDict sources = containerDict();
 
114
        DataEngine::SourceDict::const_iterator it = sources.constFind(m_sensors.value(-id - 2));
 
115
 
 
116
        const QStringList newSensorInfo = QString::fromUtf8(answer[0]).split('\t');
 
117
 
 
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"];
 
123
            return;
 
124
        }
 
125
 
 
126
        const QString sensorName = newSensorInfo[0];
 
127
        const QString min = newSensorInfo[1];
 
128
        const QString max = newSensorInfo[2];
 
129
        const QString unit = newSensorInfo[3];
 
130
 
 
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();
 
137
        }
 
138
 
 
139
        return;
 
140
    }
 
141
 
 
142
    if (id == -1) {
 
143
        QSet<QString> sensors;
 
144
        m_sensors.clear();
 
145
        int count = 0;
 
146
 
 
147
        foreach (const QByteArray &sens, answer) {
 
148
            const QStringList newSensorInfo = QString::fromUtf8(sens).split('\t');
 
149
            if (newSensorInfo.count() < 2) {
 
150
                continue;
 
151
            }
 
152
            if(newSensorInfo.at(1) == "logfile")
 
153
                continue; // logfile data type not currently supported
 
154
 
 
155
            const QString newSensor = newSensorInfo[0];
 
156
            sensors.insert(newSensor);
 
157
            m_sensors.append(newSensor);
 
158
            {
 
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 );
 
163
                if ( s ) {
 
164
                    disconnect( s, SIGNAL(becameUnused(QString)), this, SLOT(removeSource(QString)) );
 
165
                }
 
166
            }
 
167
            DataEngine::Data d;
 
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));
 
172
            ++count;
 
173
        }
 
174
 
 
175
        QHash<QString, Plasma::DataContainer*> sourceDict = containerDict();
 
176
        QHashIterator<QString, Plasma::DataContainer*> it(sourceDict);
 
177
        while (it.hasNext()) {
 
178
            it.next();
 
179
            if (!sensors.contains(it.key())) {
 
180
                removeSource(it.key());
 
181
            }
 
182
        }
 
183
 
 
184
        return;
 
185
    }
 
186
 
 
187
    m_waitingFor--;
 
188
    QString reply;
 
189
    if (!answer.isEmpty()) {
 
190
        reply = QString::fromUtf8(answer[0]);
 
191
    }
 
192
 
 
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);
 
197
    }
 
198
 
 
199
    if (m_waitingFor == 0) {
 
200
        scheduleSourcesUpdated();
 
201
    }
 
202
}
 
203
 
 
204
void SystemMonitorEngine::sensorLost( int )
 
205
{
 
206
    m_waitingFor--;
 
207
}
 
208
 
 
209
#include "systemmonitor.moc"
 
210