source: src-qt4/life-preserver/mainUI.cpp @ bfe4e62

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

Disable the main GUI display buttons whenever a background process is running, with a status message at the bottom of the screen

  • Property mode set to 100644
File size: 18.8 KB
Line 
1#include "mainUI.h"
2#include "ui_mainUI.h"
3
4mainUI::mainUI(QWidget *parent) : QMainWindow(parent), ui(new Ui::mainUI){
5  //Initialize the graphical items
6  ui->setupUi(this);  //load the mainUI.ui file
7  revMenu = new QMenu();
8  brMenu = new QMenu();
9  addMenu = new QMenu();
10  //Setup the menu system
11  ui->tool_revert->setMenu(revMenu);
12  ui->tool_browse->setMenu(brMenu);
13  ui->tool_add->setMenu(addMenu);
14  connect(revMenu,SIGNAL(triggered(QAction*)),this,SLOT(slotRevertToSnapshot(QAction*)) );
15  connect(brMenu,SIGNAL(triggered(QAction*)),this,SLOT(slotBrowseSnapshot(QAction*)) );
16  connect(addMenu, SIGNAL(triggered(QAction*)),this,SLOT(slotAddDataset(QAction*)) );
17  //Setup the Key menu items (static items, never changed)
18  keyMenu = new QMenu();
19    keyMenu->addAction(ui->actionKeyNew); //action from designer
20    keyMenu->addAction(ui->actionKeyCopy); //action from designer
21  ui->tool_keys->setMenu(keyMenu);
22  //Setup the update frequency limiter
23  freqTimer = new QTimer();
24        freqTimer->setSingleShot(true);
25        freqTimer->setInterval(15000);
26        connect(freqTimer, SIGNAL(timeout()), this, SLOT(setupUI()) );
27}
28
29mainUI::~mainUI(){
30       
31}
32
33void mainUI::setupUI(){
34  //Initialize the Hash (make sure it is not run too frequently - causes kernel panics)
35  if(lastUpdate.isNull() || lastUpdate.addSecs(15) < QTime::currentTime() ){
36    enableButtons(false); //disable the buttons temporarily
37    lastUpdate = QTime::currentTime();   
38    qDebug() << "Updating the database";
39    ui->statusbar->showMessage(tr("Updating the database"),0);
40    updateHash();
41    ui->statusbar->clearMessage();
42  }else{
43    freqTimer->start();
44  }
45  //Update the display
46  updateUI();
47  updateMenus();
48}
49
50LPDataset mainUI::newDataset(QString ds){
51  //subroutine to create and fill a new dataset with system information
52  qDebug() << "New Dataset: " << ds;
53  LPDataset DSC;
54  //List all the mountpoints in this dataset
55  QStringList subsets = LPBackend::listDatasetSubsets(ds);
56  QStringList lpsnaps = LPBackend::listLPSnapshots(ds);
57  //populate the list of snapshots available for each mountpoint
58  for(int i=0; i<subsets.length(); i++){
59    //qDebug() << "Subset:" << subsets[i];
60    QStringList snaps = LPBackend::listSnapshots(subsets[i]);
61    //qDebug() << " - Snapshots:" << snaps;
62    if(snaps.isEmpty()){
63      //invalid subset - remove it from the list
64      subsets.removeAt(i);
65      i--;
66    }else{
67      QStringList subsnaps;
68      //only list the valid snapshots that life preserver created
69      for(int s=0; s<lpsnaps.length(); s++){
70        int index = snaps.indexOf(lpsnaps[s]);
71        if(index > -1){ subsnaps << lpsnaps[s]; snaps.removeAt(index); }
72      }
73      //Now list all the other available snapshots (no certain ordering)
74      if(!snaps.isEmpty()){
75        subsnaps << "--"; //so we know that this is a divider between the sections
76        subsnaps << snaps;
77      }
78      DSC.subsetHash.insert(subsets[i],subsnaps); //add it to the internal container hash
79    }
80  }
81  //Get the time for the latest life-preserver snapshot (and total number)
82  //Find the index for the current list
83  int ci = 0;
84  while(ci < CLIST.length()){
85    if(CLIST[ci].startsWith(ds+":::")){ break; }
86    else{ ci++; }
87  }
88  if(CLIST.isEmpty()){ ci = -1; } //catch for empty list
89  if(DSC.subsetHash.size() < 1){
90    DSC.numberOfSnapshots = "0";
91    DSC.latestSnapshot= "";
92  }else{
93    DSC.numberOfSnapshots = QString::number(lpsnaps.length());
94    if(lpsnaps.isEmpty()){ DSC.latestSnapshot=""; }
95    else if(ci > -1 && ci < CLIST.length()){ 
96      QString sna = CLIST[ci].section(":::",1,1);
97      if(sna != "-"){ DSC.latestSnapshot= sna; }
98      else{ DSC.latestSnapshot = ""; }     
99    }else{ DSC.latestSnapshot=lpsnaps[0]; }
100  }
101  //List the replication status
102  if(RLIST.contains(ds) && (ci > -1)){ 
103    QString rep = CLIST[ci].section(":::",2,2);
104    if(rep != "-"){ DSC.latestReplication = rep; }
105    else{ DSC.latestReplication= tr("Enabled"); }
106  }else{ 
107    DSC.latestReplication= tr("Disabled");
108  }
109  //Return the dataset
110  return DSC;
111}
112
113void mainUI::enableButtons(bool enable){
114  if(enable){
115    //special care must be taken here to only enable the ones that are valid
116    updateMenus();
117  }else{
118    ui->tool_add->setEnabled(false);
119    ui->tool_browse->setEnabled(false);
120    ui->tool_config->setEnabled(false);
121    ui->tool_keys->setEnabled(false);
122    ui->tool_newsnapshot->setEnabled(false);
123    ui->tool_remove->setEnabled(false);
124    ui->tool_revert->setEnabled(false);
125  }
126 
127}
128
129// =================
130//    PRIVATE FUNCTIONS
131// =================
132void mainUI::updateHash(QString ds){
133  //qDebug() << "Get replication targets";
134  RLIST = LPBackend::listReplicationTargets(); //update list of replication datasets
135  //qDebug() << "Get possible datasets";
136  SLIST = LPBackend::listPossibleDatasets();
137  //qDebug() << "List current status";
138  CLIST = LPBackend::listCurrentStatus();
139  //qDebug() << "Check hash";
140  if(HLIST.contains(ds) && !ds.isEmpty()){
141    //only update the entry for the given dataset
142    HLIST.insert(ds, newDataset(ds)); //will overwrite the current entry in the hash
143  }else{
144    //Clear and fill the hash
145    //qDebug() << "Clear hash";
146    HLIST.clear();
147    //qDebug() << "List datasets";
148    QStringList dsList = LPBackend::listDatasets();
149    for(int i=0; i<dsList.length(); i++){
150      HLIST.insert( dsList[i], newDataset(dsList[i]) );
151    }
152  }
153  //qDebug() << "Done with Hash Update";
154}
155
156void mainUI::updateUI(){
157  ui->treeWidget->clear();
158  QStringList dsList = HLIST.keys();
159  for(int i=0; i<dsList.length(); i++){
160    //Create the item for the treeWidget
161    // [ dataset, latest snapshot, num snapshots, is Replicated ]
162    QStringList cols;
163          cols << dsList[i]; //[0] - dataset
164          cols << HLIST[dsList[i]].latestSnapshot; // [1] newest snapshot name
165          cols << HLIST[dsList[i]].numberOfSnapshots; // [2] total number of snapshots
166          cols << HLIST[dsList[i]].latestReplication; // [3] latest replication
167    //Add the item to the widget
168    ui->treeWidget->addTopLevelItem( new QTreeWidgetItem(cols) );
169  }
170  //Now adjust the columns in the widget
171  for(int i=0; i<4; i++){
172    ui->treeWidget->resizeColumnToContents(i);
173  }
174  //Now make sure that the add button menu only shows the available datasets
175  addMenu->clear();
176  for(int i=0; i<SLIST.length(); i++){
177    if(!HLIST.contains(SLIST[i])){ addMenu->addAction( new QAction(SLIST[i],this) ); }
178  }
179  if(addMenu->isEmpty()){ ui->tool_add->setEnabled(false); }
180  else{ ui->tool_add->setEnabled(true); }
181}
182
183void mainUI::updateMenus(){
184  //Reset the button menu's to correspond to the selected dataset
185  QString ds = getSelectedDS();
186  //Enable/disable the remove/config buttons if nothing selected
187  if(ds.isEmpty()){
188    ui->tool_remove->setVisible(false);
189    ui->tool_config->setVisible(false);
190    ui->tool_newsnapshot->setVisible(false);
191  }else{
192    ui->tool_remove->setVisible(true); ui->tool_remove->setEnabled(true);
193    ui->tool_config->setVisible(true);  ui->tool_config->setEnabled(true);
194    ui->tool_newsnapshot->setVisible(true); ui->tool_newsnapshot->setEnabled(true);
195  }
196  //Enabled/disable the SSH key management
197  if(RLIST.contains(ds) && !ds.isEmpty()){
198    ui->tool_keys->setVisible(true);
199  }else{
200    ui->tool_keys->setVisible(false);
201  }
202  //check for a valid ds/snapshot combination
203  bool ok = !ds.isEmpty();
204  if(ok){ ok = HLIST.contains(ds); }
205  if(ok){ ok = (HLIST[ds].numberOfSnapshots.toInt() > 0); }
206  //Now set the items appropriately
207  revMenu->clear();
208  brMenu->clear();
209  if(ok){
210    //Reset the Menu Contents
211    QStringList subsets = HLIST[ds].subsets(); 
212    for(int i=0; i<subsets.length(); i++){
213      //Build the menu of snapshots for this subset
214        QStringList snaps = HLIST[ds].snapshots(subsets[i]);
215        if(snaps.isEmpty()){ continue; }
216        QMenu *menu = new QMenu(subsets[i],this);
217        for(int s =0; s<snaps.length(); s++){
218          if(snaps[s] == "--"){ menu->addSeparator(); }
219          else{
220            QAction *act = new QAction(snaps[s],this);
221                act->setWhatsThis(ds+":::"+subsets[i]+":::"+snaps[s]);
222            menu->addAction(act);
223          }
224        }
225        revMenu->addMenu(menu);
226        brMenu->addMenu(menu);
227    }       
228    //Enable the buttons if appropriate
229    if(revMenu->isEmpty()){
230      ui->tool_revert->setEnabled(false);
231      ui->tool_browse->setEnabled(false);
232    }else{
233      ui->tool_revert->setEnabled(true);
234      ui->tool_browse->setEnabled(true);
235    }
236  }else{
237    //Disable the buttons
238    ui->tool_revert->setEnabled(false);
239    ui->tool_browse->setEnabled(false);
240  }
241}
242
243QString mainUI::getSelectedDS(){
244  //Get the currently selected dataset
245  QString ds="";
246  if(ui->treeWidget->topLevelItemCount() > 0){
247    if( ui->treeWidget->currentItem() != 0){
248      ds = ui->treeWidget->currentItem()->text(0); //first column
249    }
250  }
251  return ds;
252}
253
254// =================
255//        PRIVATE SLOTS
256// =================
257//  --- Buttons Clicked
258void mainUI::on_treeWidget_itemSelectionChanged(){
259  if(ui->statusbar->currentMessage().isEmpty()){
260    //only update the menu's if they are not currently disabled while a process is running
261    updateMenus();
262  }
263}
264
265void mainUI::on_tool_config_clicked(){
266  QString ds = getSelectedDS();
267  if(ds.isEmpty()){ return; }
268  LPConfig CFG(this);
269  CFG.loadDataset(ds, RLIST.contains(ds));
270  CFG.exec();
271  //Now check for return values and update appropriately
272  bool change = false;
273  if(CFG.localChanged){
274    enableButtons(false);
275    ui->statusbar->showMessage(QString(tr("Configuring dataset: %1")).arg(ds),0);
276    LPBackend::setupDataset(ds, CFG.localSchedule, CFG.localSnapshots);
277    ui->statusbar->clearMessage();
278    change = true;
279  }
280  if(CFG.remoteChanged){
281    change = true;
282    enableButtons(false);
283    if(CFG.isReplicated){
284      ui->statusbar->showMessage(QString(tr("Configuring replication: %1")).arg(ds),0);
285      LPBackend::setupReplication(ds, CFG.remoteHost, CFG.remoteUser, CFG.remotePort, CFG.remoteDataset, CFG.remoteFreq);
286      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!!"));
287    }else{
288      ui->statusbar->showMessage(QString(tr("Removing replication: %1")).arg(ds),0);
289      LPBackend::removeReplication(ds);
290    }
291    ui->statusbar->clearMessage();
292  }
293  //Now update the UI if appropriate
294  if(change){
295    setupUI();
296  }
297}
298
299void mainUI::on_tool_remove_clicked(){
300  QString ds = getSelectedDS();
301  if(!ds.isEmpty()){
302    //Verify the removal of the dataset
303    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) ){           
304      enableButtons(false);
305      //verify the removal of all the snapshots for this dataset
306      QStringList snaps = LPBackend::listLPSnapshots(ds);
307      if(!snaps.isEmpty()){
308        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) ){
309          //Remove all the snapshots
310          ui->statusbar->showMessage(QString(tr("%1: Removing snapshots")).arg(ds),0);
311          for(int i=0; i<snaps.length(); i++){
312            LPBackend::removeSnapshot(ds,snaps[i]);
313          }
314          ui->statusbar->clearMessage();
315        }
316      }
317      //Remove the dataset from life-preserver management
318      if(RLIST.contains(ds)){ 
319        ui->statusbar->showMessage(QString(tr("%1: Disabling Replication")).arg(ds),0);
320        LPBackend::removeReplication(ds); 
321        ui->statusbar->clearMessage();     
322      }
323      ui->statusbar->showMessage(QString(tr("%1: Disabling Life-Preserver Management")).arg(ds),0);
324      LPBackend::removeDataset(ds);
325      ui->statusbar->clearMessage();
326    }
327  }
328  setupUI();
329}
330
331void mainUI::on_tool_newsnapshot_clicked(){
332  QString ds = getSelectedDS();
333  if(ds.isEmpty()){return; }
334  //Get the new snapshot name from the user
335  bool ok;
336  QString name = QInputDialog::getText(this,tr("New Snapshot Name"), tr("Snapshot Name:"), QLineEdit::Normal, tr("Name"), &ok, 0, Qt::ImhUppercaseOnly | Qt::ImhLowercaseOnly | Qt::ImhDigitsOnly );
337  if(!ok || name.isEmpty()){ return; } //cancelled
338  qDebug() << "Creating a new snapshot:" << ds << name;
339  //Now create the new snapshot
340  LPBackend::newSnapshot(ds,name);
341  QMessageBox::information(this,tr("Snapshot Pending"), tr("The new snapshot creation has been added to the queue"));
342  setupUI();
343}
344
345// --- Menu Items Clicked
346void mainUI::slotRevertToSnapshot(QAction *act){
347  QString info = act->whatsThis();
348  QString ds = info.section(":::",0,0);
349  QString subset = info.section(":::",1,1);
350  QString snap = info.section(":::",2,2);
351  qDebug() << "Revert Clicked:" << ds << subset << snap;
352  if(!ds.isEmpty()){
353    //Verify the reversion
354     if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify Snapshot Reversion"),
355             QString(tr("Are you sure that you wish to revert %1 to the selected snapshot?")).arg(subset)+"\n"+tr("WARNING: This will result in the loss of any data not previously backed up."),
356             QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
357        //Perform the reversion
358        enableButtons(false);
359        ui->statusbar->showMessage(QString(tr("%1: Reverting dataset: %2")).arg(ds,subset),0);
360        if( !LPBackend::revertSnapshot(ds+subset,snap) ){
361          //Error performing the reversion
362          qDebug() << " - Error:" << ds+subset << snap;
363          QMessageBox::warning(this,tr("Reversion Error"), tr("The snapshot reversion could not be completed successfully."));
364        }else{
365          //Good reversion
366          qDebug() << " - Revert Complete";
367          QMessageBox::information(this,tr("Reversion Success"), tr("The snapshot reversion was completed successfully."));     
368        }
369        ui->statusbar->clearMessage();
370        enableButtons(true);
371     }
372  }
373}
374
375void mainUI::slotBrowseSnapshot(QAction *act){
376  QString info = act->whatsThis();
377  QString ds = info.section(":::",0,0);
378  QString subset = info.section(":::",1,1);
379  QString snap = info.section(":::",2,2);
380  //Now let the user select a file within the snapshot to revert
381  QString snapPath = subset+"/.zfs/snapshot/"+snap+"/";
382  QString filepath = QFileDialog::getOpenFileName(this,tr("Revert a file"), snapPath, tr("Backup Files (*)") );
383  qDebug() << "File to revert:" << filepath;
384  //Check to make sure that it is a valid file (within the snapshot)
385  if(filepath.isEmpty() ){
386    qDebug() << " - Cancelled";
387    //action cancelled  -  do nothing
388  }else if(!filepath.startsWith(snapPath)){
389    qDebug() << " - Invalid File";
390    QMessageBox::warning(this, tr("Invalid Snapshot File"), tr("Please select a file from within the chosen snapshot that you wish to revert"));
391  }else{
392    enableButtons(false);
393    ui->statusbar->showMessage(QString(tr("%1: Reverting File: %2")).arg(ds,filepath.remove(".zfs/snapshot/"+snap+"/").replace(QDir::homePath(),"~")),0);
394    //Revert the file
395    QString newfile = LPBackend::revertSnapshotFile(subset,snap,filepath);
396    if(newfile.isEmpty()){
397      //Error copying the new file over
398      qDebug() << " - Error copying file";
399      QMessageBox::warning(this, tr("Error Reverting File"), QString(tr("An error occurred while tring to revert the file %1. Please try again.")).arg(filepath));
400    }else{
401      //Let the user know the location of the reverted file
402      qDebug() << " - Successful reversion:" << newfile;
403      QMessageBox::information(this, tr("FIle Reverted"), QString(tr("The reverted file is now available at: %1")).arg(newfile) );
404    }
405    ui->statusbar->clearMessage();
406    enableButtons(true);
407  }
408  return;
409}
410
411void mainUI::slotAddDataset(QAction *act){
412  QString dataset = act->text();
413  qDebug() << "Start Wizard for new dataset:" << dataset;
414  LPWizard wiz(this);
415  wiz.setDataset(dataset);
416  wiz.exec();
417  //See if the wizard was cancelled or not
418  if(!wiz.cancelled){
419    enableButtons(false);
420    ui->statusbar->showMessage(QString(tr("Enabling dataset management: %1")).arg(dataset),0);
421    //run the proper commands to get the dataset enabled
422    if( LPBackend::setupDataset(dataset, wiz.localTime, wiz.totalSnapshots) ){
423      if(wiz.enableReplication){
424         LPBackend::setupReplication(dataset, wiz.remoteHost, wiz.remoteUser, wiz.remotePort, wiz.remoteDataset, wiz.remoteTime);     
425         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!!"));
426      }
427    }
428    ui->statusbar->clearMessage();
429  }
430  //Now update the UI/Hash
431  setupUI();
432}
433
434void mainUI::on_actionClose_triggered(){
435  this->close();
436}
437
438void mainUI::on_actionKeyNew_triggered(){
439  QString ds = getSelectedDS();
440  if(ds.isEmpty()){ return; }
441  qDebug() << "New SSH Key triggered for DS:" << ds;
442  enableButtons(false);
443  ui->statusbar->showMessage(QString(tr("%1: Setting up SSH Key")).arg(ds),0);
444  //Get the remote values for this dataset
445  QString remoteHost, user, remotedataset;
446  int port, time;
447  bool ok = LPBackend::replicationInfo(ds, remoteHost, user, port, remotedataset,  time);
448  if(ok){
449    if( !LPBackend::setupSSHKey(remoteHost, user, port) ){
450      QMessageBox::warning(this,tr("Failure"), tr("There was an error while creating the SSH key."));
451    }else{
452      QMessageBox::information(this,tr("Success"), tr("The SSH key was successfully generated."));
453    }
454  }else{
455    QMessageBox::warning(this,tr("Failure"), tr("There was an error in retrieving the remote replication information for this dataset. Please ensure that replication is enabled and try agin.") );
456  }
457  ui->statusbar->clearMessage();
458  enableButtons(true);
459}
460
461void mainUI::on_actionKeyCopy_triggered(){
462  QString ds = getSelectedDS(); 
463  if(ds.isEmpty()){ return; }
464  qDebug() << "Copy SSH Key triggered for DS:" << ds;
465  //Get the local hostname
466  char host[1023] = "\0";
467  gethostname(host,1023);
468  QString localHost = QString(host).simplified();
469  qDebug() << " - hostname:" << localHost;
470  //Scan for mounted USB devices
471  QStringList devs = LPBackend::findValidUSBDevices();
472  qDebug() << " - devs:" << devs;
473  if(devs.isEmpty()){
474    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."));
475    return;
476  }
477  //Ask the user which one to save the file to
478  bool ok;
479  QString dev = QInputDialog::getItem(this, tr("Select USB Device"), tr("Available USB Devices:"), devs,0,false,&ok);   
480  if(!ok or dev.isEmpty()){ return; } //cancelled
481  QString devPath = dev.section("(",0,0).simplified();
482  //Now copy the file over
483  ok = LPBackend::copySSHKey(devPath, localHost);
484  if(ok){
485    QMessageBox::information(this,tr("Success"), tr("The public SSH key file was successfully copied onto the USB device."));
486  }else{
487    QMessageBox::information(this,tr("Failure"), tr("The public SSH key file could not be copied onto the USB device."));
488  }
489}
490
491
492// =============
493//      PROTECTED
494// =============
495void mainUI::closeEvent(QCloseEvent *event){
496  //Make sure this window only gets hidden rather than closed
497  // this prevents the entire tray application from closing down as well
498  event->ignore();
499  this->hide();
500}
Note: See TracBrowser for help on using the repository browser.