source: src-qt4/life-preserver/lp-gui/LPConfig.cpp @ b22b3a5

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

Add the ability to scan the network for hosts with SSH availability (port 22) to the Configuration UI. If it works, I will add the same thing to the LPWizard.

  • Property mode set to 100644
File size: 6.7 KB
Line 
1#include "LPConfig.h"
2#include "ui_LPConfig.h"
3
4LPConfig::LPConfig(QWidget *parent) : QDialog(parent), ui(new Ui::LPConfig){
5  ui->setupUi(this); //initialize the designer UI
6  qDebug() << "Initializing Configuration Dialog";
7  //initialize the output variables as necessary
8  localChanged = false;
9  remoteChanged = false;
10  //Variables that will be changed when loading the dataset properties
11       
12  //now connect the buttons
13  connect(ui->tool_apply,SIGNAL(clicked()), this,SLOT(slotApplyChanges()) );
14  connect(ui->tool_cancel,SIGNAL(clicked()), this, SLOT(slotCancelConfig()) );
15  connect(ui->push_scanNetwork, SIGNAL(clicked()), this, SLOT(autoDetectReplicationTargets()) );
16}
17
18LPConfig::~LPConfig(){
19       
20}
21
22void LPConfig::loadDataset(QString ds, bool replicated){
23  ui->label_dataset->setText(ds);
24  loadDatasetConfiguration(ds, replicated);
25}
26
27//==========
28//     PRIVATE
29// ==========
30void LPConfig::loadDatasetConfiguration(QString dataset, bool replicated){
31  qDebug() <<" - Loading dataset configuration:" << dataset;
32  //Load the dataset values
33  isReplicated = replicated;
34  // - Local settings
35  if( !LPBackend::datasetInfo(dataset, localSchedule, localSnapshots) ){
36    localSchedule = 1; //daily at 1 AM
37    localSnapshots = 7;
38  }       
39  // - Replication settings
40  bool ok=false;
41  if(isReplicated){
42    ok = LPBackend::replicationInfo(dataset, remoteHost, remoteUser, remotePort, remoteDataset, remoteFreq);
43  }
44  if(!ok){
45    isReplicated = false;
46    remotePort = 22;
47    remoteFreq = -1; //sync
48    remoteHost = "";
49    remoteUser = "";
50    remoteDataset = "";
51  }
52  //Now put the values into the UI
53  // - local settings
54  if(localSchedule == -5){ //5 minutes
55    ui->combo_local_schedule->setCurrentIndex(4);         
56  }else if(localSchedule == -10){ //10 minutes
57    ui->combo_local_schedule->setCurrentIndex(3);       
58  }else if(localSchedule == -30){ //30 minutes
59    ui->combo_local_schedule->setCurrentIndex(2);         
60  }else if(localSchedule > 0 && localSchedule < 24){ //daily @ hour
61    ui->combo_local_schedule->setCurrentIndex(0);
62    ui->time_local_daily->setTime( QTime(localSchedule, 0) );
63  }else{ //assume hourly
64    localSchedule = -60;
65    ui->combo_local_schedule->setCurrentIndex(1);         
66  }
67  setLocalKeepNumber();
68       
69  // - Replication settings
70  ui->groupReplicate->setChecked(isReplicated);
71  ui->lineHostName->setText(remoteHost);
72  ui->lineUserName->setText(remoteUser);
73  ui->lineRemoteDataset->setText(remoteDataset);
74  ui->spinPort->setValue(remotePort);
75  if(remoteFreq >=0 && remoteFreq < 24){
76    ui->radioRepTime->setChecked(true);
77    ui->time_replicate->setTime( QTime(remoteFreq,0) );
78  }else{
79    ui->radioSYNC->setChecked(true);
80  }
81 
82}
83
84void LPConfig::checkForChanges(){
85  //Checks for changes to the settings while also updating the output variables to match the GUI
86       
87  localChanged = false;
88  remoteChanged = false;
89  //Local Settings
90  int nSchedule;
91  int schint = ui->combo_local_schedule->currentIndex();
92  if(schint == 0){ nSchedule = ui->time_local_daily->time().hour(); } //daily @ hour
93  else if(schint == 1){ nSchedule = -60; } //hourly
94  else if(schint == 2){ nSchedule = -30; } //30 min
95  else if(schint == 3){ nSchedule = -10; } //10 min
96  else{ nSchedule = -5; } //5 min
97  int nTotSnaps;
98  if( ui->combo_local_keepunits->currentIndex() == 0 && (schint != 0) ){ //days
99    int numperday = 1440/(-nSchedule);
100    nTotSnaps = numperday * ui->spin_local_numkeep->value();
101  }else{ //total number (or daily snapshots)
102    nTotSnaps = ui->spin_local_numkeep->value();
103  }
104  if(nSchedule != localSchedule){localChanged = true; localSchedule = nSchedule; }
105  if(nTotSnaps != localSnapshots){ localChanged = true; localSnapshots = nTotSnaps; }
106 
107  //Replication Settings
108  bool updateSSHKey = false;
109  if(isReplicated != ui->groupReplicate->isChecked()){
110    remoteChanged = true;
111    isReplicated = ui->groupReplicate->isChecked();
112    if(isReplicated){ updateSSHKey = true; }
113  }
114  QString tmp = ui->lineHostName->text().simplified();
115  if( tmp != remoteHost ){ remoteChanged = true; remoteHost = tmp; updateSSHKey=true;}
116  tmp = ui->lineUserName->text().simplified();
117  if( tmp != remoteUser ){ remoteChanged = true; remoteUser = tmp; updateSSHKey=true;}
118  tmp = ui->lineRemoteDataset->text().simplified();
119  if( tmp != remoteDataset ){ remoteChanged = true; remoteDataset = tmp; }
120  if( ui->spinPort->value() != remotePort){ remoteChanged = true; remotePort = ui->spinPort->value(); updateSSHKey=true;}
121  int nFreq = -1;
122  if(ui->radioRepTime->isChecked()){
123    nFreq = ui->time_replicate->time().hour();
124  }
125  if(nFreq < 0){
126    if( remoteFreq >= 0 && remoteFreq < 24){remoteChanged = true; remoteFreq = nFreq;}
127  }else{
128    if( nFreq != remoteFreq ){ remoteChanged = true; remoteFreq = nFreq; }
129  }
130  if(updateSSHKey){
131    //Prompt for the SSH key generation
132    LPBackend::setupSSHKey(remoteHost, remoteUser, remotePort);
133  }
134}
135
136void LPConfig::setLocalKeepNumber(){
137  if(localSchedule >=0){
138    ui->combo_local_keepunits->setCurrentIndex(0); //num days
139    ui->spin_local_numkeep->setValue(localSnapshots);   
140  }else{
141    int numperday = 1440/(-localSchedule);
142    if( localSnapshots % numperday == 0 ){
143        //daily
144        ui->combo_local_keepunits->setCurrentIndex(0); //num days
145        ui->spin_local_numkeep->setValue(localSnapshots/numperday);
146    }else{
147        //odd number of snapshots - must be total
148        ui->combo_local_keepunits->setCurrentIndex(1); //num total
149        ui->spin_local_numkeep->setValue(localSnapshots);
150    }
151  }
152}
153
154// =============
155//    PRIVATE SLOTS
156// =============
157void LPConfig::slotApplyChanges(){
158  checkForChanges();
159  this->close();
160}
161
162void LPConfig::slotCancelConfig(){
163  //Make sure it is flagged as unchanged before closing
164  localChanged = false;
165  remoteChanged = false; 
166  this->close();
167}
168
169void LPConfig::on_combo_local_schedule_currentIndexChanged(int index){
170  //Adjust whether the daily time box is visible
171  ui->time_local_daily->setVisible( (index == 0) );
172}
173
174void LPConfig::autoDetectReplicationTargets(){
175  QStringList targs = LPGUtils::scanNetworkSSH(); // <name>:::<address>:::<port>
176  if(targs.isEmpty()){
177    QMessageBox::warning(this,tr("No Network Targets"), tr("We could not find any systems on the local network with SSH availability (port 22)") );
178    return;
179  }
180  //Ask the user to select a target
181  QStringList targets;
182  for(int i=0; i<targs.length(); i++){
183    targets << targs[i].section(":::",0,0);
184  }
185  bool ok;
186  QString target = QInputDialog::getItem(this, tr("Select Replication Target"), tr("Hostname:"), targets, 0, false, &ok);
187  if(!ok || target.isEmpty() ){ return; } //cancelled
188  //Now look for that target in the list of info
189  for(int i=0; i<targs.length(); i++){
190    if(targs[i].startsWith(target+":::")){
191      ui->lineHostName->setText(targs[i].section(":::",1,1));
192      ui->spinPort->setValue( targs[i].section(":::",2,2).toInt() );
193      break;
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.