source: lumina/lumina-fm/FODialog.cpp @ b3b08221

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

Get lumina-fm copy/restores working properly - now the zfs file/dir restore working completely.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1//===========================================
2//  Lumina-DE source code
3//  Copyright (c) 2014, Ken Moore
4//  Available under the 3-clause BSD license
5//  See the LICENSE file for full details
6//===========================================
7#include "FODialog.h"
8#include "ui_FODialog.h"
9
10FODialog::FODialog(QWidget *parent) : QDialog(parent), ui(new Ui::FODialog){
11  ui->setupUi(this); //load the designer file
12  ui->label->setText("");
13  ui->progressBar->setVisible(false);
14  ui->push_stop->setIcon( LXDG::findIcon("edit-delete","") );
15  //Now set the internal defaults
16  isRM = isCP = isRESTORE = isMV = stopped = overwrite = noerrors = false;
17}
18
19FODialog::~FODialog(){
20  stopped = true; //just in case it might still be running when closed
21}
22
23void FODialog::setOverwrite(bool ovw){
24  overwrite = ovw;
25}
26
27//Public "start" functions
28void FODialog::RemoveFiles(QStringList paths){
29  ofiles = paths;
30  isRM = true;
31  QTimer::singleShot(10,this, SLOT(slotStartOperations()));
32}
33
34void FODialog::CopyFiles(QStringList oldPaths, QStringList newPaths){   
35  //same permissions as old files
36  if(oldPaths.length() == newPaths.length()){
37    ofiles = oldPaths; nfiles = newPaths;
38  }
39  isCP=true;
40  QTimer::singleShot(10,this, SLOT(slotStartOperations()));
41}
42
43void FODialog::RestoreFiles(QStringList oldPaths, QStringList newPaths){
44  //user/group rw permissions
45  if(oldPaths.length() == newPaths.length()){
46    ofiles = oldPaths; nfiles = newPaths;
47  }
48  isRESTORE = true;
49  QTimer::singleShot(10,this, SLOT(slotStartOperations()));
50}
51
52void FODialog::MoveFiles(QStringList oldPaths, QStringList newPaths){
53  //no change in permissions
54  if(oldPaths.length() == newPaths.length()){
55    ofiles = oldPaths; nfiles = newPaths;
56  }
57  isMV=true;
58  QTimer::singleShot(10,this, SLOT(slotStartOperations()));
59}
60
61// ==== PRIVATE ====
62QStringList FODialog::subfiles(QString dirpath){
63  QStringList out;
64  if( QFileInfo(dirpath).isDir() ){
65    QDir dir(dirpath);
66    //First list the files
67    QStringList files = dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::NoSort);
68    for(int i=0; i<files.length(); i++){ out << dir.absoluteFilePath(files[i]); }
69    //Now recursively add any subdirectories and their contents
70    QStringList subdirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::NoSort);
71    for(int i=0; i<subdirs.length(); i++){ out << subfiles(subdirs[i]); }
72  }
73  out << dirpath; //always put the parent directory after all the contents
74  return out;
75}
76
77QString FODialog::newFileName(QString path){
78  int num=1;
79  QString extension = path.section(".",-1);
80  if(!extension.isEmpty()){ 
81    extension.prepend("."); 
82    path.chop(extension.length());
83  }
84  while( QFile::exists(path+"-"+QString::number(num)+extension) ){ num++; }
85  return QString(path+"-"+QString::number(num)+extension);
86}
87
88QStringList FODialog::removeItem(QString path){
89  QStringList items = subfiles(path);
90  QStringList err;     
91  for(int i=0; i<items.length(); i++){
92    if(QFileInfo(items[i]).isDir()){
93      QDir dir;
94      if( !dir.rmdir(items[i]) ){ err << items[i]; }
95    }else{
96      if( !QFile::remove(items[i]) ){ err << items[i]; }
97    }
98  }
99  return err;
100}
101
102QStringList FODialog::copyItem(QString oldpath, QString newpath){
103  QStringList err;
104  if(QFileInfo(oldpath).isDir()){
105    QDir dir;
106    if( !dir.mkpath(newpath) ){ err << oldpath; }
107    else{
108      dir.cd(oldpath);
109      QStringList subs = dir.entryList(QDir::Files | QDir::Files | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst);
110      for(int i=0; i<subs.length(); i++){ err << copyItem(oldpath+"/"+subs[i], newpath+"/"+subs[i]); }
111    }
112  }else{
113    if( !QFile::copy(oldpath, newpath) ){ err << oldpath; }
114    else{
115      if(isCP){
116        QFile::setPermissions(newpath, QFile::permissions(oldpath));
117      }else if(isRESTORE){
118        QFile::setPermissions(newpath, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::WriteGroup);
119      }
120    }
121  }
122  return err;
123}
124
125// ==== PRIVATE SLOTS ====
126void FODialog::slotStartOperations(){
127  ui->label->setText(tr("Calculating..."));
128  //Now setup the UI
129  ui->progressBar->setRange(0,ofiles.length());
130  ui->progressBar->setValue(0);
131  ui->progressBar->setVisible(true);
132  //Now start iterating over the operations
133  QStringList errlist;
134  for(int i=0; i<ofiles.length() && !stopped; i++){
135    if(isRM){
136      ui->label->setText( QString(tr("Removing: %1")).arg(ofiles[i].section("/",-1)) );
137      errlist << removeItem(ofiles[i]);
138    }else if(isCP || isRESTORE){
139      ui->label->setText( QString(tr("Copying: %1 to %2")).arg(ofiles[i].section("/",-1), nfiles[i].section("/",-1)) );
140      if(QFile::exists(nfiles[i])){
141        if(overwrite){
142          errlist << removeItem(nfiles[i]);
143        }else{
144          nfiles[i] = newFileName(nfiles[i]);
145        }
146      }
147      errlist << copyItem(ofiles[i], nfiles[i]);
148    }else if(isMV){
149      ui->label->setText( QString(tr("Moving: %1 to %2")).arg(ofiles[i].section("/",-1), nfiles[i].section("/",-1)) );
150      if( !QFile::rename(ofiles[i], nfiles[i]) ){
151        errlist << ofiles[i];
152      } 
153    }
154    ui->progressBar->setValue(i+1);
155  }
156  //All finished, so close the dialog if successful
157  errlist.removeAll(""); //make sure to clear any empty items
158  if(!errlist.isEmpty()){
159    QString msg;
160    if(isRM){ msg = tr("Could not remove these files:"); }
161    else if(isCP){ msg = tr("Could not copy these files:"); }
162    else if(isRESTORE){ msg = tr("Could not restore these files:"); }
163    else if(isMV){ msg = tr("Could not move these files:"); }
164    QMessageBox::warning(this, tr("File Errors"), msg+"\n\n"+errlist.join("\n"));
165  }
166  noerrors = errlist.isEmpty();
167  this->close();
168}
169
170void FODialog::on_push_stop_clicked(){
171  stopped = true;
172}
Note: See TracBrowser for help on using the repository browser.