Skip to content

Commit

Permalink
* accept sources before we get our list of sensors
Browse files Browse the repository at this point in the history
* slightly more efficient grabbing of sensor list (though we can only make adding 300+ sources so fast)
* be careful with indexes in updateSourceEvent

svn path=/trunk/KDE/kdebase/workspace/; revision=1006182
  • Loading branch information
aseigo committed Aug 3, 2009
1 parent e2bf605 commit ba7444f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
59 changes: 42 additions & 17 deletions plasma/dataengines/systemmonitor/systemmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,31 @@ void SystemMonitorEngine::updateMonitorsList()

QStringList SystemMonitorEngine::sources() const
{
return m_sensors;
return m_sensors;
}

bool SystemMonitorEngine::sourceRequestEvent(const QString &name)
{
Q_UNUSED(name);
if (m_sensors.isEmpty()) {
// we don't have our first data yet, so let's trust the requester, at least fo rnow
// when we get our list of sensors later, then we'll know for sure and remove
// this source if they were wrong
setData(name, DataEngine::Data());
return true;
}

return false;
}

bool SystemMonitorEngine::updateSourceEvent(const QString &sensorName)
{
KSGRD::SensorMgr->sendRequest( "localhost", sensorName, (KSGRD::SensorClient*)this, m_sensors.indexOf(sensorName));
KSGRD::SensorMgr->sendRequest( "localhost", QString("%1?").arg(sensorName), (KSGRD::SensorClient*)this, -(m_sensors.indexOf(sensorName)+2));
const int index = m_sensors.indexOf(sensorName);

if (index != -1) {
KSGRD::SensorMgr->sendRequest("localhost", sensorName, (KSGRD::SensorClient*)this, index);
KSGRD::SensorMgr->sendRequest("localhost", QString("%1?").arg(sensorName), (KSGRD::SensorClient*)this, -(index + 2));
}

return false;
}

Expand All @@ -88,9 +100,9 @@ void SystemMonitorEngine::updateSensors()
void SystemMonitorEngine::answerReceived(int id, const QList<QByteArray> &answer)
{
if (id < -1) {
if (answer.isEmpty() || m_sensors.count() <= (-id - 2)) {
kDebug() << "sensor info answer was empty, (" << answer.isEmpty() << ") or sensors too small ("
<< m_sensors.count() << ") for index" << (-id - 2);
if (answer.isEmpty() || m_sensors.count() < (-id - 2)) {
kDebug() << "sensor info answer was empty, (" << answer.isEmpty() << ") or sensors does not exist to us ("
<< (m_sensors.count() < (-id - 2)) << ") for index" << (-id - 2);
return;
}

Expand Down Expand Up @@ -121,23 +133,36 @@ void SystemMonitorEngine::answerReceived(int id, const QList<QByteArray> &answer
}

if (id == -1) {
QStringList sensors;
QSet<QString> sensors;
m_sensors.clear();
int count = 0;

foreach (const QByteArray &sens, answer) {
const QStringList newSensorInfo = QString::fromUtf8(sens).split('\t');
const QString newSensor = newSensorInfo[0];
sensors.append(newSensor);
setData(newSensor, "value", QVariant());
setData(newSensor, "type", newSensorInfo[1]);
if (newSensorInfo.count() < 2) {
continue;
}

KSGRD::SensorMgr->sendRequest( "localhost", QString("%1?").arg(newSensor), (KSGRD::SensorClient*)this, -(sensors.indexOf(newSensor)+2));
const QString newSensor = newSensorInfo[0];
sensors.insert(newSensor);
m_sensors.append(newSensor);
DataEngine::Data d;
d.insert("value", QVariant());
d.insert("type", newSensorInfo[1]);
setData(newSensor, d);
KSGRD::SensorMgr->sendRequest( "localhost", QString("%1?").arg(newSensor), (KSGRD::SensorClient*)this, -(count + 2));
++count;
}
foreach(const QString& sensor, m_sensors) {
if (!sensors.contains(sensor)) {
removeSource(sensor);

QHash<QString, Plasma::DataContainer*> sourceDict = containerDict();
QHashIterator<QString, Plasma::DataContainer*> it(sourceDict);
while (it.hasNext()) {
it.next();
if (!sensors.contains(it.key())) {
removeSource(it.key());
}
}
m_sensors = sensors;

return;
}

Expand Down
9 changes: 4 additions & 5 deletions plasma/dataengines/systemmonitor/systemmonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ class SystemMonitorEngine : public Plasma::DataEngine, public KSGRD::SensorClien
Q_OBJECT

public:
/** Inherited from Plasma::DataEngine. Returns a list of all the sensors that ksysguardd knows about. */
/** Inherited from Plasma::DataEngine. Returns a list of all the sensors that ksysguardd knows about. */
virtual QStringList sources() const;
SystemMonitorEngine( QObject* parent, const QVariantList& args );
~SystemMonitorEngine();

protected:
bool sourceRequestEvent(const QString &name);
/** inherited from SensorClient */
/** inherited from SensorClient */
virtual void answerReceived( int id, const QList<QByteArray>&answer );
virtual void sensorLost( int );
virtual bool updateSourceEvent(const QString &sensorName);

virtual bool updateSourceEvent(const QString &sensorName);

protected slots:
void updateSensors();
Expand All @@ -55,7 +54,7 @@ class SystemMonitorEngine : public Plasma::DataEngine, public KSGRD::SensorClien
private:
QStringList m_sensors;
QTimer* m_timer;
int m_waitingFor;
int m_waitingFor;
};

K_EXPORT_PLASMA_DATAENGINE(systemmonitor, SystemMonitorEngine)
Expand Down

0 comments on commit ba7444f

Please sign in to comment.