summaryrefslogtreecommitdiffstats
path: root/src/sources/uptimesrc.cpp
blob: fd392f5c0e8eb80d91222986cb9b306109c51aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/***************************************************************************
 *   Copyright (C) 2006 by Ken Werner                                      *
 *   [email protected]                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "uptimesrc.h"
#include <qtextstream.h> 
#include <klocale.h>

UptimeSrc::UptimeSrc(QWidget* inParent, const QFile& inSourceFile):
		LabelSource(inParent),
		mSourceFile(inSourceFile.name()),
		mTrigger(this, 30000){ // 30 seconds
	mEnabled = false; // disable this source by default to to save kicker space
	mID = "Uptime";
	mName = mID;
	mDescription = i18n("This source is provided by /proc/uptime.");
}

UptimeSrc::~UptimeSrc(){
}

std::list<Source*>UptimeSrc::createInstances(QWidget* inParent){
	std::list<Source*> list;
	QFile uptimeFile("/proc/uptime" );
	if(uptimeFile.open(IO_ReadOnly))
		list.push_back(new UptimeSrc(inParent, uptimeFile));
	return list;
}

QString UptimeSrc::fetchValue(){
	QString s = "n/a";
	int secs;
	if(mSourceFile.open(IO_ReadOnly)) {
		QTextStream stream( &mSourceFile );
		stream >> secs;
		
        //QString seconds = QString::number(secs % 60).rightJustify(2, '0');
		QString minutes = QString::number(secs / 60 % 60).rightJustify(2, '0');
		QString hours = QString::number(secs / 3600 % 24).rightJustify(2, '0');
		QString days = QString::number(secs / 86400);
		if(days != "0")
			s = mTimeFormatLong.arg(days).arg(hours).arg(minutes);
		else
			s = mTimeFormatShort.arg(hours).arg(minutes);
        mSourceFile.close();
	}
	return s;
}

void UptimeSrc::realizeWidget() {
    LabelSource::realizeWidget();
    mTimeFormatLong = i18n("%1d %2:%3");
    mTimeFormatShort = i18n("%1:%2");
}