-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsedirectorythread.cpp
107 lines (91 loc) · 2.68 KB
/
parsedirectorythread.cpp
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
#include "parsedirectorythread.h"
#include <QtGui>
//Constructor
ParseDirectoryThread::ParseDirectoryThread(QObject *parent) : QObject(parent)
{
recursionLimit = 0;
pStopParsing = false;
}
void ParseDirectoryThread::parseDirectory(QString dirPath)
{
//Reset parsing to be able to stop tasks individually.
//Comment this out to enable a single use stop for all parsing
pStopParsing = false;
pRootDir = dirPath;
pFileList = new QList<FileListStruct>();
//Old method to parse directories
//parse(QDir(dirPath));
//Experimental new method to parse directory - much faster
FileListStruct f;
f.rootDir = pRootDir;
QFileInfo fi(dirPath);
if (fi.isFile())
{
f.fileName = dirPath;
pFileList->append(f);
}
else
{
QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::Readable, QDirIterator::Subdirectories);
while (it.hasNext())
{
it.next();
f.fileName = it.filePath();
pFileList->append(f);
}
}
//Check if recursion limit has been reached somewhere in the structure
//if (recursionLimit < RECURSION_LIMIT)
if (!pStopParsing)
emit done(pRootDir, pFileList, this);
else
emit failed(pRootDir, this);
}
void ParseDirectoryThread::parse(QDir dir)
{
//Ensure recursion limit hasn't been reached
//if (recursionLimit >= RECURSION_LIMIT)
// return;
//Stop parsing if variable is changed
if (pStopParsing)
return;
//Get only files for now
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfo fi(dir.path());
if (fi.isFile())
{
//If user shared a single file, add directly to list
FileListStruct f;
f.rootDir = pRootDir;
f.fileName = dir.path();
pFileList->append(f);
return;
}
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); i++)
{
//Add every file in the current directory
FileListStruct f;
f.rootDir = pRootDir;
f.fileName = list.at(i).filePath();
pFileList->append(f);
//pFileList->append(list.at(i).absoluteFilePath());
}
//Get only directories
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
list = dir.entryInfoList();
for (int k = 0; k < list.size(); k++)
{
//Iterate through all subdirectories and recursively call this function
QDir nextDir(list.at(k).filePath());
if (nextDir.isReadable())
{
recursionLimit++;
parse(nextDir);
}
}
}
void ParseDirectoryThread::stopParsing()
{
pStopParsing = true;
}