source: src-qt4/life-preserver/lp-gui/LPMain.cpp @ 109c737

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 109c737 was 109c737, checked in by Ken Moore <ken@…>, 10 months ago

Add a new "Classic Backup" dialog for customizing the home-dir archive and providing status updates while it is running.

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