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

disksensor.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 "disksensor.h"
00011 
00012 #include <qfile.h>
00013 #include <qtextstream.h>
00014 #include <qstring.h>
00015 #include <qregexp.h>
00016 #include <kprocess.h>
00017 #include <kprocio.h>
00018 
00019 DiskSensor::DiskSensor( int msec ) : Sensor( msec )
00020 {
00021     connect(&ksp, SIGNAL(receivedStdout(KProcess *, char *, int )),
00022             this,SLOT(receivedStdout(KProcess *, char *, int )));
00023     connect(&ksp, SIGNAL(processExited(KProcess *)),
00024             this,SLOT(processExited( KProcess * )));
00025 
00026     // update values on startup
00027     ksp.clearArguments();
00028     ksp << "df";
00029     ksp.start( KProcIO::Block,KProcIO::Stdout);
00030 }
00031 DiskSensor::~DiskSensor()
00032 {}
00033 
00034 int DiskSensor::getFreeSpace(QString mntPt) const
00035 {
00036     QRegExp rx( "^\\S*\\s*\\d+\\s+\\d+\\s+(\\d+)");
00037     rx.search(mntMap[mntPt]);
00038     return rx.cap(1).toInt();
00039 }
00040 
00041 int DiskSensor::getUsedSpace(QString mntPt) const
00042 {
00043     QRegExp rx( "^\\S*\\s*\\d+\\s+(\\d+)\\s+\\d+");
00044     rx.search(mntMap[mntPt]);
00045     return rx.cap(1).toInt();
00046 }
00047 
00048 int DiskSensor::getTotalSpace(QString mntPt) const
00049 {
00050 
00051     QRegExp rx( "^\\S*\\s*(\\d+)\\s+\\d+\\s+\\d+");
00052     rx.search(mntMap[mntPt]);
00053 
00054     return rx.cap(1).toInt();
00055 
00056 }
00057 
00058 int DiskSensor::getPercentUsed(QString mntPt) const
00059 {
00060     QRegExp rx( "\\s+(\\d+)%\\s+");
00061     rx.search(mntMap[mntPt]);
00062     return rx.cap(1).toInt();
00063 }
00064 
00065 int DiskSensor::getPercentFree(QString mntPt) const
00066 {
00067     return ( 100 - getPercentUsed( mntPt ) );
00068 }
00069 
00070 void DiskSensor::receivedStdout(KProcess *, char *buffer, int len )
00071 {
00072 
00073     buffer[len] = 0;
00074     sensorResult += QString( QCString(buffer) );
00075 
00076 }
00077 
00078 void DiskSensor::processExited(KProcess *)
00079 {
00080     QStringList stringList = QStringList::split('\n',sensorResult);
00081     sensorResult = "";
00082     QStringList::Iterator it = stringList.begin();
00083     //QRegExp rx( "^(/dev/).*(/\\S*)$");
00084     QRegExp rx( ".*\\s+(/\\S*)$");
00085 
00086     while( it != stringList.end())
00087     {
00088         rx.search( *it );
00089         if ( !rx.cap(0).isEmpty())
00090         {
00091             mntMap[rx.cap(1)] = *it;
00092         }
00093         it++;
00094     }
00095     stringList.clear();
00096 
00097     QString format;
00098     QString mntPt;
00099     SensorParams *sp;
00100     Meter *meter;
00101 
00102     QObjectListIt lit( *objList );
00103     while (lit != 0)
00104     {
00105         sp = (SensorParams*)(*lit);
00106         meter = sp->getMeter();
00107         format = sp->getParam("FORMAT");
00108         mntPt = sp->getParam("MOUNTPOINT");
00109         if (mntPt.length() == 0)
00110             mntPt="/";
00111 
00112         if (format.length() == 0 )
00113         {
00114             format = "%u";
00115         }
00116         format.replace( QRegExp("%fp", false),QString::number(getPercentFree(mntPt)));
00117         format.replace( QRegExp("%f", false),QString::number(getFreeSpace(mntPt)/1024));
00118         format.replace( QRegExp("%up", false),QString::number(getPercentUsed(mntPt)));
00119         format.replace( QRegExp("%u", false),QString::number(getUsedSpace(mntPt)/1024));
00120         format.replace( QRegExp("%t", false),QString::number(getTotalSpace(mntPt)/1024));
00121         meter->setValue(format);
00122         ++lit;
00123     }
00124 }
00125 
00126 void DiskSensor::update()
00127 {
00128     ksp.clearArguments();
00129     ksp << "df";
00130     ksp.start( KProcIO::NotifyOnExit,KProcIO::Stdout);
00131 }
00132 
00133 void DiskSensor::setMaxValue( SensorParams *sp )
00134 {
00135     Meter *meter;
00136     meter = sp->getMeter();
00137     const QString mntPt = sp->getParam( "MOUNTPOINT" );
00138 
00139     QString f;
00140     f = sp->getParam("FORMAT");
00141     if( f == "%fp" || f == "%up" )
00142         meter->setMax( 100 );
00143     else
00144         meter->setMax( getTotalSpace( mntPt ) / 1024 );
00145 }
00146 
00147 

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