//----------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------
|
#include "filemanage.h"
|
#include <QApplication>
|
#include <QTimer>
|
#include <QFile>
|
#include <QIODevice>
|
#include <QDirIterator>
|
#include "Common/app_define.h"
|
#include "unistd.h"
|
#include <sys/vfs.h>
|
//----------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------
|
FileManage::~FileManage()
|
{
|
Run_CP_EN = false;
|
Run_RM_EN = false;
|
wait();
|
}
|
//----------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::removeDir(const QString &fromDir)
|
{
|
QDir dir(fromDir);
|
if(!dir.exists())
|
return true;
|
|
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
|
QFileInfoList fileList = dir.entryInfoList();
|
foreach (QFileInfo fi, fileList)
|
{
|
if (fi.isFile())
|
fi.dir().remove(fi.fileName());
|
else
|
removeDir(fi.absoluteFilePath());
|
}
|
|
return dir.rmdir(dir.absolutePath());
|
}
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::removeFile(const QString &fromDir, const QString &fromFile)
|
{
|
QDir sourceDir(fromDir);
|
if(!sourceDir.remove(fromFile))
|
return false;
|
return true;
|
}
|
|
void FileManage::removeFile(QString &fname)
|
{
|
char cmd[1024];
|
|
sprintf(cmd, "rm \"%s\"",fname.toLocal8Bit().data());
|
//qDebug(cmd);
|
system(cmd);
|
system("sync");
|
}
|
|
//----------------------------------------------------------------------------------------------
|
QStringList FileManage::getDirs()
|
{
|
QStringList list;
|
QDir sourceDir(dataDir);
|
sourceDir.setSorting(QDir::Time);
|
QFileInfoList fileInfoList = sourceDir.entryInfoList();
|
foreach (QFileInfo fileInfo, fileInfoList)
|
{
|
if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
|
continue;
|
if(fileInfo.isDir())
|
{
|
list.append(fileInfo.fileName());
|
}
|
}
|
return list;
|
}
|
//----------------------------------------------------------------------------------------------
|
QStringList FileManage::getFiles(const QString &fromDir)
|
{
|
QStringList list;
|
QDir sourceDir(fromDir);
|
sourceDir.setSorting(QDir::Time);
|
QFileInfoList fileInfoList = sourceDir.entryInfoList();
|
foreach (QFileInfo fileInfo, fileInfoList)
|
{
|
if(fileInfo.isFile() && fileInfo.fileName().endsWith(testFileType))
|
{
|
list.append(fileInfo.fileName());
|
}
|
}
|
|
return list;
|
}
|
//----------------------------------------------------------------------------------------------
|
int FileManage::GetFileCount(const QString &fromDir)
|
{
|
int seedcount = 0;
|
QDir sourceDir(fromDir);
|
QFileInfoList fileInfoList = sourceDir.entryInfoList();
|
foreach(QFileInfo fileInfo, fileInfoList)
|
{
|
if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
|
continue;
|
|
if(fileInfo.isDir()) // 当为目录时,递归的进行copy //
|
{
|
seedcount += GetFileCount(fileInfo.filePath());
|
}
|
else
|
{
|
seedcount += 1;
|
}
|
}
|
|
return seedcount;
|
}
|
//----------------------------------------------------------------------------------------------
|
int FileManage::GetFileCountFromDirs(const QStringList &fromDir)
|
{
|
int seedcount = 0;
|
for(int n=0; n<fromDir.count(); n++)
|
{
|
seedcount += GetFileCount(fromDir.at(n));
|
}
|
|
return seedcount;
|
}
|
//----------------------------------------------------------------------------------------------
|
void FileManage::copyFile(const QString &fromDir, const QString &toDir)
|
{
|
QDir targetDir(toDir);
|
char cmd[1024];
|
QString path_from,path_to;
|
targetDir.mkpath(targetDir.absolutePath());
|
//path_from = "\"" + fromDir + "/" + "\"";
|
path_from = "\"" + fromDir + "\"";
|
path_to = "\"" + toDir + "/" + "\"";
|
//sprintf(cmd, "rm -r %s",
|
// path_to.toLocal8Bit().data());
|
//system(cmd);
|
sprintf(cmd, "cp -r %s %s",
|
path_from.toLocal8Bit().data(),
|
path_to.toLocal8Bit().data());
|
system(cmd);
|
system("sync");
|
//progressValue(100);
|
//progress = 100;
|
//emit progressValue(progress);
|
}
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::copyDirectoryFiles(const QStringList &fromDir, const QString &toDir,
|
bool coverFileIfExist)
|
{
|
Run_CP_EN = true;
|
Run_RM_EN = false;
|
|
from_Dir = fromDir;
|
to_Dir = toDir;
|
|
(void)coverFileIfExist;
|
|
this->start();
|
|
return true;
|
}
|
|
bool FileManage::deleteDirectoryFiles(const QStringList &fromDir)
|
{
|
Run_RM_EN = true;
|
Run_CP_EN = false;
|
|
from_Dir = fromDir;
|
|
this->start();
|
|
return true;
|
}
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::clearDirectoryFiles(const QString &fromDir, const QStringList &fileList)
|
{
|
for(int n=0; n<fileList.count(); n++)
|
{
|
removeFile(fromDir, fileList.at(n));
|
}
|
return true;
|
}
|
|
int FileManage::FindLocalFileFromPath(const QString &strFilePath, const QString filename)
|
{
|
int filecnt = 0;
|
|
if (strFilePath.isEmpty() || filename.isEmpty())
|
{
|
return filecnt;
|
}
|
|
//qDebug(strFilePath.toLocal8Bit());
|
//qDebug(filename.toLocal8Bit());
|
|
QDir dir;
|
QStringList filters;
|
|
filters << filename;//过滤条件,可以添加多个选项,可以匹配文件后缀等。我这里只是找指定文件
|
dir.setPath(strFilePath);
|
dir.setNameFilters(filters);//添加过滤器
|
//QDirIterator 此类可以很容易的返回指定目录的所有文件及文件夹,可以再递归遍历,也可以自动查找指定的文件
|
QDirIterator iter(dir,QDirIterator::Subdirectories);
|
while (iter.hasNext())
|
{
|
iter.next();
|
QFileInfo info=iter.fileInfo();
|
if (info.isFile())
|
{
|
filecnt++;
|
}
|
}
|
|
return filecnt;
|
}
|
|
bool FileManage::CheckDiskState(const QString &disk)
|
{
|
struct statfs st;
|
statfs(disk.toLocal8Bit().data(), &st);
|
if(19780 == st.f_type)
|
{
|
return true;
|
}
|
|
return false;
|
}
|
|
//----------------------------------------------------------------------------------------------
|
void FileManage::run()
|
{
|
//Run_EN = true;
|
|
int p_v = 0;
|
QString fn;
|
|
while(true == Run_CP_EN)
|
{
|
//qDebug("Run_CP_EN is true");
|
|
for(int n=0; n<from_Dir.count(); n++)
|
{
|
if(false == CheckDiskState(usbDir))
|
{
|
//p_v = from_Dir.count()+1;
|
|
qDebug("usb not exists.");
|
|
emit progressValue(65535);
|
|
break;
|
}
|
|
copyFile(from_Dir.at(n), to_Dir);
|
|
fn = from_Dir.at(n);
|
fn = fn.remove(0,testDataDir.length());
|
fn = to_Dir+fn;
|
//qDebug(fn.toLocal8Bit());
|
|
p_v = n+1;
|
|
if(!(QFile(fn).exists()))
|
{
|
//p_v = from_Dir.count()+1;
|
|
qDebug("file not exists.");
|
|
emit progressValue(65535);
|
|
break;
|
}
|
else
|
{
|
//qDebug("file exists.");
|
emit progressValue(p_v);
|
}
|
}
|
|
Run_CP_EN = false;
|
}
|
|
while(true == Run_RM_EN)
|
{
|
//qDebug("Run_RM_EN is true");
|
|
for(int n=0; n<from_Dir.count(); n++)
|
{
|
QString fn = from_Dir.at(n);
|
|
removeFile(fn);
|
|
p_v = n+1;
|
|
emit progressValue(p_v);
|
}
|
|
Run_RM_EN = false;
|
}
|
}
|
//----------------------------------------------------------------------------------------------
|