1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "fileinfothread_p.h"
41#include <qdiriterator.h>
42#include <qpointer.h>
43#include <qtimer.h>
44
45#include <QDebug>
46
47
48FileInfoThread::FileInfoThread(QObject *parent)
49 : QThread(parent),
50 abort(false),
51 scanPending(false),
52#if QT_CONFIG(filesystemwatcher)
53 watcher(nullptr),
54#endif
55 sortFlags(QDir::Name),
56 needUpdate(true),
57 folderUpdate(false),
58 sortUpdate(false),
59 showFiles(true),
60 showDirs(true),
61 showDirsFirst(false),
62 showDotAndDotDot(false),
63 showHidden(false),
64 showOnlyReadable(false),
65 caseSensitive(true)
66{
67#if QT_CONFIG(filesystemwatcher)
68 watcher = new QFileSystemWatcher(this);
69 connect(sender: watcher, SIGNAL(directoryChanged(QString)), receiver: this, SLOT(dirChanged(QString)));
70 connect(sender: watcher, SIGNAL(fileChanged(QString)), receiver: this, SLOT(updateFile(QString)));
71#endif // filesystemwatcher
72}
73
74FileInfoThread::~FileInfoThread()
75{
76 QMutexLocker locker(&mutex);
77 abort = true;
78 condition.wakeOne();
79 locker.unlock();
80 wait();
81}
82
83void FileInfoThread::clear()
84{
85 QMutexLocker locker(&mutex);
86#if QT_CONFIG(filesystemwatcher)
87 watcher->removePaths(files: watcher->files());
88 watcher->removePaths(files: watcher->directories());
89#endif
90}
91
92void FileInfoThread::removePath(const QString &path)
93{
94 QMutexLocker locker(&mutex);
95#if QT_CONFIG(filesystemwatcher)
96 if (!path.startsWith(c: QLatin1Char(':')))
97 watcher->removePath(file: path);
98#else
99 Q_UNUSED(path);
100#endif
101 currentPath.clear();
102}
103
104void FileInfoThread::setPath(const QString &path)
105{
106 Q_ASSERT(!path.isEmpty());
107
108 QMutexLocker locker(&mutex);
109#if QT_CONFIG(filesystemwatcher)
110 if (!path.startsWith(c: QLatin1Char(':')))
111 watcher->addPath(file: path);
112#endif
113 currentPath = path;
114 needUpdate = true;
115 initiateScan();
116}
117
118void FileInfoThread::setRootPath(const QString &path)
119{
120 Q_ASSERT(!path.isEmpty());
121
122 QMutexLocker locker(&mutex);
123 rootPath = path;
124}
125
126#if QT_CONFIG(filesystemwatcher)
127void FileInfoThread::dirChanged(const QString &directoryPath)
128{
129 Q_UNUSED(directoryPath);
130 QMutexLocker locker(&mutex);
131 folderUpdate = true;
132 initiateScan();
133}
134#endif
135
136void FileInfoThread::setSortFlags(QDir::SortFlags flags)
137{
138 QMutexLocker locker(&mutex);
139 sortFlags = flags;
140 sortUpdate = true;
141 needUpdate = true;
142 initiateScan();
143}
144
145void FileInfoThread::setNameFilters(const QStringList & filters)
146{
147 QMutexLocker locker(&mutex);
148 nameFilters = filters;
149 folderUpdate = true;
150 initiateScan();
151}
152
153void FileInfoThread::setShowFiles(bool show)
154{
155 QMutexLocker locker(&mutex);
156 showFiles = show;
157 folderUpdate = true;
158 initiateScan();
159}
160
161void FileInfoThread::setShowDirs(bool showFolders)
162{
163 QMutexLocker locker(&mutex);
164 showDirs = showFolders;
165 folderUpdate = true;
166 initiateScan();
167}
168
169void FileInfoThread::setShowDirsFirst(bool show)
170{
171 QMutexLocker locker(&mutex);
172 showDirsFirst = show;
173 folderUpdate = true;
174 initiateScan();
175}
176
177void FileInfoThread::setShowDotAndDotDot(bool on)
178{
179 QMutexLocker locker(&mutex);
180 showDotAndDotDot = on;
181 folderUpdate = true;
182 needUpdate = true;
183 initiateScan();
184}
185
186void FileInfoThread::setShowHidden(bool on)
187{
188 QMutexLocker locker(&mutex);
189 showHidden = on;
190 folderUpdate = true;
191 needUpdate = true;
192 initiateScan();
193}
194
195void FileInfoThread::setShowOnlyReadable(bool on)
196{
197 QMutexLocker locker(&mutex);
198 showOnlyReadable = on;
199 folderUpdate = true;
200 initiateScan();
201}
202
203void FileInfoThread::setCaseSensitive(bool on)
204{
205 QMutexLocker locker(&mutex);
206 caseSensitive = on;
207 folderUpdate = true;
208 initiateScan();
209}
210
211#if QT_CONFIG(filesystemwatcher)
212void FileInfoThread::updateFile(const QString &path)
213{
214 Q_UNUSED(path);
215 QMutexLocker locker(&mutex);
216 folderUpdate = true;
217 initiateScan();
218}
219#endif
220
221void FileInfoThread::run()
222{
223 forever {
224 bool updateFiles = false;
225 QMutexLocker locker(&mutex);
226 if (abort) {
227 return;
228 }
229 if (currentPath.isEmpty() || !needUpdate) {
230 emit statusChanged(status: currentPath.isEmpty() ? QQuickFolderListModel::Null : QQuickFolderListModel::Ready);
231 condition.wait(lockedMutex: &mutex);
232 }
233
234 if (abort) {
235 return;
236 }
237
238 if (!currentPath.isEmpty()) {
239 updateFiles = true;
240 emit statusChanged(status: QQuickFolderListModel::Loading);
241 }
242 if (updateFiles)
243 getFileInfos(path: currentPath);
244 locker.unlock();
245 }
246}
247
248void FileInfoThread::runOnce()
249{
250 if (scanPending)
251 return;
252 scanPending = true;
253 QPointer<FileInfoThread> guardedThis(this);
254
255 auto getFileInfosAsync = [guardedThis](){
256 if (!guardedThis)
257 return;
258 guardedThis->scanPending = false;
259 if (guardedThis->currentPath.isEmpty()) {
260 emit guardedThis->statusChanged(status: QQuickFolderListModel::Null);
261 return;
262 }
263 emit guardedThis->statusChanged(status: QQuickFolderListModel::Loading);
264 guardedThis->getFileInfos(path: guardedThis->currentPath);
265 emit guardedThis->statusChanged(status: QQuickFolderListModel::Ready);
266 };
267
268 QTimer::singleShot(interval: 0, slot: getFileInfosAsync);
269}
270
271void FileInfoThread::initiateScan()
272{
273#if QT_CONFIG(thread)
274 condition.wakeAll();
275#else
276 runOnce();
277#endif
278}
279
280void FileInfoThread::getFileInfos(const QString &path)
281{
282 QDir::Filters filter;
283 if (caseSensitive)
284 filter = QDir::CaseSensitive;
285 if (showFiles)
286 filter = filter | QDir::Files;
287 if (showDirs)
288 filter = filter | QDir::AllDirs | QDir::Drives;
289 if (!showDotAndDotDot)
290 filter = filter | QDir::NoDot | QDir::NoDotDot;
291 else if (path == rootPath)
292 filter = filter | QDir::NoDotDot;
293 if (showHidden)
294 filter = filter | QDir::Hidden;
295 if (showOnlyReadable)
296 filter = filter | QDir::Readable;
297 if (showDirsFirst)
298 sortFlags = sortFlags | QDir::DirsFirst;
299
300 QDir currentDir(path, QString(), sortFlags);
301 QList<FileProperty> filePropertyList;
302
303 const QFileInfoList fileInfoList = currentDir.entryInfoList(nameFilters, filters: filter, sort: sortFlags);
304
305 if (!fileInfoList.isEmpty()) {
306 filePropertyList.reserve(alloc: fileInfoList.count());
307 for (const QFileInfo &info : fileInfoList) {
308 //qDebug() << "Adding file : " << info.fileName() << "to list ";
309 filePropertyList << FileProperty(info);
310 }
311 if (folderUpdate) {
312 int fromIndex = 0;
313 int toIndex = currentFileList.size()-1;
314 findChangeRange(list: filePropertyList, fromIndex, toIndex);
315 folderUpdate = false;
316 currentFileList = filePropertyList;
317 //qDebug() << "emit directoryUpdated : " << fromIndex << " " << toIndex;
318 emit directoryUpdated(directory: path, list: filePropertyList, fromIndex, toIndex);
319 } else {
320 currentFileList = filePropertyList;
321 if (sortUpdate) {
322 emit sortFinished(list: filePropertyList);
323 sortUpdate = false;
324 } else
325 emit directoryChanged(directory: path, list: filePropertyList);
326 }
327 } else {
328 // The directory is empty
329 if (folderUpdate) {
330 int fromIndex = 0;
331 int toIndex = currentFileList.size()-1;
332 folderUpdate = false;
333 currentFileList.clear();
334 emit directoryUpdated(directory: path, list: filePropertyList, fromIndex, toIndex);
335 } else {
336 currentFileList.clear();
337 emit directoryChanged(directory: path, list: filePropertyList);
338 }
339 }
340 needUpdate = false;
341}
342
343void FileInfoThread::findChangeRange(const QList<FileProperty> &list, int &fromIndex, int &toIndex)
344{
345 if (currentFileList.size() == 0) {
346 fromIndex = 0;
347 toIndex = list.size();
348 return;
349 }
350
351 int i;
352 int listSize = list.size() < currentFileList.size() ? list.size() : currentFileList.size();
353 bool changeFound = false;
354
355 for (i=0; i < listSize; i++) {
356 if (list.at(i) != currentFileList.at(i)) {
357 changeFound = true;
358 break;
359 }
360 }
361
362 if (changeFound)
363 fromIndex = i;
364 else
365 fromIndex = i-1;
366
367 // For now I let the rest of the list be updated..
368 toIndex = list.size() > currentFileList.size() ? list.size() - 1 : currentFileList.size() - 1;
369}
370

source code of qtdeclarative/src/imports/folderlistmodel/fileinfothread.cpp