2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
5
Copyright (c) 2006 John Tapsell <john.tapsell@kde.org>
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License as
9
published by the Free Software Foundation; version 2 of
10
the License, or (at your option) version 3.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include <QDomElement>
25
#include <QHeaderView>
26
#include <QStackedLayout>
30
#include "ProcessController.moc"
31
#include "ProcessController.h"
32
#include "processui/ksysguardprocesslist.h"
33
#include "processes.h"
35
//#define DO_MODELCHECK
37
#include "modeltest.h"
39
ProcessController::ProcessController(QWidget* parent, SharedSettings *workSheetSettings)
40
: KSGRD::SensorDisplay(parent, QString(), workSheetSettings)
47
ProcessController::sensorError(int, bool err)
49
if (err == sensors().at(0)->isOk())
52
kDebug(1215) << "SensorError called with an error";
54
/* This happens only when the sensorOk status needs to be changed. */
55
sensors().at(0)->setIsOk( !err );
57
setSensorOk(sensors().at(0)->isOk());
61
ProcessController::restoreSettings(QDomElement& element)
63
bool result = addSensor(element.attribute("hostName"),
64
element.attribute("sensorName"),
65
(element.attribute("sensorType").isEmpty() ? "table" : element.attribute("sensorType")),
67
if(!result) return false;
69
int version = element.attribute("version", "0").toUInt();
70
if(version == PROCESSHEADERVERSION) { //If the header has changed, the old settings are no longer valid. Only restore if version is the same
71
mProcessList->restoreHeaderState(QByteArray::fromBase64(element.attribute("treeViewHeader").toLatin1()));
74
bool showTotals = element.attribute("showTotals", "1").toUInt();
75
mProcessList->setShowTotals(showTotals);
78
int units = element.attribute("units", QString::number((int)ProcessModel::UnitsKB)).toUInt();
79
mProcessList->setUnits((ProcessModel::Units)units);
81
int ioUnits = element.attribute("ioUnits", QString::number((int)ProcessModel::UnitsKB)).toUInt();
82
mProcessList->processModel()->setIoUnits((ProcessModel::Units)ioUnits);
84
int ioInformation = element.attribute("ioInformation", QString::number((int)ProcessModel::ActualBytesRate)).toUInt();
85
mProcessList->processModel()->setIoInformation((ProcessModel::IoInformation)ioInformation);
87
bool showCommandLineOptions = element.attribute("showCommandLineOptions", "0").toUInt();
88
mProcessList->processModel()->setShowCommandLineOptions(showCommandLineOptions);
90
bool showTooltips = element.attribute("showTooltips", "1").toUInt();
91
mProcessList->processModel()->setShowingTooltips(showTooltips);
93
bool normalizeCPUUsage = element.attribute("normalizeCPUUsage", "1").toUInt();
94
mProcessList->processModel()->setNormalizedCPUUsage(normalizeCPUUsage);
96
int filterState = element.attribute("filterState", QString::number((int)ProcessFilter::AllProcesses)).toUInt();
97
mProcessList->setState((ProcessFilter::State)filterState);
99
SensorDisplay::restoreSettings(element);
103
bool ProcessController::saveSettings(QDomDocument& doc, QDomElement& element)
107
element.setAttribute("hostName", sensors().at(0)->hostName());
108
element.setAttribute("sensorName", sensors().at(0)->name());
109
element.setAttribute("sensorType", sensors().at(0)->type());
111
element.setAttribute("version", QString::number(PROCESSHEADERVERSION));
112
element.setAttribute("treeViewHeader", QString::fromLatin1(mProcessList->treeView()->header()->saveState().toBase64()));
113
element.setAttribute("showTotals", mProcessList->showTotals()?1:0);
115
element.setAttribute("units", (int)(mProcessList->units()));
116
element.setAttribute("ioUnits", (int)(mProcessList->processModel()->ioUnits()));
117
element.setAttribute("ioInformation", (int)(mProcessList->processModel()->ioInformation()));
118
element.setAttribute("showCommandLineOptions", mProcessList->processModel()->isShowCommandLineOptions());
119
element.setAttribute("showTooltips", mProcessList->processModel()->isShowingTooltips());
120
element.setAttribute("normalizeCPUUsage", mProcessList->processModel()->isNormalizedCPUUsage());
121
element.setAttribute("filterState", (int)(mProcessList->state()));
123
SensorDisplay::saveSettings(doc, element);
128
void ProcessController::timerTick() {
129
mProcessList->updateList();
132
void ProcessController::answerReceived( int id, const QList<QByteArray>& answer ) {
134
mProcesses->answerReceived(id, answer);
137
bool ProcessController::addSensor(const QString& hostName,
138
const QString& sensorName,
139
const QString& sensorType,
140
const QString& title)
142
if (sensorType != "table")
146
QStackedLayout *layout = new QStackedLayout(this);
147
mProcessList = new KSysGuardProcessList(this, hostName);
148
mProcessList->setUpdateIntervalMSecs(0); //we will call updateList() manually
149
mProcessList->setContentsMargins(0,0,0,0);
150
mProcessList->setScriptingEnabled(true);
151
addActions(mProcessList->actions());
152
connect(mProcessList, SIGNAL(updated()), this, SIGNAL(updated()));
153
connect(mProcessList, SIGNAL(processListChanged()), this, SIGNAL(processListChanged()));
154
mProcessList->setContextMenuPolicy( Qt::CustomContextMenu );
155
connect(mProcessList, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showContextMenu(QPoint)));
157
layout->addWidget(mProcessList);
159
/** To use a remote sensor, we need to drill down through the layers, to connect to the remote processes. Then connect to its signals and slots.
160
* It's horrible I know :( */
161
if(!hostName.isEmpty() && hostName != "localhost") {
162
KSysGuard::Processes *processes = mProcessList->processModel()->processController();
163
mProcesses = processes;
165
connect( processes, SIGNAL(runCommand(const QString &, int)), SLOT(runCommand(const QString &, int)));
170
setPlotterWidget(mProcessList);
172
QTimer::singleShot(0, mProcessList->filterLineEdit(), SLOT(setFocus()));
174
registerSensor(new KSGRD::SensorProperties(hostName, sensorName, sensorType, title));
175
/* This just triggers the first communication. The full set of
176
* requests are send whenever the sensor reconnects (detected in
178
sensors().at(0)->setIsOk(true); //Assume it is okay from the start
179
setSensorOk(sensors().at(0)->isOk());
180
emit processListChanged();
184
void ProcessController::runCommand(const QString &command, int id) {
185
sendRequest(sensors().at(0)->hostName(), command, id);