diff options
Diffstat (limited to 'src/imports/folderlistmodel')
-rw-r--r-- | src/imports/folderlistmodel/fileinfothread.cpp | 58 | ||||
-rw-r--r-- | src/imports/folderlistmodel/fileinfothread_p.h | 3 |
2 files changed, 49 insertions, 12 deletions
diff --git a/src/imports/folderlistmodel/fileinfothread.cpp b/src/imports/folderlistmodel/fileinfothread.cpp index a006f659c9..a93edd3b1b 100644 --- a/src/imports/folderlistmodel/fileinfothread.cpp +++ b/src/imports/folderlistmodel/fileinfothread.cpp @@ -39,6 +39,8 @@ #include "fileinfothread_p.h" #include <qdiriterator.h> +#include <qpointer.h> +#include <qtimer.h> #include <QDebug> @@ -46,6 +48,7 @@ FileInfoThread::FileInfoThread(QObject *parent) : QThread(parent), abort(false), + scanPending(false), #if QT_CONFIG(filesystemwatcher) watcher(nullptr), #endif @@ -109,7 +112,7 @@ void FileInfoThread::setPath(const QString &path) #endif currentPath = path; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setRootPath(const QString &path) @@ -126,7 +129,7 @@ void FileInfoThread::dirChanged(const QString &directoryPath) Q_UNUSED(directoryPath); QMutexLocker locker(&mutex); folderUpdate = true; - condition.wakeAll(); + initiateScan(); } #endif @@ -136,7 +139,7 @@ void FileInfoThread::setSortFlags(QDir::SortFlags flags) sortFlags = flags; sortUpdate = true; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setNameFilters(const QStringList & filters) @@ -144,7 +147,7 @@ void FileInfoThread::setNameFilters(const QStringList & filters) QMutexLocker locker(&mutex); nameFilters = filters; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowFiles(bool show) @@ -152,7 +155,7 @@ void FileInfoThread::setShowFiles(bool show) QMutexLocker locker(&mutex); showFiles = show; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowDirs(bool showFolders) @@ -160,7 +163,7 @@ void FileInfoThread::setShowDirs(bool showFolders) QMutexLocker locker(&mutex); showDirs = showFolders; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowDirsFirst(bool show) @@ -168,7 +171,7 @@ void FileInfoThread::setShowDirsFirst(bool show) QMutexLocker locker(&mutex); showDirsFirst = show; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowDotAndDotDot(bool on) @@ -177,7 +180,7 @@ void FileInfoThread::setShowDotAndDotDot(bool on) showDotAndDotDot = on; folderUpdate = true; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowHidden(bool on) @@ -186,7 +189,7 @@ void FileInfoThread::setShowHidden(bool on) showHidden = on; folderUpdate = true; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowOnlyReadable(bool on) @@ -194,7 +197,7 @@ void FileInfoThread::setShowOnlyReadable(bool on) QMutexLocker locker(&mutex); showOnlyReadable = on; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setCaseSensitive(bool on) @@ -202,7 +205,7 @@ void FileInfoThread::setCaseSensitive(bool on) QMutexLocker locker(&mutex); caseSensitive = on; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } #if QT_CONFIG(filesystemwatcher) @@ -211,7 +214,7 @@ void FileInfoThread::updateFile(const QString &path) Q_UNUSED(path); QMutexLocker locker(&mutex); folderUpdate = true; - condition.wakeAll(); + initiateScan(); } #endif @@ -242,6 +245,37 @@ void FileInfoThread::run() } } +void FileInfoThread::runOnce() +{ + if (scanPending) + return; + scanPending = true; + QPointer<FileInfoThread> guardedThis(this); + + auto getFileInfosAsync = [guardedThis](){ + if (!guardedThis) + return; + guardedThis->scanPending = false; + if (guardedThis->currentPath.isEmpty()) { + emit guardedThis->statusChanged(QQuickFolderListModel::Null); + return; + } + emit guardedThis->statusChanged(QQuickFolderListModel::Loading); + guardedThis->getFileInfos(guardedThis->currentPath); + emit guardedThis->statusChanged(QQuickFolderListModel::Ready); + }; + + QTimer::singleShot(0, getFileInfosAsync); +} + +void FileInfoThread::initiateScan() +{ +#if QT_CONFIG(thread) + condition.wakeAll(); +#else + runOnce(); +#endif +} void FileInfoThread::getFileInfos(const QString &path) { diff --git a/src/imports/folderlistmodel/fileinfothread_p.h b/src/imports/folderlistmodel/fileinfothread_p.h index 438dea6faa..923cb29e03 100644 --- a/src/imports/folderlistmodel/fileinfothread_p.h +++ b/src/imports/folderlistmodel/fileinfothread_p.h @@ -99,6 +99,8 @@ public Q_SLOTS: protected: void run() override; + void runOnce(); + void initiateScan(); void getFileInfos(const QString &path); void findChangeRange(const QList<FileProperty> &list, int &fromIndex, int &toIndex); @@ -106,6 +108,7 @@ private: QMutex mutex; QWaitCondition condition; volatile bool abort; + bool scanPending; #if QT_CONFIG(filesystemwatcher) QFileSystemWatcher *watcher; |