~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/Workspace.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 1999 - 2002 Chris Schlaeger <cs@kde.org>
 
5
    Copyright (c) 2006 John Tapsell <tapsell@kde.org>
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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.
 
20
 
 
21
*/
 
22
 
 
23
#include <QLineEdit>
 
24
#include <QSpinBox>
 
25
 
 
26
#include <kfiledialog.h>
 
27
#include <kio/netaccess.h>
 
28
#include <klocale.h>
 
29
#include <kmessagebox.h>
 
30
#include <kstandarddirs.h>
 
31
#include <kacceleratormanager.h>
 
32
#include <kactioncollection.h>
 
33
#include <kmenu.h>
 
34
#include <knewstuff3/downloaddialog.h>
 
35
 
 
36
#include "WorkSheet.h"
 
37
#include "WorkSheetSettings.h"
 
38
 
 
39
#include "Workspace.h"
 
40
#include "ksysguard.h"
 
41
 
 
42
Workspace::Workspace( QWidget* parent)
 
43
  : KTabWidget( parent )
 
44
{
 
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 &)));
 
49
}
 
50
 
 
51
Workspace::~Workspace()
 
52
{
 
53
}
 
54
 
 
55
void Workspace::saveProperties( KConfigGroup& cfg )
 
56
{
 
57
  QStringList list;
 
58
  for(int i =0; i< mSheetList.size(); i++)
 
59
    if ( !mSheetList.at(i)->fileName().isEmpty() )
 
60
      list.append( mSheetList.at(i)->fileName() );
 
61
 
 
62
  cfg.writePathEntry( "SelectedSheets", list );
 
63
  cfg.writeEntry( "currentSheet", currentIndex() );
 
64
}
 
65
 
 
66
void Workspace::readProperties( const KConfigGroup& cfg )
 
67
{
 
68
  QStringList selectedSheets = cfg.readPathEntry( "SelectedSheets", QStringList() );
 
69
 
 
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");
 
81
  }
 
82
 
 
83
  int oldSystemLoad = selectedSheets.indexOf("SystemLoad.sgrd");
 
84
  if(oldSystemLoad != -1) {
 
85
    selectedSheets.replace(oldSystemLoad, "SystemLoad2.sgrd");
 
86
  }
 
87
 
 
88
  KStandardDirs* kstd = KGlobal::dirs();
 
89
  QString filename;
 
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);
 
94
    }
 
95
  }
 
96
 
 
97
  int idx = cfg.readEntry( "currentSheet", 0 );
 
98
  if (idx < 0 || idx > count() - 1) {
 
99
    idx = 0;
 
100
  }
 
101
  setCurrentIndex(idx);
 
102
}
 
103
 
 
104
QString Workspace::makeNameForNewSheet() const
 
105
{
 
106
  /* Find a name of the form "Sheet %d" that is not yet used by any
 
107
   * of the existing worksheets. */
 
108
  int i = 1;
 
109
  bool found = false;
 
110
  QString sheetName;
 
111
  KStandardDirs* kstd = KGlobal::dirs();
 
112
  do {
 
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());
 
116
 
 
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())
 
120
        found = true;
 
121
 
 
122
  } while ( found );
 
123
 
 
124
  return sheetName;
 
125
}
 
126
 
 
127
void Workspace::refreshActiveWorksheet()
 
128
{
 
129
    WorkSheet* currentSheet = mSheetList.at(currentIndex());
 
130
    currentSheet->refreshSheet();
 
131
}
 
132
void Workspace::newWorkSheet()
 
133
{
 
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();
 
137
 
 
138
  WorkSheetSettings dlg( this, false /*not locked.  New custom sheets aren't locked*/ );
 
139
  dlg.setSheetTitle( sheetName );
 
140
  if ( dlg.exec() ) {
 
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* )));
 
149
  }
 
150
}
 
151
void Workspace::contextMenu (int index, const QPoint &point) {
 
152
  KMenu pm;
 
153
  Q_UNUSED(index);
 
154
  Q_UNUSED(point);
 
155
//  QAction *new_worksheet = pm.addAction( Toplevel->actionCollection()->action("new_worksheet") );
 
156
 
 
157
 // QAction *action = pm.exec( point );
 
158
 
 
159
 
 
160
}
 
161
void Workspace::updateSheetTitle( QWidget* wdg )
 
162
{
 
163
  if ( wdg )
 
164
    setTabText( indexOf(wdg), static_cast<WorkSheet*>( wdg )->translatedTitle() );
 
165
}
 
166
 
 
167
bool Workspace::saveOnQuit()
 
168
{
 
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
 
179
      } else
 
180
        saveWorkSheet(mSheetList.at(i));
 
181
  }
 
182
  return true;
 
183
}
 
184
 
 
185
void Workspace::importWorkSheet()
 
186
{
 
187
  KUrl url = KFileDialog::getOpenUrl( QString(), i18n("*.sgrd|Sensor Files (*.sgrd)"), this, i18n( "Select Tab File to Import" ) );
 
188
 
 
189
  importWorkSheet( url );
 
190
}
 
191
 
 
192
void Workspace::importWorkSheet( const KUrl &url )
 
193
{
 
194
  if ( url.isEmpty() )
 
195
    return;
 
196
 
 
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
 
199
   * code. */
 
200
  QString tmpFile;
 
201
  KIO::NetAccess::download( url, tmpFile, this );
 
202
 
 
203
  // Import sheet from file.
 
204
  if ( !restoreWorkSheet( tmpFile ) )
 
205
    return;
 
206
 
 
207
  mSheetList.last()->setFileName( makeNameForNewSheet() + ".sgrd");
 
208
 
 
209
  KIO::NetAccess::removeTempFile( tmpFile );
 
210
}
 
