~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to ksysguard/gui/ksgrd/SensorManager.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

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
 
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of version 2 of the GNU General Public
 
8
    License as published by the Free Software Foundation.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 
 
19
*/
 
20
 
 
21
#include <QApplication>
 
22
#include <kdebug.h>
 
23
#include <klocale.h>
 
24
#include <QWidget>
 
25
#include <QEvent>
 
26
 
 
27
#include "SensorShellAgent.h"
 
28
#include "SensorSocketAgent.h"
 
29
 
 
30
#include "SensorManager.h"
 
31
 
 
32
using namespace KSGRD;
 
33
 
 
34
class AgentEvent : public QEvent
 
35
{
 
36
  public:
 
37
    AgentEvent( const SensorAgent *agent )
 
38
      : QEvent( QEvent::User ), mAgent( agent )
 
39
    {
 
40
    }
 
41
 
 
42
    const SensorAgent *agent() const
 
43
    {
 
44
      return mAgent;
 
45
    }
 
46
 
 
47
  private:
 
48
    const SensorAgent *mAgent;
 
49
};
 
50
 
 
51
SensorManager::MessageEvent::MessageEvent( const QString &message )
 
52
  : QEvent( QEvent::User ), mMessage( message )
 
53
{
 
54
}
 
55
 
 
56
QString SensorManager::MessageEvent::message() const
 
57
{
 
58
  return mMessage;
 
59
}
 
60
 
 
61
SensorManager* KSGRD::SensorMgr;
 
62
 
 
63
SensorManager::SensorManager()
 
