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

uptimesensor.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 "uptimesensor.h"
00011 
00012 UptimeSensor::UptimeSensor( int interval ) : Sensor( interval )
00013 {}
00014 UptimeSensor::~UptimeSensor()
00015 {}
00016 
00017 void UptimeSensor::update()
00018 {
00019     QFile file("/proc/uptime");
00020     QString line;
00021     if ( file.open(IO_ReadOnly | IO_Translate) )
00022     {
00023         // file opened successfully
00024         QTextStream t( &file );        // use a text stream
00025         line = t.readLine();         // line of text excluding '\n'
00026         file.close();
00027 
00028         QRegExp rx( "^\\d+" );
00029         rx.search(line);
00030         int uptime = rx.cap(0).toInt();
00031         int days = uptime / 86400;
00032         uptime -= days * 86400;
00033         int hours = uptime  / 3600;
00034         uptime -= hours * 3600;
00035         int mins = uptime / 60;
00036         uptime -= mins * 60;
00037         int secs = uptime;
00038 
00039 
00040         QString format;
00041         SensorParams *sp;
00042         Meter *meter;
00043 
00044         QObjectListIt it( *objList );
00045         while (it != 0)
00046         {
00047             sp = (SensorParams*)(*it);
00048             meter = sp->getMeter();
00049             format = sp->getParam("FORMAT");
00050 
00051             if (format.length() == 0 )
00052             {
00053                 format = "%dd %h:%M";
00054             }
00055             format.replace( QRegExp("%d"), QString::number(days));
00056             format.replace( QRegExp("%H"), QString::number(hours).rightJustify(2,'0'));
00057             format.replace( QRegExp("%M"), QString::number(mins).rightJustify(2,'0'));
00058             format.replace( QRegExp("%S"), QString::number(secs).rightJustify(2,'0'));
00059             format.replace( QRegExp("%h"), QString::number(hours));
00060             format.replace( QRegExp("%m"), QString::number(mins));
00061             format.replace( QRegExp("%s"), QString::number(secs));
00062 
00063             meter->setValue(format);
00064             ++it;
00065         }
00066     }
00067 }

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