Main Page   Class Hierarchy   Alphabetical List   Compound List   File List  

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(QString progName, int interval, QString encoding )
00014         : Sensor( interval )
00015 {
00016      if( encoding != "" )
00017     {
00018         codec = QTextCodec::codecForName( encoding );
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     for ( QStringList::Iterator it = stringList.begin(); it != stringList.end(); ++it )
00051     {
00052         lines.push_back(*it);
00053     }
00054 
00055     int count = (int) lines.size();
00056     QObjectListIt it( *objList );
00057     while (it != 0)
00058     {
00059         sp = (SensorParams*)(*it);
00060         meter = sp->getMeter();
00061         if( meter != 0)
00062         {
00063             lineNbr = (sp->getParam("LINE")).toInt();
00064             if ( lineNbr >= 1  && lineNbr <=  (int) count )
00065             {
00066                 meter->setValue(lines[lineNbr-1]);
00067             }
00068             if ( -lineNbr >= 1 && -lineNbr <= (int) count )
00069             {
00070                 meter->setValue(lines[count+lineNbr]);
00071             }
00072             if (lineNbr == 0)
00073             {
00074                 meter->setValue(sensorResult);
00075             }
00076         }
00077         ++it;
00078     }
00079 
00080     sensorResult = "";
00081 }
00082 
00083 void ProgramSensor::update()
00084 {
00085     ksp.clearArguments();
00086     ksp << programName;
00087 
00088 
00089     ksp.start( KProcIO::NotifyOnExit,KProcIO::Stdout);
00090 }

Generated on Tue May 6 09:52:08 2003 for karamba by doxygen1.2.18