source: src-qt4/life-preserver/lp-gui/LPMain.cpp @ 23619d0

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 23619d0 was 23619d0, checked in by Kris Moore <kris@…>, 9 months ago

Fix building life-preserver on 10

  • Property mode set to 100644
File size: 28.6 KB
Line 
1#include "LPMain.h"
2#include "ui_LPMain.h"
3#include <unistd.h>
4
5LPMain::LPMain(QWidget *parent) : QMainWindow(parent), ui(new Ui::LPMain){
6  ui->setupUi(this); //load the Qt-designer UI file
7  //Create the basic/advanced view options
8  viewBasic = new QRadioButton(tr("Basic"), ui->menuView);
9        QWidgetAction *WABasic = new QWidgetAction(this); WABasic->setDefaultWidget(viewBasic);
10        ui->menuView->addAction(WABasic);
11  viewAdvanced = new QRadioButton(tr("Advanced"), ui->menuView);
12        QWidgetAction *WAAdv = new QWidgetAction(this); WAAdv->setDefaultWidget(viewAdvanced);
13        ui->menuView->addAction(WAAdv);
14  connect(viewBasic, SIGNAL(toggled(bool)), this, SLOT(viewChanged()) );
15  //Now set the default view type
16  viewBasic->setChecked(true); //will automatically call the "viewChanged" function
17  //Create the filesystem model and tie it to the treewidget
18  fsModel = new QFileSystemModel(this);
19        fsModel->setReadOnly(true);
20        ui->treeView->setModel(fsModel);
21  //Connect the UI to all the functions
22  connect(ui->tool_refresh, SIGNAL(clicked()), this, SLOT(updatePoolList()) );
23  connect(ui->combo_pools, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTabs()) );
24  connect(ui->combo_datasets, SIGNAL(currentIndexChanged(int)), this, SLOT(updateDataset()) );
25  connect(ui->slider_snapshots, SIGNAL(valueChanged(int)), this, SLOT(updateSnapshot()) );
26  connect(ui->push_prevsnap, SIGNAL(clicked()), this, SLOT(prevSnapshot()) );
27  connect(ui->push_nextsnap, SIGNAL(clicked()), this, SLOT(nextSnapshot()) );
28  connect(ui->check_hidden, SIGNAL(stateChanged(int)), this, SLOT(setFileVisibility()) );
29  connect(ui->push_restore, SIGNAL(clicked()), this, SLOT(restoreFiles()) );
30  connect(ui->push_configure, SIGNAL(clicked()), this, SLOT(openConfigGUI()) );
31  //Connect the Menu buttons
32  connect(ui->menuManage_Pool, SIGNAL(triggered(QAction*)), this, SLOT(menuAddPool(QAction*)) );
33  connect(ui->menuUnmanage_Pool, SIGNAL(triggered(QAction*)), this, SLOT(menuRemovePool(QAction*)) );
34  connect(ui->action_SaveKeyToUSB, SIGNAL(triggered()), this, SLOT(menuSaveSSHKey()) );
35  connect(ui->actionClose_Window, SIGNAL(triggered()), this, SLOT(menuCloseWindow()) );
36  connect(ui->menuCompress_Home_Dir, SIGNAL(triggered(QAction*)), this, SLOT(menuCompressHomeDir(QAction*)) );
37  connect(ui->actionExtract_Home_Dir, SIGNAL(triggered()), this, SLOT(menuExtractHomeDir()) );
38  connect(ui->actionAdd_Disk, SIGNAL(triggered()), this, SLOT(menuAddDisk()) );
39  connect(ui->menuRemove_Disk, SIGNAL(triggered(QAction*)), this, SLOT(menuRemoveDisk(QAction*)) );
40  connect(ui->menuSet_Disk_Offline, SIGNAL(triggered(QAction*)), this, SLOT(menuOfflineDisk(QAction*)) );
41  connect(ui->menuSet_Disk_Online, SIGNAL(triggered(QAction*)), this, SLOT(menuOnlineDisk(QAction*)) );
42  connect(ui->action_startScrub, SIGNAL(triggered()), this, SLOT(menuStartScrub()) );
43  connect(ui->action_stopScrub, SIGNAL(triggered()), this, SLOT(menuStopScrub()) );
44  connect(ui->action_newSnapshot, SIGNAL(triggered()), this, SLOT(menuNewSnapshot()) );
45  connect(ui->menuDelete_Snapshot, SIGNAL(triggered(QAction*)), this, SLOT(menuRemoveSnapshot(QAction*)) );
46  //Update the interface
47  QTimer::singleShot(0,this,SLOT(updatePoolList()) );
48 
49  //Make sure the status tab is shown initially
50  ui->tabWidget->setCurrentWidget(ui->tab_status);
51}
52
53LPMain::~LPMain(){
54       
55}
56
57// ==============
58//      PUBLIC SLOTS
59// ==============
60void LPMain::slotSingleInstance(){
61  this->raise();
62  this->show();
63}
64
65// ==============
66//          PRIVATE
67// ==============
68void LPMain::showErrorDialog(QString title, QString message, QString errors){
69  QMessageBox MB(QMessageBox::Warning, title, message, QMessageBox::Ok, this);
70    MB.setDetailedText(errors);
71    MB.exec();
72}
73
74void LPMain::showWaitBox(QString message){
75  if(waitBox == 0){
76    qDebug() << "New Wait Box";
77    waitBox = new QMessageBox(QMessageBox::NoIcon, tr("Please Wait"), message, QMessageBox::NoButton, this);
78    waitBox->setWindowModality(Qt::WindowModal);
79  }else{
80    qDebug() << "Update Wait Box:" << message;
81    waitBox->setText(message);
82  }
83  if(!waitBox->isVisible()){ waitBox->show(); waitBox->raise(); }
84  QCoreApplication::processEvents();
85}
86
87void LPMain::hideWaitBox(){
88  if(waitBox != 0){
89    if(waitBox->isVisible()){ waitBox->hide(); }
90  }
91       
92}
93
94// ==============
95//     PRIVATE SLOTS
96// ==============
97void LPMain::updatePoolList(){
98  //Get the currently selected pool (if there is one)
99  qDebug() << "Update Pool List";
100  QString cPool;
101  if(ui->combo_pools->currentIndex() != -1){ cPool = ui->combo_pools->currentText(); }
102  //Get the list of managed pools
103  QStringList pools = LPBackend::listDatasets();
104  QStringList poolsAvail = LPBackend::listPossibleDatasets();
105  //Now put the lists into the UI
106  ui->combo_pools->clear();
107  if(!pools.isEmpty()){ ui->combo_pools->addItems(pools); }
108  //Now set the currently selected pools
109  if(pools.length() > 0){
110    poolSelected=true;   
111    int index = pools.indexOf(cPool);
112    if(index < 0){ ui->combo_pools->setCurrentIndex(0); }
113    else{ ui->combo_pools->setCurrentIndex(index); }
114  }else{
115    //No managed pools
116    poolSelected=false;
117    ui->combo_pools->addItem("No Managed Pools!");
118    ui->combo_pools->setCurrentIndex(0);
119  }
120  //Now update the add/remove pool menu's
121  ui->menuManage_Pool->clear();
122  for( int i=0; i<poolsAvail.length(); i++){
123    if(pools.contains(poolsAvail[i])){ continue; } //already managed
124    ui->menuManage_Pool->addAction(poolsAvail[i]);
125  }
126  ui->menuManage_Pool->setEnabled( !ui->menuManage_Pool->isEmpty() );
127  ui->menuUnmanage_Pool->clear();
128  for( int i=0; i<pools.length(); i++){
129    ui->menuUnmanage_Pool->addAction(pools[i]);
130  }
131  ui->menuUnmanage_Pool->setEnabled( !ui->menuUnmanage_Pool->isEmpty() );
132  //Now update the user's that are available for home-dir packaging
133  QDir hdir("/usr/home");
134  QStringList users = hdir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
135  ui->menuCompress_Home_Dir->clear();
136  for(int i=0; i<users.length(); i++){
137    ui->menuCompress_Home_Dir->addAction(users[i]);
138  }
139  //Now update the interface appropriately
140  ui->combo_pools->setEnabled(poolSelected);
141  updateTabs();
142}
143
144void LPMain::viewChanged(){
145  ui->menuView->hide();
146  ui->menubar->clear();
147  if(viewBasic->isChecked()){
148    ui->menubar->addMenu(ui->menuFile);
149    ui->menubar->addMenu(ui->menuView);
150    ui->menubar->addMenu(ui->menuClassic_Backups);
151  }else{
152    ui->menubar->addMenu(ui->menuFile);
153    ui->menubar->addMenu(ui->menuView);
154    ui->menubar->addMenu(ui->menuClassic_Backups);
155    ui->menubar->addMenu(ui->menuSnapshots);
156    ui->menubar->addMenu(ui->menuDisks);
157  }
158}
159
160void LPMain::updateTabs(){
161  //qDebug() << "Update Tabs" << poolSelected;
162  viewChanged();
163  ui->tabWidget->setEnabled(poolSelected);
164  ui->menuView->setEnabled(poolSelected);       
165  ui->menuDisks->setEnabled(poolSelected); 
166  ui->menuSnapshots->setEnabled(poolSelected);
167  ui->push_configure->setVisible(poolSelected);
168  ui->action_SaveKeyToUSB->setEnabled(poolSelected);
169  if(poolSelected){
170    showWaitBox(tr("Loading zpool information"));
171    POOLDATA = LPGUtils::loadPoolData(ui->combo_pools->currentText());
172    hideWaitBox();
173    //Now list the status information
174    ui->label_status->setText(POOLDATA.poolStatus);
175    ui->label_numdisks->setText( QString::number(POOLDATA.harddisks.length()) );
176    ui->label_latestsnapshot->setText(POOLDATA.latestSnapshot);
177    if(POOLDATA.finishedStatus.isEmpty()){ ui->label_finishedstat->setVisible(false); }
178    else{
179      ui->label_finishedstat->setText(POOLDATA.finishedStatus);
180      ui->label_finishedstat->setVisible(true);
181    }
182    if(POOLDATA.runningStatus.isEmpty()){ ui->label_runningstat->setVisible(false); }
183    else{
184      ui->label_runningstat->setText(POOLDATA.runningStatus);
185      ui->label_runningstat->setVisible(true);
186    }       
187    if(POOLDATA.errorStatus.isEmpty()){ ui->label_errorstat->setVisible(false); }
188    else{
189      ui->label_errorstat->setText(POOLDATA.errorStatus);
190      ui->label_errorstat->setVisible(true);
191    }       
192    //Now list the data restore options
193    QString cds = ui->combo_datasets->currentText();
194    ui->combo_datasets->clear();
195    QStringList dslist = POOLDATA.subsets();
196    ui->combo_datasets->addItems(dslist);
197    int dsin = dslist.indexOf(cds);
198    if(dsin >= 0){ ui->combo_datasets->setCurrentIndex(dsin); }
199    else if( !dslist.isEmpty() ){ ui->combo_datasets->setCurrentIndex(0); }
200    else{ ui->combo_datasets->addItem(tr("No datasets available")); }
201    //NOTE: this automatically calls the "updateDataset()" function in a new thread
202   
203    //Now update the snapshot removal menu list
204    QStringList snaps = LPBackend::listLPSnapshots(ui->combo_pools->currentText());
205    ui->menuDelete_Snapshot->clear();
206    for(int i=0; i<snaps.length(); i++){
207       ui->menuDelete_Snapshot->addAction(snaps[i]);
208    }
209    ui->menuDelete_Snapshot->setEnabled( !ui->menuDelete_Snapshot->isEmpty() );
210    //Now update the disk menu items
211    ui->menuRemove_Disk->clear();
212    ui->menuSet_Disk_Offline->clear();
213    ui->menuSet_Disk_Online->clear();
214    for(int i=0; i<POOLDATA.harddisks.length(); i++){
215      ui->menuRemove_Disk->addAction(POOLDATA.harddisks[i]);
216      if(POOLDATA.harddiskStatus[i] == "OFFLINE"){
217        ui->menuSet_Disk_Online->addAction(POOLDATA.harddisks[i]);
218      }else{
219        ui->menuSet_Disk_Offline->addAction(POOLDATA.harddisks[i]);     
220      }
221    }
222    ui->menuRemove_Disk->setEnabled(!ui->menuRemove_Disk->isEmpty());
223    ui->menuSet_Disk_Offline->setEnabled(!ui->menuSet_Disk_Offline->isEmpty());
224    ui->menuSet_Disk_Online->setEnabled(!ui->menuSet_Disk_Online->isEmpty());
225  }else{
226    //No Pool selected
227    ui->label_numdisks->clear();
228    ui->label_latestsnapshot->clear();
229    ui->label_status->clear();
230          ui->label_errorstat->setVisible(false);
231          ui->label_runningstat->setVisible(false);
232          ui->label_finishedstat->setVisible(false);
233  }
234
235}
236
237void LPMain::updateDataset(){
238  //Update the snapshots for the currently selected dataset
239  QString cds = ui->combo_datasets->currentText();
240  if(POOLDATA.subsets().indexOf(cds) >= 0){
241    QStringList snaps = POOLDATA.snapshots(cds);
242      qDebug() << "Update Dataset";
243      ui->slider_snapshots->setEnabled(true);
244      ui->slider_snapshots->setMinimum(0);
245      int max = snaps.length() -1;
246      if(max < 0){ max = 0; ui->slider_snapshots->setEnabled(false); }
247      ui->slider_snapshots->setMaximum(max);
248      ui->slider_snapshots->setValue(max); //most recent snapshot
249      updateSnapshot();
250  }else{
251    ui->slider_snapshots->setEnabled(false);
252    ui->label_snapshot->clear();
253    ui->push_nextsnap->setEnabled(false);
254    ui->push_prevsnap->setEnabled(false);
255  }
256       
257}
258
259void LPMain::updateSnapshot(){
260  int sval = ui->slider_snapshots->value();
261  QStringList snaps = POOLDATA.snapshots(ui->combo_datasets->currentText());
262  //qDebug() << "Update Snapshot";
263  //Update the previous/next buttons
264  if(sval == ui->slider_snapshots->minimum() ){ ui->push_prevsnap->setEnabled(false); }
265  else{ ui->push_prevsnap->setEnabled(true); }
266  if(sval == ui->slider_snapshots->maximum() ){ ui->push_nextsnap->setEnabled(false); }
267  else{ ui->push_nextsnap->setEnabled(true); }
268  //Now update the snapshot viewer
269  if(snaps.isEmpty()){ ui->label_snapshot->clear(); ui->slider_snapshots->setEnabled(false); }
270  else{
271    QString snap = snaps.at(sval);
272    QString path = ui->combo_datasets->currentText() + "/.zfs/snapshot/"+snap;
273    //qDebug() << "Snapshot path:" << path;
274    ui->label_snapshot->setText(snap);
275    //Now update the snapshot view
276    ui->treeView->setRootIndex( fsModel->setRootPath(path) );
277   
278  }
279}
280
281void LPMain::nextSnapshot(){
282  ui->slider_snapshots->setValue( ui->slider_snapshots->value()+1 );
283}
284
285void LPMain::prevSnapshot(){
286  ui->slider_snapshots->setValue( ui->slider_snapshots->value()-1 );
287}
288
289void LPMain::setFileVisibility(){
290  if(ui->check_hidden->isChecked()){
291    fsModel->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden );
292  }else{
293    fsModel->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot );
294  }
295}
296
297void LPMain::restoreFiles(){
298  QString filePath = fsModel->filePath( ui->treeView->currentIndex() );
299  qDebug() << " Restore file(s):" << filePath;
300  QString destDir = filePath.remove("/.zfs/snapshot/"+ui->label_snapshot->text());
301        destDir.chop( filePath.section("/",-1).size()+1 ); //get rid of the filename at the end
302        while(!QFile::exists(destDir)){ destDir.chop( destDir.section("/",-1).size() +1); }
303  QString newFilePath = destDir+"/"+LPGUtils::generateReversionFileName(filePath, destDir);
304  //qDebug() << "Destination:" << newFilePath;
305  //Perform the reversion(s)
306  QStringList errors;
307  if(QFileInfo(filePath).isDir()){
308    //Is a directory
309    showWaitBox( QString(tr("Restoring Directory: %1")).arg(newFilePath) );
310    errors = LPGUtils::revertDir(filePath, newFilePath);
311    hideWaitBox();
312    if(!errors.isEmpty()){
313      qDebug() << "Failed Reversions:" << errors;
314      errors.prepend(tr("File destination(s) that could not be restored:")+"\n");
315      showErrorDialog(tr("Reversion Error"), tr("Some files could not be restored from the snapshot."), errors.join("\n") );
316    }else{
317      qDebug() << "Reversion successful";           
318      QMessageBox::information(this,tr("Restore Successful"),QString(tr("The following directory was succesfully restored: %1")).arg(newFilePath) );
319    }
320  }else{
321    //Just a single file
322    showWaitBox( QString(tr("Restoring file: %1")).arg(newFilePath) );
323    bool ok = LPGUtils::revertFile(filePath, newFilePath);
324    hideWaitBox();
325    if( !ok ){
326      qDebug() << "Failed Reversion:" << newFilePath;
327      errors << QString(tr("Snapshot file: %1")).arg(filePath);
328      errors << QString(tr("Destination: %1")).arg(newFilePath);
329      errors << tr("Please check that the destination directory exists and is writable");
330      showErrorDialog(tr("Reversion Error"), tr("The file could not be restored from the snapshot."), errors.join("\n") );
331    }else{
332      qDebug() << "Reversion successful";
333      QMessageBox::information(this,tr("Restore Successful"),QString(tr("The following file was succesfully restored: %1")).arg(newFilePath) );
334    }
335  }       
336       
337}
338
339void LPMain::openConfigGUI(){
340  qDebug() << "Open Configuration UI";
341  QString ds = ui->combo_pools->currentText();
342  if(ds.isEmpty()){ return; }
343  LPConfig CFG(this);
344  CFG.loadDataset(ds, LPBackend::listReplicationTargets().contains(ds));
345  CFG.exec();
346  //Now check for return values and update appropriately
347  bool change = false;
348  if(CFG.localChanged){
349    ui->statusbar->showMessage(QString(tr("Configuring dataset: %1")).arg(ds),0);
350    LPBackend::setupDataset(ds, CFG.localSchedule, CFG.localSnapshots);
351    ui->statusbar->clearMessage();
352    change = true;
353  }
354  if(CFG.remoteChanged){
355    change = true;
356    if(CFG.isReplicated){
357      ui->statusbar->showMessage(QString(tr("Configuring replication: %1")).arg(ds),0);
358      LPBackend::setupReplication(ds, CFG.remoteHost, CFG.remoteUser, CFG.remotePort, CFG.remoteDataset, CFG.remoteFreq);
359      QMessageBox::information(this,tr("Reminder"),tr("Don't forget to save your SSH key to a USB stick so that you can restore your system from the remote host later!!"));
360    }else{
361      ui->statusbar->showMessage(QString(tr("Removing replication: %1")).arg(ds),0);
362      LPBackend::removeReplication(ds);
363    }
364    ui->statusbar->clearMessage();
365  }
366  //Now update the UI if appropriate
367  if(change){
368    updateTabs();
369  }     
370}
371
372// -----------------------------------------------
373//   MENU SLOTS
374// -----------------------------------------------
375// ==== File Menu ====
376void LPMain::menuAddPool(QAction *act){
377  QString dataset = act->text();
378  qDebug() << "Start Wizard for new managing pool:" << dataset;
379  LPWizard wiz(this);
380  wiz.setDataset(dataset);
381  wiz.exec();
382  //See if the wizard was cancelled or not
383  if(!wiz.cancelled){
384    ui->statusbar->showMessage(QString(tr("Enabling dataset management: %1")).arg(dataset),0);
385    //run the proper commands to get the dataset enabled
386    if( LPBackend::setupDataset(dataset, wiz.localTime, wiz.totalSnapshots) ){
387      if(wiz.enableReplication){
388         LPBackend::setupReplication(dataset, wiz.remoteHost, wiz.remoteUser, wiz.remotePort, wiz.remoteDataset, wiz.remoteTime);     
389         QMessageBox::information(this,tr("Reminder"),tr("Don't forget to save your SSH key to a USB stick so that you can restore your system from the remote host later!!"));
390      }
391    }
392    ui->statusbar->clearMessage();
393    //Now update the list of pools
394    updatePoolList();
395  }     
396}
397
398void LPMain::menuRemovePool(QAction *act){
399  QString ds = act->text();
400  qDebug() << "Remove Pool:" << ds;
401  if(!ds.isEmpty()){
402    //Verify the removal of the dataset
403    if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify Dataset Backup Removal"),tr("Are you sure that you wish to cancel automated snapshots and/or replication of the following dataset?")+"\n\n"+ds,QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){           
404      //verify the removal of all the snapshots for this dataset
405      QStringList snaps = LPBackend::listLPSnapshots(ds);
406      if(!snaps.isEmpty()){
407        if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify Snapshot Deletion"),tr("Do you wish to remove the local snapshots for this dataset?")+"\n"+tr("WARNING: This is a permanant change that cannot be reversed"),QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
408          //Remove all the snapshots
409          ui->statusbar->showMessage(QString(tr("%1: Removing snapshots")).arg(ds),0);
410          showWaitBox(tr("Removing snapshots"));
411          for(int i=0; i<snaps.length(); i++){
412            LPBackend::removeSnapshot(ds,snaps[i]);
413          }
414          ui->statusbar->clearMessage();
415        }
416      }
417      //Remove the dataset from life-preserver management
418      if(LPBackend::listReplicationTargets().contains(ds)){ 
419        ui->statusbar->showMessage(QString(tr("%1: Disabling Replication")).arg(ds),0);
420        showWaitBox(tr("Disabling Replication"));
421        LPBackend::removeReplication(ds); 
422        ui->statusbar->clearMessage();     
423      }
424      ui->statusbar->showMessage(QString(tr("%1: Disabling Life-Preserver Management")).arg(ds),0);
425      showWaitBox(tr("Removing Life Preserver Schedules"));
426      LPBackend::removeDataset(ds);
427      ui->statusbar->clearMessage();
428      updatePoolList();
429      hideWaitBox();
430    }
431  } //end check for empty ds
432
433}
434
435void LPMain::menuSaveSSHKey(){
436  QString ds = ui->combo_pools->currentText(); 
437  qDebug() << "Save SSH Key:" << ds;
438  if(ds.isEmpty()){ return; }
439  //Get the local hostname
440  char host[1023] = "\0";
441  gethostname(host,1023);
442  QString localHost = QString(host).simplified();
443  qDebug() << " - hostname:" << localHost;
444  //Scan for mounted USB devices
445  QStringList devs = LPBackend::findValidUSBDevices();
446  qDebug() << " - devs:" << devs;
447  if(devs.isEmpty()){
448    QMessageBox::warning(this,tr("No Valid USB Devices"), tr("No valid USB devices could be found. Please mount a FAT32 formatted USB stick and try again."));
449    return;
450  }
451  //Ask the user which one to save the file to
452  bool ok;
453  QString dev = QInputDialog::getItem(this, tr("Select USB Device"), tr("Available USB Devices:"), devs,0,false,&ok);   
454  if(!ok or dev.isEmpty()){ return; } //cancelled
455  QString devPath = dev.section("(",0,0).simplified();
456  //Now copy the file over
457  ok = LPBackend::copySSHKey(devPath, localHost);
458  if(ok){
459    QMessageBox::information(this,tr("Success"), tr("The public SSH key file was successfully copied onto the USB device."));
460  }else{
461    QMessageBox::information(this,tr("Failure"), tr("The public SSH key file could not be copied onto the USB device."));
462  }
463}
464
465void LPMain::menuCloseWindow(){
466  this->close();
467}
468
469// ==== Classic Backups Menu ====
470void LPMain::menuCompressHomeDir(QAction* act){
471  QString user = act->text();
472  qDebug() << "Compress Home Dir:" << user;
473  //Prompt for the package name
474  QString pkgName = user+"-"+QDateTime::currentDateTime().toString("yyyyMMdd-hhmm");
475  bool ok;
476  pkgName = QInputDialog::getText(this, tr("Package Name"), tr("Name of the package to create:"), QLineEdit::Normal, pkgName, &ok);
477  if(!ok || pkgName.isEmpty() ){ return; } //cancelled
478  //Now create the package
479  showWaitBox(tr("Packaging home directory"));
480  QString pkgPath = LPGUtils::packageHomeDir(user, pkgName);
481  hideWaitBox();
482  //Now inform the user of the result
483  if(pkgPath.isEmpty()){
484    qDebug() << "No Package created";
485    QMessageBox::warning(this,tr("Package Failure"), tr("The home directory package could not be created."));
486  }else{
487    qDebug() << "Package created at:" << pkgPath;
488    QMessageBox::information(this,tr("Package Success"), tr("The home directory package was successfully created.")+"\n\n"+pkgPath);
489  }       
490}
491
492void LPMain::menuExtractHomeDir(){
493  qDebug() << "Extract Home Dir";
494  //Get the file path from the user
495  QString filePath = QFileDialog::getOpenFileName(this,tr("Find Home Dir Package"), "/usr/home", tr("Home Dir Package (*.home.tar.gz)") );
496  if(filePath.isEmpty() || !QFile::exists(filePath)){ return; } //cancelled
497  //Now check if the user in the package is also on the system
498  QString username;
499  bool ok = LPGUtils::checkPackageUserPath(filePath, &username);
500  if(!ok){
501    QMessageBox::warning(this,tr("User Missing"),QString(tr("The user (%1) does not exist on this system. Please create this user first and then try again.")).arg(username) );
502    return;
503  }
504  //Now extract the package
505  showWaitBox(tr("Extracting Home Directory"));
506  ok = LPGUtils::extractHomeDirPackage(filePath);
507  hideWaitBox();
508  //Now report the results
509  if(ok){
510    QMessageBox::information(this,tr("Package Extracted"), QString(tr("The package was successfully extracted within %1")).arg("/usr/home/"+username) );
511  }else{
512    QMessageBox::warning(this, tr("Package Failure"), QString(tr("The package could not be extracted within %1")).arg("/usr/home/"+username) );
513  }
514 
515}
516
517// ==== Disks Menu ====
518void LPMain::menuAddDisk(){
519  QString pool = ui->combo_pools->currentText();
520  //Get the available disks and remove the current disks
521  QStringList adisks = LPGUtils::listAvailableHardDisks();
522  for(int i=0; i<POOLDATA.harddisks.length(); i++){
523    adisks.removeAll( POOLDATA.harddisks[i].section("s",0,0,QString::SectionSkipEmpty) );
524  }
525  if(adisks.isEmpty()){
526    QMessageBox::information(this,tr("Attach New Disk"), tr("No available disks could be found"));
527    return;
528  }
529  //Find a disk that can be added to the system
530  bool ok=false;
531  QString disk = QInputDialog::getItem(this, tr("Attach New Disk"),tr("Detected Disks:"), adisks,0,false, &ok);
532  if( !ok || disk.isEmpty() ){ return; }
533  qDebug() << "Add Disk:" << disk << pool;
534  showWaitBox(tr("Attaching disk"));
535  ok = LPBackend::attachDisk(pool, disk);
536  hideWaitBox();
537  if(ok){
538    QMessageBox::information(this,tr("Disk Attached"),QString(tr("Success: %1 was added to %2")).arg(disk,pool) );
539    QTimer::singleShot(0,this,SLOT(updateTabs()) );
540  }else{
541    QMessageBox::warning(this,tr("Disk Attach Error"),QString(tr("Failure: %1 could not be attached to %2.")).arg(disk,pool) );
542  }     
543}
544
545void LPMain::menuRemoveDisk(QAction *act){
546  QString disk = act->text();
547  QString pool = ui->combo_pools->currentText();
548  //Verify action
549  if(QMessageBox::Yes != QMessageBox::question(this,tr("Verify Disk Removal"),QString(tr("Are you sure that you want to remove %1 from %2?")).arg(disk,pool) + "\n\n" + tr("CAUTION: This disk can only be re-attached later as a brand new disk"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
550    return; //cancelled
551  }
552  qDebug() << "Remove Disk:" << disk << pool;
553  showWaitBox(tr("Detaching disk"));
554  bool ok = LPBackend::detachDisk(pool, disk);
555  hideWaitBox();
556  if(ok){
557    QMessageBox::information(this,tr("Disk Removal Success"),QString(tr("Success: %1 was removed from %2")).arg(disk, pool) );
558    QTimer::singleShot(0,this,SLOT(updateTabs()) );
559  }else{
560    QMessageBox::warning(this,tr("Disk Removal Failure"),QString(tr("Failure: %1 could not be removed from %2 at this time.")).arg(disk, pool) );
561  }
562}
563
564void LPMain::menuOfflineDisk(QAction *act){
565  QString disk = act->text();
566  QString pool = ui->combo_pools->currentText();
567  //Verify action
568  if(QMessageBox::Yes != QMessageBox::question(this,tr("Verify Disk Offline"),QString(tr("Are you sure you wish to set %1 offline?")).arg(disk), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
569    return; //cancelled
570  }
571  qDebug() << "Offline Disk:" << disk << pool;
572  showWaitBox(tr("Setting disk offline"));
573  bool ok = LPBackend::setDiskOffline(pool, disk);
574  hideWaitBox();
575  if(ok){
576    QMessageBox::information(this,tr("Disk Offline Success"),QString(tr("Success: %1 has been taken offline.")).arg(disk) );
577    QTimer::singleShot(0,this,SLOT(updateTabs()) );
578  }else{
579    QMessageBox::warning(this,tr("Disk Offline Failure"),QString(tr("Failure: %1 could not be taken offline at this time.")).arg(disk) );
580  }
581}
582
583void LPMain::menuOnlineDisk(QAction *act){
584  QString disk = act->text();
585  QString pool = ui->combo_pools->currentText();
586  //Verify action
587  if(QMessageBox::Yes != QMessageBox::question(this,tr("Verify Disk Online"),QString(tr("Are you sure you wish to set %1 online?")).arg(disk), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
588    return; //cancelled
589  }
590  qDebug() << "Online Disk:" << disk << pool;
591  showWaitBox(tr("Setting disk online"));
592  bool ok = LPBackend::setDiskOnline(pool, disk);
593  hideWaitBox();
594  if(ok){
595    QMessageBox::information(this,tr("Disk Online Success"),QString(tr("Success: %1 has been set online.")).arg(disk) );
596    QTimer::singleShot(0,this,SLOT(updateTabs()) );
597  }else{
598    QMessageBox::warning(this,tr("Disk Online Failure"),QString(tr("Failure: %1 could not be set online at this time.")).arg(disk) );
599  }
600}
601
602void LPMain::menuStartScrub(){
603  QString pool = ui->combo_pools->currentText();
604  //Verify starting a scrub
605  if( QMessageBox::Yes != QMessageBox::question(this,tr("Verify Scrub"),QString(tr("Are you sure you want to start a scrub on %1?")).arg(pool) + "\n"+tr("NOTE: This may take quite a while to complete"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
606    return; //cancelled   
607  }
608  qDebug() << "Start Scrub:" << pool;
609  QString cmd = "zpool scrub "+pool;
610  showWaitBox(tr("Trying to start a scrub"));
611  int ret = system(cmd.toUtf8());
612  hideWaitBox();
613  if(ret == 0){
614    //Now let te user know that one has been triggered
615    QMessageBox::information(this,tr("Scrub Started"),QString(tr("A scrub has just been started on %1")).arg(pool));
616    QTimer::singleShot(0,this,SLOT(updateTabs()) );
617  }else{
618    QMessageBox::warning(this,tr("Scrub Not Started"), QString(tr("A scrub on %1 could not be started at this time.")).arg(pool) + "\n"+tr("Please wait until any current resilvering or scrubs are finished before trying again.") );
619  }
620}
621
622void LPMain::menuStopScrub(){
623  QString pool = ui->combo_pools->currentText();
624  //Verify stopping a scrub
625  if( QMessageBox::Yes != QMessageBox::question(this,tr("Verify Scrub"),QString(tr("Are you sure you want to stop the scrub on %1?")).arg(pool), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
626    return; //cancelled   
627  }
628  qDebug() << "Stop Scrub:" << pool;
629  QString cmd = "zpool scrub -s "+pool;
630  showWaitBox(tr("Trying to stop scrub"));
631  int ret = system(cmd.toUtf8());
632  hideWaitBox();
633  if(ret == 0){
634    //Now let te user know that one has been triggered
635    QMessageBox::information(this,tr("Scrub Stopped"),QString(tr("The scrub on %1 has been stopped.")).arg(pool));
636    QTimer::singleShot(0,this,SLOT(updateTabs()) );
637  }else{
638    QMessageBox::warning(this,tr("Scrub Not Running"), QString(tr("There was no scrub running on %1.")).arg(pool) );
639  }     
640}
641
642// ==== Snapshots Menu ====
643void LPMain::menuNewSnapshot(){
644  qDebug() << "New Snapshot";
645  QString ds = ui->combo_pools->currentText();
646  if(ds.isEmpty()){return; }
647  //Get the new snapshot name from the user
648  bool ok;
649  QString name = QInputDialog::getText(this,tr("New Snapshot Name"), tr("Snapshot Name:"), QLineEdit::Normal, tr("Name"), &ok, 0, Qt::ImhUppercaseOnly | Qt::ImhLowercaseOnly | Qt::ImhDigitsOnly );
650  if(!ok || name.isEmpty()){ return; } //cancelled
651  qDebug() << "Creating a new snapshot:" << ds << name;
652  //Now create the new snapshot
653  LPBackend::newSnapshot(ds,name);
654  QMessageBox::information(this,tr("Snapshot Pending"), tr("The new snapshot creation has been added to the queue"));
655  updateTabs();
656}
657
658void LPMain::menuRemoveSnapshot(QAction *act){
659  QString snapshot = act->text();
660  QString pool = ui->combo_pools->currentText();
661  qDebug() << "Remove Snapshot:" << snapshot;
662  //verify snapshot removal
663  if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify Snapshot Deletion"),QString(tr("Do you wish to delete this snapshot? %1")).arg(pool+"/"+snapshot)+"\n"+tr("WARNING: This is a permanant change that cannot be reversed"),QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
664    bool ok = LPBackend::removeSnapshot(ui->combo_pools->currentText(), snapshot);
665    if(ok){
666      QMessageBox::information(this,tr("Snapshot Removed"),tr("The snapshot was successfully deleted"));
667    }else{
668      QMessageBox::information(this,tr("Snapshot Removal Failure"),tr("The snapshot removal experienced an error and it not be completed at this time."));
669    }
670    updateTabs();
671  }
672}
Note: See TracBrowser for help on using the repository browser.