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

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

Clean up when the SSH key generation is automatically run, and remind the user if the remote host information changes to save the SSH Key to a USB stick

  • Property mode set to 100644
File size: 15.2 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    lastUpdate = QTime::currentTime();   
37    qDebug() << "Updating the database";
38    updateHash();
39  }else{
40    freqTimer->start();
41  }
42  //Update the display
43  updateUI();
44  updateMenus();
45}
46
47LPDataset mainUI::newDataset(QString ds){
48  //subroutine to create and fill a new dataset with system information
49  qDebug() << "New Dataset: " << ds;
50  LPDataset DSC;
51  //List all the mountpoints in this dataset
52  QStringList subsets = LPBackend::listDatasetSubsets(ds);
53  //populate the list of snapshots available for each mountpoint
54  for(int i=0; i<subsets.length(); i++){
55    //qDebug() << "Subset:" << subsets[i];
56    QStringList snaps = LPBackend::listSnapshots(subsets[i]);
57    //qDebug() << " - Snapshots:" << snaps;
58    if(snaps.isEmpty()){
59      //invalid subset - remove it from the list
60      subsets.removeAt(i);
61      i--;
62    }else{
63      DSC.subsetHash.insert(subsets[i],snaps); //add it to the internal container hash
64    }
65  }
66  //Get the time for the latest life-preserver snapshot (and total number)
67  //Find the index for the current list
68  int ci = 0;
69  while(ci < CLIST.length()){
70    if(CLIST[ci].startsWith(ds+":::")){ break; }
71    else{ ci++; }
72  }
73  if(CLIST.isEmpty()){ ci = -1; } //catch for empty list
74  if(DSC.subsetHash.size() < 1){
75    DSC.numberOfSnapshots = "0";
76    DSC.latestSnapshot= "";
77  }else{
78    QStringList fSnap = DSC.subsetHash[subsets[0]].filter("auto-"); //filtered snapshot list (just life preserver snapshots)
79    DSC.numberOfSnapshots = QString::number(fSnap.length());
80    if(fSnap.isEmpty()){ DSC.latestSnapshot=""; }
81    else if(ci > -1 && ci < CLIST.length()){ 
82      QString sna = CLIST[ci].section(":::",1,1);
83      if(sna != "-"){ DSC.latestSnapshot= sna; }
84      else{ DSC.latestSnapshot = ""; }     
85    }else{ DSC.latestSnapshot=fSnap[0]; }
86  }
87  //List the replication status
88  if(RLIST.contains(ds) && (ci > -1)){ 
89    QString rep = CLIST[ci].section(":::",2,2);
90    if(rep != "-"){ DSC.latestReplication = rep; }
91    else{ DSC.latestReplication= tr("Enabled"); }
92  }else{ 
93    DSC.latestReplication= tr("Disabled");
94  }
95  //Return the dataset
96  return DSC;
97}
98
99// =================
100//    PRIVATE FUNCTIONS
101// =================
102void mainUI::updateHash(QString ds){
103  //qDebug() << "Get replication targets";
104  RLIST = LPBackend::listReplicationTargets(); //update list of replication datasets
105  //qDebug() << "Get possible datasets";
106  SLIST = LPBackend::listPossibleDatasets();
107  //qDebug() << "List current status";
108  CLIST = LPBackend::listCurrentStatus();
109  //qDebug() << "Check hash";
110  if(HLIST.contains(ds) && !ds.isEmpty()){
111    //only update the entry for the given dataset
112    HLIST.insert(ds, newDataset(ds)); //will overwrite the current entry in the hash
113  }else{
114    //Clear and fill the hash
115    //qDebug() << "Clear hash";
116    HLIST.clear();
117    //qDebug() << "List datasets";
118    QStringList dsList = LPBackend::listDatasets();
119    for(int i=0; i<dsList.length(); i++){
120      HLIST.insert( dsList[i], newDataset(dsList[i]) );
121    }
122  }
123  //qDebug() << "Done with Hash Update";
124}
125
126void mainUI::updateUI(){
127  ui->treeWidget->clear();
128  QStringList dsList = HLIST.keys();
129  for(int i=0; i<dsList.length(); i++){
130    //Create the item for the treeWidget
131    // [ dataset, latest snapshot, num snapshots, is Replicated ]
132    QStringList cols;
133          cols << dsList[i]; //[0] - dataset
134          cols << HLIST[dsList[i]].latestSnapshot; // [1] newest snapshot name
135          cols << HLIST[dsList[i]].numberOfSnapshots; // [2] total number of snapshots
136          cols << HLIST[dsList[i]].latestReplication; // [3] latest replication
137    //Add the item to the widget
138    ui->treeWidget->addTopLevelItem( new QTreeWidgetItem(cols) );
139  }
140  //Now adjust the columns in the widget
141  for(int i=0; i<4; i++){
142    ui->treeWidget->resizeColumnToContents(i);
143  }
144  //Now make sure that the add button menu only shows the available datasets
145  addMenu->clear();
146  for(int i=0; i<SLIST.length(); i++){
147    if(!HLIST.contains(SLIST[i])){ addMenu->addAction( new QAction(SLIST[i],this) ); }
148  }
149  if(addMenu->isEmpty()){ ui->tool_add->setEnabled(false); }
150  else{ ui->tool_add->setEnabled(true); }
151}
152
153void mainUI::updateMenus(){
154  //Reset the button menu's to correspond to the selected dataset
155  QString ds = getSelectedDS();
156  //Enable/disable the remove/config buttons if nothing selected
157  if(ds.isEmpty()){
158    ui->tool_remove->setVisible(false);
159    ui->tool_config->setVisible(false);
160
161  }else{
162    ui->tool_remove->setVisible(true);
163    ui->tool_config->setVisible(true);   
164  }
165  //Enabled/disable the SSH key management
166  if(RLIST.contains(ds) && !ds.isEmpty()){
167    ui->tool_keys->setVisible(true);
168  }else{
169    ui->tool_keys->setVisible(false);
170  }
171  //check for a valid ds/snapshot combination
172  bool ok = !ds.isEmpty();
173  if(ok){ ok = HLIST.contains(ds); }
174  if(ok){ ok = (HLIST[ds].numberOfSnapshots.toInt() > 0); }
175  //Now set the items appropriately
176  revMenu->clear();
177  brMenu->clear();
178  if(ok){
179    //Reset the Menu Contents
180    QStringList subsets = HLIST[ds].subsets(); 
181    for(int i=0; i<subsets.length(); i++){
182      //Build the menu of snapshots for this subset
183        QStringList snaps = HLIST[ds].snapshots(subsets[i]);
184        if(snaps.isEmpty()){ continue; }
185        QMenu *menu = new QMenu(subsets[i],this);
186        for(int s =0; s<snaps.length(); s++){
187          QAction *act = new QAction(snaps[s],this);
188                act->setWhatsThis(ds+":::"+subsets[i]+":::"+snaps[s]);
189          menu->addAction(act);
190        }
191        revMenu->addMenu(menu);
192        brMenu->addMenu(menu);
193    }       
194    //Enable the buttons if appropriate
195    if(revMenu->isEmpty()){
196      ui->tool_revert->setEnabled(false);
197      ui->tool_browse->setEnabled(false);
198    }else{
199      ui->tool_revert->setEnabled(true);
200      ui->tool_browse->setEnabled(true);
201    }
202  }else{
203    //Disable the buttons
204    ui->tool_revert->setEnabled(false);
205    ui->tool_browse->setEnabled(false);
206  }
207}
208
209QString mainUI::getSelectedDS(){
210  //Get the currently selected dataset
211  QString ds="";
212  if(ui->treeWidget->topLevelItemCount() > 0){
213    if( ui->treeWidget->currentItem() != 0){
214      ds = ui->treeWidget->currentItem()->text(0); //first column
215    }
216  }
217  return ds;
218}
219
220// =================
221//        PRIVATE SLOTS
222// =================
223//  --- Buttons Clicked
224void mainUI::on_treeWidget_itemSelectionChanged(){
225  updateMenus();
226}
227
228void mainUI::on_tool_config_clicked(){
229  QString ds = getSelectedDS();
230  if(ds.isEmpty()){ return; }
231  LPConfig CFG(this);
232  CFG.loadDataset(ds, RLIST.contains(ds));
233  CFG.exec();
234  //Now check for return values and update appropriately
235  bool change = false;
236  if(CFG.localChanged){
237    LPBackend::setupDataset(ds, CFG.localSchedule, CFG.localSnapshots);
238    change = true;
239  }
240  if(CFG.remoteChanged){
241    change = true;
242    if(CFG.isReplicated){
243      LPBackend::setupReplication(ds, CFG.remoteHost, CFG.remoteUser, CFG.remotePort, CFG.remoteDataset, CFG.remoteFreq);
244      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!!"));
245    }else{
246      LPBackend::removeReplication(ds);
247    }
248  }
249  //Now update the UI if appropriate
250  if(change){
251    setupUI();
252  }
253}
254
255void mainUI::on_tool_remove_clicked(){
256  QString ds = getSelectedDS();
257  if(!ds.isEmpty()){
258    //Verify the removal of the dataset
259    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) ){           
260      //verify the removal of all the snapshots for this dataset
261      QStringList snaps = LPBackend::listLPSnapshots(ds);
262      if(!snaps.isEmpty()){
263        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) ){
264          //Remove all the snapshots
265          for(int i=0; i<snaps.length(); i++){
266            LPBackend::removeSnapshot(ds,snaps[i]);
267          }
268        }
269      }
270      //Remove the dataset from life-preserver management
271      if(RLIST.contains(ds)){ LPBackend::removeReplication(ds); }
272      LPBackend::removeDataset(ds);
273    }
274  }
275  setupUI();
276}
277
278
279// --- Menu Items Clicked
280void mainUI::slotRevertToSnapshot(QAction *act){
281  QString info = act->whatsThis();
282  QString ds = info.section(":::",0,0);
283  QString subset = info.section(":::",1,1);
284  QString snap = info.section(":::",2,2);
285  qDebug() << "Revert Clicked:" << ds << subset << snap;
286  if(!ds.isEmpty()){
287    //Verify the reversion
288     if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify Snapshot Reversion"),
289             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."),
290             QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
291        //Perform the reversion
292        if( !LPBackend::revertSnapshot(ds+subset,snap) ){
293          //Error performing the reversion
294          qDebug() << " - Error:" << ds+subset << snap;
295          QMessageBox::warning(this,tr("Reversion Error"), tr("The snapshot reversion could not be completed successfully."));
296        }else{
297          //Good reversion
298          qDebug() << " - Revert Complete";
299          QMessageBox::information(this,tr("Reversion Success"), tr("The snapshot reversion was completed successfully."));     
300        }
301     }
302  }
303}
304
305void mainUI::slotBrowseSnapshot(QAction *act){
306  QString info = act->whatsThis();
307  QString ds = info.section(":::",0,0);
308  QString subset = info.section(":::",1,1);
309  QString snap = info.section(":::",2,2);
310  //Now let the user select a file within the snapshot to revert
311  QString snapPath = subset+"/.zfs/snapshot/"+snap+"/";
312  QString filepath = QFileDialog::getOpenFileName(this,tr("Revert a file"), snapPath, tr("Backup Files (*)") );
313  qDebug() << "File to revert:" << filepath;
314  //Check to make sure that it is a valid file (within the snapshot)
315  if(filepath.isEmpty() ){
316    qDebug() << " - Cancelled";
317    //action cancelled  -  do nothing
318  }else if(!filepath.startsWith(snapPath)){
319    qDebug() << " - Invalid File";
320    QMessageBox::warning(this, tr("Invalid Snapshot File"), tr("Please select a file from within the chosen snapshot that you wish to revert"));
321  }else{
322    //Revert the file
323    QString newfile = LPBackend::revertSnapshotFile(subset,snap,filepath);
324    if(newfile.isEmpty()){
325      //Error copying the new file over
326      qDebug() << " - Error copying file";
327      QMessageBox::warning(this, tr("Error Reverting File"), QString(tr("An error occurred while tring to revert the file %1. Please try again.")).arg(filepath));
328    }else{
329      //Let the user know the location of the reverted file
330      qDebug() << " - Successful reversion:" << newfile;
331      QMessageBox::information(this, tr("FIle Reverted"), QString(tr("The reverted file is now available at: %1")).arg(newfile) );
332    }
333  }
334  return;
335}
336
337void mainUI::slotAddDataset(QAction *act){
338  QString dataset = act->text();
339  qDebug() << "Start Wizard for new dataset:" << dataset;
340  LPWizard wiz(this);
341  wiz.setDataset(dataset);
342  wiz.exec();
343  //See if the wizard was cancelled or not
344  if(!wiz.cancelled){
345    //run the proper commands to get the dataset enabled
346    if( LPBackend::setupDataset(dataset, wiz.localTime, wiz.totalSnapshots) ){
347      if(wiz.enableReplication){
348         LPBackend::setupReplication(dataset, wiz.remoteHost, wiz.remoteUser, wiz.remotePort, wiz.remoteDataset, wiz.remoteTime);     
349         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!!"));
350      }
351    }
352  }
353  //Now update the UI/Hash
354  setupUI();
355}
356
357void mainUI::on_actionClose_triggered(){
358  this->close();
359}
360
361void mainUI::on_actionKeyNew_triggered(){
362  QString ds = getSelectedDS();
363  qDebug() << "New SSH Key triggered for DS:" << ds;
364  //Get the remote values for this dataset
365  QString remoteHost, user, remotedataset;
366  int port, time;
367  bool ok = LPBackend::replicationInfo(ds, remoteHost, user, port, remotedataset,  time);
368  if(ok){
369    if( !LPBackend::setupSSHKey(remoteHost, user, port) ){
370      QMessageBox::warning(this,tr("Failure"), tr("There was an error while creating the SSH key."));
371    }else{
372      QMessageBox::information(this,tr("Success"), tr("The SSH key was successfully generated."));
373    }
374  }else{
375    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.") );
376  }
377}
378
379void mainUI::on_actionKeyCopy_triggered(){
380  QString ds = getSelectedDS(); 
381  qDebug() << "Copy SSH Key triggered for DS:" << ds;
382  //Get the local hostname
383  char host[1023] = "\0";
384  gethostname(host,1023);
385  QString localHost = QString(host).simplified();
386  qDebug() << " - hostname:" << localHost;
387  //Scan for mounted USB devices
388  QStringList devs = LPBackend::findValidUSBDevices();
389  qDebug() << " - devs:" << devs;
390  if(devs.isEmpty()){
391    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."));
392    return;
393  }
394  //Ask the user which one to save the file to
395  bool ok;
396  QString dev = QInputDialog::getItem(this, tr("Select USB Device"), tr("Available USB Devices:"), devs,0,false,&ok);   
397  if(!ok or dev.isEmpty()){ return; } //cancelled
398  QString devPath = dev.section("(",0,0).simplified();
399  //Now copy the file over
400  ok = LPBackend::copySSHKey(devPath, localHost);
401  if(ok){
402    QMessageBox::information(this,tr("Success"), tr("The public SSH key file was successfully copied onto the USB device."));
403  }else{
404    QMessageBox::information(this,tr("Failure"), tr("The public SSH key file could not be copied onto the USB device."));
405  }
406}
407
408
409// =============
410//      PROTECTED
411// =============
412void mainUI::closeEvent(QCloseEvent *event){
413  //Make sure this window only gets hidden rather than closed
414  // this prevents the entire tray application from closing down as well
415  event->ignore();
416  this->hide();
417}
Note: See TracBrowser for help on using the repository browser.