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.
413 lines
11 KiB
413 lines
11 KiB
15 years ago
|
/*
|
||
|
KSysGuard, the KDE System Guard
|
||
|
|
||
|
Copyright (c) 1999, 2000 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!
|
||
|
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <tqdragobject.h>
|
||
|
#include <tqtooltip.h>
|
||
|
#include <tqwhatsthis.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <kiconloader.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
|
#include <tdemessagebox.h>
|
||
15 years ago
|
#include <ksgrd/SensorManager.h>
|
||
|
|
||
|
#include "SensorBrowser.h"
|
||
|
|
||
14 years ago
|
class HostItem : public TQListViewItem
|
||
15 years ago
|
{
|
||
|
public:
|
||
14 years ago
|
HostItem( SensorBrowser *parent, const TQString &text, int id,
|
||
15 years ago
|
KSGRD::SensorAgent *host)
|
||
14 years ago
|
: TQListViewItem( parent, text ), mInited( false ), mId( id ),
|
||
15 years ago
|
mHost( host ), mParent( parent )
|
||
|
{
|
||
|
setExpandable( true );
|
||
|
}
|
||
|
|
||
|
void setOpen( bool open )
|
||
|
{
|
||
|
if ( open && !mInited ) {
|
||
|
mInited = true;
|
||
|
// request sensor list from host
|
||
|
mHost->sendRequest( "monitors", mParent, mId );
|
||
|
}
|
||
|
|
||
14 years ago
|
TQListViewItem::setOpen( open );
|
||
15 years ago
|
}
|
||
|
|
||
|
private:
|
||
|
bool mInited;
|
||
|
int mId;
|
||
|
KSGRD::SensorAgent* mHost;
|
||
|
SensorBrowser* mParent;
|
||
|
};
|
||
|
|
||
14 years ago
|
SensorBrowser::SensorBrowser( TQWidget* parent, KSGRD::SensorManager* sm,
|
||
15 years ago
|
const char* name)
|
||
12 years ago
|
: TDEListView( parent, name ), mSensorManager( sm )
|
||
15 years ago
|
{
|
||
|
mHostInfoList.setAutoDelete(true);
|
||
|
|
||
14 years ago
|
connect( mSensorManager, TQT_SIGNAL( update() ), TQT_SLOT( update() ) );
|
||
|
connect( this, TQT_SIGNAL( clicked( TQListViewItem* ) ),
|
||
|
TQT_SLOT( newItemSelected( TQListViewItem* ) ) );
|
||
|
connect( this, TQT_SIGNAL( returnPressed( TQListViewItem* ) ),
|
||
|
TQT_SLOT( newItemSelected( TQListViewItem* ) ) );
|
||
15 years ago
|
|
||
|
addColumn( i18n( "Sensor Browser" ) );
|
||
|
addColumn( i18n( "Sensor Type" ) );
|
||
|
setFullWidth( true );
|
||
|
|
||
14 years ago
|
TQToolTip::add( this, i18n( "Drag sensors to empty cells of a worksheet "
|
||
15 years ago
|
"or the panel applet." ) );
|
||
|
setRootIsDecorated( true );
|
||
|
|
||
|
// The sensor browser can be completely hidden.
|
||
|
setMinimumWidth( 1 );
|
||
|
|
||
14 years ago
|
TQWhatsThis::add( this, i18n( "The sensor browser lists the connected hosts and the sensors "
|
||
15 years ago
|
"that they provide. Click and drag sensors into drop zones "
|
||
|
"of a worksheet or the panel applet. A display will appear "
|
||
|
"that visualizes the "
|
||
|
"values provided by the sensor. Some sensor displays can "
|
||
|
"display values of multiple sensors. Simply drag other "
|
||
|
"sensors on to the display to add more sensors." ) );
|
||
|
}
|
||
|
|
||
|
SensorBrowser::~SensorBrowser()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void SensorBrowser::disconnect()
|
||
|
{
|
||
14 years ago
|
TQPtrListIterator<HostInfo> it( mHostInfoList );
|
||
15 years ago
|
|
||
|
for ( ; it.current(); ++it )
|
||
|
if ( (*it)->listViewItem()->isSelected() ) {
|
||
|
kdDebug(1215) << "Disconnecting " << (*it)->hostName() << endl;
|
||
|
KSGRD::SensorMgr->requestDisengage( (*it)->sensorAgent() );
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void SensorBrowser::hostReconfigured( const TQString& )
|
||
15 years ago
|
{
|
||
|
// TODO: not yet implemented.
|
||
|
}
|
||
|
|
||
|
void SensorBrowser::update()
|
||
|
{
|
||
|
static int id = 0;
|
||
|
|
||
|
KSGRD::SensorManagerIterator it( mSensorManager );
|
||
|
|
||
|
mHostInfoList.clear();
|
||
|
clear();
|
||
|
|
||
|
KSGRD::SensorAgent* host;
|
||
|
for ( int i = 0 ; ( host = it.current() ); ++it, ++i ) {
|
||
14 years ago
|
TQString hostName = mSensorManager->hostName( host );
|
||
15 years ago
|
HostItem* lvi = new HostItem( this, hostName, id, host );
|
||
|
|
||
12 years ago
|
TQPixmap pix = TDEGlobal::iconLoader()->loadIcon( "computer", TDEIcon::Desktop, TDEIcon::SizeSmall );
|
||
15 years ago
|
lvi->setPixmap( 0, pix );
|
||
|
|
||
|
HostInfo* hostInfo = new HostInfo( id, host, hostName, lvi );
|
||
|
mHostInfoList.append( hostInfo );
|
||
|
++id;
|
||
|
}
|
||
|
|
||
|
setMouseTracking( false );
|
||
|
}
|
||
|
|
||
14 years ago
|
void SensorBrowser::newItemSelected( TQListViewItem *item )
|
||
15 years ago
|
{
|
||
|
static bool showAnnoyingPopup = true;
|
||
|
if ( item && item->pixmap( 0 ) && showAnnoyingPopup)
|
||
|
{
|
||
|
showAnnoyingPopup = false;
|
||
|
KMessageBox::information( this, i18n( "Drag sensors to empty fields in a worksheet." ),
|
||
14 years ago
|
TQString::null, "ShowSBUseInfo" );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void SensorBrowser::answerReceived( int id, const TQString &answer )
|
||
15 years ago
|
{
|
||
|
/* An answer has the following format:
|
||
|
|
||
|
cpu/idle integer
|
||
|
cpu/sys integer
|
||
|
cpu/nice integer
|
||
|
cpu/user integer
|
||
|
ps table
|
||
|
*/
|
||
|
|
||
14 years ago
|
TQPtrListIterator<HostInfo> it( mHostInfoList );
|
||
15 years ago
|
|
||
|
/* Check if id is still valid. It can get obsolete by rapid calls
|
||
|
* of update() or when the sensor died. */
|
||
|
for ( ; it.current(); ++it )
|
||
|
if ( (*it)->id() == id )
|
||
|
break;
|
||
|
|
||
|
if ( !it.current() )
|
||
|
return;
|
||
|
|
||
|
KSGRD::SensorTokenizer lines( answer, '\n' );
|
||
|
|
||
|
for ( uint i = 0; i < lines.count(); ++i ) {
|
||
|
if ( lines[ i ].isEmpty() )
|
||
|
break;
|
||
|
|
||
|
KSGRD::SensorTokenizer words( lines[ i ], '\t' );
|
||
14 years ago
|
TQString sensorName = words[ 0 ];
|
||
|
TQString sensorType = words[ 1 ];
|
||
15 years ago
|
|
||
|
/* Calling update() a rapid sequence will create pending
|
||
|
* requests that do not get erased by calling
|
||
|
* clear(). Subsequent updates will receive the old pending
|
||
|
* answers so we need to make sure that we register each
|
||
|
* sensor only once. */
|
||
|
if ( (*it)->isRegistered( sensorName ) )
|
||
|
return;
|
||
|
|
||
|
/* The sensor browser can display sensors in a hierachical order.
|
||
|
* Sensors can be grouped into nodes by seperating the hierachical
|
||
|
* nodes through slashes in the sensor name. E. g. cpu/user is
|
||
|
* the sensor user in the cpu node. There is no limit for the
|
||
|
* depth of nodes. */
|
||
|
KSGRD::SensorTokenizer absolutePath( sensorName, '/' );
|
||
|
|
||
14 years ago
|
TQListViewItem* parent = (*it)->listViewItem();
|
||
15 years ago
|
for ( uint j = 0; j < absolutePath.count(); ++j ) {
|
||
|
// Localize the sensor name part by part.
|
||
14 years ago
|
TQString name = KSGRD::SensorMgr->translateSensorPath( absolutePath[ j ] );
|
||
15 years ago
|
|
||
|
bool found = false;
|
||
14 years ago
|
TQListViewItem* sibling = parent->firstChild();
|
||
15 years ago
|
while ( sibling && !found ) {
|
||
|
if (sibling->text( 0 ) == name ) {
|
||
|
// The node or sensor is already known.
|
||
|
found = true;
|
||
|
} else
|
||
|
sibling = sibling->nextSibling();
|
||
|
}
|
||
|
if ( !found ) {
|
||
14 years ago
|
TQListViewItem* lvi = new TQListViewItem( parent, name );
|
||
15 years ago
|
if ( j == absolutePath.count() - 1 ) {
|
||
12 years ago
|
TQPixmap pix = TDEGlobal::iconLoader()->loadIcon( "ksysguardd", TDEIcon::Desktop,
|
||
|
TDEIcon::SizeSmall );
|
||
15 years ago
|
lvi->setPixmap( 0, pix );
|
||
|
lvi->setText( 1, KSGRD::SensorMgr->translateSensorType( sensorType ) );
|
||
|
|
||
|
// add sensor info to internal data structure
|
||
|
(*it)->addSensor( lvi, sensorName, name, sensorType );
|
||
|
} else
|
||
|
parent = lvi;
|
||
|
|
||
|
// The child indicator might need to be updated.
|
||
14 years ago
|
repaintItem( parent );
|
||
15 years ago
|
} else
|
||
|
parent = sibling;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
repaintItem( (*it)->listViewItem() );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void SensorBrowser::viewportMouseMoveEvent( TQMouseEvent *e )
|
||
15 years ago
|
{
|
||
|
/* setMouseTracking(false) seems to be broken. With current Qt
|
||
|
* mouse tracking cannot be turned off. So we have to check each event
|
||
|
* whether the LMB is really pressed. */
|
||
|
|
||
14 years ago
|
if ( !( e->state() & Qt::LeftButton ) )
|
||
15 years ago
|
return;
|
||
|
|
||
14 years ago
|
TQListViewItem* item = itemAt( e->pos() );
|
||
15 years ago
|
if ( !item ) // no item under cursor
|
||
|
return;
|
||
|
|
||
|
// Make sure that a sensor and not a node or hostname has been picked.
|
||
14 years ago
|
TQPtrListIterator<HostInfo> it( mHostInfoList );
|
||
15 years ago
|
for ( ; it.current() && !(*it)->isRegistered( item ); ++it );
|
||
|
if ( !it.current() )
|
||
|
return;
|
||
|
|
||
|
// Create text drag object as
|
||
|
// "<hostname> <sensorname> <sensortype> <sensordescription>".
|
||
|
// Only the description may contain blanks.
|
||
|
mDragText = (*it)->hostName() + " " +
|
||
|
(*it)->sensorName( item ) + " " +
|
||
|
(*it)->sensorType( item ) + " " +
|
||
|
(*it)->sensorDescription( item );
|
||
|
|
||
14 years ago
|
TQDragObject* dragObject = new TQTextDrag( mDragText, this );
|
||
15 years ago
|
dragObject->dragCopy();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQStringList SensorBrowser::listHosts()
|
||
15 years ago
|
{
|
||
14 years ago
|
TQStringList hostList;
|
||
15 years ago
|
|
||
14 years ago
|
TQPtrListIterator<HostInfo> it( mHostInfoList );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
hostList.append( (*it)->hostName() );
|
||
|
|
||
|
return hostList;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQStringList SensorBrowser::listSensors( const TQString &hostName )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPtrListIterator<HostInfo> it( mHostInfoList );
|
||
15 years ago
|
for ( ; it.current(); ++it ) {
|
||
|
if ( (*it)->hostName() == hostName ) {
|
||
|
return (*it)->allSensorNames();
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
return TQStringList();
|
||
15 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
Helper classes
|
||
|
*/
|
||
14 years ago
|
SensorInfo::SensorInfo( TQListViewItem *lvi, const TQString &name,
|
||
|
const TQString &desc, const TQString &type )
|
||
15 years ago
|
: mLvi( lvi ), mName( name ), mDesc( desc ), mType( type )
|
||
|
{
|
||
|
}
|
||
|
|
||
14 years ago
|
TQListViewItem* SensorInfo::listViewItem() const
|
||
15 years ago
|
{
|
||
|
return mLvi;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString& SensorInfo::name() const
|
||
15 years ago
|
{
|
||
|
return mName;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString& SensorInfo::type() const
|
||
15 years ago
|
{
|
||
|
return mType;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString& SensorInfo::description() const
|
||
15 years ago
|
{
|
||
|
return mDesc;
|
||
|
}
|
||
|
|
||
|
HostInfo::HostInfo( int id, const KSGRD::SensorAgent *agent,
|
||
14 years ago
|
const TQString &name, TQListViewItem *lvi )
|
||
15 years ago
|
: mId( id ), mSensorAgent( agent ), mHostName( name ), mLvi( lvi )
|
||
|
{
|
||
|
mSensorList.setAutoDelete( true );
|
||
|
}
|
||
|
|
||
|
int HostInfo::id() const
|
||
|
{
|
||
|
return ( mId );
|
||
|
}
|
||
|
|
||
|
const KSGRD::SensorAgent* HostInfo::sensorAgent() const
|
||
|
{
|
||
|
return mSensorAgent;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString& HostInfo::hostName() const
|
||
15 years ago
|
{
|
||
|
return mHostName;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQListViewItem* HostInfo::listViewItem() const
|
||
15 years ago
|
{
|
||
|
return mLvi;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString& HostInfo::sensorName( const TQListViewItem *lvi ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPtrListIterator<SensorInfo> it( mSensorList );
|
||
15 years ago
|
for ( ; it.current() && (*it)->listViewItem() != lvi; ++it );
|
||
|
|
||
|
Q_ASSERT( it.current() );
|
||
|
return ( (*it)->name() );
|
||
|
}
|
||
|
|
||
14 years ago
|
TQStringList HostInfo::allSensorNames() const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQStringList list;
|
||
15 years ago
|
|
||
14 years ago
|
TQPtrListIterator<SensorInfo> it( mSensorList );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
list.append( it.current()->name() );
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString& HostInfo::sensorType( const TQListViewItem *lvi ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPtrListIterator<SensorInfo> it( mSensorList );
|
||
15 years ago
|
for ( ; it.current() && (*it)->listViewItem() != lvi; ++it );
|
||
|
|
||
|
Q_ASSERT( it.current() );
|
||
|
return ( (*it)->type() );
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString& HostInfo::sensorDescription( const TQListViewItem *lvi ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPtrListIterator<SensorInfo> it( mSensorList );
|
||
15 years ago
|
for ( ; it.current() && (*it)->listViewItem() != lvi; ++it );
|
||
|
|
||
|
Q_ASSERT( it.current() );
|
||
|
return ( (*it)->description() );
|
||
|
}
|
||
|
|
||
14 years ago
|
void HostInfo::addSensor( TQListViewItem *lvi, const TQString& name,
|
||
|
const TQString& desc, const TQString& type )
|
||
15 years ago
|
{
|
||
|
SensorInfo* info = new SensorInfo( lvi, name, desc, type );
|
||
|
mSensorList.append( info );
|
||
|
}
|
||
|
|
||
14 years ago
|
bool HostInfo::isRegistered( const TQString& name ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPtrListIterator<SensorInfo> it( mSensorList );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
if ( (*it)->name() == name )
|
||
|
return true;
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
bool HostInfo::isRegistered( TQListViewItem *lvi ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPtrListIterator<SensorInfo> it( mSensorList );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
if ( (*it)->listViewItem() == lvi )
|
||
|
return true;
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
#include "SensorBrowser.moc"
|