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

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

Add the ability to create an SSH Key file, and copy that key file to a msdosfs/FAT32 formatted USB stick (still needs testing)

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