#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);
    if(! targetDir.exists() && ! targetDir.mkdir(".")) return false;
    QFileInfoList fileInfos = QDir(fromDir).entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
    foreach(QFileInfo fileInfo, fileInfos) {
        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;
}
