qt/LedOK/program/copydirthread.cpp

59 lines
2.4 KiB
C++
Raw Permalink Normal View History

2023-04-18 14:14:46 +08:00
#include "copydirthread.h"
#include <QDir>
CopyDirThread::CopyDirThread() {
connect(this, &CopyDirThread::finished, this, &CopyDirThread::deleteLater);
}
void CopyDirThread::run() {
for(; i<dirSrcs.size(); i++) {
copiedSize = 0;
copyDir(dirSrcs[i], dirDst+"/"+QFileInfo(dirSrcs[i]).fileName(), true);
}
}
bool CopyDirThread::copyDir(const QString &fromDir, const QString &toDir, bool coverIfExist) {
QDir targetDir(toDir);
2023-04-28 16:02:14 +08:00
if(! targetDir.exists() && ! targetDir.mkdir(toDir)) return false;
2023-04-18 14:14:46 +08:00
QFileInfoList fileInfos = QDir(fromDir).entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
2024-02-21 18:08:50 +08:00
for(auto &fileInfo : fileInfos) {
2023-04-18 14:14:46 +08:00
if(fileInfo.isDir()) { //< 当为目录时递归的进行copy
if(! copyDir(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()), coverIfExist)) return false;
} else { //当允许覆盖操作时,将旧文件进行删除操作
if(coverIfExist && targetDir.exists(fileInfo.fileName())) targetDir.remove(fileInfo.fileName());
if(! QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))) return false;
else {
copiedSize += fileInfo.size();
emit sigProgress(i, copiedSize);
}
}
}
return true;
}
2024-02-21 18:08:50 +08:00
void CopyDirThread::move() {
for(; i<dirSrcs.size(); i++) {
copiedSize = 0;
moveDir(dirSrcs[i], dirDst+"/"+QFileInfo(dirSrcs[i]).fileName());
}
}
bool CopyDirThread::moveDir(const QString &fromDir, const QString &toDir) {
QDir targetDir(toDir);
if(! targetDir.exists() && ! targetDir.mkdir(toDir)) return false;
QFileInfoList fileInfos = QDir(fromDir).entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
foreach(QFileInfo fileInfo, fileInfos) {
if(fileInfo.isDir()) { //< 当为目录时递归的进行copy
if(! moveDir(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))) return false;
} else { //当允许覆盖操作时,将旧文件进行删除操作
if(! QFile::rename(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))) return false;
else {
copiedSize += fileInfo.size();
emit sigProgress(i, copiedSize);
}
}
}
return true;
}