-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesearcher.cpp
67 lines (51 loc) · 1.63 KB
/
filesearcher.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
/*
This file is part of Waver
Copyright (C) 2021 Peter Papp
Please visit https://launchpad.net/waver for details
*/
#include "filesearcher.h"
#include "qdebug.h"
FileSearcher::FileSearcher(QString dir, QString filter, QString uiData) : QThread()
{
this->dir = dir;
this->uiData = uiData;
filter.replace(QRegExp("\\s+"), "[\\s_-]+");
this->filter.setPattern(filter);
this->filter.setCaseSensitivity(Qt::CaseInsensitive);
}
void FileSearcher::run()
{
searchDir(QDir(dir));
}
QString FileSearcher::getUiData()
{
return uiData;
}
QList<QFileInfo> FileSearcher::getResults()
{
std::sort(results.begin(), results.end(), [](QFileInfo a, QFileInfo b) {
return a.baseName().compare(b.baseName(), Qt::CaseInsensitive) < 0;
});
return results;
}
void FileSearcher::searchDir(QDir dirToSearch)
{
if (dirToSearch.exists()) {
const QFileInfoList entries = dirToSearch.entryInfoList();
foreach (QFileInfo entry, entries) {
if (isInterruptionRequested()) {
break;
}
if ((entry.fileName().compare(".") == 0) || (entry.fileName().compare("..") == 0)) {
continue;
}
if (entry.isDir()) {
searchDir(QDir(entry.absoluteFilePath()));
continue;
}
if (entry.isFile() && !entry.isSymLink() && (entry.fileName().endsWith(".mp3", Qt::CaseInsensitive) || mimeDatabase.mimeTypeForFile(entry).name().startsWith("audio", Qt::CaseInsensitive)) && entry.fileName().contains(filter)) {
results.append(entry);
}
}
}
}