~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/SensorDisplayLib/ProcessController.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
 
5
    Copyright (c) 2006 John Tapsell <john.tapsell@kde.org>
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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/>.
 
19
*/
 
20
#include <QDomElement>
 
21
#include <QTimer>
 
22
#include <QLineEdit>
 
23
#include <QTreeView>
 
24
#include <QCheckBox>
 
25
#include <QHeaderView>
 
26
#include <QStackedLayout>
 
27
 
 
28
#include <kdebug.h>
 
29
 
 
30
#include "ProcessController.moc"
 
31
#include "ProcessController.h"
 
32
#include "processui/ksysguardprocesslist.h"
 
33
#include "processes.h"
 
34
 
 
35
//#define DO_MODELCHECK
 
36
#ifdef DO_MODELCHECK
 
37
#include "modeltest.h"
 
38
#endif
 
39
ProcessController::ProcessController(QWidget* parent,  SharedSettings *workSheetSettings)
 
40
    : KSGRD::SensorDisplay(parent, QString(), workSheetSettings)
 
41
{
 
42
    mProcessList = NULL;
 
43
    mProcesses = NULL;
 
44
}
 
45
 
 
46
void
 
47
ProcessController::sensorError(int, bool err)
 
48
{
 
49
    if (err == sensors().at(0)->isOk())
 
50
    {
 
51
        if (err)  {
 
52
            kDebug(1215) << "SensorError called with an error";
 
53
        }
 
54
        /* This happens only when the sensorOk status needs to be changed. */
 
55
        sensors().at(0)->setIsOk( !err );
 
56
    }
 
57
    setSensorOk(sensors().at(0)->isOk());
 
58
}
 
59
 
 
60
bool
 
61
ProcessController::restoreSettings(QDomElement& element)
 
62
{
 
63
    bool result = addSensor(element.attribute("hostName"),
 
64
                element.attribute("sensorName"),
 
65
                (element.attribute("sensorType").isEmpty() ? "table" : element.attribute("sensorType")),
 
66
                QString());
 
67
    if(!result) return false;
 
68
 
 
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()));
 
72
    }
 
73
 
 
74
    bool showTotals = element.attribute("showTotals", "1").toUInt();
 
75
    mProcessList->setShowTotals(showTotals);
 
76
 
 
77
 
 
78
    int units = element.attribute("units", QString::number((int)ProcessModel::UnitsKB)).toUInt();
 
79
    mProcessList->setUnits((ProcessModel::Units)units);
 
80
 
 
81
    int ioUnits = element.attribute("ioUnits", QString::number((int)ProcessModel::UnitsKB)).toUInt();
 
82
    mProcessList->processModel()->setIoUnits((ProcessModel::Units)ioUnits);
 
83
 
 
84
    int ioInformation = element.attribute("ioInformation", QString::number((int)ProcessModel::ActualBytesRate)).toUInt();
 
85
    mProcessList->processModel()->setIoInformation((ProcessModel::IoInformation)ioInformation);
 
86
 
 
87
    bool showCommandLineOptions = element.attribute("showCommandLineOptions", "0").toUInt();
 
88
    mProcessList->processModel()->setShowCommandLineOptions(showCommandLineOptions);
 
89
 
 
90
    bool showTooltips = element.attribute("showTooltips", "1").toUInt();
 
91
    mProcessList->processModel()->setShowingTooltips(showTooltips);
 
92
 
 
93
    bool normalizeCPUUsage = element.attribute("normalizeCPUUsage", "1").toUInt();
 
94
    mProcessList->processModel()->setNormalizedCPUUsage(normalizeCPUUsage);
 
95
 
 
96
    int filterState = element.attribute("filterState", QString::number((int)ProcessFilter::AllProcesses)).toUInt();
 
97
    mProcessList->setState((ProcessFilter::State)filterState);
 
98
 
 
99
    SensorDisplay::restoreSettings(element);
 
100
    return result;
 
101
}
 
102
 
 
103
bool ProcessController::saveSettings(QDomDocument& doc, QDomElement& element)
 
104
{
 
105
    if(!mProcessList)
 
106
        return false;
 
107
    element.setAttribute("hostName", sensors().at(0)->hostName());
 
108
    element.setAttribute("sensorName", sensors().at(0)->name());
 
109
    element.setAttribute("sensorType", sensors().at(0)->type());
 
110
 
 
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);
 
114
 
 
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()));
 
122
 
 
123
    SensorDisplay::saveSettings(doc, element);
 
124
 
 
125
    return true;
 
126
}
 
127
 
 
128
void ProcessController::timerTick()  {
 
129
    mProcessList->updateList();
 
130
 
 
131
}
 
132
void ProcessController::answerReceived( int id, const QList<QByteArray>& answer ) {
 
133
    if(mProcesses)
 
134
        mProcesses->answerReceived(id, answer);
 
135
}
 
136
 
 
137
bool ProcessController::addSensor(const QString& hostName,
 
138
                                 const QString& sensorName,
 
139
                                 const QString& sensorType,
 
140
                                 const QString& title)
 
141
{
 
142
    if (sensorType != "table")
 
143
        return false;
 
144
 
 
145
 
 
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)));
 
156
 
 
157
    layout->addWidget(mProcessList);
 
158
 
 
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;
 
164
        if(processes) {
 
165
            connect( processes, SIGNAL(runCommand(const QString &, int)), SLOT(runCommand(const QString &, int)));
 
166
        }
 
167
 
 
168
    }
 
169
 
 
170
    setPlotterWidget(mProcessList);
 
171
 
 
172
    QTimer::singleShot(0, mProcessList->filterLineEdit(), SLOT(setFocus()));
 
173
 
 
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
 
177
    * sensorError(). */
 
178
    sensors().at(0)->setIsOk(true); //Assume it is okay from the start
 
179
    setSensorOk(sensors().at(0)->isOk());
 
180
    emit processListChanged();
 
181
    return true;
 
182
}
 
183
 
 
184
void ProcessController::runCommand(const QString &command, int id) {
 
185
    sendRequest(sensors().at(0)->hostName(), command, id);
 
186
}
 
187