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

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

Put the new wait box code in a try/catch statement (just in case).

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