summaryrefslogtreecommitdiffstats
path: root/src/sources/uptimesrc.cpp
blob: 8ec0db59456114cc6cfac4769dd47f158e54b442 (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 <tqtextstream.h> 
#include <tdelocale.h>

UptimeSrc::UptimeSrc(TQWidget* inParent, const TQFile& 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(TQWidget* inParent){
	std::list<Source*> list;
	TQFile uptimeFile("/proc/uptime" );
	if(uptimeFile.open(IO_ReadOnly))
		list.push_back(new UptimeSrc(inParent, uptimeFile));
	return list;
}

TQString UptimeSrc::fetchValue(){
	TQString s = "n/a";
	int secs;
	if(mSourceFile.open(IO_ReadOnly)) {
		TQTextStream stream( &mSourceFile );
		stream >> secs;
		
        //TQString seconds = TQString::number(secs % 60).rightJustify(2, '0');
		TQString minutes = TQString::number(secs / 60 % 60).rightJustify(2, '0');
		TQString hours = TQString::number(secs / 3600 % 24).rightJustify(2, '0');
		TQString days = TQString::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");
}