211
 
 
212
bool Workspace::saveWorkSheet( WorkSheet *sheet )
 
213
{
 
214
  if ( !sheet ) {
 
215
    KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
 
216
    return false;
 
217
  }
 
218
 
 
219
  KStandardDirs* kstd = KGlobal::dirs();
 
220
  QString fileName = kstd->saveLocation( "data", "ksysguard") + sheet->fileName();
 
221
 
 
222
  if ( !sheet->save( fileName ) ) {
 
223
    return false;
 
224
  }
 
225
  return true;
 
226
}
 
227
 
 
228
void Workspace::exportWorkSheet()
 
229
{
 
230
  exportWorkSheet( (WorkSheet*)currentWidget() );
 
231
}
 
232
 
 
233
void Workspace::exportWorkSheet( WorkSheet *sheet )
 
234
{
 
235
  if ( !sheet ) {
 
236
    KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
 
237
    return;
 
238
  }
 
239
 
 
240
  QString fileName;
 
241
  do {
 
242
    fileName = KFileDialog::getSaveFileName( QString(tabText(indexOf( currentWidget() ))+ ".sgrd"),
 
243
                                    QLatin1String("*.sgrd"), this, i18n("Export Tab") );
 
244
    if ( fileName.isEmpty() )
 
245
      return;
 
246
 
 
247
  } while ( !sheet->exportWorkSheet( fileName ) );
 
248
 
 
249
}
 
250
 
 
251
void Workspace::removeWorkSheet()
 
252
{
 
253
  WorkSheet *current = (WorkSheet*)currentWidget();
 
254
 
 
255
  if ( current ) {
 
256
    saveWorkSheet( current );
 
257
 
 
258
    removeTab(indexOf( current ));
 
259
    mSheetList.removeAll( current );
 
260
  } else {
 
261
    QString msg = i18n( "There are no tabs that could be deleted." );
 
262
    KMessageBox::error( this, msg );
 
263
  }
 
264
}
 
265
 
 
266
void Workspace::removeAllWorkSheets()
 
267
{
 
268
  WorkSheet *sheet;
 
269
  while ( ( sheet = (WorkSheet*)currentWidget() ) != 0 ) {
 
270
    saveWorkSheet( sheet );
 
271
    removeTab(indexOf( sheet ));
 
272
    mSheetList.removeAll( sheet );
 
273
    delete sheet;
 
274
  }
 
275
}
 
276
 
 
277
void Workspace::removeWorkSheet( const QString &fileName )
 
278
{
 
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 );
 
285
      delete sheet;
 
286
      return;
 
287
    }
 
288
  }
 
289
}
 
290
 
 
291
WorkSheet *Workspace::currentWorkSheet()
 
292
{
 
293
    return (WorkSheet*)currentWidget();
 
294
}
 
295
void Workspace::uploadHotNewWorksheet()
 
296
{
 
297
    WorkSheet *currentWorksheet = currentWorkSheet();
 
298
    if(!currentWorksheet)
 
299
        return;
 
300
 
 
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);
 
302
}
 
303
void Workspace::getHotNewWorksheet()
 
304
{
 
305
    KNS3::DownloadDialog dialog("ksysguard.knsrc");
 
306
    if( dialog.exec() == QDialog::Rejected )
 
307
        return;
 
308
 
 
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);
 
314
        }
 
315
    }
 
316
}
 
317
 
 
318
bool Workspace::restoreWorkSheet( const QString &fileName, bool switchToTab)
 
319
{
 
320
  // extract filename without path
 
321
  QString baseName = fileName.right( fileName.length() - fileName.lastIndexOf( '/' ) - 1 );
 
322
 
 
323
  foreach( WorkSheet *sheet, mSheetList ) {
 
324
          if(sheet->fileName() == baseName)
 
325
                  return false; //Don't add the same sheet twice
 
326
  }
 
327
 
 
328
  WorkSheet *sheet = new WorkSheet( 0 );
 
329
  sheet->setFileName( baseName );
 
330
  if ( !sheet->load( fileName ) ) {
 
331
    delete sheet;
 
332
    return false;
 
333
  }
 
334
  mSheetList.append( sheet );
 
335
 
 
336
  connect( sheet, SIGNAL( titleChanged( QWidget* ) ),
 
337
    SLOT( updateSheetTitle( QWidget* )));
 
338
 
 
339
  insertTab(-1, sheet, sheet->translatedTitle() );
 
340
  if(switchToTab)
 
341
   setCurrentIndex(indexOf(sheet));
 
342
 
 
343
  //Watch the file incase it is deleted
 
344
  mDirWatch.addFile(fileName);
 
345
 
 
346
  return true;
 
347
}
 
348
 
 
349
void Workspace::cut()
 
350
{
 
351
  WorkSheet *current = currentWorkSheet();
 
352
 
 
353
  if ( current )
 
354
    current->cut();
 
355
}
 
356
 
 
357
void Workspace::copy()
 
358
{
 
359
  WorkSheet *current = currentWorkSheet();
 
360
 
 
361
  if ( current )
 
362
    current->copy();
 
363
}
 
364
 
 
365
void Workspace::paste()
 
366
{
 
367
  WorkSheet *current = currentWorkSheet();
 
368
 
 
369
  if ( current )
 
370
    current->paste();
 
371
}
 
372
 
 
373
void Workspace::configure()
 
374
{
 
375
  WorkSheet *current = currentWorkSheet();
 
376
 
 
377
  if ( current )
 
378
      current->settings();
 
379
}
 
380
 
 
381
void Workspace::applyStyle()
 
382
{
 
383
  WorkSheet *current = currentWorkSheet();
 
384
  if ( current )
 
385
    current->applyStyle();
 
386
}
 
387
 
 
388
#include "Workspace.moc"