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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/* ksim - a system monitor for kde
*
* Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
*
* 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 <tqtooltip.h>
#include <tqlayout.h>
#include <tqtimer.h>
#include <tqregexp.h>
#include <tqtextstream.h>
#include <tqdatetime.h>
#include <kdebug.h>
#include <klocale.h>
#include <kapplication.h>
#include <kglobal.h>
#include "ksimsysinfo.h"
#include "ksimsysinfo.moc"
#include "systeminfo.h"
#include <label.h>
#include <ksimconfig.h>
#include <progress.h>
#include <themetypes.h>
#include <time.h>
#include <string.h>
KSim::Sysinfo::Sysinfo(KSim::Config *config,
TQWidget *parent, const char *name, WFlags fl)
: DCOPObject("sysinfo"), TQWidget(parent, name, fl)
{
m_config = config;
m_layout = new TQVBoxLayout(this);
m_timeLabel = 0L;
m_dateLabel = 0L;
m_uptimeLabel = 0L;
m_memLabel = 0L;
m_swapLabel = 0L;
//m_procsLabel = 0L;
m_timer = new TQTimer(this);
connect(m_timer, TQT_SIGNAL(timeout()), TQT_SLOT(clockUptimeUpdate()));
m_sysTimer = new TQTimer(this);
connect(m_sysTimer, TQT_SIGNAL(timeout()), TQT_SLOT(sysUpdate()));
createView();
}
KSim::Sysinfo::~Sysinfo()
{
// remember to cleanup
System::cleanup();
}
void KSim::Sysinfo::clockUptimeUpdate()
{
TQString time;
static bool updateDate = true;
if (m_timeLabel) {
TQTime now = TQTime::currentTime();
time = KGlobal::locale()->formatTime(now, true);
if ( now == TQTime(0, 0) )
updateDate = true;
m_timeLabel->setText(time);
}
// only update the date when necessary
if (m_dateLabel) {
if (updateDate) {
m_dateLabel->setText(KGlobal::locale()->formatDate(TQDate::currentDate()));
updateDate = false;
}
}
else {
updateDate = true;
}
if (m_uptimeLabel) {
TQString uptime = m_config->uptimeFormat();
++m_totalUptime;
long uptimeDays, uptimeHours, uptimeMins, uptimeSecs;
uptimeHours = m_totalUptime / 3600;
uptimeDays = uptimeHours / 24;
uptimeMins = (m_totalUptime - uptimeHours * 3600) / 60;
uptimeSecs = m_totalUptime % 60;
TQString days;
TQString hours;
TQString minutes;
TQString seconds;
// found days so we have to modify hours
if (uptime.find(TQRegExp("%d" ), 0) >= 0)
uptimeHours -= (uptimeDays * 24);
days.sprintf("%02li", uptimeDays);
hours.sprintf("%02li", uptimeHours);
minutes.sprintf("%02li", uptimeMins);
seconds.sprintf("%02li", uptimeSecs);
uptime.replace(TQRegExp("%d"), days);
uptime.replace(TQRegExp("%h"), hours);
uptime.replace(TQRegExp("%m"), minutes);
uptime.replace(TQRegExp("%s"), seconds);
m_uptimeLabel->setText(uptime);
}
}
void KSim::Sysinfo::sysUpdate()
{
const System &system = System::self();
m_totalUptime = system.uptime();
if (m_memLabel) {
TQString memory = m_config->memoryFormat();
unsigned long total = system.totalRam();
unsigned long free = system.freeRam();
unsigned long shared = system.sharedRam();
unsigned long buffer = system.bufferRam();
unsigned long cache = system.cacheRam();
unsigned long used = system.usedRam();
unsigned long allFree = free + buffer + cache;
bool all = memory.find( "%F" ) != -1;
memory.replace(TQRegExp("%s"), TQString::number(System::bytesToMegs(shared)));
memory.replace(TQRegExp("%b"), TQString::number(System::bytesToMegs(buffer)));
memory.replace(TQRegExp("%c"), TQString::number(System::bytesToMegs(cache)));
memory.replace(TQRegExp("%u"), TQString::number(System::bytesToMegs(used)));
memory.replace(TQRegExp("%t"), TQString::number(System::bytesToMegs(total)));
memory.replace(TQRegExp("%f"), TQString::number(System::bytesToMegs(free)));
memory.replace(TQRegExp("%F"), TQString::number(System::bytesToMegs(allFree)));
kdDebug(2003) << memory << endl;
m_memLabel->setText("Memory");
TQToolTip::add(m_memLabel, memory );
if ( !all )
m_memLabel->setValue( System::bytesToMegs( total ) - System::bytesToMegs( free ) );
else
m_memLabel->setValue( System::bytesToMegs( total ) - System::bytesToMegs( allFree ) );
}
if (m_swapLabel) {
TQString swap = m_config->swapFormat();
unsigned long total = system.totalSwap();
unsigned long free = system.freeSwap();
unsigned long swapUsed = system.usedSwap();
swap.replace(TQRegExp("%u"), TQString::number(System::bytesToMegs(swapUsed)));
swap.replace(TQRegExp("%t"), TQString::number(System::bytesToMegs(total)));
swap.replace(TQRegExp("%f"), TQString::number(System::bytesToMegs(free)));
m_swapLabel->setText("Swap");
TQToolTip::add(m_swapLabel, swap);
m_swapLabel->setValue(System::bytesToMegs(total) - System::bytesToMegs(free));
}
// if (m_config->showProcs()) {
// m_procsLabel->setText(i18n("Procs: %1").tqarg(sysInfo.procs));
// }
}
void KSim::Sysinfo::createView()
{
stopTimers();
const System &system = System::self();
int timeLocation = 0;
int dateLocation = 1;
int uptimeLocation = 2;
int memLocation = 3;
int swapLocation = 4;
// int procsLocation = 5;
int offset = 0;
if (m_config->showTime()) {
if (!m_timeLabel) {
m_timeLabel = new KSim::Label(this);
m_layout->insertWidget(timeLocation - offset, m_timeLabel);
}
TQToolTip::add(m_timeLabel, i18n("Current system time"));
m_timeLabel->show();
}
else {
offset++;
delete m_timeLabel;
m_timeLabel = 0L;
}
if (m_config->showDate()) {
if (!m_dateLabel) {
m_dateLabel = new KSim::Label(this);
m_layout->insertWidget(dateLocation - offset, m_dateLabel);
}
TQToolTip::add(m_dateLabel, i18n("Current system date"));
m_dateLabel->show();
}
else {
offset++;
delete m_dateLabel;
m_dateLabel = 0L;
}
kdDebug(2003) << m_dateLabel << endl;
if (m_config->showUptime()) {
if (!m_uptimeLabel) {
m_uptimeLabel = new KSim::Label(KSim::Types::Uptime, this);
m_layout->insertWidget(uptimeLocation - offset, m_uptimeLabel);
}
TQToolTip::add(m_uptimeLabel, i18n("System uptime"));
m_uptimeLabel->show();
}
else {
offset++;
delete m_uptimeLabel;
m_uptimeLabel = 0L;
}
if (m_config->showMemory()) {
if (!m_memLabel) {
m_memLabel = new KSim::Progress(System::bytesToMegs(system.totalRam()),
KSim::Types::Mem, this);
m_layout->insertWidget(memLocation - offset, m_memLabel);
}
m_memLabel->show();
}
else {
offset++;
delete m_memLabel;
m_memLabel = 0L;
}
if (m_config->showSwap()) {
if (!m_swapLabel) {
m_swapLabel = new KSim::Progress(System::bytesToMegs(system.totalSwap()),
KSim::Types::Swap, this);
m_layout->insertWidget(swapLocation - offset, m_swapLabel);
}
m_swapLabel->show();
}
else {
offset++;
delete m_swapLabel;
m_swapLabel = 0L;
}
/*if (m_config->showProcs()) {
if (!m_procsLabel) {
m_procsLabel = new KSimLabel(this);
m_layout->insertWidget(procsLocation, m_procsLabel);
}
m_procsLabel->show();
}
else {
delete m_procsLabel;
m_procsLabel = 0L;
}*/
// m_layout->tqinvalidate();
updateGeometry();
adjustSize();
startTimers();
sysUpdate();
clockUptimeUpdate();
}
void KSim::Sysinfo::stopTimers()
{
m_timer->stop();
m_sysTimer->stop();
}
void KSim::Sysinfo::startTimers()
{
// Cellecting meminfo on fbsd seems to be expensive :(
// so better update the memory display every 5 seconds rather than 1
// until i can find a better solution (if there is one)
if (!m_sysTimer->isActive() && !m_timer->isActive()) {
m_sysTimer->start(5000);
m_timer->start(1000);
}
}
TQString KSim::Sysinfo::uptime() const
{
if (m_uptimeLabel)
return m_uptimeLabel->text();
return i18n("Uptime display disabled");
}
TQString KSim::Sysinfo::memInfo() const
{
if (m_memLabel)
return m_memLabel->text();
return i18n("Memory display disabled");
}
TQString KSim::Sysinfo::swapInfo() const
{
if (m_swapLabel)
return m_swapLabel->text();
return i18n("Swap display disabled");
}
|