64
{
 
65
  // Fill the sensor description dictionary.
 
66
 
 
67
  mDict.insert( QLatin1String( "Delta" ), i18n( "Change" ) );
 
68
  mDict.insert( QLatin1String( "Rate" ), i18n( "Rate" ) );
 
69
 
 
70
  mDict.insert( QLatin1String( "cpu" ), i18n( "CPU Load" ) );
 
71
  mDict.insert( QLatin1String( "idle" ), i18n( "Idling" ) );
 
72
  mDict.insert( QLatin1String( "nice" ), i18n( "Nice Load" ) );
 
73
  mDict.insert( QLatin1String( "user" ), i18n( "User Load" ) );
 
74
  mDict.insert( QLatin1String( "sys" ), i18n( "System Load" ) );
 
75
  mDict.insert( QLatin1String( "wait" ), i18n( "Waiting" ) );
 
76
  mDict.insert( QLatin1String( "TotalLoad" ), i18n( "Total Load" ) );
 
77
  mDict.insert( QLatin1String( "mem" ), i18n( "Memory" ) );
 
78
  mDict.insert( QLatin1String( "physical" ), i18n( "Physical Memory" ) );
 
79
  mDict.insert( QLatin1String( "swap" ), i18n( "Swap Memory" ) );
 
80
  mDict.insert( QLatin1String( "cached" ), i18n( "Cached Memory" ) );
 
81
  mDict.insert( QLatin1String( "buf" ), i18n( "Buffered Memory" ) );
 
82
  mDict.insert( QLatin1String( "used" ), i18n( "Used Memory" ) );
 
83
  mDict.insert( QLatin1String( "application" ), i18n( "Application Memory" ) );
 
84
  mDict.insert( QLatin1String( "free" ), i18n( "Free Memory" ) );
 
85
  mDict.insert( QLatin1String( "pscount" ), i18n( "Process Count" ) );
 
86
  mDict.insert( QLatin1String( "ps" ), i18n( "Process Controller" ) );
 
87
  mDict.insert( QLatin1String( "disk" ), i18n( "Disk Throughput" ) );
 
88
  mDict.insert( QLatin1String( "load" ), i18nc( "CPU Load", "Load" ) );
 
89
  mDict.insert( QLatin1String( "totalio" ), i18n( "Total Accesses" ) );
 
90
  mDict.insert( QLatin1String( "rio" ), i18n( "Read Accesses" ) );
 
91
  mDict.insert( QLatin1String( "wio" ), i18n( "Write Accesses" ) );
 
92
  mDict.insert( QLatin1String( "rblk" ), i18n( "Read Data" ) );
 
93
  mDict.insert( QLatin1String( "wblk" ), i18n( "Written Data" ) );
 
94
  mDict.insert( QLatin1String( "rtim" ), i18n( "Milliseconds spent reading" ) );
 
95
  mDict.insert( QLatin1String( "wtim" ), i18n( "Milliseconds spent writing" ) );
 
96
  mDict.insert( QLatin1String( "ioqueue" ), i18n( "I/Os currently in progress" ) );
 
97
  mDict.insert( QLatin1String( "pageIn" ), i18n( "Pages In" ) );
 
98
  mDict.insert( QLatin1String( "pageOut" ), i18n( "Pages Out" ) );
 
99
  mDict.insert( QLatin1String( "context" ), i18n( "Context Switches" ) );
 
100
  mDict.insert( QLatin1String( "network" ), i18n( "Network" ) );
 
101
  mDict.insert( QLatin1String( "interfaces" ), i18n( "Interfaces" ) );
 
102
  mDict.insert( QLatin1String( "receiver" ), i18n( "Receiver" ) );
 
103
  mDict.insert( QLatin1String( "transmitter" ), i18n( "Transmitter" ) );
 
104
  mDict.insert( QLatin1String( "data" ), i18n( "Data" ) );
 
105
  mDict.insert( QLatin1String( "compressed" ), i18n( "Compressed Packets" ) );
 
106
  mDict.insert( QLatin1String( "drops" ), i18n( "Dropped Packets" ) );
 
107
  mDict.insert( QLatin1String( "errors" ), i18n( "Errors" ) );
 
108
  mDict.insert( QLatin1String( "fifo" ), i18n( "FIFO Overruns" ) );
 
109
  mDict.insert( QLatin1String( "frame" ), i18n( "Frame Errors" ) );
 
110
  mDict.insert( QLatin1String( "multicast" ), i18n( "Multicast" ) );
 
111
  mDict.insert( QLatin1String( "packets" ), i18n( "Packets" ) );
 
112
  mDict.insert( QLatin1String( "carrier" ), i18n( "Carrier" ) );
 
113
  mDict.insert( QLatin1String( "collisions" ), i18n( "Collisions" ) );
 
114
  mDict.insert( QLatin1String( "sockets" ), i18n( "Sockets" ) );
 
115
  mDict.insert( QLatin1String( "count" ), i18n( "Total Number" ) );
 
116
  mDict.insert( QLatin1String( "list" ), i18n( "Table" ) );
 
117
  mDict.insert( QLatin1String( "apm" ), i18n( "Advanced Power Management" ) );
 
118
  mDict.insert( QLatin1String( "acpi" ), i18n( "ACPI" ) );
 
119
  mDict.insert( QLatin1String( "thermal_zone" ), i18n( "Thermal Zone" ) );
 
120
  mDict.insert( QLatin1String( "temperature" ), i18n( "Temperature" ) );
 
121
  mDict.insert( QLatin1String( "fan" ), i18n( "Fan" ) );
 
122
  mDict.insert( QLatin1String( "state" ), i18n( "State" ) );
 
123
  mDict.insert( QLatin1String( "battery" ), i18n( "Battery" ) );
 
124
  mDict.insert( QLatin1String( "batterycharge" ), i18n( "Battery Charge" ) );
 
125
  mDict.insert( QLatin1String( "batteryusage" ), i18n( "Battery Usage" ) );
 
126
  mDict.insert( QLatin1String( "remainingtime" ), i18n( "Remaining Time" ) );
 
127
  mDict.insert( QLatin1String( "interrupts" ), i18n( "Interrupts" ) );
 
128
  mDict.insert( QLatin1String( "loadavg1" ), i18n( "Load Average (1 min)" ) );
 
129
  mDict.insert( QLatin1String( "loadavg5" ), i18n( "Load Average (5 min)" ) );
 
130
  mDict.insert( QLatin1String( "loadavg15" ), i18n( "Load Average (15 min)" ) );
 
131
  mDict.insert( QLatin1String( "clock" ), i18n( "Clock Frequency" ) );
 
132
  mDict.insert( QLatin1String( "lmsensors" ), i18n( "Hardware Sensors" ) );
 
133
  mDict.insert( QLatin1String( "partitions" ), i18n( "Partition Usage" ) );
 
134
  mDict.insert( QLatin1String( "usedspace" ), i18n( "Used Space" ) );
 
135
  mDict.insert( QLatin1String( "freespace" ), i18n( "Free Space" ) );
 
136
  mDict.insert( QLatin1String( "filllevel" ), i18n( "Fill Level" ) );
 
137
  mDict.insert( QLatin1String( "system" ), i18n( "System" ) );
 
138
  mDict.insert( QLatin1String( "uptime" ), i18n( "Uptime" ) );
 
139
  mDict.insert( QLatin1String( "SoftRaid" ), i18n( "Linux Soft Raid (md)" ) );
 
140
 
 
141
  for ( int i = 0; i < 32; i++ ) {
 
142
    mDict.insert( QLatin1String( "cpu" ) + QString::number( i ), i18n( "CPU %1", i+1 ) );
 
143
    mDict.insert( QLatin1String( "disk" ) + QString::number( i ), i18n( "Disk %1", i+1 ) );
 
144
  }
 
145
 
 
146
  for ( int i = 1; i < 6; i++) {
 
147
    mDict.insert( QLatin1String( "fan" ) + QString::number( i ), i18n( "Fan %1", i ) );
 
148
    mDict.insert( QLatin1String( "temp" ) + QString::number( i ), i18n( "Temperature %1", i ) );
 
149
  }
 
150
 
 
151
  mDict.insert( QLatin1String( "int00" ), i18n( "Total" ) );
 
152
 
 
153
  QString num;
 
154
  for ( int i = 1; i < 25; i++ ) {
 
155
    num.sprintf( "%.2d", i );
 
156
    mDict.insert( QLatin1String( "int" ) + num, ki18n( "Int %1" ).subs( i - 1, 3 ).toString() );
 
157
  }
 
158
 
 
159
  // TODO: translated descriptions not yet implemented.
 
160
 
 
161
  mUnits.insert( QLatin1String( "1/s" ), i18nc( "the unit 1 per second", "1/s" ) );
 
162
  mUnits.insert( QLatin1String( "kBytes" ), i18n( "kBytes" ) );
 
163
  mUnits.insert( QLatin1String( "min" ), i18nc( "the unit minutes", "min" ) );
 
164
  mUnits.insert( QLatin1String( "MHz" ), i18nc( "the frequency unit", "MHz" ) );
 
165
  mUnits.insert( QLatin1String( "%" ), i18nc( "a percentage", "%" ) );
 
166
 
 
167
  mTypes.insert( QLatin1String( "integer" ), i18n( "Integer Value" ) );
 
168
  mTypes.insert( QLatin1String( "float" ), i18n( "Floating Point Value" ) );
 
169
  mTypes.insert( QLatin1String( "table" ), i18n( "Process Controller" ) );
 
170
  mTypes.insert( QLatin1String( "listview" ), i18n( "Table" ) );
 
171
 
 
172
  mBroadcaster = 0;
 
173
 
 
174
}
 
175
 
 
176
SensorManager::~SensorManager()
 
177
{
 
178
}
 
179
 
 
180
bool SensorManager::engage( const QString &hostName, const QString &shell,
 
181
                            const QString &command, int port )
 
182
{
 
183
  if ( !mAgents.contains( hostName ) ) {
 
184
    SensorAgent *agent = 0;
 
185
 
 
186
    if ( port == -1 )
 
187
      agent = new SensorShellAgent( this );
 
188
    else
 
189
      agent = new SensorSocketAgent( this );
 
190
 
 
191
    if ( !agent->start( hostName.toAscii(), shell, command, port ) ) {
 
192
      delete agent;
 
193
      return false;
 
194
    }
 
195
 
 
196
    mAgents.insert( hostName, agent );
 
197
    connect( agent, SIGNAL( reconfigure( const SensorAgent* ) ),
 
198
             SLOT( reconfigure( const SensorAgent* ) ) );
 
199
 
 
200
    emit update();
 
201
    return true;
 
202
  }
 
203
 
 
204
  return false;
 
205
}
 
206
 
 
207
void SensorManager::requestDisengage( const SensorAgent *agent )
 
208
{
 
209
  /* When a sensor agent becomes disfunctional it calls this function
 
210
   * to request that it is being removed from the SensorManager. It must
 
211
   * not call disengage() directly since it would trigger ~SensorAgent()
 
212
   * while we are still in a SensorAgent member function.
 
213
   * So we have to post an event which is later caught by
 
214
   * SensorManger::customEvent(). */
 
215
  AgentEvent* event = new AgentEvent( agent );
 
216
  qApp->postEvent( this, event );
 
217
}
 
218
 
 
219
bool SensorManager::disengage( const SensorAgent *agent )
 
220
{
 
221
  const QString key = mAgents.key( const_cast<SensorAgent*>( agent ) );
 
222
  if ( !key.isEmpty() ) {
 
223
    mAgents.remove( key );
 
224
 
 
225
    emit update();
 
226
    return true;
 
227
  }
 
228
 
 
229
  return false;
 
230
}
 
231
 
 
232
bool SensorManager::isConnected( const QString &hostName )
 
233
{
 
234
  return mAgents.contains( hostName );
 
235
}
 
236
bool SensorManager::disengage( const QString &hostName )
 
237
{
 
238
  if ( mAgents.contains( hostName ) ) {
 
239
    mAgents.remove( hostName );
 
240
 
 
241
    emit update();
 
242
    return true;
 
243
  }
 
244
 
 
245
  return false;
 
246
}
 
247
 
 
248
bool SensorManager::resynchronize( const QString &hostName )
 
249
{
 
250
  const SensorAgent *agent = mAgents.value( hostName );
 
251
 
 
252
  if ( !agent )
 
253
    return false;
 
254
 
 
255
  QString shell, command;
 
256
  int port;
 
257
  hostInfo( hostName, shell, command, port );
 
258
 
 
259
  disengage( hostName );
 
260
 
 
261
  kDebug (1215) << "Re-synchronizing connection to " << hostName;
 
262
 
 
263
  return engage( hostName, shell, command );
 
264
}
 
265
 
 
266
void SensorManager::hostLost( const SensorAgent *agent )
 
267
{
 
268
  emit hostConnectionLost( agent->hostName() );
 
269
 
 
270
  notify( i18n( "Connection to %1 has been lost.", agent->hostName() ) );
 
271
}
 
272
 
 
273
void SensorManager::notify( const QString &msg ) const
 
274
{
 
275
  /* This function relays text messages to the toplevel widget that
 
276
   * displays the message in a pop-up box. It must be used for objects
 
277
   * that might have been deleted before the pop-up box is closed. */
 
278
  if ( mBroadcaster ) {
 
279
    MessageEvent *event = new MessageEvent( msg );
 
280
    qApp->postEvent( mBroadcaster, event );
 
281
  }
 
282
}
 
283
 
 
284
void SensorManager::setBroadcaster( QWidget *wdg )
 
285
{
 
286
  mBroadcaster = wdg;
 
287
}
 
288
 
 
289
void SensorManager::reconfigure( const SensorAgent* )
 
290
{
 
291
  emit update();
 
292
}
 
293
 
 
294
bool SensorManager::event( QEvent *event )
 
