uptimesensor.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 <qglobal.h>
00011 
00012 #if defined __FreeBSD__
00013 #include <sys/time.h>
00014 #include <sys/sysctl.h>
00015 #endif
00016 
00017 #if defined(Q_OS_NETBSD)
00018 #include <sys/param.h>
00019 #include <sys/time.h>
00020 #include <sys/sysctl.h>
00021 #endif
00022 
00023 #include "uptimesensor.h"
00024 
00025 UptimeSensor::UptimeSensor( int interval ) : Sensor( interval )
00026 {}
00027 UptimeSensor::~UptimeSensor()
00028 {}
00029 
00030 void UptimeSensor::update()
00031 {
00032 #if defined __FreeBSD__ || defined(Q_OS_NETBSD)
00033       struct timeval  boottime;
00034       time_t          now;            /* the current time of day */
00035 
00036       double avenrun[3];
00037       time_t uptime;
00038       int days, hours, i, mins, secs;
00039       int mib[2];
00040       size_t size;
00041       char buf[256];
00042 
00043         /*
00044          * Get time of day.
00045          */
00046         (void)time(&now);
00047 
00048         /*
00049          * Determine how long system has been up.
00050          * (Found by looking getting "boottime" from the kernel)
00051          */
00052         mib[0] = CTL_KERN;
00053         mib[1] = KERN_BOOTTIME;
00054         size = sizeof(boottime);
00055         if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
00056             boottime.tv_sec != 0) {
00057                 uptime = now - boottime.tv_sec;
00058                 if (uptime > 60)
00059                         uptime += 30;
00060                 days = uptime / 86400;
00061                 uptime %= 86400;
00062                 hours = uptime / 3600;
00063                 uptime %= 3600;
00064                 mins = uptime / 60;
00065                 secs = uptime % 60;
00066         }
00067 #else
00068     QFile file("/proc/uptime");
00069     QString line;
00070     if ( file.open(IO_ReadOnly | IO_Translate) )
00071     {
00072         // file opened successfully
00073         QTextStream t( &file );        // use a text stream
00074         line = t.readLine();         // line of text excluding '\n'
00075         file.close();
00076 
00077         QRegExp rx( "^\\d+" );
00078         rx.search(line);
00079         int uptime = rx.cap(0).toInt();
00080         int days = uptime / 86400;
00081         uptime -= days * 86400;
00082         int hours = uptime  / 3600;
00083         uptime -= hours * 3600;
00084         int mins = uptime / 60;
00085         uptime -= mins * 60;
00086         int secs = uptime;
00087 #endif
00088 
00089         QString format;
00090         SensorParams *sp;
00091         Meter *meter;
00092 
00093         QObjectListIt it( *objList );
00094         while (it != 0)
00095         {
00096             sp = (SensorParams*)(*it);
00097             meter = sp->getMeter();
00098             format = sp->getParam("FORMAT");
00099 
00100             if (format.length() == 0 )
00101             {
00102                 format = "%dd %h:%M";
00103             }
00104             format.replace( QRegExp("%d"), QString::number(days));
00105             format.replace( QRegExp("%H"), QString::number(hours).rightJustify(2,'0'));
00106             format.replace( QRegExp("%M"), QString::number(mins).rightJustify(2,'0'));
00107             format.replace( QRegExp("%S"), QString::number(secs).rightJustify(2,'0'));
00108             format.replace( QRegExp("%h"), QString::number(hours));
00109             format.replace( QRegExp("%m"), QString::number(mins));
00110             format.replace( QRegExp("%s"), QString::number(secs));
00111 
00112             meter->setValue(format);
00113             ++it;
00114         }
00115 
00116 #if !defined __FreeBSD__ && !defined(Q_OS_NETBSD)
00117   }
00118 #endif
00119 }
KDE Home | KDE Accessibility Home | Description of Access Keys