summaryrefslogtreecommitdiffstats
path: root/kshutdown/mstatstab.cpp
blob: 635af2d562c4948303a62038fbabc727349f8476 (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
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
/*
	mstatstab.cpp - A statistics dialog
	Copyright (C) 2003  Konrad Twardowski <kdtonline@poczta.onet.pl>

	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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "configuration.h"
#include "mmainwindow.h"
#include "miscutils.h"
#include "mstatstab.h"

#include <ntqcheckbox.h>
#include <ntqprocess.h>
#include <ntqregexp.h>
#include <ntqtextedit.h>
#include <ntqvbox.h>
#include <ntqvgroupbox.h>
#include <ntqwhatsthis.h>

#include <kiconloader.h>
#include <tdelocale.h>
#include <kpushbutton.h>

// public

MStatsTab *MStatsTab::_instance = 0;

MStatsTab::MStatsTab()
	: KDialogBase(
		ks_main,
		"MStatsTab",
		false, // modeless
		i18n("Statistics"),
		Close | Details,
		Close // default button
	),
	_buf(TQString::null)
{
	_process = new TQProcess(this);
	connect(_process, SIGNAL(processExited()), SLOT(slotProcessExit()));
	connect(_process, SIGNAL(readyReadStdout()), SLOT(slotReadStdout()));

	TQVBox *main = new TQVBox(this);
	setMainWidget(main);

	// output
	te_output = new TQTextEdit(main, "TQTextEdit::te_output");
	te_output->setMinimumSize(640, 320);
	te_output->setPaletteBackgroundColor(white);
	te_output->setPaletteForegroundColor(black);
	te_output->setReadOnly(true);
	te_output->setTextFormat(RichText); // allow HTML tags
	te_output->setWordWrap(TQTextEdit::NoWrap);
	// based on the "Linux User's Manual" (man w)
	TQWhatsThis::add(
		te_output,
		MiscUtils::HTML(i18n(
			"This view displays information about the users currently on the " \
			"machine, and their processes.<br>" \
			"The header shows how long the system has been running."
		))
	);

	TQHBox *buttons = new TQHBox(main);
	// refresh
	b_refresh = new KPushButton(SmallIcon("reload"), i18n("Refresh"), buttons, "KPushButton::b_refresh");
	connect(b_refresh, SIGNAL(clicked()), SLOT(slotRefresh()));

	// options
	TQVGroupBox *options = new TQVGroupBox(i18n("Options"), main);
	// long format
	c_longFormat = new TQCheckBox(i18n("More information"), options);
	c_longFormat->setChecked(kshutdownrc->statsLongFormat);
	connect(c_longFormat, SIGNAL(clicked()), SLOT(slotRefresh()));
	MiscUtils::setHint(c_longFormat, i18n("Show login time, JCPU and PCPU times."));
	// toggle from field
	c_toggleFromField = new TQCheckBox(i18n("Toggle \"FROM\""), options);
	c_toggleFromField->setChecked(kshutdownrc->statsToggleFromField);
	MiscUtils::setHint(c_toggleFromField, i18n("Toggle the \"FROM\" (remote hostname) field."));
	connect(c_toggleFromField, SIGNAL(clicked()), SLOT(slotRefresh()));

	setButtonGuiItem(Details, KStdGuiItem::configure());
	setDetailsWidget(options);
	adjustSize();
}

MStatsTab::~MStatsTab()
{
	killProcess();
}

// protected

void MStatsTab::keyPressEvent(TQKeyEvent *e) {
	// Ctrl+R, F5 = Refresh
	if (
		b_refresh->isEnabled() &&
		(
			((e->key() == Key_F5) && (e->state() == 0)) ||
			((e->key() == Key_R) && (e->state() == ControlButton))
		)
	) {
		slotRefresh();
		e->accept();
	}
	else {
		KDialogBase::keyPressEvent(e);
	}
}

// private

void MStatsTab::killProcess() const
{
	// kill previous process (if any)
	if (_process && _process->isRunning())
		_process->kill();
}

// private slots

void MStatsTab::slotProcessExit() {
	_buf.replace("\n", "<br>");
	_buf.replace(" ", "&nbsp;");

	#define KS_HEADER(text) \
		_buf.replace(text, "<font color=\"#a0a0a0\">" text "</font>");

	#define KS_WARNING(text) \
		_buf.replace(TQRegExp("\\b" text "\\b"), "<font color=\"#ff0000\">" text "</font>");

	// underline header
	KS_HEADER("USER")
	KS_HEADER("TTY")
	KS_HEADER("FROM")
	KS_HEADER("LOGIN@")
	KS_HEADER("JCPU")
	KS_HEADER("PCPU")
	KS_HEADER("IDLE")
	KS_HEADER("WHAT")
	// highlight some important words
// TODO: 2.0: add more keywords (e.g. backup)
	KS_WARNING("root")
	KS_WARNING("ssh")
	KS_WARNING("su")
	KS_WARNING("sudo")
	KS_WARNING("telnet")

	_buf.prepend("<pre style=\"font-family: " + TDEGlobalSettings::fixedFont().family() + "\"><b>");
	_buf.append("</b></pre>");

	te_output->setText(MiscUtils::HTML(_buf));
	b_refresh->setEnabled(true);
}

void MStatsTab::slotReadStdout() {
	_buf.append(_process->readStdout());
}

void MStatsTab::slotRefresh()
{
	b_refresh->setEnabled(false);

	// update config
	kshutdownrc->statsLongFormat = c_longFormat->isChecked();
	kshutdownrc->statsToggleFromField = c_toggleFromField->isChecked();

	_buf = "";
	killProcess();
	_process->clearArguments();
	_process->addArgument("w");
	if (!kshutdownrc->statsLongFormat)
		_process->addArgument("-s"); // -s - short format
	if (kshutdownrc->statsToggleFromField)
		_process->addArgument("-f"); // -f - toggle "from" field
	if (!_process->start()) {
		_buf = "<b><font color=\"#ff0000\">" + i18n("Error") + "</font><br><br>";
		_buf.append(i18n("Command: %1").arg(_process->arguments().join(" ")));
		_buf.append("</b>");
		te_output->setText(_buf);
		b_refresh->setEnabled(true);
	}
}