//----------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------
|
#include "filemanage.h"
|
|
//----------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------
|
FileManage::~FileManage()
|
{
|
Run_EN = false;
|
wait();
|
}
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::createDir(const QString &newDir)
|
{
|
QDir *dir = new QDir();
|
bool exist = dir->exists(dataDir+newDir);
|
bool ifok = false;
|
if(!exist)
|
{
|
ifok = dir->mkdir(dataDir+newDir);
|
}
|
return ifok;
|
}
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::createFile(const QString &fromDir,const QString &newFile)
|
{
|
QFile file(dataDir+fromDir+"/"+newFile);
|
bool res = file.open(QIODevice::ReadWrite);
|
if(res)
|
file.close();
|
return res;
|
}
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::removeDir(const QString &fromDir)
|
{
|
//qDebug(fromDir.toLatin1().data());
|
//if (fromDir.isEmpty())
|
// return false;
|
|
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(dataDir+fromDir);
|
if(!sourceDir.remove(fromFile))
|
return false;
|
return true;
|
}
|
//----------------------------------------------------------------------------------------------
|
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::getDirs(bool ifDefault)
|
{
|
QStringList list;
|
QDir sourceDir(dataDir);
|
sourceDir.setSorting(QDir::Time);
|
QFileInfoList fileInfoList = sourceDir.entryInfoList();
|
foreach (QFileInfo fileInfo, fileInfoList)
|
{
|
if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
|
continue;
|
if(ifDefault)
|
{
|
if(fileInfo.isDir())
|
{
|
list.append(fileInfo.fileName());
|
}
|
}
|
else
|
{
|
if(fileInfo.isDir() && fileInfo.fileName() != "Default_Battery")
|
{
|
list.append(fileInfo.fileName());
|
}
|
}
|
}
|
return list;
|
}
|
//----------------------------------------------------------------------------------------------
|
QStringList FileManage::getFiles(const QString &fromDir,const QString &fileType)
|
{
|
QStringList list;
|
QDir sourceDir(dataDir+fromDir);
|
sourceDir.setSorting(QDir::Time);
|
QFileInfoList fileInfoList = sourceDir.entryInfoList();
|
foreach (QFileInfo fileInfo, fileInfoList)
|
{
|
//if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
|
// continue;
|
if(fileInfo.isFile() && fileInfo.fileName().endsWith(fileType))
|
{
|
if(fileType == ".xml")
|
list.append(fileInfo.baseName());
|
else
|
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;
|
}
|
//----------------------------------------------------------------------------------------------
|
bool FileManage::copyFiles(const QString &fromDir, const QString &toDir,
|
bool coverFileIfExist)
|
{
|
QDir sourceDir(fromDir);
|
QDir targetDir(toDir);
|
if(!targetDir.exists())
|
{ // 如果目标目录不存在,则进行创建 //
|
if(!targetDir.mkdir(targetDir.absolutePath()))
|
return false;
|
}
|
|
QFileInfoList fileInfoList = sourceDir.entryInfoList();
|
foreach(QFileInfo fileInfo, fileInfoList)
|
{
|
if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
|
continue;
|
|
if(fileInfo.isDir())
|
{ // 当为目录时,递归的进行copy //
|
if(!copyFiles(fileInfo.filePath(),
|
targetDir.filePath(fileInfo.fileName()),
|
coverFileIfExist))
|
return false;
|
}
|
else
|
{
|
// 当允许覆盖操作时,将旧文件进行删除操作 //
|
if(coverFileIfExist && targetDir.exists(fileInfo.fileName()))
|
targetDir.remove(fileInfo.fileName());
|
/// 进行文件copy
|
char cmd[256] = {0};
|
sprintf(cmd, "cp -rf %s %s", fileInfo.filePath().toLocal8Bit().data(),
|
targetDir.filePath(fileInfo.fileName()).toLocal8Bit().data());
|
|
//------------------------------------------------------------------
|
//不拷贝电池参数文件(BATT_PARAM_FILE_NAME)和记录文件(TEST_REC_FILE_NAME)
|
if( (fileInfo.fileName() != "battparam.xml")
|
&& (fileInfo.fileName() != "battparamd.xml") )
|
//------------------------------------------------------------------
|
{
|
system( cmd );
|
}
|
}
|
|
//emit progressValue(++progress);
|
}
|
|
return true;
|
}
|
|
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)
|
{
|
copy_or_clear = true;
|
from_Dir = fromDir;
|
to_Dir = toDir;
|
cover_FileIfExist = coverFileIfExist;
|
|
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;
|
}
|
|
void FileManage::renameDir(const QString &oldName,const QString &newName)
|
{
|
// char cmd[1024];
|
// QString path_old,path_new;
|
// path_old = "\"" + oldName + "\"";
|
// path_new = "\"" + newName + "/" + "\"";
|
// sprintf(cmd, "mv %s %s",
|
// path_old.toLocal8Bit().data(),
|
// path_new.toLocal8Bit().data());
|
// system(cmd);
|
// system("sync");
|
QDir dir;
|
dir.rename(oldName,newName);
|
}
|
|
int FileManage::FindLocalFileFromPath(const QString &strFilePath, const QString filename)
|
{
|
QStringList m_Filelist;//找到的文件存入此队列
|
if (strFilePath.isEmpty() || filename.isEmpty())
|
{
|
return 0;
|
}
|
|
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())
|
{
|
m_Filelist.append(info.absoluteFilePath());
|
}
|
}
|
return m_Filelist.count();
|
}
|
|
//----------------------------------------------------------------------------------------------
|
void FileManage::run()
|
{
|
Run_EN = true;
|
//progress = 0;
|
|
while(true == Run_EN)
|
{
|
for(int n=0; n<from_Dir.count(); n++)
|
{
|
//copyFiles(from_Dir.at(n), to_Dir+"/"+from_Dir.at(n), cover_FileIfExist);
|
copyFile(from_Dir.at(n), to_Dir);
|
}
|
|
Run_EN = false;
|
}
|
}
|
//----------------------------------------------------------------------------------------------
|