1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef FILEINFOTHREAD_P_H
5#define FILEINFOTHREAD_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QThread>
19#include <QMutex>
20#include <QWaitCondition>
21#if QT_CONFIG(filesystemwatcher)
22#include <QFileSystemWatcher>
23#endif
24#include <QFileInfo>
25#include <QDir>
26
27#include "fileproperty_p.h"
28#include "qquickfolderlistmodel_p.h"
29
30QT_BEGIN_NAMESPACE
31
32class FileInfoThread : public QThread
33{
34 Q_OBJECT
35
36Q_SIGNALS:
37 void directoryChanged(const QString &directory, const QList<FileProperty> &list) const;
38 void directoryUpdated(const QString &directory, const QList<FileProperty> &list, int fromIndex, int toIndex) const;
39 void sortFinished(const QList<FileProperty> &list) const;
40 void statusChanged(QQuickFolderListModel::Status status) const;
41
42public:
43 FileInfoThread(QObject *parent = nullptr);
44 ~FileInfoThread();
45
46 void clear();
47 void removePath(const QString &path);
48 void setPath(const QString &path);
49 void setRootPath(const QString &path);
50 void setSortFlags(QDir::SortFlags flags);
51 void setNameFilters(const QStringList & nameFilters);
52 void setShowFiles(bool show);
53 void setShowDirs(bool showFolders);
54 void setShowDirsFirst(bool show);
55 void setShowDotAndDotDot(bool on);
56 void setShowHidden(bool on);
57 void setShowOnlyReadable(bool on);
58 void setCaseSensitive(bool on);
59
60public Q_SLOTS:
61#if QT_CONFIG(filesystemwatcher)
62 void dirChanged(const QString &directoryPath);
63 void updateFile(const QString &path);
64#endif
65
66protected:
67 void run() override;
68 void runOnce();
69 void initiateScan();
70 void getFileInfos(const QString &path);
71 void findChangeRange(const QList<FileProperty> &list, int &fromIndex, int &toIndex);
72
73private:
74 enum class UpdateType {
75 None = 1 << 0,
76 // The order of the files in the current folder changed.
77 Sort = 1 << 1,
78 // A subset of files in the current folder changed.
79 Contents = 1 << 2
80 };
81 Q_DECLARE_FLAGS(UpdateTypes, UpdateType)
82
83 // Declare these ourselves, as Q_DECLARE_OPERATORS_FOR_FLAGS needs the enum to be public.
84 friend constexpr UpdateTypes operator|(UpdateType f1, UpdateTypes f2) noexcept;
85 friend constexpr UpdateTypes operator&(UpdateType f1, UpdateTypes f2) noexcept;
86
87 QMutex mutex;
88 QWaitCondition condition;
89 volatile bool abort;
90 bool scanPending;
91
92#if QT_CONFIG(filesystemwatcher)
93 QFileSystemWatcher *watcher;
94#endif
95 QList<FileProperty> currentFileList;
96 QDir::SortFlags sortFlags;
97 QString currentPath;
98 QString rootPath;
99 QStringList nameFilters;
100 bool needUpdate;
101 UpdateTypes updateTypes;
102 bool showFiles;
103 bool showDirs;
104 bool showDirsFirst;
105 bool showDotAndDotDot;
106 bool showHidden;
107 bool showOnlyReadable;
108 bool caseSensitive;
109};
110
111QT_END_NAMESPACE
112
113#endif // FILEINFOTHREAD_P_H
114

source code of qtdeclarative/src/labs/folderlistmodel/fileinfothread_p.h