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.
27 #include <kfiledialog.h>
28 #include <kio/netaccess.h>
30 #include <kmessagebox.h>
31 #include <kstandarddirs.h>
32 #include <kacceleratormanager.h>
33 #include <kactioncollection.h>
35 #include <knewstuff2/engine.h>
37 #include "WorkSheet.h"
38 #include "WorkSheetSettings.h"
40 #include "Workspace.h"
41 #include "ksysguard.h"
43 Workspace::Workspace( QWidget
* parent
)
44 : KTabWidget( parent
)
46 KAcceleratorManager::setNoAccel(this);
47 this->setWhatsThis( i18n( "This is your work space. It holds your tabs. You need "
48 "to create a new tab (Menu File->New) before "
49 "you can drag sensors here." ) );
50 setSizePolicy(QSizePolicy::Expanding
,QSizePolicy::Expanding
);
53 Workspace::~Workspace()
57 void Workspace::saveProperties( KConfigGroup
& cfg
)
60 for(int i
=0; i
< mSheetList
.size(); i
++)
61 if ( !mSheetList
.at(i
)->fileName().isEmpty() )
62 list
.append( mSheetList
.at(i
)->fileName() );
64 cfg
.writePathEntry( "SelectedSheets", list
);
67 void Workspace::readProperties( const KConfigGroup
& cfg
)
69 QStringList selectedSheets
= cfg
.readPathEntry( "SelectedSheets", QStringList() );
70 kDebug() << "Selected Sheets = " << selectedSheets
;
72 if ( selectedSheets
.isEmpty() ) {
73 /* If SelectedSheets config entry is not there, then it's
74 * probably the first time the user has started KSysGuard. We
75 * then "restore" a special default configuration. */
76 selectedSheets
<< "ProcessTable.sgrd";
77 selectedSheets
<< "SystemLoad.sgrd";
78 } else if(selectedSheets
[0] != "ProcessTable.sgrd") {
79 //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.
80 //Plus this migrates users from the kde3 setup
81 selectedSheets
.removeAll("ProcessTable.sgrd");
82 selectedSheets
.prepend( "ProcessTable.sgrd");
85 KStandardDirs
* kstd
= KGlobal::dirs();
87 for ( QStringList::Iterator it
= selectedSheets
.begin(); it
!= selectedSheets
.end(); ++it
) {
88 filename
= kstd
->findResource( "data", "ksysguard/" + *it
);
89 if(!filename
.isEmpty()) {
90 restoreWorkSheet( filename
, false);
93 //We know that the first tab is the process table
95 emit
currentChanged(0);
98 QString
Workspace::makeNameForNewSheet() const
100 /* Find a name of the form "Sheet %d" that is not yet used by any
101 * of the existing worksheets. */
105 KStandardDirs
* kstd
= KGlobal::dirs();
107 sheetName
= i18n( "Sheet %1" , i
++ );
108 //Check we don't have any existing files with this name
109 found
= !(kstd
->findResource( "data", "ksysguard/" + sheetName
+ ".sgrd").isEmpty());
111 //Check if we have any sheets with the same tab name or file name
112 for(int i
= 0; !found
&& i
< mSheetList
.size(); i
++)
113 if ( tabText(indexOf(mSheetList
.at(i
))) == sheetName
|| sheetName
+".sgrd" == mSheetList
.at(i
)->fileName())
121 void Workspace::newWorkSheet()
123 /* Find a name of the form "Sheet %d" that is not yet used by any
124 * of the existing worksheets. */
125 QString sheetName
= makeNameForNewSheet();
127 WorkSheetSettings
dlg( this, false /*not locked. New custom sheets aren't locked*/ );
128 dlg
.setSheetTitle( sheetName
);
130 WorkSheet
* sheet
= new WorkSheet( dlg
.rows(), dlg
.columns(), dlg
.interval(), 0 );
131 sheet
->setTitle( dlg
.sheetTitle() );
132 sheet
->setFileName( sheetName
+ ".sgrd" );
133 insertTab(-1, sheet
, dlg
.sheetTitle() );
134 mSheetList
.append( sheet
);
135 setCurrentIndex(indexOf( sheet
));
136 connect( sheet
, SIGNAL( titleChanged( QWidget
* ) ),
137 SLOT( updateSheetTitle( QWidget
* )));
140 void Workspace::contextMenu (int index
, const QPoint
&point
) {
143 // QAction *new_worksheet = pm.addAction( Toplevel->actionCollection()->action("new_worksheet") );
145 // QAction *action = pm.exec( point );
149 void Workspace::updateSheetTitle( QWidget
* wdg
)
151 kDebug() << "update sheet title";
153 setTabText( indexOf(wdg
), static_cast<WorkSheet
*>( wdg
)->translatedTitle() );
156 bool Workspace::saveOnQuit()
158 kDebug() << "In saveOnQuit()";
159 for(int i
= 0; i
< mSheetList
.size(); i
++) {
160 if ( mSheetList
.at(i
)->fileName().isEmpty() ) {
161 int res
= KMessageBox::warningYesNoCancel( this,
162 i18n( "The tab '%1' contains unsaved data.\n"
163 "Do you want to save the tab?",
164 tabText(indexOf( mSheetList
.at(i
) )) ), QString(), KStandardGuiItem::save(), KStandardGuiItem::discard() );
165 if ( res
== KMessageBox::Yes
)
166 saveWorkSheet( mSheetList
.at(i
) );
167 else if ( res
== KMessageBox::Cancel
)
168 return false; // abort quit
170 saveWorkSheet(mSheetList
.at(i
));
175 void Workspace::importWorkSheet()
177 KUrl url
= KFileDialog::getOpenUrl( QString(), i18n("*.sgrd|Sensor Files (*.sgrd)"), this, i18n( "Select Tab File to Import" ) );
179 importWorkSheet( url
);
182 void Workspace::importWorkSheet( const KUrl
&url
)
187 /* It's probably not worth the effort to make this really network
188 * transparent. Unless s/o beats me up I use this pseudo transparent
191 KIO::NetAccess::download( url
, tmpFile
, this );
193 // Import sheet from file.
194 if ( !restoreWorkSheet( tmpFile
) )
197 mSheetList
.last()->setFileName( makeNameForNewSheet() + ".sgrd");
199 KIO::NetAccess::removeTempFile( tmpFile
);
202 bool Workspace::saveWorkSheet( WorkSheet
*sheet
)
205 KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
209 KStandardDirs
* kstd
= KGlobal::dirs();
210 QString fileName
= kstd
->saveLocation( "data", "ksysguard") + sheet
->fileName();
212 if ( !sheet
->save( fileName
) ) {
218 void Workspace::exportWorkSheet()
220 exportWorkSheet( (WorkSheet
*)currentWidget() );
223 void Workspace::exportWorkSheet( WorkSheet
*sheet
)
226 KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
232 fileName
= KFileDialog::getSaveFileName( tabText(indexOf( currentWidget() ))+ ".sgrd",
233 "*.sgrd", this, i18n("Export Tab") );
234 if ( fileName
.isEmpty() )
237 } while ( !sheet
->exportWorkSheet( fileName
) );
241 void Workspace::removeWorkSheet()
243 WorkSheet
*current
= (WorkSheet
*)currentWidget();
246 saveWorkSheet( current
);
248 removeTab(indexOf( current
));
249 mSheetList
.removeAll( current
);
251 QString msg
= i18n( "There are no tabs that could be deleted." );
252 KMessageBox::error( this, msg
);
256 void Workspace::removeAllWorkSheets()
259 while ( ( sheet
= (WorkSheet
*)currentWidget() ) != 0 ) {
260 saveWorkSheet( sheet
);
261 removeTab(indexOf( sheet
));
262 mSheetList
.removeAll( sheet
);
267 void Workspace::removeWorkSheet( const QString
&fileName
)
269 for(int i
= 0; i
< mSheetList
.size(); i
++) {
270 WorkSheet
*sheet
= mSheetList
.at(i
);
271 if ( sheet
->fileName() == fileName
) {
272 removeTab(indexOf( sheet
));
273 mSheetList
.removeAt( i
);
280 void Workspace::getHotNewWorksheet()
282 kDebug() << "Need to handle get new stuff";
284 KNS::Engine
engine(this);
285 if(engine
.init("ksysguard.knsrc"))
287 KNS::Entry::List entries
= engine
.downloadDialogModal(this);
288 //TODO: inspect entries here
289 //Don't qDeleteAll entry it's already done when engine is deleted otherwise double delete
290 //qDeleteAll(entries);
294 bool Workspace::restoreWorkSheet( const QString
&fileName
, bool switchToTab
)
296 // extract filename without path
297 QString baseName
= fileName
.right( fileName
.length() - fileName
.lastIndexOf( '/' ) - 1 );
299 foreach( WorkSheet
*sheet
, mSheetList
) {
300 if(sheet
->fileName() == baseName
)
301 return false; //Don't add the same sheet twice
304 WorkSheet
*sheet
= new WorkSheet( 0 );
305 sheet
->setFileName( baseName
);
306 if ( !sheet
->load( fileName
) ) {
310 mSheetList
.append( sheet
);
312 connect( sheet
, SIGNAL( titleChanged( QWidget
* ) ),
313 SLOT( updateSheetTitle( QWidget
* )));
315 insertTab(-1, sheet
, sheet
->translatedTitle() );
317 setCurrentIndex(indexOf(sheet
));
322 void Workspace::cut()
324 WorkSheet
*current
= (WorkSheet
*)currentWidget();
330 void Workspace::copy()
332 WorkSheet
*current
= (WorkSheet
*)currentWidget();
338 void Workspace::paste()
340 WorkSheet
*current
= (WorkSheet
*)currentWidget();
346 void Workspace::configure()
348 WorkSheet
*current
= (WorkSheet
*)currentWidget();
356 void Workspace::applyStyle()
358 if ( currentWidget() )
359 ((WorkSheet
*)currentWidget())->applyStyle();
362 #include "Workspace.moc"