source: src-qt4/life-preserver/lp-gui/LPClassic.cpp @ 109c737

enter/10releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2
Last change on this file since 109c737 was 109c737, checked in by Ken Moore <ken@…>, 15 months ago

Add a new "Classic Backup" dialog for customizing the home-dir archive and providing status updates while it is running.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1#include "LPClassic.h"
2#include "ui_LPClassic.h"
3
4//====PUBLIC=====
5LPClassic::LPClassic(QWidget *parent) : QDialog(parent), ui(new Ui::LPClassic){
6  ui->setupUi(this); //load the designer file
7  //initialize the file check timer
8  fCheck = new QTimer(this);
9    fCheck->setInterval(2000); //2 second update interval
10    fCheck->setSingleShot(true);
11    connect(fCheck, SIGNAL(timeout()), this, SLOT(updateFCheck()) );
12  //initialize the tar process
13  tarProc = new QProcess(this);
14    tarProc->setProcessEnvironment( QProcessEnvironment::systemEnvironment());
15    connect(tarProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotTarDone()) );
16  //initialize the QFileInfo
17  tarInfo = new QFileInfo();
18        tarInfo->setCaching(false);
19  //set the initial stopped flag
20  stopped = false;
21  running = false;
22  ui->list_exclude->clear(); //make sure the exclude list is cleared (possibly different base-dir)
23  //Make sure the initial state of the UI is correct
24  on_list_exclude_itemSelectionChanged();
25}
26
27LPClassic::~LPClassic(){
28       
29}
30
31void LPClassic::setHomeDir(QString home){
32  //Generate the recommended filename
33  tarDir = home.section("/",-1);
34  tarBaseDir = home;
35        tarBaseDir.chop(tarDir.length() +1);
36  ui->line_filename->setText(tarDir+"-"+QDateTime::currentDateTime().toString("yyyyMMdd-hhmm"));
37  //Now set the process to start in the parent directory
38  tarProc->setWorkingDirectory(tarBaseDir);
39  //Now make sure we start on the right page
40  ui->stackedWidget->setCurrentWidget(ui->page_setup);
41}
42
43//====PRIVATE====
44void LPClassic::updateFCheck(){
45  //Get the current size of the file
46  tarInfo->refresh();
47  double size = tarInfo->size();
48  QStringList labels; labels << "B" << "KB" << "MB" << "GB" << "TB" << "PB" << "EB";
49  int i=0;
50  while( size > 1024 && i < labels.length() ){
51    size = size/1024; 
52    i++;
53  }
54  //Round to 2 decimel places if GB or larger
55  if(i>2){ size = int(size*100)/100.0; }
56  else{ size = int(size); }
57  //Now display that size
58  ui->label_size->setText( QString::number(size)+" "+labels[i] );
59  if(!stopped){ fCheck->start(); }
60}
61
62void LPClassic::slotTarDone(){
63  running = false;
64  fCheck->stop();
65  updateFCheck(); //Final update for the file size
66  if(stopped){
67    qDebug() << "Home-Dir Package Cancelled";
68    ui->label_status->setText(tr("Cancelled"));
69    //Now try to delete the partial file
70    QString file = tarBaseDir+"/"+tarFile;
71    qDebug() << " - Removing partial archive file:" << file;
72    QFile::remove(file);
73  }else{
74    qDebug() << "Home-Dir Package Finished";
75    ui->label_status->setText(tr("FINISHED"));
76  }
77  ui->push_stop->setVisible(false);
78  ui->push_finished->setVisible(true);
79  //Set the progress bar as finished
80  ui->progressBar->setRange(0,1);
81  ui->progressBar->setValue(1);
82}
83
84//Exclude list controls
85void LPClassic::on_tool_rmexclude_clicked(){
86  delete ui->list_exclude->takeItem( ui->list_exclude->currentRow() );
87}
88
89void LPClassic::on_tool_addexcludefile_clicked(){
90  QStringList paths = QFileDialog::getOpenFileNames(this, tr("Exclude Files"), tarBaseDir+"/"+tarDir, tr("All Files (*)"));
91  for(int i=0; i<paths.length(); i++){
92    if(!paths[i].startsWith(tarBaseDir+"/"+tarDir)){ continue; } //invalid file
93    ui->list_exclude->addItem(paths[i]);
94  }
95}
96
97void LPClassic::on_tool_addexcludedir_clicked(){
98  QString path = QFileDialog::getExistingDirectory(this, tr("Exclude Directory"), tarBaseDir+"/"+tarDir);
99  if(path.isEmpty() || !path.startsWith(tarBaseDir+"/"+tarDir) ){ return; }
100  if(!path.endsWith("/")){ path.append("/"); } //make sure there is a / on the end of directories
101  ui->list_exclude->addItem(path); 
102}
103
104void LPClassic::on_list_exclude_itemSelectionChanged(){
105  ui->tool_rmexclude->setEnabled( ui->list_exclude->currentItem() != 0 );
106}
107
108
109//Main Buttons
110void LPClassic::on_push_cancel_clicked(){
111  this->close(); //Just close this window
112}
113
114void LPClassic::on_push_start_clicked(){
115  //Read the UI to create the exclude list
116  stopped = false;
117  running = true;
118  tarFile = ui->line_filename->text()+".home.tar.gz";
119  tarInfo->setFile(tarBaseDir+"/"+tarFile);
120  //Generate the command
121  QStringList args;
122  args << "-czf" << tarBaseDir+"/"+tarFile << "-C" << tarBaseDir << tarDir;
123  //Create the exclude list
124  QStringList excludes;
125    excludes << "*flashplayer*"; //Always exclude the flashplayer library
126    excludes << "*/.zfs/*"; //Always exclude the hidden zfs  directoriess
127  if(ui->check_pbidesktop->isChecked()){ excludes  << "*/PBI-*.desktop"; } //Don't include PBI entries
128  if(ui->check_userbin->isChecked()){ excludes << "*/bin/*"; } //exclude "bin" directories
129  for( int i=0; i<ui->list_exclude->count(); i++){
130    QString path = ui->list_exclude->item(i)->text();
131    //Remove the base homedir path
132    path.remove(0,tarBaseDir.length()+tarDir.length()+1);
133    if(path.endsWith("/")){ path.append("*"); } //don't put the asterisk on the end of files
134    excludes << "*"+path;
135  }
136  //Now convert the exclude list into additional arguments
137  for(int i=0; i<excludes.length(); i++){
138    args.prepend(excludes[i]); args.prepend("--exclude");
139  }
140  //qDebug() << "Package command:" << "tar" << args;
141  //Start the process
142  tarProc->start("tar",args); //complex command, use args individually
143  tarProc->waitForStarted(1000);
144  //Start the file watcher
145  fCheck->start();
146  //Now show the proper page with correct elements
147  ui->label_fullfilename->setText(tarBaseDir+"/"+tarFile);
148  ui->push_finished->setVisible(false);
149  ui->push_stop->setVisible(true);
150  ui->label_status->setText(tr("Packaging Home Directory...") );
151  ui->progressBar->setRange(0,0);
152  ui->progressBar->setValue(-1);
153  ui->stackedWidget->setCurrentWidget(ui->page_running);
154}
155
156void LPClassic::on_push_stop_clicked(){
157  stopped = true;
158  tarProc->kill();
159  tarProc->waitForFinished(1000); //wait a second to let it die
160  if(tarProc->state() != QProcess::NotRunning){ 
161    tarProc->terminate(); //force kill it
162    tarProc->waitForFinished(300);
163  } 
164}
165
166void LPClassic::on_push_finished_clicked(){
167  this->close(); //Just close this window
168}
169
Note: See TracBrowser for help on using the repository browser.