source: src-qt4/life-preserver/lp-gui/LPMain.cpp @ 95a74cd

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 95a74cd was 95a74cd, checked in by Ken Moore <ken@…>, 11 months ago

Make sure that the waitBox pointer is initialized to 0 in lp-gui

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