programsensor.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Hans Karlsson                                   *
00003  *   karlsson.h@home.se                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 #include "programsensor.h"
00011 
00012 #include <qstringlist.h>
00013 ProgramSensor::ProgramSensor(const QString &progName, int interval, QString encoding )
00014         : Sensor( interval )
00015 {
00016      if( !encoding.isEmpty())
00017     {
00018         codec = QTextCodec::codecForName( encoding.ascii() );
00019         if ( codec == 0)
00020             codec = QTextCodec::codecForLocale();
00021     }
00022     else
00023         codec = QTextCodec::codecForLocale();
00024 
00025 
00026     programName = progName;
00027     //update();
00028     connect(&ksp, SIGNAL(receivedStdout(KProcess *, char *, int )),
00029             this,SLOT(receivedStdout(KProcess *, char *, int )));
00030     connect(&ksp, SIGNAL(processExited(KProcess *)),
00031             this,SLOT(processExited( KProcess * )));
00032 }
00033 
00034 ProgramSensor::~ProgramSensor()
00035 {}
00036 
00037 void ProgramSensor::receivedStdout(KProcess *, char *buffer, int len)
00038 {
00039     buffer[len] = 0;
00040     sensorResult += codec->toUnicode( QCString(buffer) );
00041 }
00042 
00043 void ProgramSensor::processExited(KProcess *)
00044 {
00045     int lineNbr;
00046     SensorParams *sp;
00047     Meter *meter;
00048     QValueVector<QString> lines;
00049     QStringList stringList = QStringList::split('\n',sensorResult,true);
00050     QStringList::ConstIterator end( stringList.end() );
00051     for ( QStringList::ConstIterator it = stringList.begin(); it != end; ++it )
00052     {
00053         lines.push_back(*it);
00054     }
00055 
00056     int count = (int) lines.size();
00057     QObjectListIt it( *objList );
00058     while (it != 0)
00059     {
00060         sp = (SensorParams*)(*it);
00061         meter = sp->getMeter();
00062         if( meter != 0)
00063         {
00064             lineNbr = (sp->getParam("LINE")).toInt();
00065             if ( lineNbr >= 1  && lineNbr <=  (int) count )
00066             {
00067                 meter->setValue(lines[lineNbr-1]);
00068             }
00069             if ( -lineNbr >= 1 && -lineNbr <= (int) count )
00070             {
00071                 meter->setValue(lines[count+lineNbr]);
00072             }
00073             if (lineNbr == 0)
00074             {
00075                 meter->setValue(sensorResult);
00076             }
00077         }
00078         ++it;
00079     }
00080 
00081     sensorResult = "";
00082 }
00083 
00084 void ProgramSensor::update()
00085 {
00086     ksp.clearArguments();
00087     ksp << programName;
00088 
00089 
00090     ksp.start( KProcIO::NotifyOnExit,KProcIO::Stdout);
00091 }
00092 
00093 #include "programsensor.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys