You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
496 lines
14 KiB
496 lines
14 KiB
15 years ago
|
/*
|
||
|
KSysGuard, the KDE System Guard
|
||
|
|
||
|
Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
|
||
|
|
||
|
This program is free software; you can redistribute it and/or
|
||
|
modify it under the terms of version 2 of the GNU General Public
|
||
|
License as published by the Free Software Foundation.
|
||
|
|
||
|
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.
|
||
|
|
||
|
KSysGuard is currently maintained by Chris Schlaeger
|
||
|
<cs@kde.org>. Please do not commit any changes without consulting
|
||
|
me first. Thanks!
|
||
|
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqcursor.h>
|
||
|
#include <tqdom.h>
|
||
|
#include <tqdragobject.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqpushbutton.h>
|
||
|
#include <tqspinbox.h>
|
||
|
#include <tqtooltip.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
|
#include <tdemessagebox.h>
|
||
15 years ago
|
#include <ksavefile.h>
|
||
|
#include <kstandarddirs.h>
|
||
12 years ago
|
#include <tdepopupmenu.h>
|
||
15 years ago
|
|
||
|
#include <ksgrd/SensorClient.h>
|
||
|
#include <ksgrd/SensorManager.h>
|
||
|
#include <ksgrd/StyleEngine.h>
|
||
|
|
||
|
#include "DancingBars.h"
|
||
|
#include "FancyPlotter.h"
|
||
|
#include "KSGAppletSettings.h"
|
||
|
#include "MultiMeter.h"
|
||
|
|
||
|
#include "KSysGuardApplet.h"
|
||
|
|
||
|
extern "C"
|
||
|
{
|
||
15 years ago
|
KDE_EXPORT KPanelApplet* init( TQWidget *parent, const TQString& configFile )
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEGlobal::locale()->insertCatalogue( "ksysguard" );
|
||
15 years ago
|
return new KSysGuardApplet( configFile, KPanelApplet::Normal,
|
||
|
KPanelApplet::Preferences, parent,
|
||
|
"ksysguardapplet" );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
KSysGuardApplet::KSysGuardApplet( const TQString& configFile, Type type,
|
||
|
int actions, TQWidget *parent,
|
||
15 years ago
|
const char *name )
|
||
|
: KPanelApplet( configFile, type, actions, parent, name)
|
||
|
{
|
||
|
mSettingsDlg = 0;
|
||
|
|
||
|
KSGRD::SensorMgr = new KSGRD::SensorManager();
|
||
|
|
||
|
KSGRD::Style = new KSGRD::StyleEngine();
|
||
|
|
||
|
mDockCount = 1;
|
||
15 years ago
|
mDockList = new TQWidget*[ mDockCount ];
|
||
15 years ago
|
|
||
|
mSizeRatio = 1.0;
|
||
|
addEmptyDisplay( mDockList, 0 );
|
||
|
|
||
|
updateInterval( 2 );
|
||
|
|
||
|
load();
|
||
|
|
||
|
setAcceptDrops( true );
|
||
|
}
|
||
|
|
||
|
KSysGuardApplet::~KSysGuardApplet()
|
||
|
{
|
||
|
save();
|
||
|
|
||
|
delete [] mDockList;
|
||
|
mDockList = 0;
|
||
|
|
||
|
delete mSettingsDlg;
|
||
|
mSettingsDlg = 0;
|
||
|
|
||
|
delete KSGRD::Style;
|
||
|
delete KSGRD::SensorMgr;
|
||
|
KSGRD::SensorMgr = 0;
|
||
|
}
|
||
|
|
||
|
int KSysGuardApplet::widthForHeight( int height ) const
|
||
|
{
|
||
|
return ( (int) ( height * mSizeRatio + 0.5 ) * mDockCount );
|
||
|
}
|
||
|
|
||
|
int KSysGuardApplet::heightForWidth( int width ) const
|
||
|
{
|
||
|
return ( (int) ( width * mSizeRatio + 0.5 ) * mDockCount );
|
||
|
}
|
||
|
|
||
15 years ago
|
void KSysGuardApplet::resizeEvent( TQResizeEvent* )
|
||
15 years ago
|
{
|
||
14 years ago
|
layout();
|
||
15 years ago
|
}
|
||
|
|
||
|
void KSysGuardApplet::preferences()
|
||
|
{
|
||
|
if(mSettingsDlg) {
|
||
|
return;
|
||
|
}
|
||
|
mSettingsDlg = new KSGAppletSettings( this );
|
||
|
|
||
15 years ago
|
connect( mSettingsDlg, TQT_SIGNAL( applyClicked() ), TQT_SLOT( applySettings() ) );
|
||
|
connect( mSettingsDlg, TQT_SIGNAL( okClicked() ), TQT_SLOT( applySettings() ) );
|
||
|
connect( mSettingsDlg, TQT_SIGNAL( finished() ), TQT_SLOT( preferencesFinished() ) );
|
||
15 years ago
|
|
||
|
mSettingsDlg->setNumDisplay( mDockCount );
|
||
|
mSettingsDlg->setSizeRatio( (int) ( mSizeRatio * 100.0 + 0.5 ) );
|
||
|
mSettingsDlg->setUpdateInterval( updateInterval() );
|
||
|
|
||
|
mSettingsDlg->show();
|
||
|
}
|
||
|
void KSysGuardApplet::preferencesFinished()
|
||
|
{
|
||
|
mSettingsDlg->delayedDestruct();
|
||
|
mSettingsDlg = 0;
|
||
|
}
|
||
|
void KSysGuardApplet::applySettings()
|
||
|
{
|
||
|
updateInterval( mSettingsDlg->updateInterval() );
|
||
|
mSizeRatio = mSettingsDlg->sizeRatio() / 100.0;
|
||
|
resizeDocks( mSettingsDlg->numDisplay() );
|
||
|
|
||
|
for ( uint i = 0; i < mDockCount; ++i )
|
||
14 years ago
|
if ( !mDockList[ i ]->isA( TQFRAME_OBJECT_NAME_STRING ) )
|
||
15 years ago
|
((KSGRD::SensorDisplay*)mDockList[ i ])->setUpdateInterval( updateInterval() );
|
||
|
|
||
|
save();
|
||
|
}
|
||
|
|
||
|
void KSysGuardApplet::sensorDisplayModified( bool modified )
|
||
|
{
|
||
|
if ( modified )
|
||
|
save();
|
||
|
}
|
||
|
|
||
14 years ago
|
void KSysGuardApplet::layout()
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( orientation() == Qt::Horizontal ) {
|
||
15 years ago
|
int h = height();
|
||
|
int w = (int) ( h * mSizeRatio + 0.5 );
|
||
|
for ( uint i = 0; i < mDockCount; ++i )
|
||
|
if ( mDockList[ i ] )
|
||
|
mDockList[ i ]->setGeometry( i * w, 0, w, h );
|
||
|
} else {
|
||
|
int w = width();
|
||
|
int h = (int) ( w * mSizeRatio + 0.5 );
|
||
|
for ( uint i = 0; i < mDockCount; ++i )
|
||
|
if ( mDockList[ i ] )
|
||
|
mDockList[ i ]->setGeometry( 0, i * h, w, h );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
int KSysGuardApplet::findDock( const TQPoint& point )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( orientation() == Qt::Horizontal )
|
||
15 years ago
|
return ( point.x() / (int) ( height() * mSizeRatio + 0.5 ) );
|
||
|
else
|
||
|
return ( point.y() / (int) ( width() * mSizeRatio + 0.5 ) );
|
||
|
}
|
||
|
|
||
15 years ago
|
void KSysGuardApplet::dragEnterEvent( TQDragEnterEvent *e )
|
||
15 years ago
|
{
|
||
15 years ago
|
e->accept( TQTextDrag::canDecode( e ) );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void KSysGuardApplet::dropEvent( TQDropEvent *e )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString dragObject;
|
||
15 years ago
|
|
||
15 years ago
|
if ( TQTextDrag::decode( e, dragObject ) ) {
|
||
15 years ago
|
// The host name, sensor name and type are seperated by a ' '.
|
||
15 years ago
|
TQStringList parts = TQStringList::split( ' ', dragObject );
|
||
15 years ago
|
|
||
15 years ago
|
TQString hostName = parts[ 0 ];
|
||
|
TQString sensorName = parts[ 1 ];
|
||
|
TQString sensorType = parts[ 2 ];
|
||
|
TQString sensorDescr = parts[ 3 ];
|
||
15 years ago
|
|
||
|
if ( hostName.isEmpty() || sensorName.isEmpty() || sensorType.isEmpty() )
|
||
|
return;
|
||
|
|
||
|
int dock = findDock( e->pos() );
|
||
14 years ago
|
if ( mDockList[ dock ]->isA( TQFRAME_OBJECT_NAME_STRING ) ) {
|
||
15 years ago
|
if ( sensorType == "integer" || sensorType == "float" ) {
|
||
12 years ago
|
TDEPopupMenu popup;
|
||
15 years ago
|
TQWidget *wdg = 0;
|
||
15 years ago
|
|
||
|
popup.insertTitle( i18n( "Select Display Type" ) );
|
||
|
popup.insertItem( i18n( "&Signal Plotter" ), 1 );
|
||
|
popup.insertItem( i18n( "&Multimeter" ), 2 );
|
||
|
popup.insertItem( i18n( "&Dancing Bars" ), 3 );
|
||
15 years ago
|
switch ( popup.exec( TQCursor::pos() ) ) {
|
||
15 years ago
|
case 1:
|
||
|
wdg = new FancyPlotter( this, "FancyPlotter", sensorDescr,
|
||
|
100.0, 100.0, true );
|
||
|
break;
|
||
|
|
||
|
case 2:
|
||
|
wdg = new MultiMeter( this, "MultiMeter", sensorDescr,
|
||
|
100.0, 100.0, true );
|
||
|
break;
|
||
|
|
||
|
case 3:
|
||
|
wdg = new DancingBars( this, "DancingBars", sensorDescr,
|
||
|
100, 100, true );
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if ( wdg ) {
|
||
|
delete mDockList[ dock ];
|
||
|
mDockList[ dock ] = wdg;
|
||
14 years ago
|
layout();
|
||
15 years ago
|
|
||
15 years ago
|
connect( wdg, TQT_SIGNAL( modified( bool ) ),
|
||
|
TQT_SLOT( sensorDisplayModified( bool ) ) );
|
||
15 years ago
|
|
||
|
mDockList[ dock ]->show();
|
||
|
}
|
||
|
} else {
|
||
|
KMessageBox::sorry( this,
|
||
|
i18n( "The KSysGuard applet does not support displaying of "
|
||
|
"this type of sensor. Please choose another sensor." ) );
|
||
|
|
||
14 years ago
|
layout();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
if ( !mDockList[ dock ]->isA( TQFRAME_OBJECT_NAME_STRING ) )
|
||
15 years ago
|
((KSGRD::SensorDisplay*)mDockList[ dock ])->
|
||
|
addSensor( hostName, sensorName, sensorType, sensorDescr );
|
||
|
}
|
||
|
|
||
|
save();
|
||
|
}
|
||
|
|
||
15 years ago
|
void KSysGuardApplet::customEvent( TQCustomEvent *e )
|
||
15 years ago
|
{
|
||
15 years ago
|
if ( e->type() == TQEvent::User ) {
|
||
15 years ago
|
// SensorDisplays send out this event if they want to be removed.
|
||
|
removeDisplay( (KSGRD::SensorDisplay*)e->data() );
|
||
|
save();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KSysGuardApplet::removeDisplay( KSGRD::SensorDisplay *display )
|
||
|
{
|
||
|
for ( uint i = 0; i < mDockCount; ++i )
|
||
|
if ( display == mDockList[i] ) {
|
||
|
delete mDockList[ i ];
|
||
|
|
||
|
addEmptyDisplay( mDockList, i );
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KSysGuardApplet::resizeDocks( uint newDockCount )
|
||
|
{
|
||
|
/* This function alters the number of available docks. The number of
|
||
|
* docks can be increased or decreased. We try to preserve existing
|
||
|
* docks if possible. */
|
||
|
|
||
|
if ( newDockCount == mDockCount ) {
|
||
|
emit updateLayout();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Create and initialize new dock array.
|
||
15 years ago
|
TQWidget** tmp = new TQWidget*[ newDockCount ];
|
||
15 years ago
|
|
||
|
uint i;
|
||
|
for ( i = 0; ( i < newDockCount ) && ( i < mDockCount ); ++i )
|
||
|
tmp[ i ] = mDockList[ i ];
|
||
|
|
||
|
for ( i = newDockCount; i < mDockCount; ++i )
|
||
|
if ( mDockList[ i ] )
|
||
|
delete mDockList[ i ];
|
||
|
|
||
|
for ( i = mDockCount; i < newDockCount; ++i )
|
||
|
addEmptyDisplay( tmp, i );
|
||
|
|
||
|
delete [] mDockList;
|
||
|
|
||
|
mDockList = tmp;
|
||
|
mDockCount = newDockCount;
|
||
|
|
||
|
emit updateLayout();
|
||
|
}
|
||
|
|
||
|
bool KSysGuardApplet::load()
|
||
|
{
|
||
12 years ago
|
TDEStandardDirs* kstd = TDEGlobal::dirs();
|
||
15 years ago
|
kstd->addResourceType( "data", "share/apps/ksysguard" );
|
||
15 years ago
|
TQString fileName = kstd->findResource( "data", "KSysGuardApplet.xml" );
|
||
15 years ago
|
|
||
15 years ago
|
TQFile file( fileName );
|
||
15 years ago
|
if ( !file.open( IO_ReadOnly ) ) {
|
||
|
KMessageBox::sorry( this, i18n( "Cannot open the file %1." ).arg( fileName ) );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Parse the XML file.
|
||
15 years ago
|
TQDomDocument doc;
|
||
15 years ago
|
|
||
|
// Read in file and check for a valid XML header.
|
||
|
if ( !doc.setContent( &file ) ) {
|
||
|
KMessageBox::sorry( this, i18n( "The file %1 does not contain valid XML." )
|
||
|
.arg( fileName ) );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Check for proper document type.
|
||
|
if ( doc.doctype().name() != "KSysGuardApplet" ) {
|
||
|
KMessageBox::sorry( this, i18n( "The file %1 does not contain a valid applet "
|
||
|
"definition, which must have a document type 'KSysGuardApplet'." )
|
||
|
.arg( fileName ) );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDomElement element = doc.documentElement();
|
||
15 years ago
|
bool ok;
|
||
|
uint count = element.attribute( "dockCnt" ).toUInt( &ok );
|
||
|
if ( !ok )
|
||
|
count = 1;
|
||
|
|
||
|
mSizeRatio = element.attribute( "sizeRatio" ).toDouble( &ok );
|
||
|
if ( !ok )
|
||
|
mSizeRatio = 1.0;
|
||
|
|
||
|
updateInterval( element.attribute( "interval" ).toUInt( &ok ) );
|
||
|
if ( !ok )
|
||
|
updateInterval( 2 );
|
||
|
|
||
|
resizeDocks( count );
|
||
|
|
||
|
/* Load lists of hosts that are needed for the work sheet and try
|
||
|
* to establish a connection. */
|
||
15 years ago
|
TQDomNodeList dnList = element.elementsByTagName( "host" );
|
||
15 years ago
|
uint i;
|
||
|
for ( i = 0; i < dnList.count(); ++i ) {
|
||
15 years ago
|
TQDomElement element = dnList.item( i ).toElement();
|
||
15 years ago
|
int port = element.attribute( "port" ).toInt( &ok );
|
||
|
if ( !ok )
|
||
|
port = -1;
|
||
|
|
||
|
KSGRD::SensorMgr->engage( element.attribute( "name" ),
|
||
|
element.attribute( "shell" ),
|
||
|
element.attribute( "command" ), port );
|
||
|
}
|
||
|
//if no hosts are specified, at least connect to localhost
|
||
|
if(dnList.count() == 0)
|
||
|
KSGRD::SensorMgr->engage( "localhost", "", "ksysguardd", -1);
|
||
|
|
||
|
// Load the displays and place them into the work sheet.
|
||
|
dnList = element.elementsByTagName( "display" );
|
||
|
for ( i = 0; i < dnList.count(); ++i ) {
|
||
15 years ago
|
TQDomElement element = dnList.item( i ).toElement();
|
||
15 years ago
|
uint dock = element.attribute( "dock" ).toUInt();
|
||
|
if ( i >= mDockCount ) {
|
||
|
kdDebug (1215) << "Dock number " << i << " out of range "
|
||
|
<< mDockCount << endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString classType = element.attribute( "class" );
|
||
15 years ago
|
KSGRD::SensorDisplay* newDisplay;
|
||
|
if ( classType == "FancyPlotter" )
|
||
|
newDisplay = new FancyPlotter( this, "FancyPlotter", "Dummy", 100.0, 100.0, true /*no frame*/, true /*run ksysguard menu*/);
|
||
|
else if ( classType == "MultiMeter" )
|
||
|
newDisplay = new MultiMeter( this, "MultiMeter", "Dummy", 100.0, 100.0, true /*no frame*/, true /*run ksysguard menu*/ );
|
||
|
else if ( classType == "DancingBars" )
|
||
|
newDisplay = new DancingBars( this, "DancingBars", "Dummy", 100, 100, true /*no frame*/, true /*run ksysguard menu*/);
|
||
|
else {
|
||
|
KMessageBox::sorry( this, i18n( "The KSysGuard applet does not support displaying of "
|
||
|
"this type of sensor. Please choose another sensor." ) );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
newDisplay->setUpdateInterval( updateInterval() );
|
||
|
|
||
|
// load display specific settings
|
||
|
if ( !newDisplay->restoreSettings( element ) )
|
||
|
return false;
|
||
|
|
||
|
delete mDockList[ dock ];
|
||
|
mDockList[ dock ] = newDisplay;
|
||
|
|
||
15 years ago
|
connect( newDisplay, TQT_SIGNAL( modified( bool ) ),
|
||
|
TQT_SLOT( sensorDisplayModified( bool ) ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool KSysGuardApplet::save()
|
||
|
{
|
||
|
// Parse the XML file.
|
||
15 years ago
|
TQDomDocument doc( "KSysGuardApplet" );
|
||
15 years ago
|
doc.appendChild( doc.createProcessingInstruction(
|
||
|
"xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
|
||
|
|
||
|
// save work sheet information
|
||
15 years ago
|
TQDomElement ws = doc.createElement( "WorkSheet" );
|
||
15 years ago
|
doc.appendChild( ws );
|
||
|
ws.setAttribute( "dockCnt", mDockCount );
|
||
|
ws.setAttribute( "sizeRatio", mSizeRatio );
|
||
|
ws.setAttribute( "interval", updateInterval() );
|
||
|
|
||
15 years ago
|
TQStringList hosts;
|
||
15 years ago
|
uint i;
|
||
|
for ( i = 0; i < mDockCount; ++i )
|
||
14 years ago
|
if ( !mDockList[ i ]->isA( TQFRAME_OBJECT_NAME_STRING ) )
|
||
15 years ago
|
((KSGRD::SensorDisplay*)mDockList[ i ])->hosts( hosts );
|
||
|
|
||
|
// save host information (name, shell, etc.)
|
||
15 years ago
|
TQStringList::Iterator it;
|
||
15 years ago
|
for ( it = hosts.begin(); it != hosts.end(); ++it ) {
|
||
15 years ago
|
TQString shell, command;
|
||
15 years ago
|
int port;
|
||
|
|
||
|
if ( KSGRD::SensorMgr->hostInfo( *it, shell, command, port ) ) {
|
||
15 years ago
|
TQDomElement host = doc.createElement( "host" );
|
||
15 years ago
|
ws.appendChild( host );
|
||
|
host.setAttribute( "name", *it );
|
||
|
host.setAttribute( "shell", shell );
|
||
|
host.setAttribute( "command", command );
|
||
|
host.setAttribute( "port", port );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for ( i = 0; i < mDockCount; ++i )
|
||
14 years ago
|
if ( !mDockList[ i ]->isA( TQFRAME_OBJECT_NAME_STRING ) ) {
|
||
15 years ago
|
TQDomElement element = doc.createElement( "display" );
|
||
15 years ago
|
ws.appendChild( element );
|
||
|
element.setAttribute( "dock", i );
|
||
|
element.setAttribute( "class", mDockList[ i ]->className() );
|
||
|
|
||
|
((KSGRD::SensorDisplay*)mDockList[ i ])->saveSettings( doc, element );
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEStandardDirs* kstd = TDEGlobal::dirs();
|
||
15 years ago
|
kstd->addResourceType( "data", "share/apps/ksysguard" );
|
||
15 years ago
|
TQString fileName = kstd->saveLocation( "data", "ksysguard" );
|
||
15 years ago
|
fileName += "/KSysGuardApplet.xml";
|
||
|
|
||
|
KSaveFile file( fileName, 0644 );
|
||
|
|
||
|
if ( file.status() == 0 )
|
||
|
{
|
||
15 years ago
|
file.textStream()->setEncoding( TQTextStream::UnicodeUTF8 );
|
||
15 years ago
|
*(file.textStream()) << doc;
|
||
|
file.close();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
KMessageBox::sorry( this, i18n( "Cannot save file %1" ).arg( fileName ) );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KSysGuardApplet::addEmptyDisplay( TQWidget **dock, uint pos )
|
||
15 years ago
|
{
|
||
15 years ago
|
dock[ pos ] = new TQFrame( this );
|
||
|
((TQFrame*)dock[ pos ])->setFrameStyle( TQFrame::WinPanel | TQFrame::Sunken );
|
||
|
TQToolTip::add( dock[ pos ],
|
||
13 years ago
|
i18n( "Drag sensors from the TDE System Guard into this cell." ) );
|
||
15 years ago
|
|
||
14 years ago
|
layout();
|
||
15 years ago
|
if ( isVisible() )
|
||
|
dock[ pos ]->show();
|
||
|
}
|
||
|
|
||
|
#include "KSysGuardApplet.moc"
|