• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • sensors
uptime.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003 by Hans Karlsson *
3  * karlsson.h@home.se *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "uptime.h"
12 
13 #include <QFile>
14 #include <QTextStream>
15 
16 #if defined __FreeBSD__
17 #include <sys/time.h>
18 #include <sys/sysctl.h>
19 #endif
20 
21 #if defined(Q_OS_NETBSD)
22 #include <sys/param.h>
23 #include <sys/time.h>
24 #include <sys/sysctl.h>
25 #endif
26 
27 UptimeSensor::UptimeSensor(int interval) : Sensor(interval)
28 {}
29 UptimeSensor::~UptimeSensor()
30 {}
31 
32 void UptimeSensor::update()
33 {
34 #if defined __FreeBSD__ || defined(Q_OS_NETBSD)
35  struct timeval boottime;
36  time_t now; /* the current time of day */
37 
38  double avenrun[3];
39  time_t uptime;
40  int days, hours, i, mins, secs;
41  int mib[2];
42  size_t size;
43  char buf[256];
44 
45  /*
46  * Get time of day.
47  */
48  (void)time(&now);
49 
50  /*
51  * Determine how long system has been up.
52  * (Found by looking getting "boottime" from the kernel)
53  */
54  mib[0] = CTL_KERN;
55  mib[1] = KERN_BOOTTIME;
56  size = sizeof(boottime);
57  if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
58  boottime.tv_sec != 0) {
59  uptime = now - boottime.tv_sec;
60  if (uptime > 60)
61  uptime += 30;
62  days = uptime / 86400;
63  uptime %= 86400;
64  hours = uptime / 3600;
65  uptime %= 3600;
66  mins = uptime / 60;
67  secs = uptime % 60;
68  }
69 #else
70  QFile file("/proc/uptime");
71  QString line;
72  if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
73  // file opened successfully
74  QTextStream t(&file); // use a text stream
75  line = t.readLine(); // line of text excluding '\n'
76  file.close();
77 
78  QRegExp rx("^\\d+");
79  rx.indexIn(line);
80  int uptime = rx.cap(0).toInt();
81  int days = uptime / 86400;
82  uptime -= days * 86400;
83  int hours = uptime / 3600;
84  uptime -= hours * 3600;
85  int mins = uptime / 60;
86  uptime -= mins * 60;
87  int secs = uptime;
88 #endif
89 
90  QString format;
91  SensorParams *sp;
92  Meter *meter;
93  QObject *obj;
94 
95  foreach(obj, *objList) {
96  sp = (SensorParams*)(obj);
97  meter = sp->getMeter();
98  format = sp->getParam("FORMAT");
99 
100  if (format.length() == 0) {
101  format = "%dd %h:%M";
102  }
103  format.replace(QRegExp("%d"), QString::number(days));
104  format.replace(QRegExp("%H"), QString::number(hours).rightJustified(2, '0'));
105  format.replace(QRegExp("%M"), QString::number(mins).rightJustified(2, '0'));
106  format.replace(QRegExp("%S"), QString::number(secs).rightJustified(2, '0'));
107  format.replace(QRegExp("%h"), QString::number(hours));
108  format.replace(QRegExp("%m"), QString::number(mins));
109  format.replace(QRegExp("%s"), QString::number(secs));
110 
111  meter->setValue(format);
112  }
113 
114 #if !defined __FreeBSD__ && !defined(Q_OS_NETBSD)
115 }
116 #endif
117 }
UptimeSensor::UptimeSensor
UptimeSensor(int interval)
Definition: uptime.cpp:27
uptime.h
SensorParams::getMeter
Meter * getMeter() const
Definition: sensorparams.cpp:31
QObject
Sensor
Definition: sensor.h:17
UptimeSensor::update
void update()
Definition: uptime.cpp:32
SensorParams
Hans Karlsson.
Definition: sensorparams.h:30
SensorParams::getParam
QString getParam(const QString &) const
Definition: sensorparams.cpp:26
UptimeSensor::~UptimeSensor
~UptimeSensor()
Definition: uptime.cpp:29
Meter
Definition: meters/meter.h:23
Meter::setValue
virtual void setValue(int)
Definition: meters/meter.cpp:136
Sensor::objList
QList< QObject * > * objList
Definition: sensor.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal