-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
103 lines (86 loc) · 3.47 KB
/
Copy pathFileSystem.cpp
File metadata and controls
103 lines (86 loc) · 3.47 KB
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
#include "FileSystem.hpp"
#include "FileEntry.hpp"
#include "FileUtils.hpp"
#include <unistd.h>
namespace {
struct FileNameLess {
bool operator()(const FileEntry* a, const FileEntry* b) const {
return a->fileName().compare(b->fileName()) < 0;
}
};
}
FileSystem::FileSystem(const QStringList& filters, QObject *parent)
: QObject(parent),
m_filters(filters)
{}
bool FileSystem::fileMatches(const QString& fileName) const {
const QString& extension = FileUtils::extension(fileName);
return m_filters.contains(extension, Qt::CaseInsensitive);
}
bb::cascades::ArrayDataModel* FileSystem::listRoot(QObject *parent) {
static const char * locations[] = {
"/accounts/1000/shared/music",
"/accounts/1000/shared/documents",
"/accounts/1000/shared/downloads",
"/accounts/1000/removable/sdcard",
"/accounts/1000/shared/Box",
"/accounts/1000/shared/Dropbox",
"/accounts/1000/shared/OneDrive"
};
bb::cascades::ArrayDataModel * model = new bb::cascades::ArrayDataModel(parent);
for(size_t i = 0; i < sizeof(locations)/sizeof(locations[0]); ++i) {
// Note, not using stat64 here
if(0 == ::access(locations[i], F_OK)) {
struct stat64 st;
::memset(&st, 0, sizeof(struct stat64));
st.st_mode = S_IFDIR; // file system access performance improvement
FileEntry *fileEntry = new FileEntry(locations[i], st, parent);
QObject *obj = static_cast<QObject*>(fileEntry);
model->append(QVariant::fromValue(obj));
}
}
return model;
}
bb::cascades::ArrayDataModel* FileSystem::listFiles(const QString& path, QObject *parent) {
if(path.isEmpty() || path == "/") {
return listRoot();
}
bb::cascades::ArrayDataModel * model = new bb::cascades::ArrayDataModel(parent);
QList<FileEntry*> foundFiles;
QList<FileEntry*> foundDirectories;
DIR *dirp;
if ((dirp = ::opendir(path.toUtf8().constData())) != NULL) {
struct dirent64 *direntItem;
do {
if ((direntItem = ::readdir64(dirp)) != NULL) {
if(direntItem->d_name[0] == '.') {
continue;
}
QString fileName = QString::fromUtf8(direntItem->d_name);
QString absoluteFileName = FileUtils::joinPath(path, fileName);
struct stat64 st;
if(0 == ::stat64(absoluteFileName.toUtf8().constData(), &st)) {
if(st.st_mode & S_IFREG) {
if(fileMatches(fileName)) {
FileEntry * fileEntry = new FileEntry(absoluteFileName, st, model);
foundFiles << fileEntry;
}
} else if(st.st_mode & S_IFDIR) {
FileEntry * fileEntry = new FileEntry(absoluteFileName, st, model);
foundDirectories << fileEntry;
}
}
}
} while (direntItem != NULL);
::closedir(dirp);
}
::qSort(foundDirectories.begin(), foundDirectories.end(), FileNameLess());
::qSort(foundFiles.begin(), foundFiles.end(), FileNameLess());
foreach(FileEntry * directory, foundDirectories) {
model->append(QVariant::fromValue(static_cast<QObject*>(directory)));
}
foreach(FileEntry * file, foundFiles) {
model->append(QVariant::fromValue(static_cast<QObject*>(file)));
}
return model;
}