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.
464 lines
13 KiB
464 lines
13 KiB
15 years ago
|
/*
|
||
|
KSysGuard, the KDE System Guard
|
||
|
|
||
|
Copyright (c) 1999 - 2002 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 <tqlineedit.h>
|
||
|
#include <tqspinbox.h>
|
||
|
#include <tqwhatsthis.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdefiledialog.h>
|
||
|
#include <tdeio/netaccess.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
|
#include <tdemessagebox.h>
|
||
15 years ago
|
#include <kstandarddirs.h>
|
||
12 years ago
|
#include <tdeaccelmanager.h>
|
||
15 years ago
|
|
||
|
#include "WorkSheet.h"
|
||
|
#include "WorkSheetSettings.h"
|
||
|
#include "ProcessController.h"
|
||
|
|
||
|
#include "Workspace.h"
|
||
|
|
||
14 years ago
|
Workspace::Workspace( TQWidget* parent, const char* name )
|
||
|
: TQTabWidget( parent, name )
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEAcceleratorManager::setNoAccel(this);
|
||
15 years ago
|
|
||
|
mSheetList.setAutoDelete( true );
|
||
|
mAutoSave = true;
|
||
|
|
||
14 years ago
|
connect( this, TQT_SIGNAL( currentChanged( TQWidget* ) ),
|
||
|
TQT_SLOT( updateCaption( TQWidget* ) ) );
|
||
15 years ago
|
|
||
14 years ago
|
TQWhatsThis::add( this, i18n( "This is your work space. It holds your worksheets. You need "
|
||
15 years ago
|
"to create a new worksheet (Menu File->New) before "
|
||
|
"you can drag sensors here." ) );
|
||
|
}
|
||
|
|
||
|
Workspace::~Workspace()
|
||
|
{
|
||
|
/* This workaround is necessary to prevent a crash when the last
|
||
|
* page is not the current page. It seems like the the signal/slot
|
||
|
* administration data is already deleted but slots are still
|
||
|
* being triggered. TODO: I need to ask the Trolls about this. */
|
||
|
|
||
14 years ago
|
disconnect( this, TQT_SIGNAL( currentChanged( TQWidget* ) ), this,
|
||
|
TQT_SLOT( updateCaption( TQWidget* ) ) );
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
void Workspace::saveProperties( TDEConfig *cfg )
|
||
15 years ago
|
{
|
||
|
cfg->writePathEntry( "WorkDir", mWorkDir );
|
||
|
cfg->writeEntry( "CurrentSheet", tabLabel( currentPage() ) );
|
||
|
|
||
14 years ago
|
TQPtrListIterator<WorkSheet> it( mSheetList);
|
||
15 years ago
|
|
||
14 years ago
|
TQStringList list;
|
||
15 years ago
|
for ( int i = 0; it.current(); ++it, ++i )
|
||
|
if ( !(*it)->fileName().isEmpty() )
|
||
|
list.append( (*it)->fileName() );
|
||
|
|
||
|
cfg->writePathEntry( "Sheets", list );
|
||
|
}
|
||
|
|
||
12 years ago
|
void Workspace::readProperties( TDEConfig *cfg )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString currentSheet;
|
||
15 years ago
|
|
||
|
mWorkDir = cfg->readPathEntry( "WorkDir" );
|
||
|
|
||
|
if ( mWorkDir.isEmpty() ) {
|
||
|
/* If workDir is not specified in the config file, it's
|
||
|
* probably the first time the user has started KSysGuard. We
|
||
|
* then "restore" a special default configuration. */
|
||
12 years ago
|
TDEStandardDirs* kstd = TDEGlobal::dirs();
|
||
15 years ago
|
kstd->addResourceType( "data", "share/apps/ksysguard" );
|
||
|
|
||
|
mWorkDir = kstd->saveLocation( "data", "ksysguard" );
|
||
|
|
||
14 years ago
|
TQString origFile = kstd->findResource( "data", "SystemLoad.sgrd" );
|
||
|
TQString newFile = mWorkDir + "/" + i18n( "System Load" ) + ".sgrd";
|
||
15 years ago
|
if ( !origFile.isEmpty() )
|
||
|
restoreWorkSheet( origFile, newFile );
|
||
|
|
||
|
origFile = kstd->findResource( "data", "ProcessTable.sgrd" );
|
||
|
newFile = mWorkDir + "/" + i18n( "Process Table" ) + ".sgrd";
|
||
|
if ( !origFile.isEmpty() )
|
||
|
restoreWorkSheet( origFile, newFile );
|
||
|
|
||
|
currentSheet = i18n( "System Load" );
|
||
|
} else {
|
||
|
currentSheet = cfg->readEntry( "CurrentSheet" );
|
||
14 years ago
|
TQStringList list = cfg->readPathListEntry( "Sheets" );
|
||
|
for ( TQStringList::Iterator it = list.begin(); it != list.end(); ++it )
|
||
15 years ago
|
restoreWorkSheet( *it );
|
||
|
}
|
||
|
|
||
|
// Determine visible sheet.
|
||
14 years ago
|
TQPtrListIterator<WorkSheet> it( mSheetList );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
if ( currentSheet == tabLabel(*it) ) {
|
||
|
showPage( *it );
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Workspace::newWorkSheet()
|
||
|
{
|
||
|
/* Find a name of the form "Sheet %d" that is not yet used by any
|
||
|
* of the existing worksheets. */
|
||
14 years ago
|
TQString sheetName;
|
||
15 years ago
|
bool found;
|
||
|
|
||
|
int i = 1;
|
||
|
do {
|
||
|
sheetName = i18n( "Sheet %1" ).arg( i++ );
|
||
14 years ago
|
TQPtrListIterator<WorkSheet> it( mSheetList );
|
||
15 years ago
|
found = false;
|
||
|
for ( ; it.current() && !found; ++it )
|
||
|
if ( tabLabel(*it) == sheetName )
|
||
|
found = true;
|
||
|
} while ( found );
|
||
|
|
||
|
WorkSheetSettings dlg( this );
|
||
|
dlg.setSheetTitle( sheetName );
|
||
|
if ( dlg.exec() ) {
|
||
|
WorkSheet* sheet = new WorkSheet( dlg.rows(), dlg.columns(), dlg.interval(), this );
|
||
|
sheet->setTitle( dlg.sheetTitle() );
|
||
|
insertTab( sheet, dlg.sheetTitle() );
|
||
|
mSheetList.append( sheet );
|
||
|
showPage( sheet );
|
||
14 years ago
|
connect( sheet, TQT_SIGNAL( sheetModified( TQWidget* ) ),
|
||
|
TQT_SLOT( updateCaption( TQWidget* ) ) );
|
||
|
connect( sheet, TQT_SIGNAL( titleChanged( TQWidget* ) ),
|
||
|
TQT_SLOT( updateSheetTitle( TQWidget* ) ) );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
bool Workspace::saveOnQuit()
|
||
|
{
|
||
14 years ago
|
TQPtrListIterator<WorkSheet> it( mSheetList );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
if ( (*it)->modified() ) {
|
||
|
if ( !mAutoSave || (*it)->fileName().isEmpty() ) {
|
||
|
int res = KMessageBox::warningYesNoCancel( this,
|
||
14 years ago
|
i18n( "The worksheet '%1' contains unsaved data.\n"
|
||
15 years ago
|
"Do you want to save the worksheet?")
|
||
14 years ago
|
.arg( tabLabel( *it ) ), TQString::null, KStdGuiItem::save(), KStdGuiItem::discard() );
|
||
15 years ago
|
if ( res == KMessageBox::Yes )
|
||
|
saveWorkSheet( *it );
|
||
|
else if ( res == KMessageBox::Cancel )
|
||
|
return false; // abort quit
|
||
|
} else
|
||
|
saveWorkSheet(*it);
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void Workspace::loadWorkSheet()
|
||
|
{
|
||
|
KFileDialog dlg( 0, i18n( "*.sgrd|Sensor Files" ), this,
|
||
|
"LoadFileDialog", true );
|
||
|
|
||
|
KURL url = dlg.getOpenURL( mWorkDir, "*.sgrd", 0, i18n( "Select Worksheet to Load" ) );
|
||
|
|
||
|
loadWorkSheet( url );
|
||
|
}
|
||
|
|
||
|
void Workspace::loadWorkSheet( const KURL &url )
|
||
|
{
|
||
|
if ( url.isEmpty() )
|
||
|
return;
|
||
|
|
||
|
/* It's probably not worth the effort to make this really network
|
||
|
* transparent. Unless s/o beats me up I use this pseudo transparent
|
||
|
* code. */
|
||
14 years ago
|
TQString tmpFile;
|
||
12 years ago
|
TDEIO::NetAccess::download( url, tmpFile, this );
|
||
13 years ago
|
mWorkDir = tmpFile.left( tmpFile.findRev( '/' ) );
|
||
15 years ago
|
|
||
|
// Load sheet from file.
|
||
|
if ( !restoreWorkSheet( tmpFile ) )
|
||
|
return;
|
||
|
|
||
|
/* If we have loaded a non-local file we clear the file name so that
|
||
|
* the users is prompted for a new name for saving the file. */
|
||
|
KURL tmpFileUrl;
|
||
|
tmpFileUrl.setPath( tmpFile );
|
||
|
if ( tmpFileUrl != url.url() )
|
||
14 years ago
|
mSheetList.last()->setFileName( TQString::null );
|
||
12 years ago
|
TDEIO::NetAccess::removeTempFile( tmpFile );
|
||
15 years ago
|
|
||
|
emit announceRecentURL( KURL( url ) );
|
||
|
}
|
||
|
|
||
|
void Workspace::saveWorkSheet()
|
||
|
{
|
||
|
saveWorkSheet( (WorkSheet*)currentPage() );
|
||
|
}
|
||
|
|
||
|
void Workspace::saveWorkSheetAs()
|
||
|
{
|
||
|
saveWorkSheetAs( (WorkSheet*)currentPage() );
|
||
|
}
|
||
|
|
||
|
void Workspace::saveWorkSheet( WorkSheet *sheet )
|
||
|
{
|
||
|
if ( !sheet ) {
|
||
|
KMessageBox::sorry( this, i18n( "You do not have a worksheet that could be saved." ) );
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString fileName = sheet->fileName();
|
||
15 years ago
|
if ( fileName.isEmpty() ) {
|
||
|
KFileDialog dlg( 0, i18n( "*.sgrd|Sensor Files" ), this,
|
||
|
"LoadFileDialog", true );
|
||
|
fileName = dlg.getSaveFileName( mWorkDir + "/" + tabLabel( sheet ) +
|
||
|
".sgrd", "*.sgrd", 0,
|
||
|
i18n( "Save Current Worksheet As" ) );
|
||
|
if ( fileName.isEmpty() )
|
||
|
return;
|
||
|
|
||
13 years ago
|
mWorkDir = fileName.left( fileName.findRev( '/' ) );
|
||
15 years ago
|
|
||
|
// extract filename without path
|
||
13 years ago
|
TQString baseName = fileName.right( fileName.length() - fileName.findRev( '/' ) - 1 );
|
||
15 years ago
|
|
||
|
// chop off extension (usually '.sgrd')
|
||
13 years ago
|
baseName = baseName.left( baseName.findRev( '.' ) );
|
||
15 years ago
|
changeTab( sheet, baseName );
|
||
|
}
|
||
|
|
||
|
/* If we cannot save the file is probably write protected. So we need
|
||
|
* to ask the user for a new name. */
|
||
|
if ( !sheet->save( fileName ) ) {
|
||
|
saveWorkSheetAs( sheet );
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/* Add file to recent documents menue. */
|
||
|
KURL url;
|
||
|
url.setPath( fileName );
|
||
|
emit announceRecentURL( url );
|
||
|
}
|
||
|
|
||
|
void Workspace::saveWorkSheetAs( WorkSheet *sheet )
|
||
|
{
|
||
|
if ( !sheet ) {
|
||
|
KMessageBox::sorry( this, i18n( "You do not have a worksheet that could be saved." ) );
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString fileName;
|
||
15 years ago
|
do {
|
||
|
KFileDialog dlg( 0, "*.sgrd", this, "LoadFileDialog", true );
|
||
|
fileName = dlg.getSaveFileName( mWorkDir + "/" + tabLabel( currentPage() ) +
|
||
|
".sgrd", "*.sgrd" );
|
||
|
if ( fileName.isEmpty() )
|
||
|
return;
|
||
|
|
||
13 years ago
|
mWorkDir = fileName.left( fileName.findRev( '/' ) );
|
||
15 years ago
|
|
||
|
// extract filename without path
|
||
13 years ago
|
TQString baseName = fileName.right( fileName.length() - fileName.findRev( '/' ) - 1 );
|
||
15 years ago
|
|
||
|
// chop off extension (usually '.sgrd')
|
||
13 years ago
|
baseName = baseName.left( baseName.findRev( '.' ) );
|
||
15 years ago
|
changeTab( sheet, baseName );
|
||
|
} while ( !sheet->save( fileName ) );
|
||
|
|
||
|
/* Add file to recent documents menue. */
|
||
|
KURL url;
|
||
|
url.setPath( fileName );
|
||
|
emit announceRecentURL( url );
|
||
|
}
|
||
|
|
||
|
void Workspace::deleteWorkSheet()
|
||
|
{
|
||
|
WorkSheet *current = (WorkSheet*)currentPage();
|
||
|
|
||
|
if ( current ) {
|
||
|
if ( current->modified() ) {
|
||
|
if ( !mAutoSave || current->fileName().isEmpty() ) {
|
||
|
int res = KMessageBox::warningYesNoCancel( this,
|
||
14 years ago
|
i18n( "The worksheet '%1' contains unsaved data.\n"
|
||
15 years ago
|
"Do you want to save the worksheet?" )
|
||
14 years ago
|
.arg( tabLabel( current ) ), TQString::null, KStdGuiItem::save(), KStdGuiItem::discard() );
|
||
15 years ago
|
if ( res == KMessageBox::Cancel )
|
||
|
return;
|
||
|
|
||
|
if ( res == KMessageBox::Yes )
|
||
|
saveWorkSheet( current );
|
||
|
} else
|
||
|
saveWorkSheet( current );
|
||
|
}
|
||
|
|
||
|
removePage( current );
|
||
|
mSheetList.remove( current );
|
||
|
} else {
|
||
14 years ago
|
TQString msg = i18n( "There are no worksheets that could be deleted." );
|
||
15 years ago
|
KMessageBox::error( this, msg );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Workspace::removeAllWorkSheets()
|
||
|
{
|
||
|
WorkSheet *sheet;
|
||
|
while ( ( sheet = (WorkSheet*)currentPage() ) != 0 ) {
|
||
|
removePage( sheet );
|
||
|
mSheetList.remove( sheet );
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void Workspace::deleteWorkSheet( const TQString &fileName )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPtrListIterator<WorkSheet> it( mSheetList );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
if ( (*it)->fileName() == fileName ) {
|
||
|
removePage( *it );
|
||
|
mSheetList.remove( *it );
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
WorkSheet *Workspace::restoreWorkSheet( const TQString &fileName, const TQString &newName )
|
||
15 years ago
|
{
|
||
|
/* We might want to save the worksheet under a different name later. This
|
||
|
* name can be specified by newName. If newName is empty we use the
|
||
|
* original name to save the work sheet. */
|
||
14 years ago
|
TQString tmpStr;
|
||
15 years ago
|
if ( newName.isEmpty() )
|
||
|
tmpStr = fileName;
|
||
|
else
|
||
|
tmpStr = newName;
|
||
|
|
||
|
// extract filename without path
|
||
13 years ago
|
TQString baseName = tmpStr.right( tmpStr.length() - tmpStr.findRev( '/' ) - 1 );
|
||
15 years ago
|
|
||
|
// chop off extension (usually '.sgrd')
|
||
13 years ago
|
baseName = baseName.left( baseName.findRev( '.' ) );
|
||
15 years ago
|
|
||
|
WorkSheet *sheet = new WorkSheet( this );
|
||
|
sheet->setTitle( baseName );
|
||
|
insertTab( sheet, baseName );
|
||
|
showPage( sheet );
|
||
|
|
||
|
if ( !sheet->load( fileName ) ) {
|
||
|
delete sheet;
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
mSheetList.append( sheet );
|
||
14 years ago
|
connect( sheet, TQT_SIGNAL( sheetModified( TQWidget* ) ),
|
||
|
TQT_SLOT( updateCaption( TQWidget* ) ) );
|
||
15 years ago
|
|
||
|
/* Force the file name to be the new name. This also sets the modified
|
||
|
* flag, so that the file will get saved on exit. */
|
||
|
if ( !newName.isEmpty() )
|
||
|
sheet->setFileName( newName );
|
||
|
|
||
|
return sheet;
|
||
|
}
|
||
|
|
||
|
void Workspace::cut()
|
||
|
{
|
||
|
WorkSheet *current = (WorkSheet*)currentPage();
|
||
|
|
||
|
if ( current )
|
||
|
current->cut();
|
||
|
}
|
||
|
|
||
|
void Workspace::copy()
|
||
|
{
|
||
|
WorkSheet *current = (WorkSheet*)currentPage();
|
||
|
|
||
|
if ( current )
|
||
|
current->copy();
|
||
|
}
|
||
|
|
||
|
void Workspace::paste()
|
||
|
{
|
||
|
WorkSheet *current = (WorkSheet*)currentPage();
|
||
|
|
||
|
if ( current )
|
||
|
current->paste();
|
||
|
}
|
||
|
|
||
|
void Workspace::configure()
|
||
|
{
|
||
|
WorkSheet *current = (WorkSheet*)currentPage();
|
||
|
|
||
|
if ( !current )
|
||
|
return;
|
||
|
|
||
|
current->settings();
|
||
|
}
|
||
|
|
||
14 years ago
|
void Workspace::updateCaption( TQWidget* wdg )
|
||
15 years ago
|
{
|
||
|
if ( wdg )
|
||
|
emit setCaption( tabLabel( wdg ), ((WorkSheet*)wdg)->modified() );
|
||
|
else
|
||
14 years ago
|
emit setCaption( TQString::null, false );
|
||
15 years ago
|
|
||
|
for ( WorkSheet* s = mSheetList.first(); s != 0; s = mSheetList.next() )
|
||
|
((WorkSheet*)s)->setIsOnTop( s == wdg );
|
||
|
}
|
||
|
|
||
14 years ago
|
void Workspace::updateSheetTitle( TQWidget* wdg )
|
||
15 years ago
|
{
|
||
|
if ( wdg )
|
||
|
changeTab( wdg, static_cast<WorkSheet*>( wdg )->title() );
|
||
|
}
|
||
|
|
||
|
void Workspace::applyStyle()
|
||
|
{
|
||
|
if ( currentPage() )
|
||
|
((WorkSheet*)currentPage())->applyStyle();
|
||
|
}
|
||
|
|
||
|
void Workspace::showProcesses()
|
||
|
{
|
||
12 years ago
|
TDEStandardDirs* kstd = TDEGlobal::dirs();
|
||
15 years ago
|
kstd->addResourceType( "data", "share/apps/ksysguard" );
|
||
|
|
||
14 years ago
|
TQString file = kstd->findResource( "data", "ProcessTable.sgrd" );
|
||
15 years ago
|
if ( file.isEmpty() ) {
|
||
|
KMessageBox::error( this, i18n( "Cannot find file ProcessTable.sgrd." ) );
|
||
|
return;
|
||
|
}
|
||
|
WorkSheet *processSheet = restoreWorkSheet( file );
|
||
|
if(!processSheet) return;
|
||
|
|
||
|
//Set the focus of the search line. This is nasty I know, but I don't know how better to do this :(
|
||
|
KSGRD::SensorDisplay *processSensor = processSheet->display( 0,0 );
|
||
|
if(!processSensor || !processSensor->isA("ProcessController")) return;
|
||
|
ProcessController *controller = dynamic_cast<ProcessController *>(processSensor);
|
||
|
if(!controller) return;
|
||
|
controller->setSearchFocus();
|
||
|
|
||
|
}
|
||
|
|
||
|
#include "Workspace.moc"
|