Ubuntu12.04.4_lts
2023-08-01 961efe36f62f7eba688d504ec0cdf3e1daa4dd74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
#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;
    }
}
//----------------------------------------------------------------------------------------------