Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

programsensor.cpp

Go to the documentation of this file.
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         ksp = new KProcess;
00017         if( encoding != "" )
00018         {
00019                 codec = QTextCodec::codecForName( encoding );
00020                 if ( codec == 0)
00021                 codec = QTextCodec::codecForLocale();
00022         }
00023         else{
00024                 codec = QTextCodec::codecForLocale();
00025         }
00026         ksp->setUseShell(true, "/bin/bash");
00027         programName = progName;
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         kdDebug("ProgramSensor") << "Recieving data from " << programName << endl;
00040         buffer[len] = 0;
00041         sensorResult += codec->toUnicode( QCString(buffer) );
00042 }
00043 
00044 void ProgramSensor::processExited(KProcess *)
00045 {
00046         kdDebug("ProgramSensor") << "Program " << programName << " exited." << endl;
00047         int lineNbr;
00048         SensorParams *sp;
00049         Meter *meter;
00050         QValueVector<QString> lines;
00051         QStringList stringList = QStringList::split('\n',sensorResult,true);
00052         for ( QStringList::Iterator it = stringList.begin(); it != stringList.end(); ++it )
00053         {
00054                 lines.push_back(*it);
00055         }
00056         
00057         int count = (int) lines.size();
00058         QObjectListIt it( *objList );
00059         while (it != 0)
00060         {
00061                 sp = (SensorParams*)(*it);
00062                 meter = sp->getMeter();
00063                 if( meter != 0)
00064                 {
00065                 lineNbr = (sp->getParam("LINE")).toInt();
00066                 if ( lineNbr >= 1  && lineNbr <=  (int) count )
00067                 {
00068                         meter->setValue(lines[lineNbr-1]);
00069                 }
00070                 if ( -lineNbr >= 1 && -lineNbr <= (int) count )
00071                 {
00072                         meter->setValue(lines[count+lineNbr]);
00073                 }
00074                 if (lineNbr == 0)
00075                 {
00076                         meter->setValue(sensorResult);
00077                 }
00078                 }
00079                 ++it;
00080         }
00081         
00082         sensorResult = "";
00083 }
00084 
00085 void ProgramSensor::update()
00086 {
00087         ksp->clearArguments();
00088         *ksp << programName;
00089         if(ksp->start( KProcIO::NotifyOnExit,KProcIO::Stdout)){
00090                 kdDebug("ProgramSensor") << "Starting program: " << programName << endl;
00091         }
00092         else{
00093                 kdDebug("ProgramSensor") << "Can't start program: " << programName << endl;
00094         }
00095 }

Generated on Mon May 16 13:59:20 2005 for karamba by  doxygen 1.3.9.1