-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemModel.cpp
More file actions
382 lines (338 loc) · 10.2 KB
/
FilesystemModel.cpp
File metadata and controls
382 lines (338 loc) · 10.2 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "FilesystemModel.h"
#include <chrono>
#include <iostream>
#include <stdexcept>
#include <regex>
using namespace Hix;
using namespace Hix::QML;
#ifdef __arm__
constexpr auto HOME = "/media/pi";
#else
constexpr auto HOME = "/home/jsh/USBtest";
#endif
inline QString fromStdPath(const std::filesystem::path& path)
{
auto u8str = path.u8string();
return QString::fromUtf8(u8str.c_str(), u8str.size());
}
inline std::filesystem::path fromQtPath(const QString& path)
{
return std::filesystem::u8path(path.toUtf8().toStdString());
}
//jesus what the fuck?
enum Roles {
FileNameRole = Qt::UserRole + 1,
FilePathRole = Qt::UserRole + 2,
FileBaseNameRole = Qt::UserRole + 3,
FileSuffixRole = Qt::UserRole + 4,
FileSizeRole = Qt::UserRole + 5,
FileLastModifiedRole = Qt::UserRole + 6,
//FileLastReadRole = Qt::UserRole + 7,
FileIsDirRole = Qt::UserRole + 7,
//FileUrlRole = Qt::UserRole + 9
};
Hix::QML::FilesystemModel::FilesystemModel(QObject* parent)
{
_roleNames[FileNameRole] = "fileName";
_roleNames[FilePathRole] = "filePath";
_roleNames[FileBaseNameRole] = "fileBaseName";
_roleNames[FileSuffixRole] = "fileSuffix";
_roleNames[FileSizeRole] = "fileSize";
_roleNames[FileLastModifiedRole] = "fileModified";
//_roleNames[FileLastReadRole] = "fileAccessed";
_roleNames[FileIsDirRole] = "fileIsDir";
//_roleNames[FileUrlRole] = "fileURL";
}
int Hix::QML::FilesystemModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)
return _data.size();
}
QModelIndex Hix::QML::FilesystemModel::index(int row, int column, const QModelIndex& parent) const
{
return createIndex(row, 0);
}
QVariant Hix::QML::FilesystemModel::data(const QModelIndex& index, int role) const
{
QVariant rv;
if (index.row() >= _data.size())
return rv;
std::error_code hiddenError;
auto& e = _data[index.row()];
switch (role)
{
case FileNameRole:
rv = fromStdPath(e.filename());
break;
case FilePathRole:
rv = fromStdPath(e);
break;
case FileBaseNameRole:
rv = fromStdPath(e.stem());
break;
case FileSuffixRole:
rv = fromStdPath(e.extension());
break;
case FileSizeRole:
rv= (uint)std::filesystem::file_size(e, hiddenError);
break;
case FileLastModifiedRole:
{
auto timePt = std::filesystem::last_write_time(e, hiddenError);
auto microsecondsUTC = std::chrono::duration_cast<std::chrono::microseconds>(timePt.time_since_epoch()).count();
rv = (uint)microsecondsUTC;
break;
}
//case FileLastReadRole:
//{
// auto timePt = std::filesystem::last_write_time(e, hiddenError);
// auto microsecondsUTC = std::chrono::duration_cast<std::chrono::microseconds>(timePt.time_since_epoch()).count();
// rv = microsecondsUTC;
// break;
//}
case FileIsDirRole:
rv = std::filesystem::is_directory(e);
break;
//case FileUrlRole:
// rv = QUrl::fromLocalFile(e.filePath());
// break;
default:
break;
}
return rv;
}
QHash<int, QByteArray> Hix::QML::FilesystemModel::roleNames() const
{
return _roleNames;
}
Hix::QML::FilesystemModel::SortField Hix::QML::FilesystemModel::sortField() const
{
return _sortField;
}
void Hix::QML::FilesystemModel::setSortField(SortField field)
{
if (field != _sortField) {
_sortField = field;
updateSorting();
}
}
QString Hix::QML::FilesystemModel::folder() const
{
return fromStdPath(_current);
}
void Hix::QML::FilesystemModel::setFolder(const QString& folder)
{
try
{
// std::cout << "setFolder start" << folder.toStdString() << std::endl;
auto path = fromQtPath(folder);
beginResetModel();
_current = path;
if (!std::filesystem::exists(_current) || !std::filesystem::is_directory(_current))
{
_data.clear();
endResetModel();
emit rowCountChanged();
emit folderChangeError();
}
else
{
_data.clear();
//populate file list
for (const auto& entry : std::filesystem::directory_iterator(path))
{
if (std::filesystem::is_regular_file(entry) && _showFiles)
{
_data.emplace_back(entry);
}
if (std::filesystem::is_directory(entry) && _showDirs)
{
_data.emplace_back(entry);
}
}
applyNameFilter();
updateSorting();
//signal end of change
endResetModel();
emit rowCountChanged();
emit folderChanged();
}
}
catch (std::runtime_error& e)
{
std::cout << "error setFolder: " << e.what() << std::endl;;
} catch(...)
{
std::cout << "unknown except" << std::endl;
}
}
QString Hix::QML::FilesystemModel::parentFolder() const
{
return fromStdPath(_current.parent_path());
}
bool Hix::QML::FilesystemModel::showFiles() const
{
return _showFiles;
}
void Hix::QML::FilesystemModel::setShowFiles(bool showFiles)
{
_showFiles = showFiles;
}
bool Hix::QML::FilesystemModel::showDirs() const
{
return _showDirs;
}
void Hix::QML::FilesystemModel::setShowDirs(bool showDirs)
{
_showDirs = showDirs;
}
bool Hix::QML::FilesystemModel::showDirsFirst() const
{
return _showDirsFirst;
}
void Hix::QML::FilesystemModel::setShowDirsFirst(bool showDirsFirst)
{
_showDirsFirst = showDirsFirst;
}
QStringList Hix::QML::FilesystemModel::nameFilters() const
{
QStringList qtList;
for (auto& e : _nameFilters)
{
qtList.push_back(fromStdPath(e));
}
return qtList;
}
void Hix::QML::FilesystemModel::setNameFilters(const QStringList& filters)
{
std::unordered_set<std::filesystem::path> newFilters;
for (auto& e : filters)
{
newFilters.insert(fromQtPath(e));
}
if (_nameFilters != newFilters)
{
_nameFilters = newFilters;
applyNameFilter();
}
}
bool Hix::QML::FilesystemModel::sortReversed() const
{
return _sortReversed;
}
void Hix::QML::FilesystemModel::setSortReversed(bool rev)
{
_sortReversed = rev;
}
bool Hix::QML::FilesystemModel::isFolder(int index) const
{
return std::filesystem::is_directory(_current);
}
QVariant Hix::QML::FilesystemModel::get(int idx, const QString& property) const
{
return QVariant();
}
int Hix::QML::FilesystemModel::indexOf(const QString& file) const
{
return int();
}
QString FilesystemModel::getUSB() const
{
std::filesystem::path home = HOME;
auto dirItr = std::filesystem::directory_iterator(home);
for(auto& e: dirItr)
{
auto path = e.path();
std::regex re("recoveryfs[0-9]*");
if(std::filesystem::is_directory(path) && !std::regex_search(path.u8string(),re))
{
return fromStdPath(path);
}
}
return "";
}
bool FilesystemModel::fileExists(const QString& target) const
{
std::filesystem::path path = fromQtPath(target);
return std::filesystem::exists(path);
}
void updateSortingImpl(bool isDir, bool isReverse,
FilesystemModel::SortField sort, std::deque<std::filesystem::path>& paths)
{
switch (sort) {
case FilesystemModel::SortField::Unsorted:
break;
return;
case FilesystemModel::SortField::Name:
std::sort(paths.begin(), paths.end(),
[isReverse](const std::filesystem::path& a, const std::filesystem::path& b) ->bool {
return isReverse ^ (a.filename() > b.filename());
});
break;
case FilesystemModel::SortField::Time:
std::sort(paths.begin(), paths.end(),
[isReverse](const std::filesystem::path& a, const std::filesystem::path& b) ->bool {
std::error_code hiddenError;
return isReverse ^ (std::filesystem::last_write_time(a, hiddenError) >
std::filesystem::last_write_time(b, hiddenError));
});
break;
case FilesystemModel::SortField::Size:
if (!isDir)
{
std::sort(paths.begin(), paths.end(),
[isReverse](const std::filesystem::path& a, const std::filesystem::path& b) ->bool {
std::error_code hiddenError;
return isReverse ^ (std::filesystem::file_size(a, hiddenError) >
std::filesystem::file_size(b, hiddenError));
});
}
break;
case FilesystemModel::SortField::Type:
if (!isDir)
{
std::sort(paths.begin(), paths.end(),
[isReverse](const std::filesystem::path& a, const std::filesystem::path& b) ->bool {
return isReverse ^ (a.extension() > b.extension());
});
}
break;
}
}
void Hix::QML::FilesystemModel::updateSorting()
{
//divide into directories and files
std::deque<std::filesystem::path> files;
std::deque<std::filesystem::path> dirs;
for (auto& e : _data)
{
if (std::filesystem::is_directory(e))
dirs.emplace_back(e);
else
files.emplace_back(e);
}
updateSortingImpl(true, _sortReversed, _sortField, dirs);
updateSortingImpl(false, _sortReversed, _sortField, files);
//if show directory first
if (_showDirsFirst)
{
dirs.insert(dirs.end(), files.begin(), files.end());
_data = std::move(dirs);
}
else
{
files.insert(files.end(), dirs.begin(), dirs.end());
_data = std::move(files);
}
}
void Hix::QML::FilesystemModel::applyNameFilter()
{
auto itr = _data.begin();
while (itr != _data.end())
{
if (std::filesystem::is_regular_file(*itr) && _nameFilters.find(itr->extension()) == _nameFilters.cend())
itr = _data.erase(itr);
else
++itr;
}
}