|
1 | 1 | #include "directorymanager.h"
|
2 | 2 |
|
| 3 | +#include <QDir> |
| 4 | +#include <QFileInfo> |
| 5 | + |
3 | 6 | namespace fs = std::filesystem;
|
4 | 7 |
|
5 | 8 | DirectoryManager::DirectoryManager() :
|
@@ -320,64 +323,45 @@ void DirectoryManager::loadEntryList(QString directoryPath, bool recursive) {
|
320 | 323 |
|
321 | 324 | // both directories & files
|
322 | 325 | void DirectoryManager::addEntriesFromDirectory(std::vector<FSEntry> &entryVec, QString directoryPath) {
|
323 |
| - QRegularExpressionMatch match; |
324 |
| - for(const auto & entry : fs::directory_iterator(toStdString(directoryPath))) { |
325 |
| - QString name = QString::fromStdString(entry.path().filename().generic_string()); |
| 326 | + QDir root(directoryPath); |
| 327 | + root.setFilter(QDir::Dirs | QDir::Files |
326 | 328 | #ifndef Q_OS_WIN32
|
327 |
| - // ignore hidden files |
328 |
| - if(name.startsWith(".")) |
329 |
| - continue; |
| 329 | + | QDir::NoDot |
330 | 330 | #endif
|
331 |
| - QString path = QString::fromStdString(entry.path().generic_string()); |
332 |
| - match = regex.match(name); |
333 |
| - if(entry.is_directory()) { // this can still throw std::bad_alloc .. |
334 |
| - FSEntry newEntry; |
335 |
| - try { |
336 |
| - newEntry.name = name; |
337 |
| - newEntry.path = path; |
338 |
| - newEntry.isDirectory = true; |
339 |
| - //newEntry.size = entry.file_size(); |
340 |
| - //newEntry.modifyTime = entry.last_write_time(); |
341 |
| - } catch (const std::filesystem::filesystem_error &err) { |
342 |
| - qDebug() << "[DirectoryManager]" << err.what(); |
343 |
| - continue; |
344 |
| - } |
| 331 | + ); |
| 332 | + |
| 333 | + QRegularExpressionMatch match{}; |
| 334 | + for (const auto &entry : root.entryInfoList()) { |
| 335 | + match = regex.match(entry.absoluteFilePath()); |
| 336 | + if (!entry.isDir() && !match.hasMatch()) { continue; } |
| 337 | + FSEntry newEntry{}; |
| 338 | + newEntry.name = entry.fileName(); |
| 339 | + newEntry.path = entry.absoluteFilePath(); |
| 340 | + newEntry.isDirectory = entry.isDir(); |
| 341 | + if (newEntry.isDirectory) { |
345 | 342 | dirEntryVec.emplace_back(newEntry);
|
346 |
| - } else if (match.hasMatch()) { |
347 |
| - FSEntry newEntry; |
348 |
| - try { |
349 |
| - newEntry.name = name; |
350 |
| - newEntry.path = path; |
351 |
| - newEntry.isDirectory = false; |
352 |
| - newEntry.size = entry.file_size(); |
353 |
| - newEntry.modifyTime = entry.last_write_time(); |
354 |
| - } catch (const std::filesystem::filesystem_error &err) { |
355 |
| - qDebug() << "[DirectoryManager]" << err.what(); |
356 |
| - continue; |
357 |
| - } |
| 343 | + } else { |
| 344 | + newEntry.size = entry.size(); |
| 345 | + newEntry.modifyTime = entry.lastModified(); |
358 | 346 | entryVec.emplace_back(newEntry);
|
359 | 347 | }
|
360 | 348 | }
|
361 | 349 | }
|
362 | 350 |
|
363 | 351 | void DirectoryManager::addEntriesFromDirectoryRecursive(std::vector<FSEntry> &entryVec, QString directoryPath) {
|
364 |
| - QRegularExpressionMatch match; |
365 |
| - for(const auto & entry : fs::recursive_directory_iterator(toStdString(directoryPath))) { |
366 |
| - QString name = QString::fromStdString(entry.path().filename().generic_string()); |
367 |
| - QString path = QString::fromStdString(entry.path().generic_string()); |
368 |
| - match = regex.match(name); |
369 |
| - if(!entry.is_directory() && match.hasMatch()) { |
370 |
| - FSEntry newEntry; |
371 |
| - try { |
372 |
| - newEntry.name = name; |
373 |
| - newEntry.path = path; |
374 |
| - newEntry.isDirectory = false; |
375 |
| - newEntry.size = entry.file_size(); |
376 |
| - newEntry.modifyTime = entry.last_write_time(); |
377 |
| - } catch (const std::filesystem::filesystem_error &err) { |
378 |
| - qDebug() << "[DirectoryManager]" << err.what(); |
379 |
| - continue; |
380 |
| - } |
| 352 | + QDir root(directoryPath); |
| 353 | + root.setFilter(QDir::Files); |
| 354 | + |
| 355 | + QRegularExpressionMatch match{}; |
| 356 | + for (const auto &entry : root.entryInfoList()) { |
| 357 | + match = regex.match(entry.absoluteFilePath()); |
| 358 | + if (match.hasMatch()) { |
| 359 | + FSEntry newEntry{}; |
| 360 | + newEntry.name = entry.fileName(); |
| 361 | + newEntry.path = entry.absoluteFilePath(); |
| 362 | + newEntry.isDirectory = entry.isDir(); |
| 363 | + newEntry.size = entry.size(); |
| 364 | + newEntry.modifyTime = entry.lastModified(); |
381 | 365 | entryVec.emplace_back(newEntry);
|
382 | 366 | }
|
383 | 367 | }
|
@@ -417,9 +401,8 @@ bool DirectoryManager::insertFileEntry(const QString &filePath) {
|
417 | 401 | bool DirectoryManager::forceInsertFileEntry(const QString &filePath) {
|
418 | 402 | if(!this->isFile(filePath) || containsFile(filePath))
|
419 | 403 | return false;
|
420 |
| - std::filesystem::directory_entry stdEntry(toStdString(filePath)); |
421 |
| - QString fileName = QString::fromStdString(stdEntry.path().filename().generic_string()); // isn't it beautiful |
422 |
| - FSEntry FSEntry(filePath, fileName, stdEntry.file_size(), stdEntry.last_write_time(), stdEntry.is_directory()); |
| 404 | + QFileInfo file(filePath); |
| 405 | + FSEntry FSEntry(file.absoluteFilePath(), file.fileName(), file.size(), file.lastModified(), file.isDir()); |
423 | 406 | insert_sorted(fileEntryVec, FSEntry, std::bind(compareFunction(), this, std::placeholders::_1, std::placeholders::_2));
|
424 | 407 | if(!directoryPath().isEmpty()) {
|
425 | 408 | qDebug() << "fileIns" << filePath << directoryPath();
|
@@ -471,8 +454,8 @@ void DirectoryManager::renameFileEntry(const QString &oldFilePath, const QString
|
471 | 454 | int oldIndex = indexOfFile(oldFilePath);
|
472 | 455 | fileEntryVec.erase(fileEntryVec.begin() + oldIndex);
|
473 | 456 | // insert
|
474 |
| - std::filesystem::directory_entry stdEntry(toStdString(newFilePath)); |
475 |
| - FSEntry FSEntry(newFilePath, newFileName, stdEntry.file_size(), stdEntry.last_write_time(), stdEntry.is_directory()); |
| 457 | + QFileInfo file(newFilePath); |
| 458 | + FSEntry FSEntry(file.absoluteFilePath(), file.fileName(), file.size(), file.lastModified(), file.isDir()); |
476 | 459 | insert_sorted(fileEntryVec, FSEntry, std::bind(compareFunction(), this, std::placeholders::_1, std::placeholders::_2));
|
477 | 460 | qDebug() << "fileRen" << oldFilePath << newFilePath;
|
478 | 461 | emit fileRenamed(oldFilePath, oldIndex, newFilePath, indexOfFile(newFilePath));
|
|
0 commit comments