295
{
 
296
  if ( event->type() == QEvent::User ) {
 
297
    disengage( static_cast<AgentEvent*>( event )->agent() );
 
298
    return true;
 
299
  }
 
300
 
 
301
  return false;
 
302
}
 
303
 
 
304
bool SensorManager::sendRequest( const QString &hostName, const QString &req,
 
305
                                 SensorClient *client, int id )
 
306
{
 
307
  SensorAgent *agent = mAgents.value( hostName );
 
308
  if ( !agent && hostName == "localhost") {
 
309
    //we should always be able to reconnect to localhost
 
310
    engage("localhost", "", "ksysguardd", -1);
 
311
    agent = mAgents.value( hostName );
 
312
  }
 
313
  if ( agent ) {
 
314
    agent->sendRequest( req, client, id );
 
315
    return true;
 
316
  }
 
317
 
 
318
  return false;
 
319
}
 
320
 
 
321
const QString SensorManager::hostName( const SensorAgent *agent ) const
 
322
{
 
323
  return mAgents.key( const_cast<SensorAgent*>( agent ) );
 
324
}
 
325
 
 
326
bool SensorManager::hostInfo( const QString &hostName, QString &shell,
 
327
                              QString &command, int &port )
 
328
{
 
329
  const SensorAgent *agent = mAgents.value( hostName );
 
330
  if ( agent ) {
 
331
    agent->hostInfo( shell, command, port );
 
332
    return true;
 
333
  }
 
334
 
 
335
  return false;
 
336
}
 
337
 
 
338
QString SensorManager::translateUnit( const QString &unit ) const
 
339
{
 
340
  if ( !unit.isEmpty() && mUnits.contains( unit ) )
 
341
    return mUnits[ unit ];
 
342
  else
 
343
    return unit;
 
344
}
 
345
 
 
346
QString SensorManager::translateSensorPath( const QString &path ) const
 
347
{
 
348
  if ( !path.isEmpty() && mDict.contains( path ) )
 
349
    return mDict[ path ];
 
350
  else
 
351
    return path;
 
352
}
 
353
 
 
354
QString SensorManager::translateSensorType( const QString &type ) const
 
355
{
 
356
  if ( !type.isEmpty() && mTypes.contains( type ) )
 
357
    return mTypes[ type ];
 
358
  else
 
359
    return type;
 
360
}
 
361
 
 
362
QString SensorManager::translateSensor( const QString &sensor ) const
 
363
{
 
364
  QString token, out;
 
365
  int start = 0, end = 0;
 
366
  for ( ; ; ) {
 
367
    end = sensor.indexOf( '/', start );
 
368
    if ( end > 0 )
 
369
      out += translateSensorPath( sensor.mid( start, end - start ) ) + '/';
 
370
    else {
 
371
      out += translateSensorPath( sensor.right( sensor.length() - start ) );
 
372
      break;
 
373
    }
 
374
    start = end + 1;
 
375
  }
 
376
 
 
377
  return out;
 
378
}
 
379
 
 
380
void SensorManager::readProperties( const KConfigGroup& cfg )
 
381
{
 
382
  mHostList = cfg.readEntry( "HostList" ,QStringList());
 
383
  mCommandList = cfg.readEntry( "CommandList",QStringList() );
 
384
}
 
385
 
 
386
void
 
387
SensorManager::saveProperties( KConfigGroup &cfg )
 
388
{
 
389
  cfg.writeEntry( "HostList", mHostList );
 
390
  cfg.writeEntry( "CommandList", mCommandList );
 
391
}
 
392
 
 
393
void SensorManager::disconnectClient( SensorClient *client )
 
394
{
 
395
  QHashIterator<QString, SensorAgent*> it( mAgents );
 
396
 
 
397
  while ( it.hasNext() )
 
398
    it.next().value()->disconnectClient( client );
 
399
}
 
400
 
 
401
#include "SensorManager.moc"