source: lumina/lumina-fm/FODialog.cpp @ 08438b40

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

Clean up the directory searching and recursive removal in lumina-fm

  • Property mode set to 100644
File size: 5.6 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(dir.absoluteFilePath(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(items[i]==path){
93      QDir dir;
94      if( !dir.rmdir(items[i]) ){ err << items[i]; }       
95    }else if(QFileInfo(items[i]).isDir()){
96      err << removeItem(items[i]);
97    }else{
98      if( !QFile::remove(items[i]) ){ err << items[i]; }
99    }
100  }
101  return err;
102}
103
104QStringList FODialog::copyItem(QString oldpath, QString newpath){
105  QStringList err;
106  if(QFileInfo(oldpath).isDir()){
107    QDir dir;
108    if( !dir.mkpath(newpath) ){ err << oldpath; }
109    else{
110      dir.cd(oldpath);
111      QStringList subs = dir.entryList(QDir::Files | QDir::Files | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst);
112      for(int i=0; i<subs.length(); i++){ err << copyItem(oldpath+"/"+subs[i], newpath+"/"+subs[i]); }
113    }
114  }else{
115    if( !QFile::copy(oldpath, newpath) ){ err << oldpath; }
116    else{
117      if(isCP){
118        QFile::setPermissions(newpath, QFile::permissions(oldpath));
119      }else if(isRESTORE){
120        QFile::setPermissions(newpath, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::WriteGroup);
121      }
122    }
123  }
124  return err;
125}
126
127// ==== PRIVATE SLOTS ====
128void FODialog::slotStartOperations(){
129  ui->label->setText(tr("Calculating..."));
130  //Now setup the UI
131  ui->progressBar->setRange(0,ofiles.length());
132  ui->progressBar->setValue(0);
133  ui->progressBar->setVisible(true);
134  //Now start iterating over the operations
135  QStringList errlist;
136  for(int i=0; i<ofiles.length() && !stopped; i++){
137    if(isRM){
138      ui->label->setText( QString(tr("Removing: %1")).arg(ofiles[i].section("/",-1)) );
139      errlist << removeItem(ofiles[i]);
140    }else if(isCP || isRESTORE){
141      ui->label->setText( QString(tr("Copying: %1 to %2")).arg(ofiles[i].section("/",-1), nfiles[i].section("/",-1)) );
142      if(QFile::exists(nfiles[i])){
143        if(overwrite){
144          errlist << removeItem(nfiles[i]);
145        }else{
146          nfiles[i] = newFileName(nfiles[i]);
147        }
148      }
149      errlist << copyItem(ofiles[i], nfiles[i]);
150    }else if(isMV){
151      ui->label->setText( QString(tr("Moving: %1 to %2")).arg(ofiles[i].section("/",-1), nfiles[i].section("/",-1)) );
152      if( !QFile::rename(ofiles[i], nfiles[i]) ){
153        errlist << ofiles[i];
154      } 
155    }
156    ui->progressBar->setValue(i+1);
157  }
158  //All finished, so close the dialog if successful
159  errlist.removeAll(""); //make sure to clear any empty items
160  if(!errlist.isEmpty()){
161    QString msg;
162    if(isRM){ msg = tr("Could not remove these files:"); }
163    else if(isCP){ msg = tr("Could not copy these files:"); }
164    else if(isRESTORE){ msg = tr("Could not restore these files:"); }
165    else if(isMV){ msg = tr("Could not move these files:"); }
166    QMessageBox::warning(this, tr("File Errors"), msg+"\n\n"+errlist.join("\n"));
167  }
168  noerrors = errlist.isEmpty();
169  this->close();
170}
171
172void FODialog::on_push_stop_clicked(){
173  stopped = true;
174}
Note: See TracBrowser for help on using the repository browser.