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

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

Add a new button to the life-preserver for creating a snapshot right then

  • Property mode set to 100644
File size: 15.9 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
278void mainUI::on_tool_newsnapshot_clicked(){
279  QString ds = getSelectedDS();
280  if(ds.isEmpty()){return; }
281  //Get the new snapshot name from the user
282  bool ok;
283  QString name = QInputDialog::getText(this,tr("New Snapshot Name"), tr("Snapshot Name:"), QLineEdit::Normal, tr("Name"), &ok, 0, Qt::ImhUppercaseOnly | Qt::ImhLowercaseOnly | Qt::ImhDigitsOnly );
284  if(!ok || name.isEmpty()){ return; } //cancelled
285  qDebug() << "Creating a new snapshot:" << ds << name;
286  //Now create the new snapshot
287  ok = LPBackend::newSnapshot(ds,name);
288  if( ok ){
289    QMessageBox::information(this,tr("Success"), tr("The new snapshot has been created"));
290  }else{
291    QMessageBox::warning(this,tr("Failure"), tr("The new snapshot could not be created"));
292  }
293  setupUI();
294}
295
296// --- Menu Items Clicked
297void mainUI::slotRevertToSnapshot(QAction *act){
298  QString info = act->whatsThis();
299  QString ds = info.section(":::",0,0);
300  QString subset = info.section(":::",1,1);
301  QString snap = info.section(":::",2,2);
302  qDebug() << "Revert Clicked:" << ds << subset << snap;
303  if(!ds.isEmpty()){
304    //Verify the reversion
305     if( QMessageBox::Yes == QMessageBox::question(this,tr("Verify Snapshot Reversion"),
306             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."),
307             QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
308        //Perform the reversion
309        if( !LPBackend::revertSnapshot(ds+subset,snap) ){
310          //Error performing the reversion
311          qDebug() << " - Error:" << ds+subset << snap;
312          QMessageBox::warning(this,tr("Reversion Error"), tr("The snapshot reversion could not be completed successfully."));
313        }else{
314          //Good reversion
315          qDebug() << " - Revert Complete";
316          QMessageBox::information(this,tr("Reversion Success"), tr("The snapshot reversion was completed successfully."));     
317        }
318     }
319  }
320}
321
322void mainUI::slotBrowseSnapshot(QAction *act){
323  QString info = act->whatsThis();
324  QString ds = info.section(":::",0,0);
325  QString subset = info.section(":::",1,1);
326  QString snap = info.section(":::",2,2);
327  //Now let the user select a file within the snapshot to revert
328  QString snapPath = subset+"/.zfs/snapshot/"+snap+"/";
329  QString filepath = QFileDialog::getOpenFileName(this,tr("Revert a file"), snapPath, tr("Backup Files (*)") );
330  qDebug() << "File to revert:" << filepath;
331  //Check to make sure that it is a valid file (within the snapshot)
332  if(filepath.isEmpty() ){
333    qDebug() << " - Cancelled";
334    //action cancelled  -  do nothing
335  }else if(!filepath.startsWith(snapPath)){
336    qDebug() << " - Invalid File";
337    QMessageBox::warning(this, tr("Invalid Snapshot File"), tr("Please select a file from within the chosen snapshot that you wish to revert"));
338  }else{
339    //Revert the file
340    QString newfile = LPBackend::revertSnapshotFile(subset,snap,filepath);
341    if(newfile.isEmpty()){
342      //Error copying the new file over
343      qDebug() << " - Error copying file";
344      QMessageBox::warning(this, tr("Error Reverting File"), QString(tr("An error occurred while tring to revert the file %1. Please try again.")).arg(filepath));
345    }else{
346      //Let the user know the location of the reverted file
347      qDebug() << " - Successful reversion:" << newfile;
348      QMessageBox::information(this, tr("FIle Reverted"), QString(tr("The reverted file is now available at: %1")).arg(newfile) );
349    }
350  }
351  return;
352}
353
354void mainUI::slotAddDataset(QAction *act){
355  QString dataset = act->text();
356  qDebug() << "Start Wizard for new dataset:" << dataset;
357  LPWizard wiz(this);
358  wiz.setDataset(dataset);
359  wiz.exec();
360  //See if the wizard was cancelled or not
361  if(!wiz.cancelled){
362    //run the proper commands to get the dataset enabled
363    if( LPBackend::setupDataset(dataset, wiz.localTime, wiz.totalSnapshots) ){
364      if(wiz.enableReplication){
365         LPBackend::setupReplication(dataset, wiz.remoteHost, wiz.remoteUser, wiz.remotePort, wiz.remoteDataset, wiz.remoteTime);     
366         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!!"));
367      }
368    }
369  }
370  //Now update the UI/Hash
371  setupUI();
372}
373
374void mainUI::on_actionClose_triggered(){
375  this->close();
376}
377
378void mainUI::on_actionKeyNew_triggered(){
379  QString ds = getSelectedDS();
380  qDebug() << "New SSH Key triggered for DS:" << ds;
381  //Get the remote values for this dataset
382  QString remoteHost, user, remotedataset;
383  int port, time;
384  bool ok = LPBackend::replicationInfo(ds, remoteHost, user, port, remotedataset,  time);
385  if(ok){
386    if( !LPBackend::setupSSHKey(remoteHost, user, port) ){
387      QMessageBox::warning(this,tr("Failure"), tr("There was an error while creating the SSH key."));
388    }else{
389      QMessageBox::information(this,tr("Success"), tr("The SSH key was successfully generated."));
390    }
391  }else{
392    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.") );
393  }
394}
395
396void mainUI::on_actionKeyCopy_triggered(){
397  QString ds = getSelectedDS(); 
398  qDebug() << "Copy SSH Key triggered for DS:" << ds;
399  //Get the local hostname
400  char host[1023] = "\0";
401  gethostname(host,1023);
402  QString localHost = QString(host).simplified();
403  qDebug() << " - hostname:" << localHost;
404  //Scan for mounted USB devices
405  QStringList devs = LPBackend::findValidUSBDevices();
406  qDebug() << " - devs:" << devs;
407  if(devs.isEmpty()){
408    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."));
409    return;
410  }
411  //Ask the user which one to save the file to
412  bool ok;
413  QString dev = QInputDialog::getItem(this, tr("Select USB Device"), tr("Available USB Devices:"), devs,0,false,&ok);   
414  if(!ok or dev.isEmpty()){ return; } //cancelled
415  QString devPath = dev.section("(",0,0).simplified();
416  //Now copy the file over
417  ok = LPBackend::copySSHKey(devPath, localHost);
418  if(ok){
419    QMessageBox::information(this,tr("Success"), tr("The public SSH key file was successfully copied onto the USB device."));
420  }else{
421    QMessageBox::information(this,tr("Failure"), tr("The public SSH key file could not be copied onto the USB device."));
422  }
423}
424
425
426// =============
427//      PROTECTED
428// =============
429void mainUI::closeEvent(QCloseEvent *event){
430  //Make sure this window only gets hidden rather than closed
431  // this prevents the entire tray application from closing down as well
432  event->ignore();
433  this->hide();
434}
Note: See TracBrowser for help on using the repository browser.