2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999 - 2002 Chris Schlaeger <cs@kde.org>
5
Copyright (c) 2006 John Tapsell <tapsell@kde.org>
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public
9
License version 2 or at your option version 3 as published by
10
the Free Software Foundation.
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.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26
#include <kfiledialog.h>
27
#include <kio/netaccess.h>
29
#include <kmessagebox.h>
30
#include <kstandarddirs.h>
31
#include <kacceleratormanager.h>
32
#include <kactioncollection.h>
34
#include <knewstuff3/downloaddialog.h>
36
#include "WorkSheet.h"
37
#include "WorkSheetSettings.h"
39
#include "Workspace.h"
40
#include "ksysguard.h"
42
Workspace::Workspace( QWidget* parent)
43
: KTabWidget( parent )
45
KAcceleratorManager::setNoAccel(this);
46
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
47
setDocumentMode(true);
48
connect(&mDirWatch, SIGNAL(deleted(const QString&)), this, SLOT(removeWorkSheet(const QString &)));
51
Workspace::~Workspace()
55
void Workspace::saveProperties( KConfigGroup& cfg )
58
for(int i =0; i< mSheetList.size(); i++)
59
if ( !mSheetList.at(i)->fileName().isEmpty() )
60
list.append( mSheetList.at(i)->fileName() );
62
cfg.writePathEntry( "SelectedSheets", list );
63
cfg.writeEntry( "currentSheet", currentIndex() );
66
void Workspace::readProperties( const KConfigGroup& cfg )
68
QStringList selectedSheets = cfg.readPathEntry( "SelectedSheets", QStringList() );
70
if ( selectedSheets.isEmpty() ) {
71
/* If SelectedSheets config entry is not there, then it's
72
* probably the first time the user has started KSysGuard. We
73
* then "restore" a special default configuration. */
74
selectedSheets << "ProcessTable.sgrd";
75
selectedSheets << "SystemLoad2.sgrd";
76
} else if(selectedSheets[0] != "ProcessTable.sgrd") {
77
//We need to make sure that this is really is the process table on the first tab. No GUI way of changing this, but should make sure anyway.
78
//Plus this migrates users from the kde3 setup
79
selectedSheets.removeAll("ProcessTable.sgrd");
80
selectedSheets.prepend( "ProcessTable.sgrd");
83
int oldSystemLoad = selectedSheets.indexOf("SystemLoad.sgrd");
84
if(oldSystemLoad != -1) {
85
selectedSheets.replace(oldSystemLoad, "SystemLoad2.sgrd");
88
KStandardDirs* kstd = KGlobal::dirs();
90
for ( QStringList::Iterator it = selectedSheets.begin(); it != selectedSheets.end(); ++it ) {
91
filename = kstd->findResource( "data", "ksysguard/" + *it);
92
if(!filename.isEmpty()) {
93
restoreWorkSheet( filename, false);
97
int idx = cfg.readEntry( "currentSheet", 0 );
98
if (idx < 0 || idx > count() - 1) {
101
setCurrentIndex(idx);
104
QString Workspace::makeNameForNewSheet() const
106
/* Find a name of the form "Sheet %d" that is not yet used by any
107
* of the existing worksheets. */
111
KStandardDirs* kstd = KGlobal::dirs();
113
sheetName = i18n( "Sheet %1" , i++ );
114
//Check we don't have any existing files with this name
115
found = !(kstd->findResource( "data", "ksysguard/" + sheetName + ".sgrd").isEmpty());
117
//Check if we have any sheets with the same tab name or file name
118
for(int i = 0; !found && i < mSheetList.size(); i++)
119
if ( tabText(indexOf(mSheetList.at(i))) == sheetName || QString(sheetName+".sgrd") == mSheetList.at(i)->fileName())
127
void Workspace::refreshActiveWorksheet()
129
WorkSheet* currentSheet = mSheetList.at(currentIndex());
130
currentSheet->refreshSheet();
132
void Workspace::newWorkSheet()
134
/* Find a name of the form "Sheet %d" that is not yet used by any
135
* of the existing worksheets. */
136
QString sheetName = makeNameForNewSheet();
138
WorkSheetSettings dlg( this, false /*not locked. New custom sheets aren't locked*/ );
139
dlg.setSheetTitle( sheetName );
141
WorkSheet* sheet = new WorkSheet( dlg.rows(), dlg.columns(), dlg.interval(), 0 );
142
sheet->setTitle( dlg.sheetTitle() );
143
sheet->setFileName( sheetName + ".sgrd" );
144
insertTab(-1, sheet, dlg.sheetTitle() );
145
mSheetList.append( sheet );
146
setCurrentIndex(indexOf( sheet ));
147
connect( sheet, SIGNAL( titleChanged( QWidget* ) ),
148
SLOT( updateSheetTitle( QWidget* )));
151
void Workspace::contextMenu (int index, const QPoint &point) {
155
// QAction *new_worksheet = pm.addAction( Toplevel->actionCollection()->action("new_worksheet") );
157
// QAction *action = pm.exec( point );
161
void Workspace::updateSheetTitle( QWidget* wdg )
164
setTabText( indexOf(wdg), static_cast<WorkSheet*>( wdg )->translatedTitle() );
167
bool Workspace::saveOnQuit()
169
for(int i = 0; i < mSheetList.size(); i++) {
170
if ( mSheetList.at(i)->fileName().isEmpty() ) {
171
int res = KMessageBox::warningYesNoCancel( this,
172
i18n( "The tab '%1' contains unsaved data.\n"
173
"Do you want to save the tab?",
174
tabText(indexOf( mSheetList.at(i) )) ), QString(), KStandardGuiItem::save(), KStandardGuiItem::discard() );
175
if ( res == KMessageBox::Yes )
176
saveWorkSheet( mSheetList.at(i) );
177
else if ( res == KMessageBox::Cancel )
178
return false; // abort quit
180
saveWorkSheet(mSheetList.at(i));
185
void Workspace::importWorkSheet()
187
KUrl url = KFileDialog::getOpenUrl( QString(), i18n("*.sgrd|Sensor Files (*.sgrd)"), this, i18n( "Select Tab File to Import" ) );
189
importWorkSheet( url );
192
void Workspace::importWorkSheet( const KUrl &url )
197
/* It's probably not worth the effort to make this really network
198
* transparent. Unless s/o beats me up I use this pseudo transparent
201
KIO::NetAccess::download( url, tmpFile, this );
203
// Import sheet from file.
204
if ( !restoreWorkSheet( tmpFile ) )
207
mSheetList.last()->setFileName( makeNameForNewSheet() + ".sgrd");
209
KIO::NetAccess::removeTempFile( tmpFile );
212
bool Workspace::saveWorkSheet( WorkSheet *sheet )
215
KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
219
KStandardDirs* kstd = KGlobal::dirs();
220
QString fileName = kstd->saveLocation( "data", "ksysguard") + sheet->fileName();
222
if ( !sheet->save( fileName ) ) {
228
void Workspace::exportWorkSheet()
230
exportWorkSheet( (WorkSheet*)currentWidget() );
233
void Workspace::exportWorkSheet( WorkSheet *sheet )
236
KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
242
fileName = KFileDialog::getSaveFileName( QString(tabText(indexOf( currentWidget() ))+ ".sgrd"),
243
QLatin1String("*.sgrd"), this, i18n("Export Tab") );
244
if ( fileName.isEmpty() )
247
} while ( !sheet->exportWorkSheet( fileName ) );
251
void Workspace::removeWorkSheet()
253
WorkSheet *current = (WorkSheet*)currentWidget();
256
saveWorkSheet( current );
258
removeTab(indexOf( current ));
259
mSheetList.removeAll( current );
261
QString msg = i18n( "There are no tabs that could be deleted." );
262
KMessageBox::error( this, msg );
266
void Workspace::removeAllWorkSheets()
269
while ( ( sheet = (WorkSheet*)currentWidget() ) != 0 ) {
270
saveWorkSheet( sheet );
271
removeTab(indexOf( sheet ));
272
mSheetList.removeAll( sheet );
277
void Workspace::removeWorkSheet( const QString &fileName )
279
QString baseName = fileName.right( fileName.length() - fileName.lastIndexOf( '/' ) - 1 );
280
for(int i = 0; i < mSheetList.size(); i++) {
281
WorkSheet *sheet = mSheetList.at(i);
282
if ( sheet->fileName() == baseName ) {
283
removeTab(indexOf( sheet ));
284
mSheetList.removeAt( i );
291
WorkSheet *Workspace::currentWorkSheet()
293
return (WorkSheet*)currentWidget();
295
void Workspace::uploadHotNewWorksheet()
297
WorkSheet *currentWorksheet = currentWorkSheet();
298
if(!currentWorksheet)
301
KMessageBox::information(this, i18n("<qt>To propose the current custom tab as a new System Monitor tab, email <br><a href=\"file:%1\">%2</a><br> to <a href=\"mailto:john.tapsell@kde.org?subject='System Monitor Tab'&attach='file://%2'\">john.tapsell@kde.org</a></qt>", currentWorksheet->fullFileName().section('/',0,-2), currentWorksheet->fullFileName()), i18n("Upload custom System Monitor tab"), QString::null, KMessageBox::AllowLink);
303
void Workspace::getHotNewWorksheet()
305
KNS3::DownloadDialog dialog("ksysguard.knsrc");
306
if( dialog.exec() == QDialog::Rejected )
309
KNS3::Entry::List entries = dialog.installedEntries();
310
foreach(KNS3::Entry entry, entries) {
311
if(!entry.installedFiles().isEmpty()) {
312
QString filename = entry.installedFiles().first();
313
restoreWorkSheet(filename, true);
318
bool Workspace::restoreWorkSheet( const QString &fileName, bool switchToTab)
320
// extract filename without path
321
QString baseName = fileName.right( fileName.length() - fileName.lastIndexOf( '/' ) - 1 );
323
foreach( WorkSheet *sheet, mSheetList ) {
324
if(sheet->fileName() == baseName)
325
return false; //Don't add the same sheet twice
328
WorkSheet *sheet = new WorkSheet( 0 );
329
sheet->setFileName( baseName );
330
if ( !sheet->load( fileName ) ) {
334
mSheetList.append( sheet );
336
connect( sheet, SIGNAL( titleChanged( QWidget* ) ),
337
SLOT( updateSheetTitle( QWidget* )));
339
insertTab(-1, sheet, sheet->translatedTitle() );
341
setCurrentIndex(indexOf(sheet));
343
//Watch the file incase it is deleted
344
mDirWatch.addFile(fileName);
349
void Workspace::cut()
351
WorkSheet *current = currentWorkSheet();
357
void Workspace::copy()
359
WorkSheet *current = currentWorkSheet();
365
void Workspace::paste()
367
WorkSheet *current = currentWorkSheet();
373
void Workspace::configure()
375
WorkSheet *current = currentWorkSheet();
381
void Workspace::applyStyle()
383
WorkSheet *current = currentWorkSheet();
385
current->applyStyle();
388
#include "Workspace.moc"