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 <tqstringlist.h> 00013 ProgramSensor::ProgramSensor(const TQString &progName, int interval, TQString encoding ) 00014 : Sensor( interval ) 00015 { 00016 if( !encoding.isEmpty()) 00017 { 00018 codec = TQTextCodec::codecForName( encoding.ascii() ); 00019 if ( codec == 0) 00020 codec = TQTextCodec::codecForLocale(); 00021 } 00022 else 00023 codec = TQTextCodec::codecForLocale(); 00024 00025 00026 programName = progName; 00027 //update(); 00028 connect(&ksp, TQT_SIGNAL(receivedStdout(KProcess *, char *, int )), 00029 this,TQT_SLOT(receivedStdout(KProcess *, char *, int ))); 00030 connect(&ksp, TQT_SIGNAL(processExited(KProcess *)), 00031 this,TQT_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( TQCString(buffer) ); 00041 } 00042 00043 void ProgramSensor::processExited(KProcess *) 00044 { 00045 int lineNbr; 00046 SensorParams *sp; 00047 Meter *meter; 00048 TQValueVector<TQString> lines; 00049 TQStringList stringList = TQStringList::split('\n',sensorResult,true); 00050 TQStringList::ConstIterator end( stringList.end() ); 00051 for ( TQStringList::ConstIterator it = stringList.begin(); it != end; ++it ) 00052 { 00053 lines.push_back(*it); 00054 } 00055 00056 int count = (int) lines.size(); 00057 TQObjectListIt 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"